Skip to content

Commit 31bf0e5

Browse files
committed
adapter: Don't panic when the same parameter is coerced twice
1 parent d2df862 commit 31bf0e5

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

src/sql/src/plan/typeconv.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use dynfmt::{Format, SimpleCurlyFormat};
1818
use itertools::Itertools;
1919
use mz_expr::func::{CastArrayToJsonb, CastListToJsonb};
2020
use mz_expr::{VariadicFunc, func};
21-
use mz_ore::assert_none;
2221
use mz_repr::{ColumnName, ColumnType, Datum, RelationType, ScalarBaseType, ScalarType};
2322

2423
use crate::catalog::TypeCategory;
@@ -1134,7 +1133,16 @@ pub fn plan_coerce<'a>(
11341133

11351134
Parameter(n) => {
11361135
let prev = ecx.param_types().borrow_mut().insert(n, coerce_to.clone());
1137-
assert_none!(prev);
1136+
if let Some(prev) = prev {
1137+
if prev != *coerce_to {
1138+
sql_bail!(
1139+
"there are contradicting constraints for the type of parameter ${}: should be both {} and {}",
1140+
n,
1141+
ecx.humanize_scalar_type(&prev, false),
1142+
ecx.humanize_scalar_type(&coerce_to, false),
1143+
);
1144+
}
1145+
}
11381146
HirScalarExpr::parameter(n)
11391147
}
11401148
})

test/sqllogictest/prepare.slt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
# For more prepared statement tests, see `order_by.slt` and `test_bind_params`.
11+
12+
statement ok
13+
PREPARE p1 AS
14+
SELECT $1 + $1::bigint;
15+
16+
query I
17+
EXECUTE p1(5);
18+
----
19+
10
20+
21+
statement ok
22+
PREPARE p2 AS
23+
SELECT $1::bigint + $1::bigint;
24+
25+
query I
26+
EXECUTE p2(7);
27+
----
28+
14
29+
30+
statement ok
31+
PREPARE p3 AS
32+
SELECT $1 || $1;
33+
34+
query T
35+
EXECUTE p3('abc');
36+
----
37+
abcabc
38+
39+
statement ok
40+
PREPARE p4 AS
41+
SELECT $1::text || $1::bigint::text;
42+
43+
query error db error: ERROR: invalid input syntax for type bigint: invalid digit found in string: "abc"
44+
EXECUTE p4('abc');
45+
46+
query T
47+
EXECUTE p4('123');
48+
----
49+
123123
50+
51+
statement ok
52+
PREPARE p5 AS
53+
SELECT $1, $1::bigint;
54+
55+
query II
56+
EXECUTE p5(7);
57+
----
58+
0
59+
7
60+
61+
query error db error: ERROR: operator does not exist: bigint \|\| bigint
62+
PREPARE p6 AS
63+
SELECT $1 + $1::bigint, $1 || $1;
64+
65+
query error db error: ERROR: operator does not exist: text \+ bigint
66+
PREPARE p7 AS
67+
SELECT $1 || $1, $1 + $1::bigint;
68+
69+
query error db error: ERROR: there are contradicting constraints for the type of parameter \$1: should be both text and integer
70+
PREPARE p AS SELECT repeat($1, $1);

0 commit comments

Comments
 (0)