variant_struct_builtins.sql 1.1 KB

123456789101112131415161718192021222324252627282930
  1. $vartype = Variant<num: Int32, flag: Bool, str: String>;
  2. $handle_num = ($x) -> { return 2 * $x; };
  3. $handle_flag = ($x) -> { return If($x, 200, 10); };
  4. $handle_str = ($x) -> { return Unwrap(CAST(LENGTH($x) AS Int32)); };
  5. $visitor = ($var) -> { return Visit($var, $handle_num AS num, $handle_flag AS flag, $handle_str AS str); };
  6. SELECT
  7. $visitor(Variant(5, "num", $vartype)),
  8. $visitor(Just(Variant(True, "flag", $vartype))),
  9. $visitor(Just(Variant("somestr", "str", $vartype))),
  10. $visitor(Nothing(OptionalType($vartype))),
  11. $visitor(NULL)
  12. ;
  13. $visitor_def = ($var) -> { return VisitOrDefault($var, 999, $handle_num AS num, $handle_flag AS flag); };
  14. SELECT
  15. $visitor_def(Variant(5, "num", $vartype)),
  16. $visitor_def(Just(Variant(True, "flag", $vartype))),
  17. $visitor_def(Just(Variant("somestr", "str", $vartype))),
  18. $visitor_def(Nothing(OptionalType($vartype))),
  19. $visitor_def(NULL)
  20. ;
  21. $vartype1 = Variant<num1: Int32, num2: Int32, num3: Int32>;
  22. SELECT
  23. VariantItem(Variant(7, "num2", $vartype1)),
  24. VariantItem(Just(Variant(5, "num1", $vartype1))),
  25. VariantItem(Nothing(OptionalType($vartype1))),
  26. VariantItem(NULL)
  27. ;