variant_tuple_builtins.sql 1.0 KB

123456789101112131415161718192021222324252627282930
  1. $vartype = Variant<Int32, Bool, 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, $handle_flag, $handle_str); };
  6. SELECT
  7. $visitor(Variant(5, "0", $vartype)),
  8. $visitor(Just(Variant(True, "1", $vartype))),
  9. $visitor(Just(Variant("somestr", "2", $vartype))),
  10. $visitor(Nothing(OptionalType($vartype))),
  11. $visitor(NULL)
  12. ;
  13. $visitor_def = ($var) -> { return VisitOrDefault($var, 999, $handle_num, $handle_flag); };
  14. SELECT
  15. $visitor_def(Variant(5, "0", $vartype)),
  16. $visitor_def(Just(Variant(True, "1", $vartype))),
  17. $visitor_def(Just(Variant("somestr", "2", $vartype))),
  18. $visitor_def(Nothing(OptionalType($vartype))),
  19. $visitor_def(NULL)
  20. ;
  21. $vartype1 = Variant<Int32, Int32, Int32>;
  22. SELECT
  23. VariantItem(Variant(7, "1", $vartype1)),
  24. VariantItem(Just(Variant(5, "0", $vartype1))),
  25. VariantItem(Nothing(OptionalType($vartype1))),
  26. VariantItem(NULL)
  27. ;