-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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: add builtin function json_length #7739
Changes from 3 commits
3821748
4edb95e
d5f9755
d7ad4e8
e7e1a57
29f2841
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ var ( | |
_ builtinFunc = &builtinJSONRemoveSig{} | ||
_ builtinFunc = &builtinJSONMergeSig{} | ||
_ builtinFunc = &builtinJSONContainsSig{} | ||
_ builtinFunc = &builtinJSONLengthSig{} | ||
) | ||
|
||
type jsonTypeFunctionClass struct { | ||
|
@@ -772,6 +773,65 @@ type jsonLengthFunctionClass struct { | |
baseFunctionClass | ||
} | ||
|
||
type builtinJSONLengthSig struct { | ||
baseBuiltinFunc | ||
} | ||
|
||
func (b *builtinJSONLengthSig) Clone() builtinFunc { | ||
newSig := &builtinJSONLengthSig{} | ||
newSig.cloneFrom(&b.baseBuiltinFunc) | ||
return newSig | ||
} | ||
|
||
func (c *jsonLengthFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) { | ||
return nil, errFunctionNotExists.GenWithStackByArgs("FUNCTION", "JSON_LENGTH") | ||
if err := c.verifyArgs(args); err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
|
||
argTps := make([]types.EvalType, 0, len(args)) | ||
argTps = append(argTps, types.ETJson) | ||
if len(args) == 2 { | ||
argTps = append(argTps, types.ETString) | ||
} | ||
|
||
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETInt, argTps...) | ||
sig := &builtinJSONLengthSig{bf} | ||
sig.setPbCode(tipb.ScalarFuncSig_JsonLengthSig) | ||
return sig, nil | ||
} | ||
|
||
func (b *builtinJSONLengthSig) evalInt(row chunk.Row) (res int64, isNull bool, err error) { | ||
obj, isNull, err := b.args[0].EvalJSON(b.ctx, row) | ||
if isNull || err != nil { | ||
return res, isNull, errors.Trace(err) | ||
} | ||
|
||
if obj.Type() != "OBJECT" && obj.Type() != "ARRAY" { | ||
return 1, false, nil | ||
} | ||
|
||
if len(b.args) == 2 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about: if len(b.args) == 1 {
return int64(obj.GetElemCount()), false, nil
}
// handle the case that len(b.args) == 2
... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, I'm ready to extract the common path visit but in a other pr(some other pr are conflicted with that) |
||
path, isNull, err := b.args[1].EvalString(b.ctx, row) | ||
if isNull || err != nil { | ||
return res, isNull, errors.Trace(err) | ||
} | ||
|
||
pathExpr, err := json.ParseJSONPathExpr(path) | ||
if err != nil { | ||
return res, true, errors.Trace(err) | ||
} | ||
if pathExpr.ContainsAnyAsterisk() { | ||
return res, true, json.ErrInvalidJSONPathWildcard | ||
} | ||
|
||
obj, exists := obj.Extract([]json.PathExpression{pathExpr}) | ||
if !exists { | ||
return res, true, nil | ||
} | ||
if obj.Type() != "OBJECT" && obj.Type() != "ARRAY" { | ||
return 1, false, nil | ||
} | ||
return int64(obj.GetElemCount()), false, nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line is useless. |
||
} | ||
return int64(obj.GetElemCount()), false, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3301,6 +3301,19 @@ func (s *testIntegrationSuite) TestFuncJSON(c *C) { | |
json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'all', '$[*]') | ||
`) | ||
r.Check(testkit.Rows("1 0 1 0")) | ||
|
||
r = tk.MustQuery(`select json_length('1')`) | ||
r.Check(testkit.Rows("1")) | ||
r = tk.MustQuery(`select json_length('{}')`) | ||
r.Check(testkit.Rows("0")) | ||
r = tk.MustQuery(`select json_length('[]')`) | ||
r.Check(testkit.Rows("0")) | ||
r = tk.MustQuery(`select json_length('{"a": 1}')`) | ||
r.Check(testkit.Rows("1")) | ||
r = tk.MustQuery(`select json_length('{"a": 1, "b": 2}')`) | ||
r.Check(testkit.Rows("2")) | ||
r = tk.MustQuery(`select json_length('[1, 2, 3]')`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. merge testcases to save lines. |
||
r.Check(testkit.Rows("3")) | ||
} | ||
|
||
func (s *testIntegrationSuite) TestColumnInfoModified(c *C) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use
Typecode
here?