Skip to content

Commit

Permalink
[BugFix] fix crash of json array compare (StarRocks#9322)
Browse files Browse the repository at this point in the history
  • Loading branch information
murphyatwork authored Jul 28, 2022
1 parent c6c91ce commit ffc3d8d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
20 changes: 10 additions & 10 deletions be/src/util/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,18 @@ static int sliceCompare(const vpack::Slice& left, const vpack::Slice& right) {
return 1;
}
}
return 0;
return left.length() - right.length();
} else if (left.isArray() && right.isArray()) {
int idx = 0;
for (auto it : vpack::ArrayIterator(left)) {
auto sub = right.at(idx);
if (!sub.isNone()) {
int x = sliceCompare(it, sub);
if (x != 0) {
return x;
}
if (left.length() != right.length()) {
return left.length() - right.length();
}
for (size_t i = 0; i < left.length(); i++) {
auto left_item = left.at(i);
auto right_item = right.at(i);
int x = sliceCompare(left_item, right_item);
if (x != 0) {
return x;
}
idx++;
}
return 0;
} else if (vpack::valueTypeGroup(left.type()) == vpack::valueTypeGroup(right.type())) {
Expand Down
24 changes: 24 additions & 0 deletions be/test/column/json_column_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,30 @@ PARALLEL_TEST(JsonColumnTest, test_compare) {
}
}

PARALLEL_TEST(JsonColumnTest, test_compare_array) {
auto array0 = JsonValue::parse("[]").value();
auto array1 = JsonValue::parse("[1]").value();
auto array2 = JsonValue::parse("[1, 2]").value();
EXPECT_EQ(0, array0.compare(array0));
EXPECT_EQ(0, array1.compare(array1));
EXPECT_EQ(0, array2.compare(array2));
EXPECT_LT(array0.compare(array1), 0);
EXPECT_LT(array0.compare(array2), 0);
EXPECT_LT(array1.compare(array2), 0);
}

PARALLEL_TEST(JsonColumnTest, test_compare_object) {
auto obj0 = JsonValue::parse("{}").value();
auto obj1 = JsonValue::parse(R"( {"a": 1} )").value();
auto obj2 = JsonValue::parse(R"( {"a": 1, "b": 2} )").value();
EXPECT_EQ(0, obj0.compare(obj0));
EXPECT_EQ(0, obj1.compare(obj1));
EXPECT_EQ(0, obj2.compare(obj2));
EXPECT_LT(obj0.compare(obj1), 0);
EXPECT_LT(obj0.compare(obj2), 0);
EXPECT_LT(obj1.compare(obj2), 0);
}

// NOLINTNEXTLINE
PARALLEL_TEST(JsonColumnTest, test_hash) {
JsonValue x = JsonValue::parse(R"({"a": 1, "b": 2})").value();
Expand Down

0 comments on commit ffc3d8d

Please sign in to comment.