Skip to content

Commit

Permalink
[BACKPORT] [#6766] YCQL: Fix JSONB operator execution when applying t…
Browse files Browse the repository at this point in the history
…o NULL objects

Summary: Back port to 2.2

Test Plan: TestJson and TestJsonIndex

Differential Revision: https://phabricator.dev.yugabyte.com/D10243
  • Loading branch information
nocaway committed Dec 30, 2020
1 parent e09ee0c commit 3e03d2a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
5 changes: 4 additions & 1 deletion java/yb-cql/src/test/java/org/yb/cql/TestJson.java
Expand Up @@ -97,7 +97,10 @@ public void testJson() throws Exception {
session.execute("INSERT INTO test_json(c1, c2) values (6, 'null');");
session.execute("INSERT INTO test_json(c1, c2) values (7, '2.0');");
session.execute("INSERT INTO test_json(c1, c2) values (8, '{\"b\" : 1}');");
verifyResultSet(session.execute("SELECT * FROM test_json WHERE c1 = 1"));
// SELECT and verify JSONB column content.
verifyResultSet(session.execute("SELECT * FROM test_json WHERE c1 = 1;"));
// Apply JSONB operators to NULL and singular objects.
verifyEmptyRows(session.execute("SELECT c2->>'non_existing_value' FROM test_json;"), 8);

// Invalid inserts.
runInvalidStmt("INSERT INTO test_json(c1, c2) values (123, abc);");
Expand Down
14 changes: 13 additions & 1 deletion java/yb-cql/src/test/java/org/yb/cql/TestJsonIndex.java
Expand Up @@ -40,14 +40,26 @@ public void testIndex() throws Exception {
session.execute("CREATE INDEX jidx ON test_json_index(j1->'a'->>'b')");
session.execute("CREATE INDEX cidx ON test_json_index(a_column)");

for (int h = 0; h < 3000; h++) {
int h;
for (h = 0; h < 3000; h++) {
String jvalue = String.format("{ \"a\" : { \"b\" : \"bvalue_%d\" }," +
" \"a_column\" : %d }", h, h );
String stmt = String.format("INSERT INTO test_json_index(h, j1, j2) VALUES (%d, '%s', '%s');",
h, jvalue, jvalue);
session.execute(stmt);
}

// Insert various value formats to the JSONB column to make sure that the JSONB expression
// index supports null values.
session.execute(String.format("INSERT INTO test_json_index(h) values (%d);", h++));
session.execute(String.format("INSERT INTO test_json_index(h, j1) values (%d, 'null');", h++));
session.execute(String.format("INSERT INTO test_json_index(h, j1) values (%d, '\"abc\"');",
h++));
session.execute(String.format("INSERT INTO test_json_index(h, j1) values (%d, '3');", h++));
session.execute(String.format("INSERT INTO test_json_index(h, j1) values (%d, 'true');", h++));
session.execute(String.format("INSERT INTO test_json_index(h, j1) values (%d, 'false');", h++));
session.execute(String.format("INSERT INTO test_json_index(h, j1) values (%d, '2.0');", h++));

long runtimeMillis;
String query;

Expand Down
18 changes: 15 additions & 3 deletions src/yb/common/ql_expr.cc
Expand Up @@ -43,9 +43,13 @@ CHECKED_STATUS QLExprExecutor::EvalExpr(const QLExpressionPB& ql_expr,
QLExprResult temp;
const QLJsonColumnOperationsPB& json_ops = ql_expr.json_column();
RETURN_NOT_OK(table_row.ReadColumn(json_ops.column_id(), temp.Writer()));
common::Jsonb jsonb;
temp.MoveToJsonb(&jsonb);
RETURN_NOT_OK(jsonb.ApplyJsonbOperators(json_ops, &result_writer.NewValue()));
if (temp.IsNull()) {
result_writer.SetNull();
} else {
common::Jsonb jsonb;
temp.MoveToJsonb(&jsonb);
RETURN_NOT_OK(jsonb.ApplyJsonbOperators(json_ops, &result_writer.NewValue()));
}
break;
}

Expand Down Expand Up @@ -872,6 +876,14 @@ const QLValuePB& QLExprResult::Value() const {
return value_.value();
}

bool QLExprResult::IsNull() const {
if (existing_value_) {
return yb::IsNull(*existing_value_);
}

return value_.IsNull();
}

void QLExprResult::MoveTo(QLValuePB* out) {
if (existing_value_) {
*out = *existing_value_;
Expand Down
2 changes: 2 additions & 0 deletions src/yb/common/ql_expr.h
Expand Up @@ -55,6 +55,8 @@ class QLExprResult {

QLExprResultWriter Writer();

bool IsNull() const;

private:
friend class QLExprResultWriter;

Expand Down

0 comments on commit 3e03d2a

Please sign in to comment.