types-json.td 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. $ set-sql-timeout duration=1s
  10. #
  11. # Test the JSON data type
  12. #
  13. > CREATE SECRET mysqlpass AS '${arg.mysql-root-password}'
  14. > CREATE CONNECTION mysql_conn TO MYSQL (
  15. HOST mysql,
  16. USER root,
  17. PASSWORD SECRET mysqlpass
  18. )
  19. $ mysql-connect name=mysql url=mysql://root@mysql password=${arg.mysql-root-password}
  20. $ mysql-execute name=mysql
  21. DROP DATABASE IF EXISTS public;
  22. CREATE DATABASE public;
  23. USE public;
  24. # Insert data pre-snapshot
  25. CREATE TABLE t1 (f1 JSON);
  26. INSERT INTO t1 VALUES (CAST('{"bar": "baz", "balance": 7.77, "active": false}' AS JSON));
  27. INSERT INTO t1 VALUES (CAST('null' AS JSON));
  28. INSERT INTO t1 VALUES (JSON_ARRAY('x', 1, NULL, CAST('{"x": ["y"]}' AS JSON)));
  29. INSERT INTO t1 VALUES (NULL);
  30. INSERT INTO t1 VALUES (JSON_ARRAY(NULL));
  31. > CREATE SOURCE mz_source FROM MYSQL CONNECTION mysql_conn;
  32. > CREATE TABLE t1 FROM SOURCE mz_source (REFERENCE public.t1);
  33. > SELECT COUNT(*) > 0 FROM t1;
  34. true
  35. # Insert the same data post-snapshot
  36. $ mysql-execute name=mysql
  37. INSERT INTO t1 SELECT * FROM t1;
  38. > SELECT pg_typeof(f1) FROM t1 LIMIT 1;
  39. jsonb
  40. > SELECT * FROM t1;
  41. <null>
  42. <null>
  43. null
  44. null
  45. "{\"active\":false,\"balance\":7.77,\"bar\":\"baz\"}"
  46. "{\"active\":false,\"balance\":7.77,\"bar\":\"baz\"}"
  47. "[\"x\",1,null,{\"x\":[\"y\"]}]"
  48. "[\"x\",1,null,{\"x\":[\"y\"]}]"
  49. "[null]"
  50. "[null]"
  51. > SELECT f1->>'balance' FROM t1;
  52. <null>
  53. <null>
  54. <null>
  55. <null>
  56. <null>
  57. <null>
  58. <null>
  59. <null>
  60. 7.77
  61. 7.77
  62. > SELECT count(*) FROM t1 WHERE f1 IS NULL;
  63. 2
  64. > SELECT count(*) FROM t1 WHERE f1 = 'null'::jsonb;
  65. 2