create_as.slt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/create_as
  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. statement count 3
  23. CREATE TABLE stock (item, quantity) AS VALUES ('cups', 10), ('plates', 15), ('forks', 30)
  24. statement count 1
  25. CREATE TABLE runningOut AS SELECT * FROM stock WHERE quantity < 12
  26. query TI
  27. SELECT * FROM runningOut
  28. ----
  29. cups 10
  30. statement count 3
  31. CREATE TABLE itemColors (color) AS VALUES ('blue'), ('red'), ('green')
  32. statement count 9
  33. CREATE TABLE itemTypes AS (SELECT item, color FROM stock, itemColors)
  34. query TT rowsort
  35. SELECT * FROM itemTypes
  36. ----
  37. cups blue
  38. cups red
  39. cups green
  40. plates blue
  41. plates red
  42. plates green
  43. forks blue
  44. forks red
  45. forks green
  46. statement error pq: AS OF SYSTEM TIME must be provided on a top-level statement
  47. CREATE TABLE t AS SELECT * FROM stock AS OF SYSTEM TIME '2016-01-01'
  48. statement error pgcode 42601 CREATE TABLE specifies 3 column names, but data source has 2 columns
  49. CREATE TABLE t2 (col1, col2, col3) AS SELECT * FROM stock
  50. statement error pgcode 42601 CREATE TABLE specifies 1 column name, but data source has 2 columns
  51. CREATE TABLE t2 (col1) AS SELECT * FROM stock
  52. statement count 5
  53. CREATE TABLE unionstock AS SELECT * FROM stock UNION VALUES ('spoons', 25), ('knives', 50)
  54. query TI
  55. SELECT * FROM unionstock ORDER BY quantity
  56. ----
  57. cups 10
  58. plates 15
  59. spoons 25
  60. forks 30
  61. knives 50
  62. statement count 0
  63. CREATE TABLE IF NOT EXISTS unionstock AS VALUES ('foo', 'bar')
  64. query TI
  65. SELECT * FROM unionstock ORDER BY quantity LIMIT 1
  66. ----
  67. cups 10
  68. statement ok
  69. CREATE DATABASE smtng
  70. statement count 3
  71. CREATE TABLE smtng.something AS SELECT * FROM stock
  72. statement count 0
  73. CREATE TABLE IF NOT EXISTS smtng.something AS SELECT * FROM stock
  74. query TI
  75. SELECT * FROM smtng.something ORDER BY 1 LIMIT 1
  76. ----
  77. cups 10
  78. statement error pgcode 42P01 relation "something" does not exist
  79. SELECT * FROM something LIMIT 1
  80. # Check for memory leak (materialize#10466)
  81. statement ok
  82. CREATE TABLE foo (x, y, z) AS SELECT catalog_name, schema_name, sql_path FROM information_schema.schemata
  83. statement error pq: relation "foo" already exists
  84. CREATE TABLE foo (x, y, z) AS SELECT catalog_name, schema_name, sql_path FROM information_schema.schemata
  85. statement error pq: value type tuple cannot be used for table columns
  86. CREATE TABLE foo2 (x) AS (VALUES(ROW()))
  87. statement error pq: nested array unsupported as column type: int\[\]\[\]
  88. CREATE TABLE foo2 (x) AS (VALUES(ARRAY[ARRAY[1]]))
  89. statement error generator functions are not allowed in VALUES
  90. CREATE TABLE foo2 (x) AS (VALUES(generate_series(1,3)))
  91. statement error pq: value type unknown cannot be used for table columns
  92. CREATE TABLE foo2 (x) AS (VALUES(NULL))
  93. # Check nulls are handled properly (database-issues#3988)
  94. query I
  95. CREATE TABLE foo3 (x) AS VALUES (1), (NULL); SELECT * FROM foo3 ORDER BY x
  96. ----
  97. NULL
  98. 1
  99. # Check that CREATE TABLE AS can use subqueries (materialize#23002)
  100. query B
  101. CREATE TABLE foo4 (x) AS SELECT EXISTS(SELECT * FROM foo3 WHERE x IS NULL); SELECT * FROM foo4
  102. ----
  103. true
  104. # Regression test for cockroach#36930.
  105. statement ok
  106. CREATE TABLE bar AS SELECT 1 AS a, 2 AS b, count(*) AS c FROM foo
  107. query III colnames
  108. SELECT * FROM bar
  109. ----
  110. a b c
  111. 1 2 4
  112. statement ok
  113. CREATE TABLE baz (a, b, c) AS SELECT 1, 2, count(*) FROM foo
  114. query III colnames
  115. SELECT * FROM baz
  116. ----
  117. a b c
  118. 1 2 4