Skip to content

Commit 4edcd26

Browse files
authored
Merge pull request #33110 from ggevay/error-semantics-tests
Add tests for `coalesce`'s error semantics
2 parents 7605c6c + 1100d3c commit 4edcd26

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

test/sqllogictest/error_semantics.slt

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Copyright Materialize, Inc. and contributors. All rights reserved.
2+
#
3+
# Use of this software is governed by the Business Source License
4+
# included in the LICENSE file at the root of this repository.
5+
#
6+
# As of the Change Date specified in that file, in accordance with
7+
# the Business Source License, use of this software will be governed
8+
# by the Apache License, Version 2.0.
9+
10+
# This file is for error semantics and evaluation order/short-circuiting, see
11+
# https://github.com/MaterializeInc/database-issues/issues/4972
12+
#
13+
14+
########################################################################################################################
15+
# NOTE: THESE EXPECTED RESULTS ARE NOT SET IN STONE! The results, together with occasional comments above them, are our
16+
# current best guess of what our error semantics will be, but it's ok to change this for now as we learn more.
17+
########################################################################################################################
18+
19+
statement ok
20+
create table test (a int, b int);
21+
22+
statement ok
23+
insert into test values (1, 0);
24+
25+
query I
26+
select coalesce(a, 1/b) from test;
27+
----
28+
1
29+
30+
# Postgres errors on it, which we consider to be a bug in Postgres, because Postgres' coalesce docs say it should
31+
# short-circuit: https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL
32+
query I
33+
select coalesce(a, 1/0) from test;
34+
----
35+
1
36+
37+
query I
38+
select coalesce(a, 1/b) from test where b = 0;
39+
----
40+
1
41+
42+
query I
43+
select coalesce(7, 1/b) from test;
44+
----
45+
7
46+
47+
query I
48+
select coalesce(7, 1/0) from test;
49+
----
50+
7
51+
52+
statement ok
53+
create table test_nonnull (a int not null, b int);
54+
55+
statement ok
56+
insert into test_nonnull values (1, 0);
57+
58+
query I
59+
select coalesce(a, 1/b) from test_nonnull;
60+
----
61+
1
62+
63+
# Postgres errors, which we consider to be a bug in Postgres
64+
query I
65+
select coalesce(a, 1/0) from test_nonnull;
66+
----
67+
1
68+
69+
query I
70+
select coalesce(a, 1/b) from test_nonnull where b = 0;
71+
----
72+
1
73+
74+
query I
75+
select coalesce(7, 1/b) from test_nonnull;
76+
----
77+
7
78+
79+
query I
80+
select coalesce(7, 1/0) from test_nonnull;
81+
----
82+
7
83+
84+
# MFP CSE, see https://github.com/MaterializeInc/materialize/pull/33109
85+
query I
86+
select coalesce(a, a/b + 1, a/b + 2) from test;
87+
----
88+
1
89+
90+
# The following two results are probably wrong (but depends on what error semantics we agree on later).
91+
query error db error: ERROR: Evaluation error: division by zero
92+
select coalesce(a, (select a/b from test)) from test;
93+
94+
query error db error: ERROR: Evaluation error: division by zero
95+
select *, case when a = 5 then (select a/b from test) else 7 end from test;

test/sqllogictest/errors.slt renamed to test/sqllogictest/planning_errors.slt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
# the Business Source License, use of this software will be governed
88
# by the Apache License, Version 2.0.
99

10-
# This file is for SQL planning errors only. Parse errors should live in the
11-
# sql-parser test suite!
10+
# This file is for SQL planning errors only.
11+
# - Parse errors should live in the sql-parser test suite!
12+
# - Error semantics issues due to evaluation order live in `error_semantics.slt`
1213

1314
query error VALUES expression in FROM clause must be surrounded by parentheses
1415
SELECT * FROM VALUES (1)

0 commit comments

Comments
 (0)