# LogicTest: default

## Tests for ensuring that prepared statements can't get overwritten and for
## deallocate and deallocate all.
statement error prepared statement \"a\" does not exist
DEALLOCATE a

statement
PREPARE a AS SELECT 1

query I
EXECUTE a
----
1

query I
EXECUTE a
----
1

statement error prepared statement \"a\" already exists
PREPARE a AS SELECT 1

statement
DEALLOCATE a

statement error prepared statement \"a\" does not exist
DEALLOCATE a

statement error prepared statement \"a\" does not exist
EXECUTE a

statement
PREPARE a AS SELECT 1

statement
PREPARE b AS SELECT 1

query I
EXECUTE a
----
1

query I
EXECUTE b
----
1

statement ok
DEALLOCATE ALL

statement error prepared statement \"a\" does not exist
DEALLOCATE a

statement error prepared statement \"a\" does not exist
EXECUTE a

statement error prepared statement \"b\" does not exist
DEALLOCATE b

statement error prepared statement \"b\" does not exist
EXECUTE b

## Typing tests - no type hints
#
query error syntax error at or near \"\)\"
PREPARE a as ()

statement error could not determine data type of placeholder \$1
PREPARE a AS SELECT $1

statement
PREPARE a AS SELECT $1:::int + $2

query I
EXECUTE a(3, 1)
----
4

query error could not parse "foo" as type int
EXECUTE a('foo', 1)

query error incompatible type for EXECUTE parameter expression: int vs decimal
EXECUTE a(3.5, 1)

query error aggregate functions are not allowed in EXECUTE parameters
EXECUTE a(max(3), 1)

query error window functions are not allowed in EXECUTE parameters
EXECUTE a(rank() over (partition by 3), 1)

query error EXECUTE parameter expression '\(SELECT 3\)' may not contain variable sub-expressions
EXECUTE a((SELECT 3), 1)

query error wrong number of parameters for prepared statement \"a\": expected 2, got 3
EXECUTE a(1, 1, 1)

query error wrong number of parameters for prepared statement \"a\": expected 2, got 0
EXECUTE a

## Type hints

statement
PREPARE b (int) AS SELECT $1

query I
EXECUTE b(3)
----
3

query error could not parse "foo" as type int
EXECUTE b('foo')

statement
PREPARE allTypes(int, float, string, bytea, date, timestamp, timestamptz, bool, decimal) AS
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9

query IRTTTTTBR
EXECUTE allTypes(0, 0.0, 'foo', 'bar', '2017-08-08', '2015-08-30 03:34:45.34567', '2015-08-30 03:34:45.34567', true, 3.4)
----
0  0  foo  bar  2017-08-08 00:00:00 +0000 +0000  2015-08-30 03:34:45.34567 +0000 +0000  2015-08-30 03:34:45.34567 +0000 +0000  true  3.4

## Other

statement
PREPARE c AS SELECT COUNT(*)

query I
EXECUTE c
----
1

statement
CREATE TABLE t (a int)

statement
PREPARE i AS INSERT INTO t(a) VALUES($1) RETURNING $1 + 1

statement
PREPARE s AS SELECT * FROM t

query I
EXECUTE i(1)
----
2

query I
EXECUTE i(2)
----
3

query error could not parse "foo" as type int
EXECUTE i('foo')

query error incompatible type for EXECUTE parameter expression: int vs decimal
EXECUTE i(2.3)

query I
EXECUTE i(3.3::int)
----
4

query I
EXECUTE s
----
1
2
3

statement
DEALLOCATE ALL

# Regression test for #15970

statement
PREPARE x AS SELECT avg(column1) OVER (PARTITION BY column2) FROM (VALUES (1, 2), (3, 4))

query R
EXECUTE x
----
1
3

statement
PREPARE y AS SELECT avg(a.column1) OVER (PARTITION BY a.column2) FROM (VALUES (1, 2), (3, 4)) a

query R
EXECUTE y
----
1
3

statement
DEALLOCATE ALL

# Regression test for #16062

statement
CREATE TABLE IF NOT EXISTS f (v INT)

statement
PREPARE x AS SELECT * FROM f

statement
ALTER TABLE f ADD COLUMN u int

statement
INSERT INTO f VALUES (1, 2)

statement error cached plan must not change result type
EXECUTE x

# Ensure that plan changes prevent INSERTs from succeeding.

statement
PREPARE y AS INSERT INTO f VALUES ($1, $2) RETURNING *

statement
EXECUTE y (2, 3)

statement
ALTER TABLE f ADD COLUMN t int

statement error cached plan must not change result type
EXECUTE y (3, 4)

query III
SELECT * FROM f
----
1 2 NULL
2 3 NULL

# Ensure that we have a memory monitor for preparing statements

statement
PREPARE z AS SELECT UPPER('a')

# Ensure that GROUP BY HAVING doesn't mutate the parsed AST (#16388)
statement
CREATE TABLE foo (a int)

statement
PREPARE groupbyhaving AS SELECT min(1) FROM foo WHERE a = $1 GROUP BY a HAVING count(a) = 0

query I
EXECUTE groupbyhaving(1)
----

# Mismatch between expected and hinted types should prepare, but potentially
# fail to execute if the cast is not possible.
statement
PREPARE wrongTypePossibleCast(float) AS INSERT INTO foo VALUES ($1)

statement
EXECUTE wrongTypePossibleCast(2.3)

statement
PREPARE wrongTypeImpossibleCast(string) AS INSERT INTO foo VALUES ($1)

statement
EXECUTE wrongTypeImpossibleCast('3')

statement error could not parse "crabgas" as type int
EXECUTE wrongTypeImpossibleCast('crabgas')

# Check statement compatibility

statement ok
PREPARE s AS SELECT a FROM t; PREPARE p1 AS UPSERT INTO t(a) VALUES($1) RETURNING a

query I
EXECUTE s
----
1
2
3

query I
EXECUTE p1(123)
----
123

statement ok
PREPARE p2 AS UPDATE t SET a = a + $1 RETURNING a

query I
EXECUTE s
----
1
2
3
123

query I
EXECUTE p2(123)
----
124
125
126
246

statement ok
PREPARE p3 AS DELETE FROM t WHERE a = $1 RETURNING a

query I
EXECUTE s
----
124
125
126
246

query I
EXECUTE p3(124)
----
124

statement ok
PREPARE p4 AS CANCEL JOB $1

query error pq: job with ID 123 does not exist
EXECUTE p4(123)

statement ok
PREPARE p5 AS PAUSE JOB $1

query error pq: job with ID 123 does not exist
EXECUTE p5(123)

statement ok
PREPARE p6 AS RESUME JOB $1

query error pq: job with ID 123 does not exist
EXECUTE p6(123)

# Ensure that SET / SET CLUSTER SETTING know about placeholders
statement
PREPARE setp(string) AS SET application_name = $1

query T
SET application_name = 'foo'; SHOW application_name
----
foo

query T
EXECUTE setp('hello'); SHOW application_name
----
hello

# Note: we can't check the result of SET CLUSTER SETTING synchronously
# because it doesn't propagate immediately.

statement
PREPARE sets(string) AS SET CLUSTER SETTING cluster.organization = $1

statement
EXECUTE sets('hello')
