example.sql 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* syntax version 1 */
  2. /* postgres can not */
  3. USE plato;
  4. -- These examples are taken from [ISO/IEC TR 19075-6:2017] standard (https://www.iso.org/standard/67367.html)
  5. SELECT T.K,
  6. JSON_VALUE (T.J, 'lax $.who') AS Who
  7. FROM T;
  8. SELECT T.K,
  9. JSON_VALUE (T.J, 'lax $.who') AS Who,
  10. JSON_VALUE (T.J, 'lax $.where'
  11. NULL ON EMPTY) AS Nali
  12. FROM T;
  13. SELECT T.K,
  14. JSON_VALUE (T.J, 'strict $.who') AS Who,
  15. JSON_VALUE (T.J, 'strict $.where'
  16. DEFAULT 'no where there' ON ERROR )
  17. AS Nali
  18. FROM T;
  19. SELECT T.K,
  20. JSON_VALUE (T.J, 'lax $.who') AS Who,
  21. JSON_VALUE (T.J, 'lax $.where' NULL ON EMPTY) AS Nali,
  22. JSON_VALUE (T.J, 'lax $.friends.name' NULL ON EMPTY
  23. DEFAULT '*** error ***' ON ERROR)
  24. AS Friend
  25. FROM T;
  26. SELECT T.K,
  27. JSON_VALUE (T.J, 'strict $.who') AS Who,
  28. JSON_VALUE (T.J, 'strict $.where' NULL ON EMPTY NULL ON ERROR) AS Nali,
  29. -- NOTE: output for this particular column differs with standard.
  30. -- For row with T.K = 106 and T.J = { "who": "Louise", "where": "Iana" } output is "*** error ***", not NULL.
  31. -- This is because answer in standard (NULL) is not correct. Query "strict $.friends[*].name" is executed in strict mode
  32. -- where all structural errors are "hard" errors. Row with T.K = 106 does not have "friends" key in T.J. This is structural error
  33. -- and the result of JSON_VALUE must tolerate ON ERROR section which specifies to return "*** error ***" string.
  34. --
  35. -- 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):
  36. -- postgres=# select * from jsonb_path_query('{ "who": "Louise", "where": "Iana" }', 'strict $.friends[*].name');
  37. -- ERROR: JSON object does not contain key "friends"
  38. -- PostgreSQL shows us that hard error has happened, as expected.
  39. JSON_VALUE (T.J, 'strict $.friends[*].name' NULL ON EMPTY
  40. DEFAULT '*** error ***' ON ERROR)
  41. AS Friend
  42. FROM T;
  43. SELECT T.K,
  44. JSON_VALUE (T.J, 'lax $.who') AS Who,
  45. -- NOTE: In the original example INTEGER type was used. YQL does not have INTEGER type, Int64 was used instead
  46. JSON_VALUE (T.J, 'lax $.friends[0].rank' RETURNING Int64 NULL ON EMPTY)
  47. AS Rank
  48. FROM T;