-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-2985] Implement JSON_STORAGE_SIZE function #1150
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -139,7 +139,6 @@ data: { | |
| "ISOYEAR" | ||
| "ISOLATION" | ||
| "JAVA" | ||
| "JSON" | ||
| "K" | ||
| "KEY" | ||
| "KEY_MEMBER" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to you 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 org.apache.calcite.sql.fun; | ||
|
|
||
| import org.apache.calcite.sql.SqlFunction; | ||
| import org.apache.calcite.sql.SqlFunctionCategory; | ||
| import org.apache.calcite.sql.SqlKind; | ||
| import org.apache.calcite.sql.type.OperandTypes; | ||
| import org.apache.calcite.sql.type.ReturnTypes; | ||
| import org.apache.calcite.sql.type.SqlTypeTransforms; | ||
|
|
||
| /** | ||
| * The <code>JSON_STORAGE_SIZE</code> function. | ||
| */ | ||
| public class SqlJsonStorageSizeFunction extends SqlFunction { | ||
|
|
||
| public SqlJsonStorageSizeFunction() { | ||
| super("JSON_STORAGE_SIZE", | ||
| SqlKind.OTHER_FUNCTION, | ||
| ReturnTypes.cascade(ReturnTypes.INTEGER_NULLABLE, | ||
| SqlTypeTransforms.FORCE_NULLABLE), | ||
| null, | ||
| OperandTypes.ANY, | ||
| SqlFunctionCategory.SYSTEM); | ||
| } | ||
| } | ||
|
|
||
| // End SqlJsonStorageSizeFunction.java |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4575,6 +4575,25 @@ private void checkNullOperand(SqlTester tester, String op) { | |
| tester.checkNull("json_pretty(cast(null as varchar))"); | ||
| } | ||
|
|
||
| @Test public void testJsonStorageSize() { | ||
| tester.checkString("json_storage_size('[100, \"sakila\", [1, 3, 5], 425.05]')", | ||
| "29", "INTEGER"); | ||
| tester.checkString("json_storage_size('{\"a\": 1000,\"b\": \"aa\", \"c\": \"[1, 3, 5]\"}')", | ||
| "35", "INTEGER"); | ||
| tester.checkString("json_storage_size('{\"a\": 1000, \"b\": \"wxyz\", \"c\": \"[1, 3]\"}')", | ||
| "34", "INTEGER"); | ||
| tester.checkString("json_storage_size('[100, \"json\", [[10, 20, 30], 3, 5], 425.05]')", | ||
| "36", "INTEGER"); | ||
| tester.checkString("json_storage_size('12')", | ||
| "2", "INTEGER"); | ||
| tester.checkString("json_storage_size('null')", | ||
| "4", "INTEGER"); | ||
| tester.checkString("json_storage_size(cast(null as varchar(1)))", | ||
| null, "INTEGER"); | ||
|
Member
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. Can you explain this one? 'null' is a string, why does it return null? Is there case for
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. well,Thanks for reminding me. I adjusted this mistake. |
||
| tester.checkFails("json_storage_size('{]')", | ||
| "(?s).*Not a valid input.*", true); | ||
| } | ||
|
|
||
| @Test public void testJsonType() { | ||
| tester.setFor(SqlLibraryOperators.JSON_TYPE); | ||
| tester.checkString("json_type('\"1\"')", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2099,6 +2099,8 @@ semantics. | |
| | m | JSON_KEYS(jsonValue [, path ]) | Returns a string indicating the keys of a JSON *jsonValue* | ||
| | m | JSON_REMOVE(jsonValue, path[, path]) | Removes data from *jsonValue* using a series of *path* expressions and returns the result | ||
| | m | REVERSE(string) | Returns the reverse order of *string* | ||
| | m | JSON_STORAGE_SIZE(jsonValue) | Returns an integer value indicating the size of a *jsonValue* | ||
|
Member
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. "size" is too simple to describe the return. You can just use the words that MySQL already used, or make it anyway more detailed. |
||
| | m | JSON_STORAGE_SIZE(jsonValue) | Returns the number of bytes used to store the binary representation of a *jsonValue* | ||
| | o | DECODE(value, value1, result1 [, valueN, resultN ]* [, default ]) | Compares *value* to each *valueN* value one by one; if *value* is equal to a *valueN*, returns the corresponding *resultN*, else returns *default*, or NULL if *default* is not specified | ||
| | o | NVL(value1, value2) | Returns *value1* if *value1* is not null, otherwise *value2* | ||
| | o | LTRIM(string) | Returns *string* with all blanks removed from the start | ||
|
|
@@ -2112,6 +2114,7 @@ semantics. | |
| | m p | REPEAT(string, integer) | Returns a string of *integer* times *string*; Returns an empty string if *integer* is less than 1 | ||
| | m | SPACE(integer) | Returns a string of *integer* spaces; Returns an empty string if *integer* is less than 1 | ||
|
|
||
|
|
||
| Note: | ||
|
|
||
| * `JSON_TYPE` / `JSON_DEPTH` / `JSON_PRETTY` return null if the argument is null | ||
|
|
@@ -2231,6 +2234,27 @@ LIMIT 10; | |
| | ---------- | | ||
| | ["a", "d"] | | ||
|
|
||
|
|
||
| ##### JSON_STORAGE_SIZE example | ||
|
|
||
| SQL | ||
|
|
||
| ```SQL | ||
| SELECT | ||
| JSON_STORAGE_SIZE('[100, \"sakila\", [1, 3, 5], 425.05]') AS c1, | ||
| JSON_STORAGE_SIZE('{\"a\": 10, \"b\": \"a\", \"c\": \"[1, 3, 5, 7]\"}') AS c2, | ||
| JSON_STORAGE_SIZE('{\"a\": 10, \"b\": \"xyz\", \"c\": \"[1, 3, 5, 7]\"}') AS c3, | ||
| JSON_STORAGE_SIZE('[100, \"json\", [[10, 20, 30], 3, 5], 425.05]') AS c4 | ||
| limit 10; | ||
| ``` | ||
|
|
||
| Result | ||
|
|
||
| | c1 | c2 | c3 | c4 | | ||
| | -- | ---| ---| -- | | ||
| | 29 | 35 | 37 | 36 | | ||
|
|
||
|
|
||
| #### DECODE example | ||
|
|
||
| SQL | ||
|
|
||
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.
Any reason to remove this?
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.
Misoperation during merge.