-
-
Notifications
You must be signed in to change notification settings - Fork 256
implement JSON_TYPE()
#2351
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
implement JSON_TYPE()
#2351
Changes from all commits
4847542
dc2fff7
2515c89
c70b841
bb37f56
4de97b1
8a90e6f
757b5e3
6c08814
6794d9c
4b0dc43
0abe6e6
c43789d
62f7eab
88144d9
78f4a52
fc9057a
fed8086
ccfd141
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 |
|---|---|---|
|
|
@@ -9354,6 +9354,90 @@ from typestable`, | |
| {false}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: `SELECT json_type('{"a": [10, true, "abc"]}');`, | ||
| Expected: []sql.Row{ | ||
| {"OBJECT"}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: `SELECT json_type(json_extract('{"a": [10, true, "abc"]}', '$.a'));`, | ||
| Expected: []sql.Row{ | ||
| {"ARRAY"}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: `SELECT json_type(json_extract('{"a": [10, true, "abc"]}', '$.a[0]'));`, | ||
| Expected: []sql.Row{ | ||
| {"INTEGER"}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: `SELECT json_type(json_extract('{"a": [10, true, "abc"]}', '$.a[1]'));`, | ||
| Expected: []sql.Row{ | ||
| {"BOOLEAN"}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: `SELECT json_type(json_extract('{"a": [10, true, "abc"]}', '$.a[2]'));`, | ||
| Expected: []sql.Row{ | ||
| {"STRING"}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: `SELECT json_type(json_extract('{"a": 123.456}', '$.a'));`, | ||
| Expected: []sql.Row{ | ||
| {"DOUBLE"}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: `SELECT json_type(json_extract('{"a": null}', '$.a'));`, | ||
| Expected: []sql.Row{ | ||
| {"NULL"}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: "SELECT json_type(cast(cast('2001-01-01 12:34:56.123456' as datetime) as json));", | ||
| Expected: []sql.Row{ | ||
| {"DATETIME"}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: "SELECT json_type(cast(cast('2001-01-01 12:34:56.123456' as date) as json));", | ||
| Expected: []sql.Row{ | ||
| {"DATE"}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: "select json_type(cast(cast(1 as unsigned) as json));", | ||
| Expected: []sql.Row{ | ||
| {"UNSIGNED INTEGER"}, | ||
|
Contributor
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. oooh, nice work! The docs don't mention "UNSIGNED INTEGER" as a possible response, but that's exactly what MySQL returns! Good job reversing engineering MySQL's behavior here. |
||
| }, | ||
| }, | ||
| { | ||
| Query: "select json_type('4294967295');", | ||
| Expected: []sql.Row{ | ||
| {"INTEGER"}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: "select json_type('4294967296');", | ||
| Expected: []sql.Row{ | ||
| {"UNSIGNED INTEGER"}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: "SELECT json_type(cast(1.0 as json));", | ||
| Expected: []sql.Row{ | ||
| {"DECIMAL"}, | ||
| }, | ||
| }, | ||
| { | ||
| Query: "select json_type(cast(cast(2001 as year) as json));", | ||
| Expected: []sql.Row{ | ||
| {"UNSIGNED INTEGER"}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| var KeylessQueries = []QueryTest{ | ||
|
|
@@ -9559,6 +9643,27 @@ FROM mytable;`, | |
| Query: "select TABLE_NAME, IS_UPDATABLE from information_schema.views where table_schema = 'mydb'", | ||
| Expected: []sql.Row{{"myview1", "YES"}, {"myview2", "YES"}, {"myview3", "NO"}, {"myview4", "NO"}, {"myview5", "YES"}}, | ||
| }, | ||
| // time to json cast is broken | ||
| { | ||
| Query: "SELECT json_type(cast(cast('2001-01-01 12:34:56.123456' as time) as json));", | ||
| Expected: []sql.Row{ | ||
| {"TIME"}, | ||
| }, | ||
| }, | ||
| // binary to json cast is broken | ||
| { | ||
| Query: "SELECT json_type(cast(cast('123abc' as binary) as json));", | ||
| Expected: []sql.Row{ | ||
| {"BLOB"}, | ||
| }, | ||
| }, | ||
| // 1e2 -> 100, so we can't tell the difference between float and integer in this case | ||
| { | ||
| Query: `SELECT json_type(json_extract('{"a": 1e2}', '$.a'));`, | ||
| Expected: []sql.Row{ | ||
| {"DOUBLE"}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| var VersionedQueries = []QueryTest{ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // Copyright 2024 Dolthub, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package json | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math" | ||
|
|
||
| "github.com/dolthub/go-mysql-server/sql" | ||
| "github.com/dolthub/go-mysql-server/sql/expression" | ||
| "github.com/dolthub/go-mysql-server/sql/types" | ||
| ) | ||
|
|
||
| // JSONType (json_val) | ||
| // | ||
| // Returns a utf8mb4 string indicating the type of a JSON value. This can be an object, an array, or a scalar type. | ||
| // JSONType returns NULL if the argument is NULL. An error occurs if the argument is not a valid JSON value | ||
| // | ||
| // https://dev.mysql.com/doc/refman/8.0/en/json-attribute-functions.html#function_json-type | ||
| type JSONType struct { | ||
| JSON sql.Expression | ||
| } | ||
|
|
||
| var _ sql.FunctionExpression = &JSONType{} | ||
|
|
||
| // NewJSONType creates a new JSONType function. | ||
| func NewJSONType(args ...sql.Expression) (sql.Expression, error) { | ||
| if len(args) != 1 { | ||
| return nil, sql.ErrInvalidArgumentNumber.New("JSON_TYPE", "1", len(args)) | ||
| } | ||
| return &JSONType{JSON: args[0]}, nil | ||
| } | ||
|
|
||
| // FunctionName implements sql.FunctionExpression | ||
| func (j JSONType) FunctionName() string { | ||
| return "json_type" | ||
| } | ||
|
|
||
| // Description implements sql.FunctionExpression | ||
| func (j JSONType) Description() string { | ||
| return "returns type of JSON value." | ||
| } | ||
|
|
||
| // Resolved implements the Expression interface. | ||
| func (j JSONType) Resolved() bool { | ||
| return j.JSON.Resolved() | ||
| } | ||
|
|
||
| // String implements the fmt.Stringer interface. | ||
| func (j JSONType) String() string { | ||
| return fmt.Sprintf("%s(%s)", j.FunctionName(), j.JSON.String()) | ||
| } | ||
|
|
||
| // Type implements the Expression interface. | ||
| func (j JSONType) Type() sql.Type { | ||
| return types.Text | ||
| } | ||
|
|
||
| // IsNullable implements the Expression interface. | ||
| func (j JSONType) IsNullable() bool { | ||
| return j.JSON.IsNullable() | ||
| } | ||
|
|
||
| // Eval implements the Expression interface. | ||
| func (j JSONType) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { | ||
| span, ctx := ctx.Span(fmt.Sprintf("function.%s", j.FunctionName())) | ||
| defer span.End() | ||
|
|
||
| doc, err := getJSONDocumentFromRow(ctx, row, j.JSON) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if doc == nil { | ||
| return "NULL", nil | ||
| } | ||
|
|
||
| switch v := doc.Val.(type) { | ||
| case nil: | ||
| return "NULL", nil | ||
| case bool: | ||
| return "BOOLEAN", nil | ||
| case float64: | ||
| if conv, ok := j.JSON.(*expression.Convert); ok { | ||
| typ := conv.Child.Type() | ||
| if types.IsUnsigned(typ) || types.IsYear(typ) { | ||
| return "UNSIGNED INTEGER", nil | ||
| } | ||
| } | ||
| if math.Floor(v) == v { | ||
| if v >= (math.MaxInt32+1)*2 { | ||
| return "UNSIGNED INTEGER", nil | ||
| } | ||
| return "INTEGER", nil | ||
| } | ||
| return "DOUBLE", nil | ||
| case string: | ||
| if conv, ok := j.JSON.(*expression.Convert); ok { | ||
| typ := conv.Child.Type() | ||
| if types.IsDecimal(typ) { | ||
| return "DECIMAL", nil | ||
| } | ||
| if types.IsDatetimeType(typ) { | ||
| return "DATETIME", nil | ||
| } | ||
| if types.IsDateType(typ) { | ||
| return "DATE", nil | ||
| } | ||
| if types.IsTime(typ) { | ||
| return "TIME", nil | ||
| } | ||
| } | ||
| return "STRING", nil | ||
| case []interface{}: | ||
| return "ARRAY", nil | ||
| case map[string]interface{}: | ||
| return "OBJECT", nil | ||
| default: | ||
| return "OPAQUE", nil | ||
|
Contributor
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. Any way to trigger
Contributor
Author
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. I haven't found a query that spits out |
||
| } | ||
| } | ||
|
|
||
| // Children implements the Expression interface. | ||
| func (j JSONType) Children() []sql.Expression { | ||
| return []sql.Expression{j.JSON} | ||
| } | ||
|
|
||
| // WithChildren implements the Expression interface. | ||
| func (j JSONType) WithChildren(children ...sql.Expression) (sql.Expression, error) { | ||
| return NewJSONType(children...) | ||
| } | ||
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.
Also add tests for json_type returning "NULL" and "DOUBLE". You can express a double in a JSON literal using scientific notation (ie "{a: 10e2}")
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.
I think I was wrong about being able to use scientific notation here.
I think we should add the following tests, to make sure that we respect round-trip conversions of types:
SELECT json_type(json_extract(json_object("a", cast(10 as double)), "$.a"))-> "DOUBLE"SELECT json_type(json_extract(json_object("a", cast(10 as unsigned)), "$.a"))-> "UNSIGNED INTEGER"SELECT json_type(json_extract(json_object("a", cast(10 as signed)), "$.a"))-> "INTEGER"SELECT json_type(json_extract(json_object("a", cast(10 as decimal)), "$.a"))-> "DECIMAL"It's possible that the way we currently store JSON causes some of these to not work properly. We can disable those tests if it's out of scope to fix them now. But we should still have them as tests.
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.
didn't see this comment sorry!
test are added here:
#2355