Skip to content

Commit

Permalink
support toSet function (#3594)
Browse files Browse the repository at this point in the history
add test

Co-authored-by: Sophie <84560950+Sophie-Xie@users.noreply.github.com>
  • Loading branch information
czpmango and Sophie-Xie committed Jan 27, 2022
1 parent cab1bc0 commit a7cdcee
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/common/datatypes/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/common/datatypes/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ struct Value {
Value toBool() const;
Value toFloat() const;
Value toInt() const;
Value toSet() const;

Value lessThan(const Value& v) const;

Expand Down
10 changes: 10 additions & 0 deletions src/common/function/FunctionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ std::unordered_map<std::string, std::vector<TypeSignature>> 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),
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions tests/tck/features/expression/function/TypeConversion.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a7cdcee

Please sign in to comment.