avro-unions.td 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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-arg-default single-replica-cluster=quickstart
  10. # Test how Avro unions of varying sizes and nullabilities are converted
  11. # to Materialize types.
  12. $ set writer-schema={
  13. "name": "row",
  14. "type": "record",
  15. "fields": [
  16. {"name": "a", "type": ["long"]},
  17. {"name": "b", "type": ["long", "null"]},
  18. {"name": "c", "type": ["long", "null", "string"]},
  19. {"name": "d", "type": ["long", "string"]}
  20. ]
  21. }
  22. $ kafka-create-topic topic=data
  23. $ kafka-ingest topic=data format=avro schema=${writer-schema}
  24. {"a": {"long": 1}, "b": null, "c": null, "d": {"string": "d"}}
  25. {"a": {"long": 2}, "b": {"long": 2}, "c": {"string": "foo"}, "d": {"long": 4}}
  26. {"a": {"long": 2}, "b": {"long": 2}, "c": {"long": 3}, "d": {"string": "d"}}
  27. > CREATE CONNECTION kafka_conn
  28. TO KAFKA (BROKER '${testdrive.kafka-addr}', SECURITY PROTOCOL PLAINTEXT);
  29. > CREATE SOURCE unions
  30. IN CLUSTER ${arg.single-replica-cluster}
  31. FROM KAFKA CONNECTION kafka_conn (TOPIC 'testdrive-data-${testdrive.seed}')
  32. > CREATE TABLE unions_tbl FROM SOURCE unions (REFERENCE "testdrive-data-${testdrive.seed}")
  33. FORMAT AVRO USING SCHEMA '${writer-schema}'
  34. > SHOW COLUMNS FROM unions_tbl
  35. name nullable type comment
  36. ------------------------------------
  37. a false bigint ""
  38. b true bigint ""
  39. c1 true bigint ""
  40. c2 true text ""
  41. d1 true bigint ""
  42. d2 true text ""
  43. > SELECT * FROM unions_tbl
  44. a b c1 c2 d1 d2
  45. ------------------------------------------
  46. 1 <null> <null> <null> <null> d
  47. 2 2 <null> foo 4 <null>
  48. 2 2 3 <null> <null> d