Skip to content

Commit 49520e4

Browse files
authored
fix(interactive): Allow implicit type conversion in WithInExpr (#4514)
As titled. Fix #4513
1 parent 5d6d660 commit 49520e4

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

flex/engines/graph_db/runtime/utils/expr_impl.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,9 @@ static std::unique_ptr<ExprBase> build_expr(
558558
if (key->type() == RTAnyType::kI64Value) {
559559
return std::make_unique<WithInExpr<int64_t>>(ctx, std::move(key),
560560
rhs.const_());
561+
} else if (key->type() == RTAnyType::kU64Value) {
562+
return std::make_unique<WithInExpr<uint64_t>>(ctx, std::move(key),
563+
rhs.const_());
561564
} else if (key->type() == RTAnyType::kI32Value) {
562565
return std::make_unique<WithInExpr<int32_t>>(ctx, std::move(key),
563566
rhs.const_());

flex/engines/graph_db/runtime/utils/expr_impl.h

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,30 +172,50 @@ class VertexWithInListExpr : public ExprBase {
172172
std::unique_ptr<ExprBase> val_list_;
173173
};
174174

175+
#define PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(dst_vector_name, array_name) \
176+
size_t len = array_name.item_size(); \
177+
for (size_t idx = 0; idx < len; ++idx) { \
178+
dst_vector_name.push_back(array_name.item(idx)); \
179+
}
180+
175181
template <typename T>
176182
class WithInExpr : public ExprBase {
177183
public:
178184
WithInExpr(const Context& ctx, std::unique_ptr<ExprBase>&& key,
179185
const common::Value& array)
180186
: key_(std::move(key)) {
181187
if constexpr (std::is_same_v<T, int64_t>) {
182-
assert(array.item_case() == common::Value::kI64Array);
183-
size_t len = array.i64_array().item_size();
184-
for (size_t idx = 0; idx < len; ++idx) {
185-
container_.push_back(array.i64_array().item(idx));
188+
if (array.item_case() == common::Value::kI64Array) {
189+
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.i64_array());
190+
} else if (array.item_case() == common::Value::kI32Array) {
191+
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.i32_array());
192+
} else {
193+
// TODO(zhanglei,lexiao): We should support more types here, and if type
194+
// conversion fails, we should return an error.
195+
LOG(INFO) << "Could not convert array with type " << array.item_case()
196+
<< " to int64_t array";
197+
}
198+
} else if constexpr (std::is_same_v<T, uint64_t>) {
199+
if (array.item_case() == common::Value::kI64Array) {
200+
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.i64_array());
201+
} else if (array.item_case() == common::Value::kI32Array) {
202+
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.i32_array());
203+
} else {
204+
LOG(INFO) << "Could not convert array with type " << array.item_case()
205+
<< " to int64_t array";
186206
}
187207
} else if constexpr (std::is_same_v<T, int32_t>) {
188-
assert(array.item_case() == common::Value::kI32Array);
189-
size_t len = array.i32_array().item_size();
190-
for (size_t idx = 0; idx < len; ++idx) {
191-
container_.push_back(array.i32_array().item(idx));
208+
if (array.item_case() == common::Value::kI32Array) {
209+
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.i32_array());
210+
} else if constexpr (std::is_same_v<T, int64_t>) {
211+
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.i64_array());
212+
} else {
213+
LOG(INFO) << "Could not convert array with type " << array.item_case()
214+
<< " to int32_t array";
192215
}
193216
} else if constexpr (std::is_same_v<T, std::string>) {
194217
assert(array.item_case() == common::Value::kStrArray);
195-
size_t len = array.str_array().item_size();
196-
for (size_t idx = 0; idx < len; ++idx) {
197-
container_.push_back(array.str_array().item(idx));
198-
}
218+
PARSER_COMMON_VALUE_ARRAY_TO_VECTOR(container_, array.str_array());
199219
} else {
200220
LOG(FATAL) << "not implemented";
201221
}

interactive_engine/compiler/src/main/java/com/alibaba/graphscope/cypher/integration/suite/graphAlgo/GraphAlgoQueries.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,11 @@ public static QueryContext get_graph_algo_test14() {
247247
List<String> expected = Arrays.asList("Record<{id: 0}>");
248248
return new QueryContext(query, expected);
249249
}
250+
251+
// Test WithInExpression
252+
public static QueryContext get_graph_algo_test15() {
253+
String query = "MATCH(a)-[b:WorkOn]->(c) where elementId(a) in [0,1] return a.id;";
254+
List<String> expected = Arrays.asList("Record<{id: 0}>", "Record<{id: 1}>");
255+
return new QueryContext(query, expected);
256+
}
250257
}

interactive_engine/compiler/src/test/java/com/alibaba/graphscope/cypher/integration/graphAlgo/GraphAlgoTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ public void run_graph_query14_test() {
148148
Assert.assertEquals(testQuery.getExpectedResult().toString(), result.list().toString());
149149
}
150150

151+
@Test
152+
public void run_graph_query15_test() {
153+
QueryContext testQuery = GraphAlgoQueries.get_graph_algo_test15();
154+
Result result = session.run(testQuery.getQuery());
155+
Assert.assertEquals(testQuery.getExpectedResult().toString(), result.list().toString());
156+
}
157+
151158
@AfterClass
152159
public static void afterClass() {
153160
if (session != null) {

0 commit comments

Comments
 (0)