-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathround.sql
More file actions
106 lines (79 loc) · 3.2 KB
/
round.sql
File metadata and controls
106 lines (79 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.
statement
CREATE TABLE test_round(d double, i int) USING parquet
statement
INSERT INTO test_round VALUES (2.5, 0), (3.5, 0), (-2.5, 0), (123.456, 2), (123.456, -1), (NULL, 0), (cast('NaN' as double), 0), (cast('Infinity' as double), 0), (0.0, 0)
query
SELECT round(d, 0) FROM test_round WHERE i = 0
query
SELECT round(d, 2) FROM test_round WHERE i = 2
query
SELECT round(d, -1) FROM test_round WHERE i = -1
query
SELECT round(d) FROM test_round
-- literal + literal
query
SELECT round(123.456, 2), round(2.5, 0), round(3.5, 0), round(-2.5, 0), round(NULL, 0)
-- HALF_UP semantics: .5 always rounds away from zero
statement
CREATE TABLE test_round_half_up(d double) USING parquet
statement
INSERT INTO test_round_half_up VALUES (0.5), (1.5), (2.5), (-0.5), (-1.5), (-2.5)
query
SELECT d, round(d, 0) FROM test_round_half_up
-- various scales on a single value
query
SELECT round(123.456, 0), round(123.456, 1), round(123.456, 2), round(123.456, 3), round(123.456, 5)
query
SELECT round(123.456, -1), round(123.456, -2), round(123.456, -3)
-- special values
query
SELECT round(cast('NaN' as double), 2), round(cast('Infinity' as double), 2), round(cast('-Infinity' as double), 2)
query
SELECT round(0.0, 5), round(-0.0, 5)
-- very small values
query
SELECT round(1.0E-10, 15), round(1.0E-10, 10), round(1.0E-10, 5)
-- negative scale on doubles
query
SELECT round(9999.9, -1), round(9999.9, -2), round(9999.9, -3), round(9999.9, -4)
query
SELECT round(-9999.9, -1), round(-9999.9, -2), round(-9999.9, -3), round(-9999.9, -4)
-- float type
statement
CREATE TABLE test_round_float(f float) USING parquet
statement
INSERT INTO test_round_float VALUES (cast(2.5 as float)), (cast(3.5 as float)), (cast(-2.5 as float)), (cast(0.125 as float)), (cast(0.785 as float)), (cast(123.456 as float)), (cast('NaN' as float)), (cast('Infinity' as float)), (NULL)
query
SELECT round(f, 0) FROM test_round_float
query
SELECT round(f, 2) FROM test_round_float
query
SELECT round(f, -1) FROM test_round_float
-- BigDecimal rounding edge case from Spark
statement
CREATE TABLE test_round_edge(d double) USING parquet
statement
INSERT INTO test_round_edge VALUES (-5.81855622136895E8), (6.1317116247283497E18), (6.13171162472835E18)
query
SELECT round(d, 4), round(d, 5), round(d, 6) FROM test_round_edge
query
SELECT round('-8316362075006449156', -5)
-- round with column from table (not literals)
query
SELECT d, round(d, 0), round(d, 2), round(d, -1) FROM test_round