Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support toSet function #3594

Merged
merged 8 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -182,6 +182,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 @@ -1429,6 +1432,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:
czpmango marked this conversation as resolved.
Show resolved Hide resolved
| 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