cockroachdb / cockroachdb/cockroach
sql: invalid cast of prepared statement argument does not match Postgres
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
Consider the empty table:
```sql
CREATE TABLE t (
b BOOL,
i INT
);
```
If we query the table with an invalid cast from a string to integer expression, we get an error:
```
SELECT * FROM t WHERE b AND i = 'a'::INT;
ERROR: could not parse "a" as type int: strconv.ParseInt: parsing "a": invalid syntax
```
The same error occurs in `EXPLAIN (OPT)`, suggesting that the error originates from the optimizer:
```
EXPLAIN (OPT) SELECT * FROM t WHERE b AND i = 'a'::INT;
ERROR: could not parse "a" as type int: strconv.ParseInt: parsing "a": invalid syntax
```
This is all expected behavior and it matches Postgres. However, if we perform the same invalid cast in a prepared statement, the query does not error:
```
PREPARE p(TEXT) AS SELECT * FROM t WHERE b AND i = $1::INT;
EXECUTE p('a');
b | i
----+----
(0 rows)
```
In Postgres, the `EXECUTE` statement fails with the error:
```
psql:cast.sql:13: ERROR: 22P02: invalid input syntax for type integer: "a"
```
We should match Postgres's behavior.
Jira issue: CRDB-28357
Contributor guide
Assessment
This issue has not been assessed yet.