aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/tests/sql/suites/json/json_value/example.sql
diff options
context:
space:
mode:
authorAlexander Smirnov <alex@ydb.tech>2024-11-20 11:14:58 +0000
committerAlexander Smirnov <alex@ydb.tech>2024-11-20 11:14:58 +0000
commit31773f157bf8164364649b5f470f52dece0a4317 (patch)
tree33d0f7eef45303ab68cf08ab381ce5e5e36c5240 /yql/essentials/tests/sql/suites/json/json_value/example.sql
parent2c7938962d8689e175574fc1e817c05049f27905 (diff)
parenteff600952d5dfe17942f38f510a8ac2b203bb3a5 (diff)
downloadydb-31773f157bf8164364649b5f470f52dece0a4317.tar.gz
Merge branch 'rightlib' into mergelibs-241120-1113
Diffstat (limited to 'yql/essentials/tests/sql/suites/json/json_value/example.sql')
-rw-r--r--yql/essentials/tests/sql/suites/json/json_value/example.sql55
1 files changed, 55 insertions, 0 deletions
diff --git a/yql/essentials/tests/sql/suites/json/json_value/example.sql b/yql/essentials/tests/sql/suites/json/json_value/example.sql
new file mode 100644
index 0000000000..b62ed6648b
--- /dev/null
+++ b/yql/essentials/tests/sql/suites/json/json_value/example.sql
@@ -0,0 +1,55 @@
+/* syntax version 1 */
+/* postgres can not */
+USE plato;
+
+-- These examples are taken from [ISO/IEC TR 19075-6:2017] standard (https://www.iso.org/standard/67367.html)
+SELECT T.K,
+ JSON_VALUE (T.J, 'lax $.who') AS Who
+FROM T;
+
+SELECT T.K,
+ JSON_VALUE (T.J, 'lax $.who') AS Who,
+ JSON_VALUE (T.J, 'lax $.where'
+ NULL ON EMPTY) AS Nali
+FROM T;
+
+SELECT T.K,
+ JSON_VALUE (T.J, 'strict $.who') AS Who,
+ JSON_VALUE (T.J, 'strict $.where'
+ DEFAULT 'no where there' ON ERROR )
+ AS Nali
+FROM T;
+
+SELECT T.K,
+ JSON_VALUE (T.J, 'lax $.who') AS Who,
+ JSON_VALUE (T.J, 'lax $.where' NULL ON EMPTY) AS Nali,
+ JSON_VALUE (T.J, 'lax $.friends.name' NULL ON EMPTY
+ DEFAULT '*** error ***' ON ERROR)
+ AS Friend
+FROM T;
+
+SELECT T.K,
+ JSON_VALUE (T.J, 'strict $.who') AS Who,
+ JSON_VALUE (T.J, 'strict $.where' NULL ON EMPTY NULL ON ERROR) AS Nali,
+
+ -- NOTE: output for this particular column differs with standard.
+ -- For row with T.K = 106 and T.J = { "who": "Louise", "where": "Iana" } output is "*** error ***", not NULL.
+ -- This is because answer in standard (NULL) is not correct. Query "strict $.friends[*].name" is executed in strict mode
+ -- where all structural errors are "hard" errors. Row with T.K = 106 does not have "friends" key in T.J. This is structural error
+ -- and the result of JSON_VALUE must tolerate ON ERROR section which specifies to return "*** error ***" string.
+ --
+ -- We can check this in PostgreSQL too (at the moment of writing PostgreSQL does not support JSON_VALUE function so we use jsonb_path_query):
+ -- postgres=# select * from jsonb_path_query('{ "who": "Louise", "where": "Iana" }', 'strict $.friends[*].name');
+ -- ERROR: JSON object does not contain key "friends"
+ -- PostgreSQL shows us that hard error has happened, as expected.
+ JSON_VALUE (T.J, 'strict $.friends[*].name' NULL ON EMPTY
+ DEFAULT '*** error ***' ON ERROR)
+ AS Friend
+FROM T;
+
+SELECT T.K,
+ JSON_VALUE (T.J, 'lax $.who') AS Who,
+ -- NOTE: In the original example INTEGER type was used. YQL does not have INTEGER type, Int64 was used instead
+ JSON_VALUE (T.J, 'lax $.friends[0].rank' RETURNING Int64 NULL ON EMPTY)
+ AS Rank
+FROM T;