Skip to content

Commit

Permalink
language: Integers now use the smallest exact type (#184)
Browse files Browse the repository at this point in the history
> ISO/IEC 9075-2:2016(E), 5.3 <literal>:
> 22) The declared type of an <exact numeric literal> ENL is an
> implementation-defined exact numeric type whose scale is the number of
> <digit>s to the right of the <period>. There shall be an exact numeric
> type capable of representing the value of ENL exactly.

I misunderstood "numeric" to mean the NUMERIC type, but clearly they
mean any exact numeric type (which includes SMALLINT, INTEGER and
BIGINT).

Integers will now use the smallest type which makes casting up to
super-types much more straight forward.

Also, SQL tests will not stop on the first failure making it much easier
to cleanup multiple failing tests.
  • Loading branch information
elliotchance authored Dec 23, 2023
1 parent 50beee4 commit 1a22339
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 62 deletions.
2 changes: 1 addition & 1 deletion docs/sqlstate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ A client that receives this error should retry the transaction.
.. code-block:: sql
VALUES CAST(123 AS BOOLEAN);
-- error 42846: cannot coerce BIGINT to BOOLEAN
-- error 42846: cannot coerce SMALLINT to BOOLEAN
``42883`` function does not exist
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
10 changes: 5 additions & 5 deletions tests/arithmetic.sql
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/* types */
VALUES 1 + 2;
-- COL1: 3 (INTEGER)
-- COL1: 3 (SMALLINT)

/* types */
VALUES 1 - 2;
-- COL1: -1 (INTEGER)
-- COL1: -1 (SMALLINT)

/* types */
VALUES 2 * 3;
-- COL1: 6 (INTEGER)
-- COL1: 6 (SMALLINT)

/* types */
VALUES 6 / 2;
-- COL1: 3 (INTEGER)
-- COL1: 3 (SMALLINT)

/* types */
VALUES 1.2 + 2.4;
Expand Down Expand Up @@ -40,7 +40,7 @@ VALUES 2.5 / 0.0;

/* types */
VALUES -123;
-- COL1: -123 (NUMERIC)
-- COL1: -123 (SMALLINT)

/* types */
VALUES +1.23;
Expand Down
2 changes: 1 addition & 1 deletion tests/cast.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ VALUES CAST(123 AS SMALLINT);
-- COL1: 123 (SMALLINT)

VALUES CAST(123 AS BOOLEAN);
-- error 42846: cannot coerce NUMERIC to BOOLEAN
-- error 42846: cannot coerce SMALLINT to BOOLEAN

EXPLAIN VALUES CAST(123 AS SMALLINT);
-- EXPLAIN: VALUES (COL1 SMALLINT) = ROW(CAST(123 AS SMALLINT))
Expand Down
16 changes: 10 additions & 6 deletions tests/coalesce.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
EXPLAIN VALUES COALESCE(1);
-- EXPLAIN: VALUES (COL1 NUMERIC) = ROW(COALESCE(1))
-- EXPLAIN: VALUES (COL1 SMALLINT) = ROW(COALESCE(1))

EXPLAIN VALUES COALESCE(1, 2);
-- EXPLAIN: VALUES (COL1 NUMERIC) = ROW(COALESCE(1, 2))
-- EXPLAIN: VALUES (COL1 SMALLINT) = ROW(COALESCE(1, 2))

EXPLAIN VALUES COALESCE(1, 2, 3);
-- EXPLAIN: VALUES (COL1 NUMERIC) = ROW(COALESCE(1, 2, 3))
-- EXPLAIN: VALUES (COL1 SMALLINT) = ROW(COALESCE(1, 2, 3))

/* types */
VALUES COALESCE(1);
-- COL1: 1 (NUMERIC)
-- COL1: 1 (SMALLINT)

/* types */
VALUES COALESCE(1, 2);
-- COL1: 1 (NUMERIC)
-- COL1: 1 (SMALLINT)

/* types */
VALUES COALESCE(1, 200000000);
-- error 42804: data type mismatch in argument 2 of COALESCE: expected SMALLINT but got INTEGER

CREATE TABLE foo (f1 BIGINT);
INSERT INTO foo (f1) VALUES (NULL);
Expand All @@ -29,4 +33,4 @@ SELECT COALESCE(f1) FROM foo;
-- COL1: 3

VALUES COALESCE(1, 'hello');
-- error 42804: data type mismatch in argument 2 of COALESCE: expected NUMERIC but got CHARACTER
-- error 42804: data type mismatch in argument 2 of COALESCE: expected SMALLINT but got CHARACTER
2 changes: 1 addition & 1 deletion tests/concatenation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ VALUES 'foo' || 'bar';
-- COL1: foobar (CHARACTER VARYING(6))

VALUES 123 || 'bar';
-- error 42883: operator does not exist: NUMERIC || CHARACTER
-- error 42883: operator does not exist: SMALLINT || CHARACTER
2 changes: 1 addition & 1 deletion tests/fold.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ VALUES LOWER('Hello');
-- COL1: hello (CHARACTER VARYING(5))

VALUES UPPER(123);
-- error 42883: function does not exist: UPPER(DOUBLE PRECISION)
-- error 42883: function does not exist: UPPER(SMALLINT)

VALUES LOWER(TRUE);
-- error 42883: function does not exist: LOWER(BOOLEAN)
Expand Down
2 changes: 1 addition & 1 deletion tests/insert.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ INSERT INTO foo (x) VALUES (true);
CREATE TABLE foo (b BOOLEAN);
INSERT INTO foo (b) VALUES (123);
-- msg: CREATE TABLE 1
-- error 42846: cannot coerce NUMERIC to BOOLEAN
-- error 42846: cannot coerce SMALLINT to BOOLEAN

CREATE TABLE t1 (f1 CHARACTER VARYING(10), f2 FLOAT NOT NULL);
INSERT INTO t1 (f1, f2) VALUES ('a', 1.23);
Expand Down
59 changes: 55 additions & 4 deletions tests/literal.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* types */
VALUES 1;
-- COL1: 1 (NUMERIC)
-- COL1: 1 (SMALLINT)

/* types */
VALUES 1.23;
Expand All @@ -16,16 +16,67 @@ VALUES .23;

/* types */
VALUES 789;
-- COL1: 789 (NUMERIC)
-- COL1: 789 (SMALLINT)

/* types */
VALUES 'hello';
-- COL1: hello (CHARACTER(5))

/* types */
VALUES ROW(123, 456);
-- COL1: 123 (NUMERIC) COL2: 456 (NUMERIC)
-- COL1: 123 (SMALLINT) COL2: 456 (SMALLINT)

/* types */
VALUES ROW(2 + 3 * 5, (2 + 3) * 5);
-- COL1: 17 (INTEGER) COL2: 25 (INTEGER)
-- COL1: 17 (SMALLINT) COL2: 25 (SMALLINT)

-- # These are range tests to make sure that literals are given the correct
-- # smallest exact types.

/* types */
VALUES -9223372036854775809;
-- COL1: -9223372036854775809 (NUMERIC)

/* types */
VALUES -9223372036854775808;
-- COL1: -9223372036854775808 (BIGINT)

/* types */
VALUES -2147483649;
-- COL1: -2147483649 (BIGINT)

/* types */
VALUES -2147483648;
-- COL1: -2147483648 (INTEGER)

/* types */
VALUES -32769;
-- COL1: -32769 (INTEGER)

/* types */
VALUES -32768;
-- COL1: -32768 (SMALLINT)

/* types */
VALUES 32767;
-- COL1: 32767 (SMALLINT)

/* types */
VALUES 32768;
-- COL1: 32768 (INTEGER)

/* types */
VALUES 2147483647;
-- COL1: 2147483647 (INTEGER)

/* types */
VALUES 2147483648;
-- COL1: 2147483648 (BIGINT)

/* types */
VALUES 9223372036854775807;
-- COL1: 9223372036854775807 (BIGINT)

/* types */
VALUES 9223372036854775808;
-- COL1: 9223372036854775808 (NUMERIC)
6 changes: 3 additions & 3 deletions tests/nullif.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ EXPLAIN VALUES NULLIF(TRUE, TRUE);

/* types */
VALUES NULLIF(123, 123);
-- COL1: NULL (NUMERIC)
-- COL1: NULL (SMALLINT)

/* types */
VALUES NULLIF(123, 456);
-- COL1: 123 (NUMERIC)
-- COL1: 123 (SMALLINT)

VALUES NULLIF(123, 'hello');
-- error 42804: data type mismatch in NULLIF: expected NUMERIC but got CHARACTER(5)
-- error 42804: data type mismatch in NULLIF: expected SMALLINT but got CHARACTER(5)
42 changes: 21 additions & 21 deletions tests/values.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ SELECT * FROM (VALUES 1, 'foo', TRUE);

EXPLAIN SELECT * FROM (VALUES ROW(1, 'foo', TRUE));
-- EXPLAIN: $1:
-- EXPLAIN: VALUES (COL1 NUMERIC, COL2 CHARACTER VARYING(3), COL3 BOOLEAN) = ROW(1, 'foo', TRUE)
-- EXPLAIN: TABLE $1 (COL1 NUMERIC, COL2 CHARACTER VARYING(3), COL3 BOOLEAN)
-- EXPLAIN: EXPR ($1.COL1 NUMERIC, $1.COL2 CHARACTER VARYING(3), $1.COL3 BOOLEAN)
-- EXPLAIN: VALUES (COL1 SMALLINT, COL2 CHARACTER VARYING(3), COL3 BOOLEAN) = ROW(1, 'foo', TRUE)
-- EXPLAIN: TABLE $1 (COL1 SMALLINT, COL2 CHARACTER VARYING(3), COL3 BOOLEAN)
-- EXPLAIN: EXPR ($1.COL1 SMALLINT, $1.COL2 CHARACTER VARYING(3), $1.COL3 BOOLEAN)

EXPLAIN SELECT * FROM (VALUES 1, 'foo', TRUE) AS t1 (abc, col2, "f");
-- EXPLAIN: T1:
Expand All @@ -35,9 +35,9 @@ EXPLAIN SELECT * FROM (VALUES 1, 'foo', TRUE) AS t1 (abc, col2, "f");

EXPLAIN SELECT * FROM (VALUES ROW(1, 'foo', TRUE)) AS t1 (abc, col2, "f");
-- EXPLAIN: T1:
-- EXPLAIN: VALUES (ABC NUMERIC, COL2 CHARACTER VARYING(3), "f" BOOLEAN) = ROW(1, 'foo', TRUE)
-- EXPLAIN: TABLE T1 (ABC NUMERIC, COL2 CHARACTER VARYING(3), "f" BOOLEAN)
-- EXPLAIN: EXPR (T1.ABC NUMERIC, T1.COL2 CHARACTER VARYING(3), T1."f" BOOLEAN)
-- EXPLAIN: VALUES (ABC SMALLINT, COL2 CHARACTER VARYING(3), "f" BOOLEAN) = ROW(1, 'foo', TRUE)
-- EXPLAIN: TABLE T1 (ABC SMALLINT, COL2 CHARACTER VARYING(3), "f" BOOLEAN)
-- EXPLAIN: EXPR (T1.ABC SMALLINT, T1.COL2 CHARACTER VARYING(3), T1."f" BOOLEAN)

SELECT * FROM (VALUES 1, 'foo', TRUE) AS t1 (abc, col2, "f");
-- error 42601: syntax error: unknown column: T1.COL2
Expand Down Expand Up @@ -81,19 +81,19 @@ SELECT * FROM (VALUES ROW(123), ROW(456));

EXPLAIN SELECT * FROM (VALUES ROW(123), ROW(456));
-- EXPLAIN: $1:
-- EXPLAIN: VALUES (COL1 NUMERIC) = ROW(123), ROW(456)
-- EXPLAIN: TABLE $1 (COL1 NUMERIC)
-- EXPLAIN: EXPR ($1.COL1 NUMERIC)
-- EXPLAIN: VALUES (COL1 SMALLINT) = ROW(123), ROW(456)
-- EXPLAIN: TABLE $1 (COL1 SMALLINT)
-- EXPLAIN: EXPR ($1.COL1 SMALLINT)

SELECT * FROM (VALUES ROW(123, 'hi'), ROW(456, 'there'));
-- COL1: 123 COL2: hi
-- COL1: 456 COL2: there

EXPLAIN SELECT * FROM (VALUES ROW(123, 'hi'), ROW(456, 'there'));
-- EXPLAIN: $1:
-- EXPLAIN: VALUES (COL1 NUMERIC, COL2 CHARACTER VARYING(2)) = ROW(123, 'hi'), ROW(456, 'there')
-- EXPLAIN: TABLE $1 (COL1 NUMERIC, COL2 CHARACTER VARYING(2))
-- EXPLAIN: EXPR ($1.COL1 NUMERIC, $1.COL2 CHARACTER VARYING(2))
-- EXPLAIN: VALUES (COL1 SMALLINT, COL2 CHARACTER VARYING(2)) = ROW(123, 'hi'), ROW(456, 'there')
-- EXPLAIN: TABLE $1 (COL1 SMALLINT, COL2 CHARACTER VARYING(2))
-- EXPLAIN: EXPR ($1.COL1 SMALLINT, $1.COL2 CHARACTER VARYING(2))

SELECT * FROM (VALUES ROW(123, 'hi'), ROW(456, 'there')) AS foo (bar, baz);
-- BAR: 123 BAZ: hi
Expand All @@ -102,9 +102,9 @@ SELECT * FROM (VALUES ROW(123, 'hi'), ROW(456, 'there')) AS foo (bar, baz);
EXPLAIN SELECT *
FROM (VALUES ROW(123, 'hi'), ROW(456, 'there')) AS foo (bar, baz);
-- EXPLAIN: FOO:
-- EXPLAIN: VALUES (BAR NUMERIC, BAZ CHARACTER VARYING(2)) = ROW(123, 'hi'), ROW(456, 'there')
-- EXPLAIN: TABLE FOO (BAR NUMERIC, BAZ CHARACTER VARYING(2))
-- EXPLAIN: EXPR (FOO.BAR NUMERIC, FOO.BAZ CHARACTER VARYING(2))
-- EXPLAIN: VALUES (BAR SMALLINT, BAZ CHARACTER VARYING(2)) = ROW(123, 'hi'), ROW(456, 'there')
-- EXPLAIN: TABLE FOO (BAR SMALLINT, BAZ CHARACTER VARYING(2))
-- EXPLAIN: EXPR (FOO.BAR SMALLINT, FOO.BAZ CHARACTER VARYING(2))

SELECT * FROM (VALUES 1, 2) AS t1 (foo);
-- FOO: 1
Expand All @@ -126,10 +126,10 @@ SELECT * FROM (VALUES ROW(1), ROW(3, 4)) AS t1 (foo, bar);
EXPLAIN SELECT * FROM (VALUES ROW(1, 2), ROW(3, 4), ROW(5, 6)) AS t1 (foo, bar)
FETCH FIRST 2 ROWS ONLY;
-- EXPLAIN: T1:
-- EXPLAIN: VALUES (FOO NUMERIC, BAR NUMERIC) = ROW(1, 2), ROW(3, 4), ROW(5, 6)
-- EXPLAIN: TABLE T1 (FOO NUMERIC, BAR NUMERIC)
-- EXPLAIN: VALUES (FOO SMALLINT, BAR NUMERIC) = ROW(1, 2), ROW(3, 4), ROW(5, 6)
-- EXPLAIN: TABLE T1 (FOO SMALLINT, BAR NUMERIC)
-- EXPLAIN: FETCH FIRST 2 ROWS ONLY
-- EXPLAIN: EXPR (T1.FOO NUMERIC, T1.BAR NUMERIC)
-- EXPLAIN: EXPR (T1.FOO SMALLINT, T1.BAR NUMERIC)

SELECT * FROM (VALUES ROW(1, 2), ROW(3, 4), ROW(5, 6)) AS t1 (foo, bar)
FETCH FIRST 2 ROWS ONLY;
Expand All @@ -139,10 +139,10 @@ FETCH FIRST 2 ROWS ONLY;
EXPLAIN SELECT * FROM (VALUES ROW(1, 2), ROW(3, 4), ROW(5, 6)) AS t1 (foo, bar)
OFFSET 1 ROW;
-- EXPLAIN: T1:
-- EXPLAIN: VALUES (FOO NUMERIC, BAR NUMERIC) = ROW(1, 2), ROW(3, 4), ROW(5, 6)
-- EXPLAIN: TABLE T1 (FOO NUMERIC, BAR NUMERIC)
-- EXPLAIN: VALUES (FOO SMALLINT, BAR NUMERIC) = ROW(1, 2), ROW(3, 4), ROW(5, 6)
-- EXPLAIN: TABLE T1 (FOO SMALLINT, BAR NUMERIC)
-- EXPLAIN: OFFSET 1 ROWS
-- EXPLAIN: EXPR (T1.FOO NUMERIC, T1.BAR NUMERIC)
-- EXPLAIN: EXPR (T1.FOO SMALLINT, T1.BAR NUMERIC)

SELECT * FROM (VALUES ROW(1, 2), ROW(3, 4), ROW(5, 6)) AS t1 (foo, bar)
OFFSET 1 ROW;
Expand Down
1 change: 1 addition & 0 deletions vsql/sql_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ fn test_all() ! {
}
}

@[assert_continues]
fn run_single_test(test SQLTest, query_cache &QueryCache, verbose bool, filter_line int) ! {
if filter_line != 0 && test.line_number != filter_line {
if verbose {
Expand Down
Loading

0 comments on commit 1a22339

Please sign in to comment.