From a887bd6302b356ef31ace96ac1cc398958a88785 Mon Sep 17 00:00:00 2001 From: "kyle.cao" Date: Thu, 27 Jan 2022 17:21:59 +0800 Subject: [PATCH] support toSet function (#3594) add test Co-authored-by: Sophie <84560950+Sophie-Xie@users.noreply.github.com> --- src/common/datatypes/Value.cpp | 21 +++++++++++++ src/common/datatypes/Value.h | 1 + src/common/function/FunctionManager.cpp | 10 ++++++ .../function/TypeConversion.feature | 31 +++++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/src/common/datatypes/Value.cpp b/src/common/datatypes/Value.cpp index 032ee835202..74cb245067f 100644 --- a/src/common/datatypes/Value.cpp +++ b/src/common/datatypes/Value.cpp @@ -1893,6 +1893,27 @@ Value Value::toInt() const { } } +Value Value::toSet() const { + switch (type_) { + case Value::Type::__EMPTY__: + case Value::Type::NULLVALUE: { + return Value::kNullValue; + } + case Value::Type::SET: { + return *this; + } + case Value::Type::LIST: { + Set set; + for (auto& item : getList().values) { + set.values.emplace(item); + } + return set; + } + default: { + return Value::kNullBadType; + } + } +} Value Value::lessThan(const Value& v) const { if (empty() || v.empty()) { return (v.isNull() || isNull()) ? Value::kNullValue : Value::kEmpty; diff --git a/src/common/datatypes/Value.h b/src/common/datatypes/Value.h index 31e4ba7e666..de0e12050b9 100644 --- a/src/common/datatypes/Value.h +++ b/src/common/datatypes/Value.h @@ -347,6 +347,7 @@ struct Value { Value toBool() const; Value toFloat() const; Value toInt() const; + Value toSet() const; Value lessThan(const Value& v) const; diff --git a/src/common/function/FunctionManager.cpp b/src/common/function/FunctionManager.cpp index 7262b7fea9c..af583a144c0 100644 --- a/src/common/function/FunctionManager.cpp +++ b/src/common/function/FunctionManager.cpp @@ -183,6 +183,9 @@ std::unordered_map> FunctionManager::typ TypeSignature({Value::Type::STRING}, Value::Type::NULLVALUE), TypeSignature({Value::Type::FLOAT}, Value::Type::INT), TypeSignature({Value::Type::INT}, Value::Type::INT)}}, + {"toset", + {TypeSignature({Value::Type::LIST}, Value::Type::SET), + TypeSignature({Value::Type::SET}, Value::Type::SET)}}, {"hash", {TypeSignature({Value::Type::INT}, Value::Type::INT), TypeSignature({Value::Type::FLOAT}, Value::Type::INT), @@ -1426,6 +1429,13 @@ FunctionManager::FunctionManager() { attr.isPure_ = true; attr.body_ = [](const auto &args) -> Value { return Value(args[0].get()).toInt(); }; } + { + auto &attr = functions_["toset"]; + attr.minArity_ = 1; + attr.maxArity_ = 1; + attr.isPure_ = true; + attr.body_ = [](const auto &args) -> Value { return Value(args[0].get()).toSet(); }; + } { auto &attr = functions_["lpad"]; attr.minArity_ = 3; diff --git a/tests/tck/features/expression/function/TypeConversion.feature b/tests/tck/features/expression/function/TypeConversion.feature index c3e83ccb30b..a6b188b0bee 100644 --- a/tests/tck/features/expression/function/TypeConversion.feature +++ b/tests/tck/features/expression/function/TypeConversion.feature @@ -87,3 +87,34 @@ Feature: TypeConversion Expression YIELD [toInteger(true), toInteger(false)] AS yield_toInteger """ Then a SemanticError should be raised at runtime: Type error `toInteger(true)' + + Scenario: toSet + When executing query: + """ + RETURN toSet(list[1,2,3,1,2]) AS list2set + """ + Then the result should be, in any order: + | list2set | + | {3, 1, 2} | + When executing query: + """ + RETURN toSet(set{1,2,3,1,2}) AS set2set + """ + Then the result should be, in any order: + | set2set | + | {3, 1, 2} | + When executing query: + """ + RETURN toSet(true) AS bool2set + """ + Then a SemanticError should be raised at runtime: `toSet(true)' is not a valid expression : Parameter's type error + When executing query: + """ + RETURN toSet(1) AS int2set + """ + Then a SemanticError should be raised at runtime: `toSet(1)' is not a valid expression : Parameter's type error + When executing query: + """ + RETURN toSet(3.4) AS float2set + """ + Then a SemanticError should be raised at runtime: `toSet(3.4)' is not a valid expression : Parameter's type error