Skip to content

Commit

Permalink
feat: sqlness for decimal128 (#2822)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuenKar authored Dec 4, 2023
1 parent de41646 commit dac7a41
Show file tree
Hide file tree
Showing 12 changed files with 2,055 additions and 0 deletions.
104 changes: 104 additions & 0 deletions tests/cases/standalone/common/types/decimal/decimal_aggregates.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
-- Test aggregation functions with decimal
-- Port from https://github.com/duckdb/duckdb/blob/main/test/sql/types/decimal/decimal_aggregates.test
SELECT arrow_typeof(FIRST_VALUE('0.1'::DECIMAL(4,1)));

+----------------------------------------+
| arrow_typeof(FIRST_VALUE(Utf8("0.1"))) |
+----------------------------------------+
| Decimal128(4, 1) |
+----------------------------------------+

-- first_value
SELECT FIRST_VALUE(NULL::DECIMAL),
FIRST_VALUE('0.1'::DECIMAL(4,1))::VARCHAR,
FIRST_VALUE('4938245.1'::DECIMAL(9,1))::VARCHAR,
FIRST_VALUE('45672564564938245.1'::DECIMAL(18,1))::VARCHAR,
FIRST_VALUE('4567645908450368043562342564564938245.1'::DECIMAL(38,1))::VARCHAR;

+-------------------+--------------------------+--------------------------------+------------------------------------------+--------------------------------------------------------------+
| FIRST_VALUE(NULL) | FIRST_VALUE(Utf8("0.1")) | FIRST_VALUE(Utf8("4938245.1")) | FIRST_VALUE(Utf8("45672564564938245.1")) | FIRST_VALUE(Utf8("4567645908450368043562342564564938245.1")) |
+-------------------+--------------------------+--------------------------------+------------------------------------------+--------------------------------------------------------------+
| | 0.1 | 4938245.1 | 45672564564938245.1 | 4567645908450368043562342564564938245.1 |
+-------------------+--------------------------+--------------------------------+------------------------------------------+--------------------------------------------------------------+

-- min
SELECT MIN(NULL::DECIMAL),
MIN('0.1'::DECIMAL(4,1))::VARCHAR,
MIN('4938245.1'::DECIMAL(9,1))::VARCHAR,
MIN('45672564564938245.1'::DECIMAL(18,1))::VARCHAR,
MIN('4567645908450368043562342564564938245.1'::DECIMAL(38,1))::VARCHAR;

+-----------+------------------+------------------------+----------------------------------+------------------------------------------------------+
| MIN(NULL) | MIN(Utf8("0.1")) | MIN(Utf8("4938245.1")) | MIN(Utf8("45672564564938245.1")) | MIN(Utf8("4567645908450368043562342564564938245.1")) |
+-----------+------------------+------------------------+----------------------------------+------------------------------------------------------+
| | 0.1 | 4938245.1 | 45672564564938245.1 | 4567645908450368043562342564564938245.1 |
+-----------+------------------+------------------------+----------------------------------+------------------------------------------------------+

-- max
SELECT MAX(NULL::DECIMAL),
MAX('0.1'::DECIMAL(4,1))::VARCHAR,
MAX('4938245.1'::DECIMAL(9,1))::VARCHAR,
MAX('45672564564938245.1'::DECIMAL(18,1))::VARCHAR,
MAX('4567645908450368043562342564564938245.1'::DECIMAL(38,1))::VARCHAR;

+-----------+------------------+------------------------+----------------------------------+------------------------------------------------------+
| MAX(NULL) | MAX(Utf8("0.1")) | MAX(Utf8("4938245.1")) | MAX(Utf8("45672564564938245.1")) | MAX(Utf8("4567645908450368043562342564564938245.1")) |
+-----------+------------------+------------------------+----------------------------------+------------------------------------------------------+
| | 0.1 | 4938245.1 | 45672564564938245.1 | 4567645908450368043562342564564938245.1 |
+-----------+------------------+------------------------+----------------------------------+------------------------------------------------------+

-- sum
SELECT SUM(NULL::DECIMAL),
SUM('0.1'::DECIMAL(4,1))::VARCHAR,
SUM('4938245.1'::DECIMAL(9,1))::VARCHAR,
SUM('45672564564938245.1'::DECIMAL(18,1))::VARCHAR,
SUM('4567645908450368043562342564564938245.1'::DECIMAL(38,1))::VARCHAR;

+-----------+------------------+------------------------+----------------------------------+------------------------------------------------------+
| SUM(NULL) | SUM(Utf8("0.1")) | SUM(Utf8("4938245.1")) | SUM(Utf8("45672564564938245.1")) | SUM(Utf8("4567645908450368043562342564564938245.1")) |
+-----------+------------------+------------------------+----------------------------------+------------------------------------------------------+
| | 0.1 | 4938245.1 | 45672564564938245.1 | 4567645908450368043562342564564938245.1 |
+-----------+------------------+------------------------+----------------------------------+------------------------------------------------------+

-- decimal aggregates over a table
CREATE TABLE decimals (
d1 DECIMAL(4,1),
d2 DECIMAL(9,1),
d3 DECIMAL(18,1),
d4 DECIMAL(38,1),
ts timestamp time index,
);

Affected Rows: 0

INSERT INTO decimals values
(123,123*123,123*123*123,123*123*123*123,1000),
(456,456*456,456*456*456,456*456*456*456,2000),
(789,789*789,789*789*789,789*789*789*789,3000);

Affected Rows: 3

SELECT SUM(d1)::VARCHAR, SUM(d2)::VARCHAR, SUM(d3)::VARCHAR, SUM(d4)::VARCHAR FROM decimals;

+------------------+------------------+------------------+------------------+
| SUM(decimals.d1) | SUM(decimals.d2) | SUM(decimals.d3) | SUM(decimals.d4) |
+------------------+------------------+------------------+------------------+
| 1368.0 | 845586.0 | 587848752.0 | 430998662178.0 |
+------------------+------------------+------------------+------------------+

INSERT INTO decimals VALUES ('0.1', '0.1', '0.1', '0.1', 4000), ('0.2', '0.2', '0.2', '0.2', 5000);

Affected Rows: 2

SELECT SUM(d1)::VARCHAR, SUM(d2)::VARCHAR, SUM(d3)::VARCHAR, SUM(d4)::VARCHAR FROM decimals;

+------------------+------------------+------------------+------------------+
| SUM(decimals.d1) | SUM(decimals.d2) | SUM(decimals.d3) | SUM(decimals.d4) |
+------------------+------------------+------------------+------------------+
| 1368.3 | 845586.3 | 587848752.3 | 430998662178.3 |
+------------------+------------------+------------------+------------------+

DROP TABLE decimals;

Affected Rows: 0

59 changes: 59 additions & 0 deletions tests/cases/standalone/common/types/decimal/decimal_aggregates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
-- Test aggregation functions with decimal
-- Port from https://github.com/duckdb/duckdb/blob/main/test/sql/types/decimal/decimal_aggregates.test

SELECT arrow_typeof(FIRST_VALUE('0.1'::DECIMAL(4,1)));

-- first_value

SELECT FIRST_VALUE(NULL::DECIMAL),
FIRST_VALUE('0.1'::DECIMAL(4,1))::VARCHAR,
FIRST_VALUE('4938245.1'::DECIMAL(9,1))::VARCHAR,
FIRST_VALUE('45672564564938245.1'::DECIMAL(18,1))::VARCHAR,
FIRST_VALUE('4567645908450368043562342564564938245.1'::DECIMAL(38,1))::VARCHAR;

-- min

SELECT MIN(NULL::DECIMAL),
MIN('0.1'::DECIMAL(4,1))::VARCHAR,
MIN('4938245.1'::DECIMAL(9,1))::VARCHAR,
MIN('45672564564938245.1'::DECIMAL(18,1))::VARCHAR,
MIN('4567645908450368043562342564564938245.1'::DECIMAL(38,1))::VARCHAR;

-- max

SELECT MAX(NULL::DECIMAL),
MAX('0.1'::DECIMAL(4,1))::VARCHAR,
MAX('4938245.1'::DECIMAL(9,1))::VARCHAR,
MAX('45672564564938245.1'::DECIMAL(18,1))::VARCHAR,
MAX('4567645908450368043562342564564938245.1'::DECIMAL(38,1))::VARCHAR;

-- sum

SELECT SUM(NULL::DECIMAL),
SUM('0.1'::DECIMAL(4,1))::VARCHAR,
SUM('4938245.1'::DECIMAL(9,1))::VARCHAR,
SUM('45672564564938245.1'::DECIMAL(18,1))::VARCHAR,
SUM('4567645908450368043562342564564938245.1'::DECIMAL(38,1))::VARCHAR;

-- decimal aggregates over a table

CREATE TABLE decimals (
d1 DECIMAL(4,1),
d2 DECIMAL(9,1),
d3 DECIMAL(18,1),
d4 DECIMAL(38,1),
ts timestamp time index,
);

INSERT INTO decimals values
(123,123*123,123*123*123,123*123*123*123,1000),
(456,456*456,456*456*456,456*456*456*456,2000),
(789,789*789,789*789*789,789*789*789*789,3000);

SELECT SUM(d1)::VARCHAR, SUM(d2)::VARCHAR, SUM(d3)::VARCHAR, SUM(d4)::VARCHAR FROM decimals;

INSERT INTO decimals VALUES ('0.1', '0.1', '0.1', '0.1', 4000), ('0.2', '0.2', '0.2', '0.2', 5000);

SELECT SUM(d1)::VARCHAR, SUM(d2)::VARCHAR, SUM(d3)::VARCHAR, SUM(d4)::VARCHAR FROM decimals;

DROP TABLE decimals;
Loading

0 comments on commit dac7a41

Please sign in to comment.