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

fix(interactive): Allow implicit type conversion in WithInExpr #4514

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions flex/engines/graph_db/runtime/utils/expr_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,9 @@ static std::unique_ptr<ExprBase> build_expr(
if (key->type() == RTAnyType::kI64Value) {
return std::make_unique<WithInExpr<int64_t>>(ctx, std::move(key),
rhs.const_());
} else if (key->type() == RTAnyType::kU64Value) {
return std::make_unique<WithInExpr<uint64_t>>(ctx, std::move(key),
rhs.const_());
} else if (key->type() == RTAnyType::kI32Value) {
return std::make_unique<WithInExpr<int32_t>>(ctx, std::move(key),
rhs.const_());
Expand Down
44 changes: 32 additions & 12 deletions flex/engines/graph_db/runtime/utils/expr_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,30 +172,50 @@ class VertexWithInListExpr : public ExprBase {
std::unique_ptr<ExprBase> val_list_;
};

#define PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(dst_vector_name, array_name) \
size_t len = array_name.item_size(); \
for (size_t idx = 0; idx < len; ++idx) { \
dst_vector_name.push_back(array_name.item(idx)); \
}

template <typename T>
class WithInExpr : public ExprBase {
public:
WithInExpr(const Context& ctx, std::unique_ptr<ExprBase>&& key,
const common::Value& array)
: key_(std::move(key)) {
if constexpr (std::is_same_v<T, int64_t>) {
assert(array.item_case() == common::Value::kI64Array);
size_t len = array.i64_array().item_size();
for (size_t idx = 0; idx < len; ++idx) {
container_.push_back(array.i64_array().item(idx));
if (array.item_case() == common::Value::kI64Array) {
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.i64_array());
} else if (array.item_case() == common::Value::kI32Array) {
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.i32_array());
} else {
// TODO(zhanglei,lexiao): We should support more types here, and if type
// conversion fails, we should return an error.
LOG(INFO) << "Could not convert array with type " << array.item_case()
<< " to int64_t array";
}
} else if constexpr (std::is_same_v<T, uint64_t>) {
if (array.item_case() == common::Value::kI64Array) {
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.i64_array());
} else if (array.item_case() == common::Value::kI32Array) {
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.i32_array());
} else {
LOG(INFO) << "Could not convert array with type " << array.item_case()
<< " to int64_t array";
}
} else if constexpr (std::is_same_v<T, int32_t>) {
assert(array.item_case() == common::Value::kI32Array);
size_t len = array.i32_array().item_size();
for (size_t idx = 0; idx < len; ++idx) {
container_.push_back(array.i32_array().item(idx));
if (array.item_case() == common::Value::kI32Array) {
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.i32_array());
} else if constexpr (std::is_same_v<T, int64_t>) {
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.i64_array());
} else {
LOG(INFO) << "Could not convert array with type " << array.item_case()
<< " to int32_t array";
}
} else if constexpr (std::is_same_v<T, std::string>) {
assert(array.item_case() == common::Value::kStrArray);
size_t len = array.str_array().item_size();
for (size_t idx = 0; idx < len; ++idx) {
container_.push_back(array.str_array().item(idx));
}
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.str_array());
} else {
LOG(FATAL) << "not implemented";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,11 @@ public static QueryContext get_graph_algo_test14() {
List<String> expected = Arrays.asList("Record<{id: 0}>");
return new QueryContext(query, expected);
}

// Test WithInExpression
public static QueryContext get_graph_algo_test15() {
String query = "MATCH(a)-[b:WorkOn]->(c) where elementId(a) in [0,1] return a.id;";
List<String> expected = Arrays.asList("Record<{id: 0}>", "Record<{id: 1}>");
return new QueryContext(query, expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ public void run_graph_query14_test() {
Assert.assertEquals(testQuery.getExpectedResult().toString(), result.list().toString());
}

@Test
public void run_graph_query15_test() {
QueryContext testQuery = GraphAlgoQueries.get_graph_algo_test15();
Result result = session.run(testQuery.getQuery());
Assert.assertEquals(testQuery.getExpectedResult().toString(), result.list().toString());
}

@AfterClass
public static void afterClass() {
if (session != null) {
Expand Down
Loading