Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: fix the incorrect result of json_length(cast(1 as json), null) #50933

Merged
merged 7 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions pkg/expression/builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -1770,10 +1770,6 @@ func (b *builtinJSONLengthSig) evalInt(ctx EvalContext, row chunk.Row) (res int6
return res, isNull, err
}

if obj.TypeCode != types.JSONTypeCodeObject && obj.TypeCode != types.JSONTypeCodeArray {
return 1, false, nil
}

if len(b.args) == 2 {
path, isNull, err := b.args[1].EvalString(ctx, row)
if isNull || err != nil {
Expand All @@ -1793,9 +1789,10 @@ func (b *builtinJSONLengthSig) evalInt(ctx EvalContext, row chunk.Row) (res int6
if !exists {
return res, true, nil
}
if obj.TypeCode != types.JSONTypeCodeObject && obj.TypeCode != types.JSONTypeCodeArray {
return 1, false, nil
}
}

if obj.TypeCode != types.JSONTypeCodeObject && obj.TypeCode != types.JSONTypeCodeArray {
return 1, false, nil
}
return int64(obj.GetElemCount()), false, nil
}
25 changes: 10 additions & 15 deletions pkg/expression/builtin_json_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,9 @@ func (b *builtinJSONLengthSig) vectorized() bool {
func (b *builtinJSONLengthSig) vecEvalInt(ctx EvalContext, input *chunk.Chunk, result *chunk.Column) error {
nr := input.NumRows()

result.ResizeInt64(nr, false)
resI64s := result.Int64s()

jsonBuf, err := b.bufAllocator.get()
if err != nil {
return err
Expand All @@ -822,8 +825,7 @@ func (b *builtinJSONLengthSig) vecEvalInt(ctx EvalContext, input *chunk.Chunk, r
if err := b.args[0].VecEvalJSON(ctx, input, jsonBuf); err != nil {
return err
}
result.ResizeInt64(nr, false)
resI64s := result.Int64s()
result.MergeNulls(jsonBuf)

if len(b.args) == 2 {
pathBuf, err := b.bufAllocator.get()
Expand All @@ -834,23 +836,12 @@ func (b *builtinJSONLengthSig) vecEvalInt(ctx EvalContext, input *chunk.Chunk, r
if err := b.args[1].VecEvalString(ctx, input, pathBuf); err != nil {
return err
}
result.MergeNulls(pathBuf)

result.MergeNulls(jsonBuf)
for i := 0; i < nr; i++ {
if result.IsNull(i) {
continue
}
jsonItem := jsonBuf.GetJSON(i)

if jsonItem.TypeCode != types.JSONTypeCodeObject && jsonItem.TypeCode != types.JSONTypeCodeArray {
resI64s[i] = 1
continue
}

if pathBuf.IsNull(i) {
result.SetNull(i, true)
continue
}

pathExpr, err := types.ParseJSONPathExpr(pathBuf.GetString(i))
if err != nil {
Expand All @@ -860,6 +851,11 @@ func (b *builtinJSONLengthSig) vecEvalInt(ctx EvalContext, input *chunk.Chunk, r
return types.ErrInvalidJSONPathMultipleSelection
}

jsonItem := jsonBuf.GetJSON(i)
if jsonItem.TypeCode != types.JSONTypeCodeObject && jsonItem.TypeCode != types.JSONTypeCodeArray {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In non-vector version, if path does not exists, it returns null, but in the vector version, if the jsonItem is not JSON object or JSON array, it returns 1 even if path does not exists? According to MySQL's behavior, the non-vector version is right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result.MergeNulls(pathBuf) has been call in L839, so if path is null, continue will be call in L843

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean the path is not null, but a non exists path. Like select json_length(cast(1 as json), '$.a')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get! fixed and added test case.

resI64s[i] = 1
continue
}
obj, exists := jsonItem.Extract([]types.JSONPathExpression{pathExpr})
if !exists {
result.SetNull(i, true)
Expand All @@ -872,7 +868,6 @@ func (b *builtinJSONLengthSig) vecEvalInt(ctx EvalContext, input *chunk.Chunk, r
resI64s[i] = int64(obj.GetElemCount())
}
} else {
result.MergeNulls(jsonBuf)
for i := 0; i < nr; i++ {
if result.IsNull(i) {
continue
Expand Down
16 changes: 13 additions & 3 deletions tests/integrationtest/r/expression/json.result
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,19 @@ json_length('{}'),
json_length('[]'),
json_length('{"a": 1}'),
json_length('{"a": 1, "b": 2}'),
json_length('[1, 2, 3]');
json_length('1') json_length('{}') json_length('[]') json_length('{"a": 1}') json_length('{"a": 1, "b": 2}') json_length('[1, 2, 3]')
1 0 0 1 2 3
json_length('[1, 2, 3]'),
json_length('{}', '$'),
json_length('1', '$'),
json_length(null, '$'),
json_length(null, 'fdfd'),
json_length('{}', null),
json_length('1', null);
json_length('1') json_length('{}') json_length('[]') json_length('{"a": 1}') json_length('{"a": 1, "b": 2}') json_length('[1, 2, 3]') json_length('{}', '$') json_length('1', '$') json_length(null, '$') json_length(null, 'fdfd') json_length('{}', null) json_length('1', null)
1 0 0 1 2 3 0 1 NULL NULL NULL NULL
select json_length('{}', 'fsdfds');
Error 3143 (42000): Invalid JSON path expression. The error is around character position 1.
select json_length('1', 'fsdfds');
Error 3143 (42000): Invalid JSON path expression. The error is around character position 1.
select json_array(922337203685477580) = json_array(922337203685477581);
json_array(922337203685477580) = json_array(922337203685477581)
0
Expand Down
12 changes: 11 additions & 1 deletion tests/integrationtest/t/expression/json.test
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,17 @@ select
json_length('[]'),
json_length('{"a": 1}'),
json_length('{"a": 1, "b": 2}'),
json_length('[1, 2, 3]');
json_length('[1, 2, 3]'),
json_length('{}', '$'),
json_length('1', '$'),
json_length(null, '$'),
json_length(null, 'fdfd'),
json_length('{}', null),
json_length('1', null);
-- error 3143
select json_length('{}', 'fsdfds');
-- error 3143
select json_length('1', 'fsdfds');
# issue 16267
select json_array(922337203685477580) = json_array(922337203685477581);

Expand Down