cast_decimal.sql 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. --------------------
  2. -- Explicit casts --
  3. --------------------
  4. $strs = AsList("-1", "0", "1", "2.4", "99.77", "999.5", "-7777");
  5. $data = ListMap($strs, ($str) -> { return AsStruct($str as str); });
  6. SELECT
  7. dataset.*,
  8. -- Changing range
  9. CAST(dec_3_1 AS Decimal(5, 1)) AS upcast_3_1,
  10. CAST(dec_5_2 AS Decimal(4, 2)) AS downcast_5_2,
  11. -- Changing precision
  12. CAST(dec_5_2 AS Decimal(3, 0)) AS rounded_5_2,
  13. CAST(dec_7_0 AS Decimal(9, 2)) AS extended_7_0,
  14. -- Combinations
  15. CAST(dec_5_2 AS Decimal(6, 0)) AS upcast_rounded_5_2,
  16. CAST(dec_3_1 AS Decimal(7, 3)) AS upcast_extended_3_1,
  17. CAST(dec_5_2 AS Decimal(3, 1)) AS downcast_rounded_5_2,
  18. CAST(dec_7_0 AS Decimal(5, 2)) AS downcast_extended_7_0
  19. FROM (
  20. SELECT
  21. str AS a_str,
  22. CAST(str AS Decimal(3, 1)) AS dec_3_1,
  23. CAST(str AS Decimal(5, 2)) AS dec_5_2,
  24. CAST(str AS Decimal(7, 0)) AS dec_7_0
  25. FROM AS_TABLE($data)
  26. ) as dataset;
  27. --------------------
  28. -- Implicit cast --
  29. --------------------
  30. $lambda = ($big_dec) -> { return $big_dec; };
  31. $func = Callable(Callable<(Decimal(10, 5))->Decimal(10, 5)>, $lambda);
  32. $small_dec = Decimal("3.0", 2, 1);
  33. SELECT $func($small_dec);