on_empty.sql 1.0 KB

123456789101112131415161718192021222324252627282930
  1. /* syntax version 1 */
  2. /* postgres can not */
  3. -- Accessing absent object member will cause empty result in lax mode
  4. $json = CAST("{}" as Json);
  5. SELECT
  6. JSON_VALUE($json, "lax $.key"), -- defaults to NULL ON EMPTY
  7. JSON_VALUE($json, "lax $.key" NULL ON EMPTY),
  8. JSON_VALUE($json, "lax $.key" DEFAULT "*** empty ***" ON EMPTY);
  9. -- Null as a default value
  10. SELECT
  11. JSON_VALUE($json, "lax $.key" RETURNING Uint16 DEFAULT NULL ON EMPTY);
  12. -- Check that default value is casted to the target type
  13. SELECT
  14. JSON_VALUE($json, "lax $.key" RETURNING Int16 DEFAULT "123" ON EMPTY),
  15. JSON_VALUE($json, "lax $.key" RETURNING Int16 DEFAULT 123.456 ON EMPTY);
  16. -- Here JsonPath engine returns empty result and ON EMPTY section must be used.
  17. -- But default value in ON EMPTY section is -123 and casting it to Uint16 will fail.
  18. -- In this case ON ERROR section must be returned.
  19. SELECT
  20. JSON_VALUE(
  21. $json,
  22. "lax $.key"
  23. RETURNING Uint16
  24. DEFAULT -123 ON EMPTY
  25. DEFAULT 456 ON ERROR
  26. );