decimal.slt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. # Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # This file is derived from the logic test suite in CockroachDB. The
  12. # original file was retrieved on June 10, 2019 from:
  13. #
  14. # https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/decimal
  15. #
  16. # The original source code is subject to the terms of the Apache
  17. # 2.0 license, a copy of which can be found in the LICENSE file at the
  18. # root of this repository.
  19. mode cockroach
  20. simple conn=mz_system,user=mz_system
  21. ALTER SYSTEM SET unsafe_enable_table_keys = true
  22. ----
  23. COMPLETE 0
  24. # The following tests have results equivalent to Postgres (differences
  25. # in string representation and number of decimals returned, but otherwise
  26. # the same). These do not pass using the inf package. The inf package
  27. # (http://gopkg.in/inf.v0) is what we used to use, but it had various problems
  28. # (for example, all the test cases below), and was replaced with apd.
  29. # inf returns 0
  30. query R
  31. SELECT (1.4238790346995263e-40::DECIMAL / 6.011482313728436e+41::DECIMAL)
  32. ----
  33. 2.3685988919035999994E-82
  34. # inf returns -108.4851126682386588
  35. query R
  36. SELECT ln(7.682705743584112e-48::DECIMAL)
  37. ----
  38. -108.48511266823882051
  39. # inf returns 0
  40. query R
  41. SELECT sqrt(9.789765531128956e-34::DECIMAL)
  42. ----
  43. 3.1288601009199749773E-17
  44. # inf returns 0.1547300000000000
  45. query R
  46. SELECT pow(4.727998800941528e-14::DECIMAL, 0.06081860494226844::DECIMAL)
  47. ----
  48. 0.15472926640705911955
  49. # inf returns 0, 0
  50. query RR
  51. SELECT pow(sqrt(1e-10::DECIMAL), 2), sqrt(pow(1e-5::DECIMAL, 2))
  52. ----
  53. 1E-10 0.00001
  54. # inf returns 1e-16, 0, 2e-16
  55. query RRR
  56. SELECT 1e-16::DECIMAL / 2, 1e-16::DECIMAL / 3, 1e-16::DECIMAL / 2 * 2
  57. ----
  58. 5E-17 3.3333333333333333333E-17 1.0E-16
  59. # inf returns 1e-8, 0, 0, 0
  60. query RRRR
  61. SELECT pow(1e-4::DECIMAL, 2), pow(1e-5::DECIMAL, 2), pow(1e-8::DECIMAL, 2), pow(1e-9::DECIMAL, 2)
  62. ----
  63. 1E-8 1E-10 1E-16 1E-18
  64. # inf returns argument too large
  65. query R
  66. SELECT pow(1e-10::DECIMAL, 2)
  67. ----
  68. 1E-20
  69. # inf panics (materialize#13051)
  70. query RR
  71. SELECT 'NaN'::FLOAT::DECIMAL, 'NaN'::DECIMAL
  72. ----
  73. NaN NaN
  74. # Ensure trailing zeros are kept for decimal types with no listed scale,
  75. # and enforced when the scale is listed.
  76. statement ok
  77. CREATE TABLE t (d decimal, v decimal(3, 1))
  78. statement ok
  79. INSERT INTO t VALUES (0.000::decimal, 0.00::decimal), (1.00::decimal, 1.00::decimal), (2.0::decimal, 2.0::decimal), (3::decimal, 3::decimal)
  80. query RR
  81. SELECT * FROM t ORDER BY d
  82. ----
  83. 0.000 0.0
  84. 1.00 1.0
  85. 2.0 2.0
  86. 3 3.0
  87. # Ensure trailing zeros are kept in an index.
  88. statement ok
  89. CREATE TABLE t2 (d decimal, v decimal(3, 1), primary key (d, v))
  90. statement ok
  91. INSERT INTO t2 VALUES
  92. (1.00::decimal, 1.00::decimal),
  93. (2.0::decimal, 2.0::decimal),
  94. (3::decimal, 3::decimal),
  95. ('NaN'::decimal, 'NaN'::decimal),
  96. ('Inf'::decimal, 'Inf'::decimal),
  97. ('-Inf'::decimal, '-Inf'::decimal),
  98. ('-0.0000'::decimal, '-0.0000'::decimal)
  99. query RR
  100. SELECT * FROM t2 ORDER BY d
  101. ----
  102. NaN NaN
  103. -Infinity -Infinity
  104. 0.0000 0.0
  105. 1.00 1.0
  106. 2.0 2.0
  107. 3 3.0
  108. Infinity Infinity
  109. # Ensure uniqueness in PK columns with +/- NaN and 0.
  110. statement error duplicate key value
  111. INSERT INTO t2 VALUES ('-NaN'::decimal, '-NaN'::decimal)
  112. statement error duplicate key value
  113. INSERT INTO t2 VALUES (0, 0)
  114. # Ensure NaN cannot be signaling or negative.
  115. query RRRR
  116. SELECT 'NaN'::decimal, '-NaN'::decimal, 'sNaN'::decimal, '-sNaN'::decimal
  117. ----
  118. NaN NaN NaN NaN
  119. query RR
  120. SELECT * FROM t2 WHERE d IS NaN and v IS NaN
  121. ----
  122. NaN NaN
  123. query RR
  124. SELECT * FROM t2 WHERE d = 'Infinity' and v = 'Infinity'
  125. ----
  126. Infinity Infinity
  127. query RR
  128. SELECT * FROM t2 WHERE d = '-Infinity' and v = '-Infinity'
  129. ----
  130. -Infinity -Infinity
  131. # Ensure special values are handled correctly.
  132. statement ok
  133. CREATE TABLE s (d decimal null, index (d))
  134. statement ok
  135. INSERT INTO s VALUES
  136. (null),
  137. ('NaN'::decimal),
  138. ('-NaN'::decimal),
  139. ('Inf'::decimal),
  140. ('-Inf'::decimal),
  141. ('0'::decimal),
  142. (1),
  143. (-1)
  144. statement ok
  145. INSERT INTO s VALUES
  146. ('-0'::decimal),
  147. ('-0.0'::decimal),
  148. ('-0.00'::decimal),
  149. ('-0.00E-1'::decimal),
  150. ('-0.0E-3'::decimal)
  151. query R rowsort
  152. SELECT * FROM s WHERE d = 0
  153. ----
  154. 0
  155. 0
  156. 0.0
  157. 0.00
  158. 0.000
  159. 0.0000
  160. query R
  161. SELECT * FROM s WHERE d IS NAN
  162. ----
  163. NaN
  164. NaN
  165. query R
  166. SELECT * FROM s WHERE d = 'inf'::decimal
  167. ----
  168. Infinity
  169. query R
  170. SELECT * FROM s WHERE d = 'NaN'
  171. ----
  172. NaN
  173. NaN
  174. # In the following tests, the various zero values all compare equal to
  175. # each other so we must use two ORDER BY clauses to obtain a stable result.
  176. # Check the ordering of decimal values.
  177. query R
  178. SELECT d FROM s ORDER BY d, d::TEXT
  179. ----
  180. NULL
  181. NaN
  182. NaN
  183. -Infinity
  184. -1
  185. 0
  186. 0
  187. 0.0
  188. 0.00
  189. 0.000
  190. 0.0000
  191. 1
  192. Infinity
  193. # Just test the NaN-ness of the values.
  194. query RBBB
  195. SELECT d, d IS NaN, d = 'NaN', isnan(d) FROM s@{FORCE_INDEX=primary} ORDER BY d, d::TEXT
  196. ----
  197. NULL NULL NULL NULL
  198. NaN true true true
  199. NaN true true true
  200. -Infinity false false false
  201. -1 false false false
  202. 0 false false false
  203. 0 false false false
  204. 0.0 false false false
  205. 0.00 false false false
  206. 0.000 false false false
  207. 0.0000 false false false
  208. 1 false false false
  209. Infinity false false false
  210. # Just test the NaN-ness of the values in secondary index
  211. query RBBB
  212. SELECT d, d IS NaN, d = 'NaN', isnan(d) FROM s@{FORCE_INDEX=s_d_idx} ORDER BY d, d::TEXT
  213. ----
  214. NULL NULL NULL NULL
  215. NaN true true true
  216. NaN true true true
  217. -Infinity false false false
  218. -1 false false false
  219. 0 false false false
  220. 0 false false false
  221. 0.0 false false false
  222. 0.00 false false false
  223. 0.000 false false false
  224. 0.0000 false false false
  225. 1 false false false
  226. Infinity false false false
  227. query RB
  228. select d, d > 'NaN' from s@{FORCE_INDEX=primary} where d > 'NaN' ORDER BY d, d::TEXT
  229. ----
  230. -Infinity true
  231. -1 true
  232. 0 true
  233. 0 true
  234. 0.0 true
  235. 0.00 true
  236. 0.000 true
  237. 0.0000 true
  238. 1 true
  239. Infinity true
  240. query RB
  241. select d, d > 'NaN' from s@{FORCE_INDEX=s_d_idx} where d > 'NaN' ORDER BY d, d::TEXT
  242. ----
  243. -Infinity true
  244. -1 true
  245. 0 true
  246. 0 true
  247. 0.0 true
  248. 0.00 true
  249. 0.000 true
  250. 0.0000 true
  251. 1 true
  252. Infinity true
  253. # Verify that decimals don't lose trailing 0s even when used for an index.
  254. statement ok
  255. CREATE INDEX idx ON s (d)
  256. query R rowsort
  257. SELECT * FROM s@idx WHERE d = 0
  258. ----
  259. 0
  260. 0
  261. 0.0
  262. 0.00
  263. 0.000
  264. 0.0000
  265. statement ok
  266. INSERT INTO s VALUES
  267. ('10'::decimal),
  268. ('10.0'::decimal),
  269. ('10.00'::decimal),
  270. ('10.000'::decimal),
  271. ('100000E-4'::decimal),
  272. ('1000000E-5'::decimal),
  273. ('1.0000000E+1'::decimal)
  274. query R rowsort
  275. SELECT * FROM s@primary WHERE d = 10
  276. ----
  277. 10
  278. 10.0
  279. 10.00
  280. 10.000
  281. 10.0000
  282. 10.00000
  283. 10.000000
  284. query R rowsort
  285. SELECT * FROM s@idx WHERE d = 10
  286. ----
  287. 10
  288. 10.0
  289. 10.00
  290. 10.000
  291. 10.0000
  292. 10.00000
  293. 10.000000
  294. query R
  295. SELECT 1.00::decimal(6,4)
  296. ----
  297. 1.0000
  298. statement error value with precision 6, scale 4 must round to an absolute value less than 10\^2
  299. SELECT 101.00::decimal(6,4)
  300. statement error scale \(6\) must be between 0 and precision \(4\)
  301. SELECT 101.00::decimal(4,6)
  302. statement error value with precision 2, scale 2 must round to an absolute value less than 1
  303. SELECT 1::decimal(2, 2)
  304. # Regression test for materialize#16081
  305. statement ok
  306. CREATE TABLE a (b DECIMAL)
  307. statement ok
  308. INSERT INTO a VALUES (142378208485490985369999605144727062141206925976498256305323716858805588894693616552055968571135475510700810219028167653516982373238641332965927953273383572708760984694356069974208844865675206339235758647159337463780100273189720943242182911961627806424621091859596571173867825568394327041453823674373002756096)
  309. query R
  310. SELECT * FROM a
  311. ----
  312. 142378208485490985369999605144727062141206925976498256305323716858805588894693616552055968571135475510700810219028167653516982373238641332965927953273383572708760984694356069974208844865675206339235758647159337463780100273189720943242182911961627806424621091859596571173867825568394327041453823674373002756096
  313. # Verify that NaNs are returned instead of invalid operation.
  314. query R
  315. SELECT 'inf'::decimal + '-inf'::decimal
  316. ----
  317. NaN