Skip to content

Commit 639c5bf

Browse files
authored
fix json & array contain bug (#258)
Signed-off-by: yusheng.ma <yusheng.ma@zilliz.com>
1 parent b381bf8 commit 639c5bf

File tree

1 file changed

+58
-19
lines changed

1 file changed

+58
-19
lines changed

src/parser/parser.h

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -241,31 +241,45 @@ class PlanCCVisitor : public PlanVisitor {
241241
TRY_WITH_EXCEPTION(info.data_type() == proto::schema::DataType::Array ||
242242
info.data_type() == proto::schema::DataType::JSON);
243243
auto elem = std::any_cast<ExprWithDtype>(expr_ret[1]->accept(this));
244+
TRY_WITH_EXCEPTION(elem.expr->value_expr().has_value() &&
245+
elem.expr->value_expr().value().has_array_val());
244246
if (info.data_type() == proto::schema::DataType::Array) {
245-
proto::plan::GenericValue expr =
246-
proto::plan::GenericValue(elem.expr->value_expr().value());
247-
TRY_WITH_EXCEPTION(
248-
canBeCompared(field, toValueExpr(&expr, this->arena.get())));
247+
auto arr = elem.expr->value_expr().value().array_val();
248+
for (int idx = 0; idx < arr.array_size(); idx++) {
249+
proto::plan::GenericValue expr = arr.array(idx);
250+
TRY_WITH_EXCEPTION(canBeCompared(
251+
field, toValueExpr(&expr, this->arena.get())));
252+
}
249253
}
250254

251255
auto expr = google::protobuf::Arena::CreateMessage<proto::plan::Expr>(
252256
this->arena.get());
253257
auto json_contain_expr = google::protobuf::Arena::CreateMessage<
254258
proto::plan::JSONContainsExpr>(this->arena.get());
255-
auto value = json_contain_expr->add_elements(); // MayBe BUG
256-
value->unsafe_arena_set_allocated_array_val(
257-
CreateMessageWithCopy<proto::plan::Array>(
258-
this->arena.get(),
259-
elem.expr->value_expr().value().array_val()));
259+
260+
auto arr = elem.expr->value_expr().value().array_val();
261+
for (int idx = 0; idx < arr.array_size(); idx++) {
262+
proto::plan::GenericValue expr = arr.array(idx);
263+
auto val = json_contain_expr->add_elements();
264+
if (expr.has_bool_val())
265+
val->set_bool_val(expr.bool_val());
266+
if (expr.has_string_val())
267+
val->set_string_val(expr.string_val());
268+
if (expr.has_float_val())
269+
val->set_float_val(expr.float_val());
270+
if (expr.has_int64_val())
271+
val->set_int64_val(expr.int64_val());
272+
}
273+
260274
json_contain_expr->set_elements_same_type(
261275
elem.expr->value_expr().value().array_val().same_type());
262276
json_contain_expr->unsafe_arena_set_allocated_column_info(
263277
CreateMessageWithCopy<proto::plan::ColumnInfo>(this->arena.get(),
264278
info));
265-
266279
json_contain_expr->set_op(
267280
proto::plan::JSONContainsExpr_JSONOp_ContainsAll);
268281
expr->unsafe_arena_set_allocated_json_contains_expr(json_contain_expr);
282+
269283
return ExprWithDtype(expr, proto::schema::Bool, false);
270284
}
271285

@@ -1398,6 +1412,18 @@ class PlanCCVisitor : public PlanVisitor {
13981412
}
13991413
continue;
14001414
}
1415+
1416+
if (value.type() == typeid(std::string)) {
1417+
v->set_string_val(std::any_cast<std::string>(value));
1418+
if (dtype != proto::schema::DataType::None &&
1419+
dtype != proto::schema::DataType::VarChar) {
1420+
is_same = false;
1421+
}
1422+
if (dtype == proto::schema::DataType::None) {
1423+
dtype = proto::schema::DataType::VarChar;
1424+
}
1425+
continue;
1426+
}
14011427
}
14021428
}
14031429

@@ -1423,23 +1449,35 @@ class PlanCCVisitor : public PlanVisitor {
14231449
TRY_WITH_EXCEPTION(info.data_type() == proto::schema::DataType::Array ||
14241450
info.data_type() == proto::schema::DataType::JSON);
14251451
auto elem = std::any_cast<ExprWithDtype>(expr_ret[1]->accept(this));
1452+
TRY_WITH_EXCEPTION(elem.expr->value_expr().has_value() &&
1453+
elem.expr->value_expr().value().has_array_val());
14261454
if (info.data_type() == proto::schema::DataType::Array) {
1427-
proto::plan::GenericValue expr =
1428-
proto::plan::GenericValue(elem.expr->value_expr().value());
1429-
TRY_WITH_EXCEPTION(
1430-
canBeCompared(field, toValueExpr(&expr, this->arena.get())));
1455+
auto arr = elem.expr->value_expr().value().array_val();
1456+
for (int idx = 0; idx < arr.array_size(); idx++) {
1457+
proto::plan::GenericValue expr = arr.array(idx);
1458+
TRY_WITH_EXCEPTION(canBeCompared(
1459+
field, toValueExpr(&expr, this->arena.get())));
1460+
}
14311461
}
14321462

14331463
auto expr = google::protobuf::Arena::CreateMessage<proto::plan::Expr>(
14341464
this->arena.get());
14351465
auto json_contain_expr = google::protobuf::Arena::CreateMessage<
14361466
proto::plan::JSONContainsExpr>(this->arena.get());
14371467

1438-
auto value = json_contain_expr->add_elements();
1439-
value->unsafe_arena_set_allocated_array_val(
1440-
CreateMessageWithCopy<proto::plan::Array>(
1441-
this->arena.get(),
1442-
elem.expr->value_expr().value().array_val()));
1468+
auto arr = elem.expr->value_expr().value().array_val();
1469+
for (int idx = 0; idx < arr.array_size(); idx++) {
1470+
proto::plan::GenericValue expr = arr.array(idx);
1471+
auto val = json_contain_expr->add_elements();
1472+
if (expr.has_bool_val())
1473+
val->set_bool_val(expr.bool_val());
1474+
if (expr.has_string_val())
1475+
val->set_string_val(expr.string_val());
1476+
if (expr.has_float_val())
1477+
val->set_float_val(expr.float_val());
1478+
if (expr.has_int64_val())
1479+
val->set_int64_val(expr.int64_val());
1480+
}
14431481

14441482
json_contain_expr->set_elements_same_type(
14451483
elem.expr->value_expr().value().array_val().same_type());
@@ -1449,6 +1487,7 @@ class PlanCCVisitor : public PlanVisitor {
14491487
json_contain_expr->set_op(
14501488
proto::plan::JSONContainsExpr_JSONOp_ContainsAny);
14511489
expr->unsafe_arena_set_allocated_json_contains_expr(json_contain_expr);
1490+
14521491
return ExprWithDtype(expr, proto::schema::Bool, false);
14531492
}
14541493

0 commit comments

Comments
 (0)