Skip to content

Commit

Permalink
testing: Directive to expose types (#183)
Browse files Browse the repository at this point in the history
Use the `/* types */` directive to include each value type in the
output. This is useful to verify that literals or expressions are being
represented as the expected type.
  • Loading branch information
elliotchance authored Dec 23, 2023
1 parent 784da6b commit 50beee4
Show file tree
Hide file tree
Showing 28 changed files with 515 additions and 240 deletions.
13 changes: 13 additions & 0 deletions docs/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,19 @@ This will be replaced with the correct character before the test runs.

This is only a feature of SQL Tests, so will not work in any other context.

Exposing Types
^^^^^^^^^^^^^^

Use the ``/* types */`` directive to include each value type in the output. This
is useful to verify that literals or expressions are being represented as the
expected type.

.. code-block:: sql
/* types */
VALUES ROW(2 + 3 * 5, (2 + 3) * 5);
-- COL1: 17 (INTEGER) COL2: 25 (INTEGER)
Debugging Tests
---------------

Expand Down
37 changes: 25 additions & 12 deletions tests/arithmetic.sql
Original file line number Diff line number Diff line change
@@ -1,38 +1,51 @@
/* types */
VALUES 1 + 2;
-- COL1: 3
-- COL1: 3 (INTEGER)

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

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

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

/* types */
VALUES 1.2 + 2.4;
-- COL1: 3.6
-- COL1: 3.6 (DOUBLE PRECISION)

/* types */
VALUES 1.7 - 0.5;
-- COL1: 1.2
-- COL1: 1.2 (DOUBLE PRECISION)

/* types */
VALUES 2.2 * 3.3;
-- COL1: 7.26
-- COL1: 7.26 (DOUBLE PRECISION)

/* types */
VALUES 6.0 / 2.5;
-- COL1: 2.4
-- COL1: 2.4 (DOUBLE PRECISION)

/* types */
VALUES 0.0 / 2.5;
-- COL1: 0
-- COL1: 0 (DOUBLE PRECISION)

/* types */
VALUES 2.5 / 0.0;
-- error 22012: division by zero

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

/* types */
VALUES +1.23;
-- COL1: 1.23
-- COL1: 1.23 (NUMERIC)

/* types */
VALUES 1.5 + 2.4 * 7.0;
-- COL1: 18.3
-- COL1: 18.3 (DOUBLE PRECISION)
27 changes: 18 additions & 9 deletions tests/bigint.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,50 +64,59 @@ SELECT CAST(x AS BOOLEAN) FROM foo;
-- msg: INSERT 1
-- error 42846: cannot coerce BIGINT to BOOLEAN

/* types */
VALUES CAST(123 AS BIGINT) + 53;
-- COL1: 176
-- COL1: 176 (BIGINT)

/* types */
VALUES 53 + CAST(123 AS BIGINT);
-- COL1: 176
-- COL1: 176 (BIGINT)

VALUES CAST(5000000000000000000 AS BIGINT) + 5000000000000000000;
-- error 22003: numeric value out of range

VALUES 5000000000000000000 + CAST(5000000000000000000 AS BIGINT);
-- error 22003: numeric value out of range

/* types */
VALUES CAST(123 AS BIGINT) - 53;
-- COL1: 70
-- COL1: 70 (BIGINT)

/* types */
VALUES 53 - CAST(123 AS BIGINT);
-- COL1: -70
-- COL1: -70 (BIGINT)

VALUES CAST(-5000000000000000000 AS BIGINT) - 5000000000000000000;
-- error 22003: numeric value out of range

VALUES -5000000000000000000 - CAST(5000000000000000000 AS BIGINT);
-- error 22003: numeric value out of range

/* types */
VALUES CAST(123 AS BIGINT) * 53;
-- COL1: 6519
-- COL1: 6519 (BIGINT)

/* types */
VALUES -53 * CAST(123 AS BIGINT);
-- COL1: -6519
-- COL1: -6519 (BIGINT)

VALUES CAST(-5000000000000000000 AS BIGINT) * 200000;
-- error 22003: numeric value out of range

VALUES -5000000000000000000 * CAST(5000000000000000000 AS BIGINT);
-- error 22003: numeric value out of range

/* types */
VALUES CAST(123 AS BIGINT) / 53;
-- COL1: 2
-- COL1: 2 (BIGINT)

/* types */
VALUES -123 / CAST(53 AS BIGINT);
-- COL1: -2
-- COL1: -2 (BIGINT)

/* types */
VALUES -5000000000000000000 / CAST(3.2 AS BIGINT);
-- COL1: -1666666666666666666
-- COL1: -1666666666666666666 (BIGINT)

VALUES CAST(-5000000000000000000 AS BIGINT) / 0;
-- error 22012: division by zero
Expand Down
9 changes: 6 additions & 3 deletions tests/boolean.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ VALUES TRUE;
VALUES FALSE;
-- COL1: FALSE

/* types */
VALUES UNKNOWN;
-- COL1: UNKNOWN
-- COL1: UNKNOWN (BOOLEAN)

CREATE TABLE foo (x BOOLEAN);
INSERT INTO foo (x) VALUES (NULL);
Expand Down Expand Up @@ -47,8 +48,9 @@ VALUES TRUE AND UNKNOWN;
VALUES FALSE AND UNKNOWN;
-- COL1: FALSE

/* types */
VALUES UNKNOWN AND UNKNOWN;
-- COL1: UNKNOWN
-- COL1: UNKNOWN (BOOLEAN)

VALUES TRUE OR TRUE;
-- COL1: TRUE
Expand Down Expand Up @@ -155,5 +157,6 @@ VALUES NOT TRUE;
VALUES NOT FALSE;
-- COL1: TRUE

/* types */
VALUES NOT UNKNOWN;
-- COL1: UNKNOWN
-- COL1: UNKNOWN (BOOLEAN)
24 changes: 16 additions & 8 deletions tests/cast.sql
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
/* types */
VALUES ROW(CAST('abc' AS CHARACTER(3)), CHARACTER_LENGTH(CAST('abc' AS CHARACTER(3))));
-- COL1: abc COL2: 3
-- COL1: abc (CHARACTER(3)) COL2: 3 (INTEGER)

VALUES ROW(CAST('abcde' AS CHARACTER(3)), CHARACTER_LENGTH(CAST('abcde' AS CHARACTER(3))));
-- error 22001: string data right truncation for CHARACTER(3)

/* types */
VALUES ROW(CAST('abc ' AS CHARACTER(3)), CHARACTER_LENGTH(CAST('abc ' AS CHARACTER(3))));
-- COL1: abc COL2: 3
-- COL1: abc (CHARACTER(3)) COL2: 3 (INTEGER)

/* types */
VALUES ROW(CAST('abc ' AS CHARACTER(3)), CHARACTER_LENGTH(CAST('abc ' AS CHARACTER(3))));
-- COL1: abc COL2: 3
-- COL1: abc (CHARACTER(3)) COL2: 3 (INTEGER)

VALUES ROW(CAST('abc d' AS CHARACTER(3)), CHARACTER_LENGTH(CAST('abc d' AS CHARACTER(3))));
-- error 22001: string data right truncation for CHARACTER(3)

/* types */
VALUES ROW(CAST('abc' AS CHARACTER(4)), CHARACTER_LENGTH(CAST('abc' AS CHARACTER(4))));
-- COL1: abc COL2: 4
-- COL1: abc (CHARACTER(4)) COL2: 4 (INTEGER)

/* types */
VALUES CAST(123 AS CHARACTER(3));
-- COL1: 123
-- COL1: 123 (CHARACTER(3))

VALUES CAST(1.23 AS CHARACTER(3));
-- error 22001: string data right truncation for CHARACTER(3)

/* types */
VALUES ROW(CAST('abc' AS VARCHAR(3)), CHARACTER_LENGTH(CAST('abc' AS VARCHAR(3))));
-- COL1: abc COL2: 3
-- COL1: abc (CHARACTER VARYING(3)) COL2: 3 (INTEGER)

/* types */
VALUES ROW(CAST('abc ' AS VARCHAR(20)), CHARACTER_LENGTH(CAST('abc ' AS VARCHAR(20))));
-- COL1: abc COL2: 4
-- COL1: abc (CHARACTER VARYING(4)) COL2: 4 (INTEGER)

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

VALUES CAST(123 AS BOOLEAN);
-- error 42846: cannot coerce NUMERIC to BOOLEAN
Expand Down
6 changes: 4 additions & 2 deletions tests/coalesce.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ EXPLAIN VALUES COALESCE(1, 2);
EXPLAIN VALUES COALESCE(1, 2, 3);
-- EXPLAIN: VALUES (COL1 NUMERIC) = ROW(COALESCE(1, 2, 3))

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

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

CREATE TABLE foo (f1 BIGINT);
INSERT INTO foo (f1) VALUES (NULL);
Expand Down
3 changes: 2 additions & 1 deletion tests/concatenation.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* types */
VALUES 'foo' || 'bar';
-- COL1: foobar
-- COL1: foobar (CHARACTER VARYING(6))

VALUES 123 || 'bar';
-- error 42883: operator does not exist: NUMERIC || CHARACTER
Loading

0 comments on commit 50beee4

Please sign in to comment.