Skip to content

Commit

Permalink
cql3: fix bad optional access when executing fromJson function
Browse files Browse the repository at this point in the history
Fix fromJson(null) to return null, not a error as it did before this patch.
We use "null" as the default value when unwrapping optionals
to avoid bad optional access errors.

Fixes: #7912

Signed-off-by: Michael Huang <michaelhly@gmail.com>

Closes #15481
  • Loading branch information
michaelhly authored and avikivity committed Sep 21, 2023
1 parent 61440d2 commit a684e51
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cql3/functions/functions.cc
Expand Up @@ -223,7 +223,7 @@ make_from_json_function(data_dictionary::database db, const sstring& keyspace, d
return make_native_scalar_function<true>("fromjson", t, {utf8_type},
[keyspace, t](std::span<const bytes_opt> parameters) -> bytes_opt {
try {
rjson::value json_value = rjson::parse(utf8_type->to_string(parameters[0].value()));
rjson::value json_value = rjson::parse(utf8_type->to_string(parameters[0].value_or("null")));
bytes_opt parsed_json_value;
if (!json_value.IsNull()) {
parsed_json_value.emplace(from_json_object(*t, json_value));
Expand Down
Expand Up @@ -134,7 +134,7 @@ def testSelectJsonWithPagingWithFrozenUDT(cql, test_keyspace):
["{\"a\": 1, \"b\": 2, \"c\": [\"1\", \"2\"]}", "[\"" + str(uuid) + "\", 4]", "4"])

# Reproduces issue #7911, #7912, #7914, #7915, #7944, #7954
@pytest.mark.xfail(reason="issues #7912, #7914, #7915, #7944, #7954")
@pytest.mark.xfail(reason="issues #7914, #7915, #7944, #7954")
def testFromJsonFct(cql, test_keyspace):
abc_tuple = collections.namedtuple('abc_tuple', ['a', 'b', 'c'])
with create_type(cql, test_keyspace, "(a int, b uuid, c set<text>)") as type_name:
Expand Down
2 changes: 0 additions & 2 deletions test/cql-pytest/test_json.py
Expand Up @@ -225,15 +225,13 @@ def test_fromjson_boolean_string_prepared(cql, table1):

# Test that null argument is allowed for fromJson(), with unprepared statement
# Reproduces issue #7912.
@pytest.mark.xfail(reason="issue #7912")
def test_fromjson_null_unprepared(cql, table1):
p = unique_key_int()
cql.execute(f"INSERT INTO {table1} (p, v) VALUES ({p}, fromJson(null))")
assert list(cql.execute(f"SELECT p, v from {table1} where p = {p}")) == [(p, None)]

# Test that null argument is allowed for fromJson(), with prepared statement
# Reproduces issue #7912.
@pytest.mark.xfail(reason="issue #7912")
def test_fromjson_null_prepared(cql, table1):
p = unique_key_int()
stmt = cql.prepare(f"INSERT INTO {table1} (p, v) VALUES (?, fromJson(?))")
Expand Down

0 comments on commit a684e51

Please sign in to comment.