prepare.slt 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. # Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # This file is derived from the logic test suite in CockroachDB. The
  12. # original file was retrieved on June 10, 2019 from:
  13. #
  14. # https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/prepare
  15. #
  16. # The original source code is subject to the terms of the Apache
  17. # 2.0 license, a copy of which can be found in the LICENSE file at the
  18. # root of this repository.
  19. # not supported yet
  20. halt
  21. mode cockroach
  22. ## Tests for ensuring that prepared statements can't get overwritten and for
  23. ## deallocate and deallocate all.
  24. statement error prepared statement \"a\" does not exist
  25. DEALLOCATE a
  26. statement
  27. PREPARE a AS SELECT 1
  28. query I
  29. EXECUTE a
  30. ----
  31. 1
  32. query I
  33. EXECUTE a
  34. ----
  35. 1
  36. statement error prepared statement \"a\" already exists
  37. PREPARE a AS SELECT 1
  38. statement
  39. DEALLOCATE a
  40. statement error prepared statement \"a\" does not exist
  41. DEALLOCATE a
  42. statement error prepared statement \"a\" does not exist
  43. EXECUTE a
  44. statement
  45. PREPARE a AS SELECT 1
  46. statement
  47. PREPARE b AS SELECT 1
  48. query I
  49. EXECUTE a
  50. ----
  51. 1
  52. query I
  53. EXECUTE b
  54. ----
  55. 1
  56. statement ok
  57. DEALLOCATE ALL
  58. statement error prepared statement \"a\" does not exist
  59. DEALLOCATE a
  60. statement error prepared statement \"a\" does not exist
  61. EXECUTE a
  62. statement error prepared statement \"b\" does not exist
  63. DEALLOCATE b
  64. statement error prepared statement \"b\" does not exist
  65. EXECUTE b
  66. ## Typing tests - no type hints
  67. #
  68. query error syntax error at or near \"\)\"
  69. PREPARE a as ()
  70. statement error could not determine data type of placeholder \$1
  71. PREPARE a AS SELECT $1
  72. statement error could not determine data type of placeholder \$1
  73. PREPARE a AS SELECT $2:::int
  74. statement error could not determine data type of placeholder \$2
  75. PREPARE a AS SELECT $1:::int, $3:::int
  76. statement ok
  77. PREPARE a AS SELECT $1:::int + $2
  78. query I
  79. EXECUTE a(3, 1)
  80. ----
  81. 4
  82. query error could not parse "foo" as type int
  83. EXECUTE a('foo', 1)
  84. query error expected EXECUTE parameter expression to have type int, but '3.5' has type decimal
  85. EXECUTE a(3.5, 1)
  86. query error aggregate functions are not allowed in EXECUTE parameter
  87. EXECUTE a(max(3), 1)
  88. query error window functions are not allowed in EXECUTE parameter
  89. EXECUTE a(rank() over (partition by 3), 1)
  90. query error variable sub-expressions are not allowed in EXECUTE parameter
  91. EXECUTE a((SELECT 3), 1)
  92. query error wrong number of parameters for prepared statement \"a\": expected 2, got 3
  93. EXECUTE a(1, 1, 1)
  94. query error wrong number of parameters for prepared statement \"a\": expected 2, got 0
  95. EXECUTE a
  96. # Regression test for cockroach#36153.
  97. statement error unknown signature: array_length\(int, int\)
  98. PREPARE fail AS SELECT array_length($1, 1)
  99. ## Type hints
  100. statement
  101. PREPARE b (int) AS SELECT $1
  102. query I
  103. EXECUTE b(3)
  104. ----
  105. 3
  106. query error could not parse "foo" as type int
  107. EXECUTE b('foo')
  108. statement
  109. PREPARE allTypes(int, float, string, bytea, date, timestamp, timestamptz, bool, decimal) AS
  110. SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9
  111. query IRTTTTTBR
  112. 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)
  113. ----
  114. 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 UTC true 3.4
  115. ## Other
  116. statement
  117. PREPARE c AS SELECT count(*)
  118. query I
  119. EXECUTE c
  120. ----
  121. 1
  122. statement
  123. CREATE TABLE t (a int)
  124. statement
  125. PREPARE i AS INSERT INTO t(a) VALUES($1) RETURNING $1 + 1
  126. statement
  127. PREPARE s AS SELECT * FROM t
  128. query I
  129. EXECUTE i(1)
  130. ----
  131. 2
  132. query I
  133. EXECUTE i(2)
  134. ----
  135. 3
  136. query error could not parse "foo" as type int
  137. EXECUTE i('foo')
  138. query error expected EXECUTE parameter expression to have type int, but '2.3' has type decimal
  139. EXECUTE i(2.3)
  140. query I
  141. EXECUTE i(3.3::int)
  142. ----
  143. 4
  144. query I colnames
  145. EXECUTE s
  146. ----
  147. a
  148. 1
  149. 2
  150. 3
  151. # DISCARD ROWS drops the results, but does not affect the schema or the
  152. # internal plan.
  153. query I colnames
  154. EXECUTE s DISCARD ROWS
  155. ----
  156. a
  157. statement
  158. DEALLOCATE ALL
  159. # Regression test for materialize#15970
  160. statement
  161. PREPARE x AS SELECT avg(column1) OVER (PARTITION BY column2) FROM (VALUES (1, 2), (3, 4))
  162. query R rowsort
  163. EXECUTE x
  164. ----
  165. 1
  166. 3
  167. statement
  168. PREPARE y AS SELECT avg(a.column1) OVER (PARTITION BY a.column2) FROM (VALUES (1, 2), (3, 4)) a
  169. query R rowsort
  170. EXECUTE y
  171. ----
  172. 1
  173. 3
  174. statement
  175. DEALLOCATE ALL
  176. # Regression test for database-issues#4634
  177. statement
  178. CREATE TABLE IF NOT EXISTS f (v INT)
  179. statement
  180. PREPARE x AS SELECT * FROM f
  181. statement
  182. ALTER TABLE f ADD COLUMN u int
  183. statement
  184. INSERT INTO f VALUES (1, 2)
  185. statement error cached plan must not change result type
  186. EXECUTE x
  187. # Ensure that plan changes prevent INSERTs from succeeding.
  188. statement
  189. PREPARE y AS INSERT INTO f VALUES ($1, $2) RETURNING *
  190. statement
  191. EXECUTE y (2, 3)
  192. statement
  193. ALTER TABLE f ADD COLUMN t int
  194. statement error cached plan must not change result type
  195. EXECUTE y (3, 4)
  196. query III
  197. SELECT * FROM f
  198. ----
  199. 1 2 NULL
  200. 2 3 NULL
  201. # Ensure that we have a memory monitor for preparing statements
  202. statement
  203. PREPARE z AS SELECT upper('a')
  204. # Ensure that GROUP BY HAVING doesn't mutate the parsed AST (materialize#16388)
  205. statement
  206. CREATE TABLE foo (a int)
  207. statement
  208. PREPARE groupbyhaving AS SELECT min(1) FROM foo WHERE a = $1 GROUP BY a HAVING count(a) = 0
  209. query I
  210. EXECUTE groupbyhaving(1)
  211. ----
  212. # Mismatch between expected and hinted types should prepare, but potentially
  213. # fail to execute if the cast is not possible.
  214. statement
  215. PREPARE wrongTypePossibleCast(float) AS INSERT INTO foo VALUES ($1)
  216. statement
  217. EXECUTE wrongTypePossibleCast(2.3)
  218. statement
  219. PREPARE wrongTypeImpossibleCast(string) AS INSERT INTO foo VALUES ($1)
  220. statement
  221. EXECUTE wrongTypeImpossibleCast('3')
  222. statement error could not parse "crabgas" as type int
  223. EXECUTE wrongTypeImpossibleCast('crabgas')
  224. # Check statement compatibility
  225. statement ok
  226. PREPARE s AS SELECT a FROM t; PREPARE p1 AS UPSERT INTO t(a) VALUES($1) RETURNING a
  227. query I
  228. EXECUTE s
  229. ----
  230. 1
  231. 2
  232. 3
  233. query I
  234. EXECUTE p1(123)
  235. ----
  236. 123
  237. statement ok
  238. PREPARE p2 AS UPDATE t SET a = a + $1 RETURNING a
  239. query I
  240. EXECUTE s
  241. ----
  242. 1
  243. 2
  244. 3
  245. 123
  246. query I
  247. EXECUTE p2(123)
  248. ----
  249. 124
  250. 125
  251. 126
  252. 246
  253. statement ok
  254. PREPARE p3 AS DELETE FROM t WHERE a = $1 RETURNING a
  255. query I
  256. EXECUTE s
  257. ----
  258. 124
  259. 125
  260. 126
  261. 246
  262. query I
  263. EXECUTE p3(124)
  264. ----
  265. 124
  266. statement ok
  267. PREPARE p4 AS CANCEL JOB $1
  268. query error pq: job with ID 123 does not exist
  269. EXECUTE p4(123)
  270. statement ok
  271. PREPARE p5 AS PAUSE JOB $1
  272. query error pq: job with ID 123 does not exist
  273. EXECUTE p5(123)
  274. statement ok
  275. PREPARE p6 AS RESUME JOB $1
  276. query error pq: job with ID 123 does not exist
  277. EXECUTE p6(123)
  278. # Ensure that SET / SET CLUSTER SETTING know about placeholders
  279. statement ok
  280. PREPARE setp(string) AS SET application_name = $1
  281. query T
  282. SET application_name = 'foo'; SHOW application_name
  283. ----
  284. foo
  285. query T
  286. EXECUTE setp('hello'); SHOW application_name
  287. ----
  288. hello
  289. # Note: we can't check the result of SET CLUSTER SETTING synchronously
  290. # because it doesn't propagate immediately.
  291. statement ok
  292. PREPARE sets(string) AS SET CLUSTER SETTING cluster.organization = $1
  293. statement ok
  294. EXECUTE sets('hello')
  295. # materialize#19597
  296. statement error could not determine data type of placeholder
  297. PREPARE x19597 AS SELECT $1 IN ($2, null);
  298. statement error multiple conflicting type annotations around \$1
  299. PREPARE invalid AS SELECT $1:::int + $1:::float
  300. statement error type annotation around \$1 conflicts with specified type int
  301. PREPARE invalid (int) AS SELECT $1:::float
  302. statement ok
  303. PREPARE innerStmt AS SELECT $1:::int i, 'foo' t
  304. statement error syntax error at or near "execute"
  305. PREPARE outerStmt AS SELECT * FROM [EXECUTE innerStmt(3)] WHERE t = $1
  306. query error syntax error at or near "execute"
  307. SELECT * FROM [EXECUTE innerStmt(1)] CROSS JOIN [EXECUTE x]
  308. statement ok
  309. PREPARE selectin AS SELECT 1 in ($1, $2)
  310. statement ok
  311. PREPARE selectin2 AS SELECT $1::int in ($2, $3)
  312. query B
  313. EXECUTE selectin(5, 1)
  314. ----
  315. true
  316. query B
  317. EXECUTE selectin2(1, 5, 1)
  318. ----
  319. true
  320. # Regression tests for materialize#21701.
  321. statement ok
  322. CREATE TABLE kv (k INT PRIMARY KEY, v INT)
  323. statement ok
  324. INSERT INTO kv VALUES (1, 1), (2, 2), (3, 3)
  325. statement ok
  326. PREPARE x21701a AS SELECT * FROM kv WHERE k = $1
  327. query II
  328. EXECUTE x21701a(NULL)
  329. ----
  330. statement ok
  331. PREPARE x21701b AS SELECT * FROM kv WHERE k IS DISTINCT FROM $1
  332. query II
  333. EXECUTE x21701b(NULL)
  334. ----
  335. 1 1
  336. 2 2
  337. 3 3
  338. statement ok
  339. PREPARE x21701c AS SELECT * FROM kv WHERE k IS NOT DISTINCT FROM $1
  340. query II
  341. EXECUTE x21701c(NULL)
  342. ----
  343. statement ok
  344. DROP TABLE kv
  345. # Test that a PREPARE statement after a CREATE TABLE in the same TRANSACTION
  346. # doesn't hang.
  347. subtest 24578
  348. statement ok
  349. BEGIN TRANSACTION
  350. statement ok
  351. create table bar (id integer)
  352. statement ok
  353. PREPARE forbar AS insert into bar (id) VALUES (1)
  354. statement ok
  355. COMMIT TRANSACTION
  356. # Test placeholder in aggregate.
  357. statement ok
  358. CREATE TABLE aggtab (a INT PRIMARY KEY);
  359. INSERT INTO aggtab (a) VALUES (1)
  360. statement ok
  361. PREPARE aggprep AS SELECT max(a + $1:::int) FROM aggtab
  362. query I
  363. EXECUTE aggprep(10)
  364. ----
  365. 11
  366. query I
  367. EXECUTE aggprep(20)
  368. ----
  369. 21
  370. # Test placeholder in subquery, where the placeholder will be constant folded
  371. # and then used to select an index.
  372. statement ok
  373. CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT);
  374. CREATE TABLE xyz (x INT PRIMARY KEY, y INT, z INT, INDEX(y));
  375. INSERT INTO abc (a, b, c) VALUES (1, 10, 100);
  376. INSERT INTO xyz (x, y, z) VALUES (1, 5, 50);
  377. INSERT INTO xyz (x, y, z) VALUES (2, 6, 60);
  378. statement ok
  379. PREPARE subqueryprep AS SELECT * FROM abc WHERE EXISTS(SELECT * FROM xyz WHERE y IN ($1 + 1))
  380. query III
  381. EXECUTE subqueryprep(4)
  382. ----
  383. 1 10 100
  384. query III
  385. EXECUTE subqueryprep(5)
  386. ----
  387. 1 10 100
  388. query III
  389. EXECUTE subqueryprep(6)
  390. ----
  391. #
  392. # Test prepared statements that rely on context, and ensure they are invalidated
  393. # when that context changes.
  394. #
  395. statement ok
  396. CREATE DATABASE otherdb
  397. statement ok
  398. USE otherdb
  399. statement ok
  400. CREATE TABLE othertable (a INT PRIMARY KEY, b INT); INSERT INTO othertable (a, b) VALUES (1, 10)
  401. ## Current database change: Use current_database function, and ensure its return
  402. ## value changes when current database changes.
  403. statement ok
  404. PREPARE change_db AS SELECT current_database()
  405. query T
  406. EXECUTE change_db
  407. ----
  408. otherdb
  409. statement ok
  410. USE test
  411. query T
  412. EXECUTE change_db
  413. ----
  414. test
  415. statement ok
  416. USE otherdb
  417. ## Name resolution change: Query table in current database. Ensure that it is
  418. ## not visible in another database.
  419. statement ok
  420. PREPARE change_db_2 AS SELECT * FROM othertable
  421. query II
  422. EXECUTE change_db_2
  423. ----
  424. 1 10
  425. statement ok
  426. USE test
  427. query error pq: relation "othertable" does not exist
  428. EXECUTE change_db_2
  429. statement ok
  430. CREATE TABLE othertable (a INT PRIMARY KEY, b INT); INSERT INTO othertable (a, b) VALUES (2, 20)
  431. query II
  432. EXECUTE change_db_2
  433. ----
  434. 2 20
  435. # Same test with a query which refers to the same table twice initially, but
  436. # later the two tables are different.
  437. statement ok
  438. PREPARE change_db_3 AS SELECT * from othertable AS t1, test.othertable AS t2
  439. query IIII
  440. EXECUTE change_db_3
  441. ----
  442. 2 20 2 20
  443. statement ok
  444. USE otherdb
  445. query IIII
  446. EXECUTE change_db_3
  447. ----
  448. 1 10 2 20
  449. statement ok
  450. DROP TABLE test.othertable
  451. ## Search path change: Change the search path and ensure that the prepared plan
  452. ## is invalidated.
  453. statement ok
  454. PREPARE change_search_path AS SELECT * FROM othertable
  455. query II
  456. EXECUTE change_search_path
  457. ----
  458. 1 10
  459. statement ok
  460. SET search_path = pg_catalog
  461. query error pq: relation "othertable" does not exist
  462. EXECUTE change_search_path
  463. ## New table in search path: check tricky case where originally resolved table
  464. ## still exists but re-resolving with new search path yields another table.
  465. statement ok
  466. SET search_path=public,pg_catalog
  467. # During prepare, pg_type resolves to pg_catalog.pg_type.
  468. statement ok
  469. PREPARE new_table_in_search_path AS SELECT typname FROM pg_type
  470. statement ok
  471. CREATE TABLE pg_type(typname STRING); INSERT INTO pg_type VALUES('test')
  472. # Now, it should resolve to the table we just created.
  473. query T
  474. EXECUTE new_table_in_search_path
  475. ----
  476. test
  477. statement ok
  478. DROP TABLE pg_type
  479. ## Even more tricky case: the query has two table references that resolve to
  480. ## the same table now, but later resolve to separate tables.
  481. statement ok
  482. PREPARE new_table_in_search_path_2 AS
  483. SELECT a.typname, b.typname FROM pg_type AS a, pg_catalog.pg_type AS b ORDER BY a.typname, b.typname LIMIT 1
  484. query TT
  485. EXECUTE new_table_in_search_path_2
  486. ----
  487. _bit _bit
  488. statement ok
  489. CREATE TABLE pg_type(typname STRING); INSERT INTO pg_type VALUES('test')
  490. query TT
  491. EXECUTE new_table_in_search_path_2
  492. ----
  493. test _bit
  494. statement ok
  495. DROP TABLE pg_type
  496. statement ok
  497. RESET search_path
  498. ## Functions: Use function which depends on context, and which is constant-
  499. ## folded by the heuristic planner. Ensure that it's not constant folded when
  500. ## part of prepared plan.
  501. query B
  502. SELECT has_column_privilege('testuser', 'othertable', 1, 'SELECT')
  503. ----
  504. false
  505. statement ok
  506. GRANT ALL ON othertable TO testuser
  507. query B
  508. SELECT has_column_privilege('testuser', 'othertable', 1, 'SELECT')
  509. ----
  510. true
  511. statement ok
  512. REVOKE ALL ON othertable FROM testuser
  513. ## Location change: Change the current location (affects timezone) and make
  514. ## sure the query is invalidated.
  515. statement ok
  516. PREPARE change_loc AS SELECT '2000-01-01 18:05:10.123'::timestamptz
  517. query T
  518. EXECUTE change_loc
  519. ----
  520. 2000-01-01 18:05:10.123 +0000 UTC
  521. statement ok
  522. SET TIME ZONE 'EST';
  523. query T
  524. EXECUTE change_loc
  525. ----
  526. 2000-01-01 18:05:10.123 -0500 -0500
  527. statement ok
  528. SET TIME ZONE 'UTC';
  529. ## Permissions: Grant and then revoke permission to select from a table. The
  530. ## prepared plan should be invalidated.
  531. statement ok
  532. GRANT ALL ON othertable TO testuser
  533. user testuser
  534. statement ok
  535. USE otherdb
  536. statement ok
  537. PREPARE change_privileges AS SELECT * FROM othertable
  538. query II
  539. EXECUTE change_privileges
  540. ----
  541. 1 10
  542. user root
  543. statement ok
  544. REVOKE ALL ON othertable FROM testuser
  545. user testuser
  546. query error pq: user testuser does not have SELECT privilege on relation othertable
  547. EXECUTE change_privileges
  548. user root
  549. ## Permissions: Use UPDATE statement that requires both UPDATE and SELECT
  550. ## privileges.
  551. statement ok
  552. GRANT ALL ON othertable TO testuser
  553. user testuser
  554. statement ok
  555. USE otherdb
  556. statement ok
  557. PREPARE update_privileges AS UPDATE othertable SET b=$1
  558. user root
  559. statement ok
  560. REVOKE UPDATE ON othertable FROM testuser
  561. user testuser
  562. query error pq: user testuser does not have UPDATE privilege on relation othertable
  563. EXECUTE update_privileges(5)
  564. user root
  565. statement ok
  566. GRANT UPDATE ON othertable TO testuser
  567. statement ok
  568. REVOKE SELECT ON othertable FROM testuser
  569. user testuser
  570. query error pq: user testuser does not have SELECT privilege on relation othertable
  571. EXECUTE update_privileges(5)
  572. user root
  573. query II
  574. SELECT * FROM othertable
  575. ----
  576. 1 10
  577. user root
  578. ## Schema change (rename): Rename column in table and ensure that the prepared
  579. ## statement is updated to incorporate it.
  580. statement ok
  581. PREPARE change_rename AS SELECT * FROM othertable
  582. query II colnames
  583. EXECUTE change_rename
  584. ----
  585. a b
  586. 1 10
  587. statement ok
  588. ALTER TABLE othertable RENAME COLUMN b TO c
  589. query II colnames
  590. EXECUTE change_rename
  591. ----
  592. a c
  593. 1 10
  594. statement ok
  595. ALTER TABLE othertable RENAME COLUMN c TO b
  596. query II colnames
  597. EXECUTE change_rename
  598. ----
  599. a b
  600. 1 10
  601. ## Schema change (placeholders): Similar to previous case, but with placeholder
  602. ## present.
  603. statement ok
  604. PREPARE change_placeholders AS SELECT * FROM othertable WHERE a=$1
  605. query II colnames
  606. EXECUTE change_placeholders(1)
  607. ----
  608. a b
  609. 1 10
  610. statement ok
  611. ALTER TABLE othertable RENAME COLUMN b TO c
  612. query II colnames
  613. EXECUTE change_placeholders(1)
  614. ----
  615. a c
  616. 1 10
  617. statement ok
  618. ALTER TABLE othertable RENAME COLUMN c TO b
  619. query II colnames
  620. EXECUTE change_placeholders(1)
  621. ----
  622. a b
  623. 1 10
  624. ## Schema change (view): Change view name and ensure that prepared query is
  625. ## invalidated.
  626. statement ok
  627. CREATE VIEW otherview AS SELECT a, b FROM othertable
  628. statement ok
  629. PREPARE change_view AS SELECT * FROM otherview
  630. query II
  631. EXECUTE change_view
  632. ----
  633. 1 10
  634. statement ok
  635. ALTER VIEW otherview RENAME TO otherview2
  636. # HP and CBO return slightly different errors, so accept both.
  637. query error pq: relation "(otherdb.public.)?otherview" does not exist
  638. EXECUTE change_view
  639. statement ok
  640. DROP VIEW otherview2
  641. ## Schema change: Drop column and ensure that correct error is reported.
  642. statement ok
  643. PREPARE change_drop AS SELECT * FROM othertable WHERE b=10
  644. query II
  645. EXECUTE change_drop
  646. ----
  647. 1 10
  648. statement ok
  649. ALTER TABLE othertable DROP COLUMN b
  650. query error pq: column "b" does not exist
  651. EXECUTE change_drop
  652. statement ok
  653. ALTER TABLE othertable ADD COLUMN b INT; UPDATE othertable SET b=10
  654. query II
  655. EXECUTE change_drop
  656. ----
  657. 1 10
  658. ## Uncommitted schema change: Rename column in table in same transaction as
  659. ## execution of prepared statement and make prepared statement incorporates it.
  660. statement ok
  661. PREPARE change_schema_uncommitted AS SELECT * FROM othertable
  662. statement ok
  663. BEGIN TRANSACTION
  664. query II colnames
  665. EXECUTE change_schema_uncommitted
  666. ----
  667. a b
  668. 1 10
  669. statement ok
  670. ALTER TABLE othertable RENAME COLUMN b TO c
  671. query II colnames
  672. EXECUTE change_schema_uncommitted
  673. ----
  674. a c
  675. 1 10
  676. # Change the schema again and verify that the previously prepared plan is not
  677. # reused. Testing this is important because the second schema change won't
  678. # bump the table descriptor version again.
  679. statement ok
  680. ALTER TABLE othertable RENAME COLUMN c TO d
  681. query II colnames
  682. EXECUTE change_schema_uncommitted
  683. ----
  684. a d
  685. 1 10
  686. statement ok
  687. ROLLBACK TRANSACTION
  688. # Same virtual table in different catalogs (these virtual table instances have
  689. # the same table ID).
  690. statement ok
  691. CREATE SEQUENCE seq
  692. statement ok
  693. PREPARE pg_catalog_query AS SELECT * FROM pg_catalog.pg_sequence
  694. query OOIIIIIB colnames
  695. EXECUTE pg_catalog_query
  696. ----
  697. seqrelid seqtypid seqstart seqincrement seqmax seqmin seqcache seqcycle
  698. 67 20 1 1 9223372036854775807 1 1 false
  699. statement ok
  700. USE test
  701. query OOIIIIIB colnames
  702. EXECUTE pg_catalog_query
  703. ----
  704. seqrelid seqtypid seqstart seqincrement seqmax seqmin seqcache seqcycle
  705. # Verify error when placeholders are used without prepare.
  706. statement error no value provided for placeholder: \$1
  707. SELECT $1:::int
  708. # Verify sequences get re-resolved.
  709. statement ok
  710. CREATE SEQUENCE seq
  711. statement ok
  712. PREPARE seqsel AS SELECT * FROM seq
  713. query I
  714. SELECT nextval('seq')
  715. ----
  716. 1
  717. query IIB
  718. EXECUTE seqsel
  719. ----
  720. 1 0 true
  721. statement ok
  722. DROP SEQUENCE seq
  723. statement ok
  724. CREATE SEQUENCE seq
  725. query IIB
  726. EXECUTE seqsel
  727. ----
  728. 0 0 true
  729. # Null placeholder values need to be assigned static types. Otherwise, we won't
  730. # be able to disambiguate the concat function overloads.
  731. statement ok
  732. PREPARE foobar AS VALUES ($1:::string || $2:::string)
  733. query T
  734. EXECUTE foobar(NULL, NULL)
  735. ----
  736. NULL
  737. subtest regression_35145
  738. # Verify db-independent query behaves properly even when db does not exist
  739. statement ok
  740. SET application_name = ap35145
  741. # Prepare in custom db
  742. statement ok
  743. CREATE DATABASE d35145; SET database = d35145;
  744. statement ok
  745. PREPARE display_appname AS SELECT setting FROM pg_settings WHERE name = 'application_name'
  746. query T
  747. EXECUTE display_appname
  748. ----
  749. ap35145
  750. # Check what happens when the db where the stmt was prepared disappears "underneath".
  751. statement ok
  752. DROP DATABASE d35145
  753. query error database "d35145" does not exist
  754. EXECUTE display_appname
  755. statement ok
  756. CREATE DATABASE d35145
  757. query T
  758. EXECUTE display_appname
  759. ----
  760. ap35145
  761. # Check what happens when the stmt is executed over a non-existent, unrelated db.
  762. statement ok
  763. CREATE DATABASE d35145_2; SET database = d35145_2; DROP DATABASE d35145_2
  764. query error database "d35145_2" does not exist
  765. EXECUTE display_appname
  766. # Check what happens when the stmt is executed over no db whatsoever.
  767. statement ok
  768. SET database = ''
  769. query error cannot access virtual schema in anonymous database
  770. EXECUTE display_appname