decimal-order.td 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #
  10. # Make sure DECIMAL numbers are ordered appropriately in various contexts
  11. #
  12. #
  13. # Simple comparisons
  14. #
  15. > SELECT '0.000000000000000000000000000000000000001'::decimal < '0.000000000000000000000000000000000000002'::decimal;
  16. true
  17. > SELECT '999999999999999999999999999999999999998'::decimal < '999999999999999999999999999999999999999'::decimal;
  18. true
  19. #
  20. # Across types
  21. #
  22. > SELECT '0.000000000000000000000000000000000000001'::decimal < '0.000000000000000000000000000000000000002'::double;
  23. true
  24. # Due to loss of precision, this is unexpectedly false
  25. > SELECT '999999999999999999999999999999999999998'::decimal < '999999999999999999999999999999999999999'::double;
  26. false
  27. > SELECT '0'::double = '-0'::decimal;
  28. true
  29. > SELECT '-0'::double = '0'::decimal;
  30. true
  31. #
  32. # When used by a top-level ORDER BY
  33. #
  34. # Since testdrive sorts results by default, we use LIMIT + OFFSET to "probe" the correct placement of individual rows
  35. #
  36. #
  37. > SELECT * FROM (VALUES ('999999999999999999999999999999999999998'::decimal), ('999999999999999999999999999999999999999')) AS t1 ORDER BY 1 LIMIT 1 OFFSET 0;
  38. 999999999999999999999999999999999999998
  39. > SELECT * FROM (VALUES ('999999999999999999999999999999999999998'::decimal), ('999999999999999999999999999999999999999')) AS t1 ORDER BY 1 LIMIT 1 OFFSET 1;
  40. 999999999999999999999999999999999999999
  41. > SELECT * FROM (VALUES ('0.0'::decimal), ('0.000000000000000000000000000000000000001'::decimal)) AS t1 ORDER BY 1 LIMIT 1 OFFSET 0;
  42. 0
  43. > SELECT * FROM (VALUES ('0.0'::decimal), ('0.000000000000000000000000000000000000001'::decimal)) AS t1 ORDER BY 1 LIMIT 1 OFFSET 1;
  44. 0.000000000000000000000000000000000000001
  45. > SELECT * FROM (VALUES ('0.000000000000000000000000000000000000001'::decimal), ('0.000000000000000000000000000000000000002'::decimal)) AS t1 ORDER BY 1 LIMIT 1 OFFSET 0;
  46. 0.000000000000000000000000000000000000001
  47. > SELECT * FROM (VALUES ('0.000000000000000000000000000000000000001'::decimal), ('0.000000000000000000000000000000000000002'::decimal)) AS t1 ORDER BY 1 LIMIT 1 OFFSET 1;
  48. 0.000000000000000000000000000000000000002
  49. # Negative zero is equal to positive zero, so the two are not ordered in any way against each other, the ordering of the second column applies
  50. > SELECT * FROM (VALUES ('0'::decimal, 'row1'), ('-0', 'row2'), ('123', 'prevent optimizations')) AS t1 ORDER BY 1,2 LIMIT 1 OFFSET 0;
  51. 0 row1
  52. > SELECT * FROM (VALUES ('0'::decimal, 'row1'), ('-0', 'row2'), ('123', 'prevent optimizations')) AS t1 ORDER BY 1,2 LIMIT 1 OFFSET 1;
  53. 0 row2
  54. #
  55. # Different trailing or leading zeroes should amount to the same thing, that is, the order is determined by the second column alone
  56. > SELECT * FROM (VALUES ('0.10'::decimal, 'row1'), ('0.1', 'row2'), ('00.100', 'row3')) AS t1 ORDER BY 1,2 LIMIT 1 OFFSET 0;
  57. 0.1 row1
  58. > SELECT * FROM (VALUES ('0.10'::decimal, 'row1'), ('0.1', 'row2'), ('00.100', 'row3')) AS t1 ORDER BY 1,2 LIMIT 1 OFFSET 1;
  59. 0.1 row2
  60. > SELECT * FROM (VALUES ('0.10'::decimal, 'row1'), ('0.1', 'row2'), ('00.100', 'row3')) AS t1 ORDER BY 1,2 LIMIT 1 OFFSET 2;
  61. 0.1 row3
  62. #
  63. # When used by a TopK operator inside the dataflow
  64. #
  65. > SELECT MIN(column1 + 0) FROM (SELECT column1 + 0 AS column1 FROM (VALUES ('999999999999999999999999999999999999998'::decimal), ('999999999999999999999999999999999999999')) AS t1 ORDER BY 1 LIMIT 1 OFFSET 0);
  66. 999999999999999999999999999999999999998
  67. > SELECT MIN(column1 + 0) FROM (SELECT column1 + 0 AS column1 FROM (VALUES ('999999999999999999999999999999999999998'::decimal), ('999999999999999999999999999999999999999')) AS t1 ORDER BY 1 LIMIT 1 OFFSET 1);
  68. 999999999999999999999999999999999999999
  69. > SELECT column1 + 0, column2 FROM (
  70. SELECT column1 + 0 AS column1 , column2 FROM (
  71. SELECT '0.10'::decimal(3,2) AS column1, 'row1' AS column2 UNION ALL SELECT '0.1'::decimal(2,1), 'row2' UNION ALL SELECT '00.100'::decimal(5,3), 'row3' UNION ALL SELECT '123'::decimal, 'prevent optimizations'
  72. ) AS t1
  73. ORDER BY 1,2 LIMIT 1 OFFSET 0
  74. );
  75. 0.1 row1
  76. > SELECT column1 + 0, column2 FROM (
  77. SELECT column1 + 0 AS column1 , column2 FROM (
  78. SELECT '0.10'::decimal(3,2) AS column1, 'row1' AS column2 UNION ALL SELECT '0.1'::decimal(2,1), 'row2' UNION ALL SELECT '00.100'::decimal(5,3), 'row3' UNION ALL SELECT '123'::decimal, 'prevent optimizations'
  79. ) AS t1
  80. ORDER BY 1,2 LIMIT 1 OFFSET 1
  81. );
  82. 0.1 row2
  83. > SELECT column1 + 0, column2 FROM (
  84. SELECT column1 + 0 AS column1 , column2 FROM (
  85. SELECT '0.10'::decimal(3,2) AS column1, 'row1' AS column2 UNION ALL SELECT '0.1'::decimal(2,1), 'row2' UNION ALL SELECT '00.100'::decimal(5,3), 'row3' UNION ALL SELECT '123'::decimal, 'prevent optimizations'
  86. ) AS t1
  87. ORDER BY 1,2 LIMIT 1 OFFSET 2
  88. );
  89. 0.1 row3
  90. #
  91. # Infinity
  92. #
  93. # Positive Infinity is greater than largest decimal number
  94. > SELECT (SELECT SUM(column1) FROM (VALUES ('999999999999999999999999999999999999999'::decimal), ('999999999999999999999999999999999999999'))) > '999999999999999999999999999999999999999'::decimal;
  95. true
  96. # Negative Infinity is smaller than the smallest number
  97. > SELECT (SELECT SUM(column1) FROM (VALUES ('-999999999999999999999999999999999999999'::decimal), ('-999999999999999999999999999999999999999'))) < '-999999999999999999999999999999999999999'::decimal;
  98. true