should be it

This commit is contained in:
2025-10-24 19:21:19 -05:00
parent a4b23fc57c
commit f09560c7b1
14047 changed files with 3161551 additions and 1 deletions

View File

@@ -0,0 +1,14 @@
# name: test/sql/generated_columns/stored/basic.test
# description: Usage of STORED generated column
# group: [stored]
statement ok
PRAGMA enable_verification
statement error
CREATE TABLE tbl (
price INTEGER,
gcol AS (price) STORED,
);
----
<REGEX>:.*Invalid Input Error.*Can not create.*

View File

@@ -0,0 +1,55 @@
# name: test/sql/generated_columns/virtual/alter_table.test
# description: Test generated columns added through ALTER TABLE query
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (price INTEGER, amount_sold INTEGER);
# Cant create a column with a duplicate name
statement error
CREATE TABLE unit (price INTEGER, amount_sold INTEGER);
----
statement ok
INSERT INTO unit VALUES (5,4)
# Function doesnt exist
statement error
ALTER TABLE unit ADD COLUMN profit_total BOOLEAN GENERATED ALWAYS AS(non_existant_function() * price) VIRTUAL;
----
# Adding a generated column after table creation is not supported yet
statement error
ALTER TABLE unit ADD COLUMN profit_total BOOLEAN GENERATED ALWAYS AS(price) VIRTUAL;
----
# Expression contains a subquery
statement error
ALTER TABLE unit ADD COLUMN total_profit BOOLEAN GENERATED ALWAYS AS((SELECT 1)) VIRTUAL;
----
statement error
ALTER TABLE unit ADD COLUMN total_profit INTEGER GENERATED ALWAYS AS (price * amount_sold) VIRTUAL;
----
# Cant add to a generated column
statement error
INSERT INTO unit (total_profit) VALUES (1)
----
# Only 2 real columns exist in the table
statement error
INSERT INTO unit VALUES (1,1,1)
----
statement ok
INSERT INTO unit VALUES (0,1)
query II
SELECT * FROM unit
----
5 4
0 1

View File

@@ -0,0 +1,60 @@
# name: test/sql/generated_columns/virtual/ambiguity.test
# description: Test if the columnrefs in a generated-column expression can result in ambiguity
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (
price INTEGER,
amount_sold INTEGER,
total_profit AS (price * amount_sold)
);
# Not allowed to have qualified (tbl.name) column references
statement error
CREATE TABLE unit2 (
price INTEGER,
amount_sold INTEGER,
total_profit AS (price / amount_sold),
profit_total AS (unit2.price * unit2.amount_sold)
);
----
statement ok
CREATE TABLE unit2 (
price INTEGER,
amount_sold INTEGER,
total_profit AS (price / amount_sold),
profit_total AS (price * amount_sold)
);
statement ok
INSERT INTO unit VALUES (5,4)
# Ambiguous, both have 'total_profit'
statement error
SELECT total_profit FROM unit, unit2
----
statement ok
INSERT INTO unit2 VALUES (100, 4)
# When using the table name, we avoid ambiguity
statement ok
SELECT unit.total_profit FROM unit, unit2
# When using aliases for tables, this should still work
query I
SELECT unit.total_profit FROM unit AS unit2, unit2 AS unit
----
25
# No ambiguity here
statement ok
SELECT profit_total FROM unit2
# Aliasing the table name shouldn't break the generated expression
statement ok
SELECT profit_total FROM unit2 as unit

View File

@@ -0,0 +1,68 @@
# name: test/sql/generated_columns/virtual/cascading_delete.test
# description: Test the chain reaction effect of deleting a column that is a dependency of a generated column
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (
price INTEGER,
amount_sold INTEGER,
total_profit as (price * amount_sold),
profit_total AS (total_profit),
dependent AS (profit_total),
also_dependent AS (profit_total * dependent),
dont_delete_me AS (amount_sold),
);
statement ok
INSERT INTO unit VALUES (5,4)
query IIIIIII
SELECT * FROM unit;
----
5 4 20 20 20 400 4
# Is a dependency of 1 or more columns - CASCADE required
statement error
ALTER TABLE unit DROP COLUMN price;
----
Cannot drop column: column is a dependency of 1 or more generated column(s)
statement ok
ALTER TABLE unit DROP COLUMN price CASCADE;
query II
SELECT * FROM unit;
----
4 4
statement ok
INSERT INTO unit VALUES (5)
query II
SELECT * FROM unit;
----
4 4
5 5
statement ok
ALTER TABLE unit DROP COLUMN dont_delete_me;
query I
SELECT * FROM unit;
----
4
5
# Cant delete last column
statement error
ALTER TABLE unit DROP COLUMN amount_sold CASCADE;
----
query I
SELECT * FROM unit;
----
4
5

View File

@@ -0,0 +1,50 @@
# name: test/sql/generated_columns/virtual/casting.test
# description: Test generated column behavior when casting is involved
# group: [virtual]
statement ok
PRAGMA enable_verification
# Create a table with a generated column, that contains a CAST
statement ok
CREATE TABLE tbl (
price INTEGER,
total_price AS ((price)::DATE)
);
# Insert value(s) into the column(s)
# Because of the silently generated CHECK constraint, this insert fails
# '(Error: Conversion Error: Unimplemented type for cast (INTEGER -> DATE))'
statement error
INSERT INTO tbl VALUES (5);
----
# Test with multiple different generated columns
statement ok
CREATE TABLE a (
gen_x AS (x),
gen_y AS (y),
x TEXT,
y INTEGER
);
statement ok
INSERT INTO a VALUES ('hello', 42)
# TEXT -> TEXT: OK
statement ok
SELECT * FROM a;
# Change type to BOOLEAN
# Not supported yet
statement error
ALTER TABLE a ALTER COLUMN gen_x TYPE BOOLEAN;
----
## TEXT -> BOOLEAN: KO
#statement error
#SELECT * FROM a;
## TEXT -> BOOLEAN: KO
#statement error
#INSERT INTO a VALUES ('hello', 42)

View File

@@ -0,0 +1,50 @@
# name: test/sql/generated_columns/virtual/check.test
# description: Test generated columns with the CHECK constraint
# group: [virtual]
statement ok
PRAGMA enable_verification
#Check constraint involving a generated column is not supported
# Using a CHECK constraint that doesnt involve the generated column itself is fine
statement ok
CREATE TABLE unit (
price INTEGER,
amount_sold AS (price) CHECK (price > 5),
);
# When it references a generated column, it fails
statement error
CREATE TABLE tbl (
price INTEGER,
amount_sold AS (price) CHECK (amount_sold > 5)
);
----
<REGEX>:.*Binder Error.*Constraints on generated columns.*
# Also when used in this way
statement error
CREATE TABLE tbl (
price INTEGER,
amount_sold AS (price),
CHECK (amount_sold > price)
);
----
<REGEX>:.*Binder Error.*Constraints on generated columns.*
statement ok
CREATE TABLE chk (
g1 AS (x),
g2 AS (x),
x INTEGER,
CHECK (x > 5)
);
statement ok
INSERT INTO chk VALUES (6);
statement error
INSERT INTO chk VALUES (3);
----
<REGEX>:.*Constraint Error.*constraint failed.*

View File

@@ -0,0 +1,56 @@
# name: test/sql/generated_columns/virtual/circular_dependency_stresstest.test
# description: Test circular dependency detection
# group: [virtual]
statement ok
PRAGMA enable_verification
statement error
CREATE TABLE tbl (
gcol1 AS (gcol10),
gcol2 AS (gcol4),
gcol3 AS (gcol7),
gcol4 AS (gcol6),
gcol5 AS (x),
gcol6 AS (gcol2),
gcol7 AS (gcol8),
gcol8 AS (gcol1),
gcol9 AS (gcol2),
gcol10 AS (gcol3),
x INTEGER
);
----
<REGEX>:.*Invalid Input Error.*Circular dependency.*
statement ok
CREATE TABLE tbl (
gcol1 AS (gcol2),
gcol2 AS (gcol3),
gcol3 AS (gcol4),
gcol4 AS (gcol5),
gcol5 AS (gcol6),
gcol6 AS (gcol7),
gcol7 AS (gcol8),
gcol8 AS (gcol9),
gcol9 AS (gcol10),
gcol10 AS (x),
x INTEGER,
);
statement error
CREATE TABLE circular (
a AS (k),
k AS (j),
j AS (i),
i AS (h),
h AS (g),
g AS (f),
f AS (e),
e AS (d),
d AS (c),
c AS (b),
b AS (a),
x INTEGER,
);
----
<REGEX>:.*Invalid Input Error.*Circular dependency.*

View File

@@ -0,0 +1,27 @@
# name: test/sql/generated_columns/virtual/collate.test
# description: Test generated columns with the COLLATE option
# group: [virtual]
statement ok
PRAGMA enable_verification
#Add a collation to a generated column
statement error
CREATE TABLE tbl (
price VARCHAR,
also_price VARCHAR AS (price) COLLATE NOCASE
);
----
statement ok
CREATE TABLE coll (
g1 AS (x),
g2 AS (x),
x VARCHAR COLLATE NOCASE
);
statement ok
INSERT INTO coll VALUES ('string'), ('STRING')
statement ok
SELECT * FROM coll;

View File

@@ -0,0 +1,111 @@
# name: test/sql/generated_columns/virtual/create_table.test
# description: Add a generated column on creation of a table
# group: [virtual]
statement ok
PRAGMA enable_verification
# Creating tables with only generated columns is not supported
statement error
CREATE TEMP TABLE t0(c0 AS (1));
----
<REGEX>:.*Binder Error.*not supported.*
# Function doesn't exist
statement error
CREATE TABLE unit (
price INTEGER,
total_profit BOOLEAN GENERATED ALWAYS AS(non_existant_function() * price) VIRTUAL,
);
----
<REGEX>:.*Catalog Error.*does not exist.*
# Column doesn't exist
statement error
CREATE TABLE unit (
total_profit BOOLEAN GENERATED ALWAYS AS(price * 2) VIRTUAL,
);
----
<REGEX>:.*Binder Error.*price.*
#Expression (word) is cast to BOOLEAN
statement ok
CREATE TABLE tbl (
total_profit BOOLEAN GENERATED ALWAYS AS(word) VIRTUAL,
word VARCHAR,
price INTEGER,
amount_sold INTEGER,
);
# word doesn't contain any data yet
statement ok
SELECT * FROM tbl
# Cant convert 'string' to boolean
statement error
INSERT INTO tbl VALUES ('string', 5, 12);
----
<REGEX>:.*Constraint Error.*Incorrect value for generated column.*
# Expression contains a subquery
statement error
CREATE TABLE unit (
total_profit BOOLEAN GENERATED ALWAYS AS((SELECT 1)) VIRTUAL,
word VARCHAR,
price INTEGER,
amount_sold INTEGER,
);
----
<REGEX>:.*Parser Error.*isn't allowed.*
statement ok
CREATE MACRO my_macro() AS (
(select 42)
);
# Expression contains a subquery - through a macro
statement error
CREATE TABLE unit (
total_profit INTEGER GENERATED ALWAYS AS(my_macro()) VIRTUAL,
word VARCHAR,
price INTEGER,
amount_sold INTEGER,
);
----
<REGEX>:.*Binder Error.*Failed to bind generated column.*
# Duplicate column definition
statement error
CREATE TABLE unit (
total_profit INTEGER GENERATED ALWAYS AS(price * amount_sold) VIRTUAL,
total_profit INTEGER GENERATED ALWAYS AS(price * amount_sold) VIRTUAL,
price INTEGER,
amount_sold INTEGER,
);
----
<REGEX>:.*Catalog Error.*already exists.*
statement ok
CREATE TABLE unit (
total_profit INTEGER GENERATED ALWAYS AS(price * amount_sold) VIRTUAL,
price INTEGER,
amount_sold INTEGER,
);
statement ok
INSERT INTO unit VALUES (5,4)
query III
SELECT * FROM unit;
----
20 5 4
# Delete the generated column
statement ok
ALTER TABLE unit DROP COLUMN total_profit;
# We can now no longer select it
statement error
SELECT total_profit FROM unit;
----
<REGEX>:.*Binder Error.*not found.*

View File

@@ -0,0 +1,51 @@
# name: test/sql/generated_columns/virtual/default.test
# description: Usage of DEFAULT constraint in conjunction with GENERATED ALWAYS AS <expression> VIRTUAL
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (price INTEGER, amount_sold INTEGER);
statement ok
INSERT INTO unit VALUES (5,4)
statement ok
INSERT INTO unit VALUES (4,5)
# (ALTER) Generated columns can not be created with a DEFAULT constraint
statement error
ALTER TABLE unit ADD COLUMN total_profit INTEGER DEFAULT 1 GENERATED ALWAYS AS (price * amount_sold) VIRTUAL;
----
Parser Error: syntax error at or near "GENERATED"
# Generated column failed to create
statement error
SELECT total_profit FROM unit;
----
# (CREATE) Generated columns can not be created with a DEFAULT constraint
statement error
CREATE TABLE unit2 (
price INTEGER,
amount_sold INTEGER,
total_profit INTEGER GENERATED ALWAYS AS (price * amount_sold) VIRTUAL DEFAULT 1
);
----
# Table doesnt exist
statement error
select * from unit2
----
statement ok
CREATE TABLE tbl (
g1 AS (x),
g2 AS (x),
x INTEGER DEFAULT (5),
);
statement ok
INSERT INTO tbl VALUES (DEFAULT);

View File

@@ -0,0 +1,26 @@
# name: test/sql/generated_columns/virtual/drop.test
# description: Remove generated column
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (
price INTEGER,
amount_sold INTEGER,
total_profit AS (price * amount_sold)
);
statement ok
INSERT INTO unit VALUES (5,4)
# Delete the generated column
statement ok
ALTER TABLE unit DROP COLUMN total_profit;
# We can now no longer select it
statement error
SELECT total_profit FROM unit;
----
<REGEX>:.*Binder Error.*not found .*

View File

@@ -0,0 +1,34 @@
# name: test/sql/generated_columns/virtual/drop_dependency.test
# description: Remove dependency of a generated column
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (price INTEGER, amount_sold INTEGER);
statement ok
INSERT INTO unit VALUES (5,4)
statement error
ALTER TABLE unit ADD COLUMN total_profit INTEGER GENERATED ALWAYS AS (price * amount_sold) VIRTUAL;
----
<REGEX>:.*Parser Error.*not supported.*
statement ok
ALTER TABLE unit DROP COLUMN price;
# CREATE
statement ok
CREATE TABLE tbl (
price INTEGER,
total_profit AS (price)
);
# total_profit depends on this
statement error
ALTER TABLE tbl DROP COLUMN price;
----
<REGEX>:.*Catalog Error.*Cannot drop column.*

View File

@@ -0,0 +1,84 @@
# name: test/sql/generated_columns/virtual/foreign_key.test
# description: Test generated columns with the FOREIGN KEY constraint
# group: [virtual]
statement ok
PRAGMA enable_verification
#Create an ordinary table
statement ok
CREATE TABLE tbl1 (
a INTEGER UNIQUE,
);
#Generated column references a foreign key directly
statement error
CREATE TABLE tbl2 (
a INTEGER,
b AS (a) REFERENCES tbl1(a),
);
----
<REGEX>:.*Binder Error.*Failed to create foreign key.*
#Creating a foreign key constraint on a generated column
statement error
CREATE TABLE tbl2 (
a INTEGER,
b AS (a),
FOREIGN KEY (b) REFERENCES tbl1 (a)
);
----
<REGEX>:.*Binder Error.*Failed to create foreign key.*
# Referencing a generated column is not supported because in order to do that
# you have to create a UNIQUE/PRIMARY KEY constraint on a generated column first - which isn't supported (yet)
#Create a table
statement error
CREATE TABLE tbl2 (
price INTEGER,
also_price AS (price) UNIQUE
);
----
<REGEX>:.*Binder Error.*not supported.*
statement ok
CREATE TABLE tbl2 (
price INTEGER,
also_price AS (price)
);
#Can not reference a generated column as foreign key
statement error
CREATE TABLE tbl3 (
a INTEGER,
FOREIGN KEY (a) REFERENCES tbl2 (also_price)
);
----
<REGEX>:.*Binder Error.*Failed to create foreign key.*
#Show that normal uses of FOREIGN_KEY constraint work mixed with generated columns
statement ok
CREATE TABLE a (
a_gen1 AS (a_reg),
a_gen2 AS (a_reg),
a_reg INTEGER,
PRIMARY KEY (a_reg)
);
statement ok
CREATE TABLE b (
gen1 AS (price),
gen2 AS (price),
price INTEGER,
FOREIGN KEY (price) REFERENCES a (a_reg)
);
statement ok
INSERT INTO a VALUES (5);
# Violates primary key constraint
statement error
INSERT INTO a VALUES (5);
----
<REGEX>:.*Constraint Error.*Duplicate key.*

View File

@@ -0,0 +1,65 @@
# name: test/sql/generated_columns/virtual/foreign_key_extensive.test
# description: Re-testing 'test_foreignkey.test' with generated columns mixed in
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE album (
gen_artistid AS (artistid),
gen_albumname AS (albumname),
artistid INTEGER,
albumname TEXT,
albumcover TEXT,
UNIQUE (artistid, albumname)
);
statement ok
INSERT INTO album VALUES (1, 'A', 'A_cover'), (2, 'B', 'B_cover'), (3, 'C', 'C_cover'), (4, 'D', 'D_cover');
# The count of columns are primary keys must be equal with the count of columns are foreign keys
statement error
CREATE TABLE song(songid INTEGER, songartist INTEGER, songalbum TEXT, songname TEXT, FOREIGN KEY(songartist, songalbum) REFERENCES album(artistid));
----
statement error
CREATE TABLE song(songid INTEGER, songartist INTEGER, songalbum TEXT, songname TEXT, FOREIGN KEY(songalbum) REFERENCES album(artistid, albumname));
----
statement error
CREATE TABLE song(songid INTEGER, songartist INTEGER, songalbum TEXT, songname TEXT, FOREIGN KEY(songartist, songalbum) REFERENCES albumlist(artistid, albumname));
----
statement error
CREATE TABLE song(songid INTEGER, songartist INTEGER, songalbum TEXT, songname TEXT, FOREIGN KEY(songartist, songalbum) REFERENCES album(artistid, album_name));
----
statement error
CREATE TABLE song(songid INTEGER, songartist INTEGER, songalbum TEXT, songname TEXT, FOREIGN KEY(songartist, song_album) REFERENCES album(artistid, albumname));
----
statement ok
CREATE TABLE song(songid INTEGER, songartist INTEGER, songalbum TEXT, songname TEXT, FOREIGN KEY(songartist, songalbum) REFERENCES album(artistid, albumname));
# Any row that is inserted into the table with the foreign key must exist in the table with the primary key (constraint)
statement error
INSERT INTO song VALUES (11, 1, 'A', 'A_song'), (12, 2, 'E', 'B_song'), (13, 3, 'C', 'C_song');
----
statement error
INSERT INTO song VALUES (11, 1, 'A', 'A_song'), (12, 5, 'D', 'B_song'), (13, 3, 'C', 'C_song');
----
statement ok
INSERT INTO song VALUES (11, 1, 'A', 'A_song'), (12, 2, 'B', 'B_song'), (13, 3, 'C', 'C_song');
statement ok
DELETE FROM album WHERE albumname='D';
query ITITT
SELECT * FROM album;
----
1 A 1 A A_cover
2 B 2 B B_cover
3 C 3 C C_cover

View File

@@ -0,0 +1,46 @@
# name: test/sql/generated_columns/virtual/from_generated_column.test
# description: Expected behavior when trying to create a generated column from a generated column
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (price INTEGER, amount_sold INTEGER);
statement ok
INSERT INTO unit VALUES (5,4)
# ALTER TABLE - ADD COLUMN generated column not supported yet
statement error
ALTER TABLE unit ADD COLUMN total_profit INTEGER GENERATED ALWAYS AS (price * amount_sold) VIRTUAL;
----
statement error
SELECT total_profit FROM unit
----
statement error
ALTER TABLE unit ADD COLUMN also_total_profit INTEGER GENERATED ALWAYS AS (total_profit) VIRTUAL;
----
statement error
SELECT also_total_profit FROM unit
----
# Create a table with a generated column, and another generated column created from this other one
statement ok
CREATE TABLE unit2(
price INTEGER,
amount_sold INTEGER,
total_profit INTEGER GENERATED ALWAYS AS (price * amount_sold) VIRTUAL,
also_total_profit INTEGER GENERATED ALWAYS AS (total_profit) VIRTUAL
);
statement ok
INSERT INTO unit2 VALUES (100, 0)
query IIII
SELECT * FROM unit2
----
100 0 0 0

View File

@@ -0,0 +1,13 @@
# name: test/sql/generated_columns/virtual/gcol_duckdb_columns.test
# group: [virtual]
# Create a table containing a generated column
statement ok
create table t (i int, j int as (1));
# duckdb_columns should return the stringified generated column expression as 'column_default'
query I
select column_default from duckdb_columns;
----
NULL
CAST(1 AS INTEGER)

View File

@@ -0,0 +1,73 @@
# name: test/sql/generated_columns/virtual/group_by.test
# group: [virtual]
statement ok
pragma enable_verification;
statement ok
CREATE TABLE tbl (
foo INTEGER,
bar INTEGER GENERATED ALWAYS AS (foo+1) VIRTUAL,
foobar INTEGER
);
statement ok
insert into tbl VALUES
(1, 24),
(2342, 123),
(23523, 1),
(235, 213),
(8435, null),
(NULL, NULL),
(345, 213),
(12, 5)
statement ok
insert into tbl select i % 20, random()::INTEGER * 20 from range(100) tbl(i);
statement ok
CREATE TABLE non_generated as select * from tbl;
# Verify that the created table contains only regular columns (not generated)
query I
SELECT sql FROM sqlite_master WHERE name = 'tbl' and contains(sql, 'GENERATED');
----
CREATE TABLE tbl(foo INTEGER, bar INTEGER GENERATED ALWAYS AS((foo + 1)), foobar INTEGER);
query I
SELECT sql FROM sqlite_master WHERE name = 'non_generated' and contains(sql, 'GENERATED');
----
query III nosort r1
select * from tbl;
----
query III nosort r1
select * from non_generated;
----
# Both tables should produce the same result, regardless of the group by expression used
foreach table_name tbl non_generated
query IIIIII nosort r2
select count(*), foobar, foo, last(foo), avg(bar), count(bar) from ${table_name} group by bar, foobar, foo order by all;
----
query IIII nosort r3
select count(*), last(foo), avg(bar), count(bar) from ${table_name} group by bar order by all;
----
query III nosort r4
select count(bar order by foobar desc), count(bar), count(distinct bar), avg(bar) from ${table_name} group by foo, foobar order by all;
----
query III nosort r5
select count(bar order by foo asc), count(distinct bar) filter ((foo+1) != bar) from ${table_name} group by foo order by all;
----
query IIII nosort r6
select count(foo), count(foobar), bar from ${table_name} group by CUBE(foo, foobar, bar), bar, foo, CUBE(foo) order by all;
----
endloop

View File

@@ -0,0 +1,44 @@
# name: test/sql/generated_columns/virtual/implicit_type.test
# description: Test implicit typing of generated columns
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (
foo INTEGER,
bar GENERATED ALWAYS AS (foo+1),
foobar INTEGER DEFAULT 5
);
# Can not use implicit types without GENERATED ALWAYS AS
statement ok
CREATE TABLE unit (
foo INTEGER,
bar AS (foo+1),
)
# Attempting to use implicit type on a standard column
statement error
CREATE TABLE tbl (
foo,
bar GENERATED ALWAYS AS (foo+1)
);
----
statement ok
INSERT INTO test(foo) VALUES (1);
query I
SELECT bar FROM test
----
2
statement ok
INSERT INTO unit VALUES(5)
query I
SELECT bar FROM unit
----
6

View File

@@ -0,0 +1,83 @@
# name: test/sql/generated_columns/virtual/insert.test
# description: Test that all types of inserting work
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (
foo INTEGER,
bar INTEGER GENERATED ALWAYS AS (foo+1) VIRTUAL,
foobar INTEGER DEFAULT 5
);
#Inserting into a generated column by name
statement error
INSERT INTO test (bar) VALUES (5)
----
<REGEX>:.*Binder Error.*Cannot insert.*
#Using column names
statement ok
INSERT INTO test (foo, foobar) VALUES (1, 10);
#Relying on DEFAULT constraint
statement ok
INSERT INTO test (foo) VALUES (12)
query III
SELECT * FROM test
----
1 2 10
12 13 5
#Using VALUES with not enough values - cant rely on DEFAULT here
statement error
INSERT INTO test VALUES (22)
----
<REGEX>:.*Binder Error.*table test has 2 columns.*
#Using VALUES correctly
statement ok
INSERT INTO test VALUES (22, 10);
#Using VALUES incorrectly, too many values
statement error
INSERT INTO test VALUES (22, 10, 10)
----
<REGEX>:.*Binder Error.*table test has 2 columns.*
statement ok
CREATE TABLE tbl (
x INTEGER,
y BOOLEAN AS (x),
);
statement ok
INSERT INTO tbl VALUES (5);
query II
SELECT * FROM tbl;
----
5 True
statement ok
INSERT INTO tbl VALUES (0);
query II
SELECT * FROM tbl;
----
5 True
0 False
statement ok
CREATE TABLE tbl2 (
x TEXT,
y BOOLEAN AS (x),
);
statement error
INSERT INTO tbl2 VALUES ('test');
----
<REGEX>:.*Constraint Error.*Incorrect value for generated column.*

View File

@@ -0,0 +1,28 @@
# name: test/sql/generated_columns/virtual/natural_join.test_slow
# group: [virtual]
statement ok
pragma enable_verification;
statement ok
CREATE TABLE t1(
c1 AS (8),
c0 INT
);
query I
SELECT 1 FROM t1 NATURAL JOIN t1 t2 GROUP BY c1;
statement ok
CREATE TABLE t2(
c1 AS (j * 5),
j INT
);
statement ok
insert into t2 VALUES(4);
query I
select 1 from t2 NATURAL JOIN t2 AS t3 GROUP BY c1;
----
1

View File

@@ -0,0 +1,29 @@
# name: test/sql/generated_columns/virtual/not_referencing_columns.test
# description: Usage of valid generated columns that do not reference any columns
# group: [virtual]
statement ok
PRAGMA enable_verification
# Aggregate functions are not supported here
statement error
CREATE TABLE tbl (
price INTEGER,
amount_sold INTEGER,
gcol AS (list(5,4,3))
);
----
statement ok
CREATE TABLE tbl (
price INTEGER,
gcol AS (map())
);
statement ok
INSERT INTO tbl VALUES (5);
query II
SELECT * FROM tbl;
----
5 {}

View File

@@ -0,0 +1,32 @@
# name: test/sql/generated_columns/virtual/null.test
# description: Test generated columns with the NOT NULL constraint
# group: [virtual]
statement ok
PRAGMA enable_verification
# Not supported appended to the column definition
statement error
CREATE TABLE unit (
price INTEGER,
amount_sold AS (price) NOT NULL,
);
----
<REGEX>:.*Binder Error.*not supported yet.*
# Show that proper NOT NULL constraints still work when mixed in when generated columns
statement ok
CREATE TABLE tbl (
gen1 AS (price),
gen2 AS (price),
price INTEGER NOT NULL
);
statement ok
INSERT INTO tbl VALUES (5);
# Violates not null constraint
statement error
INSERT INTO tbl VALUES (null);
----
<REGEX>:.*Constraint Error.*constraint failed.*

View File

@@ -0,0 +1,42 @@
# name: test/sql/generated_columns/virtual/partition.test
# description: Using a generated column as a PARTITION BY key
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (
price INTEGER,
amount_sold INTEGER,
name VARCHAR,
total_profit AS (price * amount_sold)
);
statement ok
INSERT INTO unit VALUES (5,4, 'Soda can')
statement ok
INSERT INTO unit VALUES (5,8, 'Mars bar')
statement ok
INSERT INTO unit VALUES (4, 5, 'Chewing gum')
query IIII
SELECT total_profit, COUNT(total_profit), SUM(amount_sold), SUM(price) FROM unit GROUP BY total_profit ORDER BY ALL
----
20 2 9 9
40 1 8 5
query IIIII
SELECT total_profit,
name,
COUNT(total_profit) OVER(PARTITION BY total_profit) AS CountTotalProfit,
SUM(amount_sold) OVER(PARTITION BY total_profit) AS SumAmountSold,
SUM(price) OVER(PARTITION BY total_profit) AS SumPrice
FROM unit
ORDER BY 1, 2 DESC
----
20 Soda can 2 9 9
20 Chewing gum 2 9 9
40 Mars bar 1 8 5

View File

@@ -0,0 +1,21 @@
# name: test/sql/generated_columns/virtual/preserve_order.test
# description: Test that order is preserved even when mixing generated and standard columns
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (
foo INTEGER,
bar INTEGER GENERATED ALWAYS AS (foo+1) VIRTUAL,
foobar INTEGER
);
statement ok
INSERT INTO test (foo, foobar) VALUES (1, 10);
query III
SELECT * FROM test;
----
1 2 10

View File

@@ -0,0 +1,42 @@
# name: test/sql/generated_columns/virtual/primary_key.test
# description: Test generated columns with the PRIMARY KEY constraint
# group: [virtual]
statement ok
PRAGMA enable_verification
# Not supported appended to the column definition
statement error
CREATE TABLE unit (
price INTEGER,
amount_sold AS (price) PRIMARY KEY,
);
----
<REGEX>:.*Binder Error.*Constraints on generated columns.*
# Also not as a column list
statement error
CREATE TABLE unit (
price INTEGER,
amount_sold AS (price),
PRIMARY KEY (amount_sold),
);
----
<REGEX>:.*Binder Error.*Constraints on generated columns.*
# Show that proper PRIMARY KEY constraints still work when mixed in when generated columns
statement ok
CREATE TABLE tbl (
gen1 AS (price),
gen2 AS (price),
price INTEGER PRIMARY KEY
);
statement ok
INSERT INTO tbl VALUES (5);
# Violates primary key constraint
statement error
INSERT INTO tbl VALUES (5);
----
<REGEX>:.*Constraint Error.*Duplicate key.*

View File

@@ -0,0 +1,87 @@
# name: test/sql/generated_columns/virtual/referencing_gencols.test
# description: Test referencing generated columns within generated column expressions
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (
price INTEGER,
amount_sold INTEGER,
total_profit AS (price * amount_sold),
profit_total AS (total_profit * 5)
);
statement ok
INSERT INTO unit VALUES (5,4)
statement ok
INSERT INTO unit VALUES (4,5);
query IIII
SELECT * FROM unit;
----
5 4 20 100
4 5 20 100
# ON CREATE
#Circular dependency
statement error
CREATE TABLE tbl2 (
circular INTEGER AS (dependency),
dependency INTEGER AS (circular)
);
----
# Type is resolved, regardless of order
statement ok
CREATE TABLE tbl2 (
price INTEGER,
circular AS (dependency * price),
dependency AS (price),
);
#Same, but with a provided type
statement ok
CREATE TABLE tbl3 (
dependency INTEGER AS (price),
price INTEGER,
circular AS (dependency),
);
#Example with 'correct' order
statement ok
CREATE TABLE tbl (
price INTEGER,
dependency INTEGER AS (price),
circular AS (dependency * price),
);
statement ok
CREATE TABLE tbl4 (
price INTEGER,
dependency AS (price),
depth1 AS (dependency),
depth2 AS (price * dependency),
depth3 AS (depth1 * depth2 * dependency),
depth4 aS ((dependency * depth3) + depth1),
depth5 AS (depth4 - (depth3 + depth2) + (dependency * depth1))
);
statement ok
INSERT INTO tbl4 VALUES (5);
query IIIIIII
SELECT * FROM tbl4;
----
5 5 5 25 625 3130 2505
statement ok
INSERT INTO tbl VALUES (5);
query III
SELECT * FROM tbl;
----
5 5 25

View File

@@ -0,0 +1,29 @@
# name: test/sql/generated_columns/virtual/rename.test
# description: Test renaming a generated column
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (
price INTEGER,
amount_sold INTEGER,
total_profit AS (price * amount_sold)
);
statement ok
INSERT INTO unit VALUES (5,4)
query I
SELECT total_profit FROM unit
----
20
statement ok
ALTER TABLE unit RENAME COLUMN total_profit TO profit_total
query I
SELECT profit_total FROM unit
----
20

View File

@@ -0,0 +1,36 @@
# name: test/sql/generated_columns/virtual/rename_dependency.test
# description: Test renaming dependencies of generated columns
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (
price INTEGER,
amount_sold INTEGER,
total_profit AS (price * amount_sold),
profit_total AS (total_profit),
sold_amount AS (5 + 3)
);
statement ok
INSERT INTO unit VALUES (5,4)
query I
SELECT total_profit FROM unit
----
20
statement ok
ALTER TABLE unit RENAME COLUMN price TO value
# No CASCADE provided
statement error
ALTER TABLE unit DROP COLUMN value;
----
query IIIII
SELECT * FROM unit
----
5 4 20 20 8

View File

@@ -0,0 +1,24 @@
# name: test/sql/generated_columns/virtual/rename_table.test
# description: Test generated columns after renaming a table
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (
price INTEGER,
amount_sold INTEGER,
total_profit AS (price * amount_sold)
);
statement ok
INSERT INTO unit VALUES (5,4)
statement ok
ALTER TABLE unit RENAME TO not_unit;
query III
SELECT * FROM not_unit;
----
5 4 20

View File

@@ -0,0 +1,22 @@
# name: test/sql/generated_columns/virtual/row_group_fetch.test
# group: [virtual]
statement ok
pragma enable_verification;
statement ok
CREATE TABLE t1 (
tr INTEGER,
td INTEGER GENERATED ALWAYS AS (tr),
tz INTEGER
);
statement ok
CREATE INDEX id ON t1(tr);
statement ok
INSERT INTO t1(tr) VALUES (2);
# Because of the generated column, the logical and physical index of 'tz' do not align.
statement ok
SELECT tz from t1 WHERE tr < 5;

View File

@@ -0,0 +1,46 @@
# name: test/sql/generated_columns/virtual/rowid.test
# description: Create a generated column using the 'rowid' system column
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (price INTEGER, amount_sold INTEGER);
statement ok
INSERT INTO unit VALUES (5,4)
statement ok
INSERT INTO unit VALUES (4,5)
# Rowid as a system column is not supported
statement error
ALTER TABLE unit ADD COLUMN total_profit BIGINT GENERATED ALWAYS AS (price * rowid) VIRTUAL;
----
statement ok
ALTER TABLE unit ADD COLUMN rowid INTEGER;
# Cant create a generated column named 'rowid' that also tries to reference the 'rowid' system column
statement error
CREATE TABLE tbl (
price INTEGER,
rowid BIGINT GENERATED ALWAYS AS (rowid) VIRTUAL
);
----
# Reference a standard column named 'rowid'
statement ok
CREATE TABLE tbl (
rowid INTEGER,
index INTEGER GENERATED ALWAYS AS (rowid) VIRTUAL
)
statement ok
INSERT INTO tbl VALUES (5)
query I
SELECT index FROM tbl;
----
5

View File

@@ -0,0 +1,46 @@
# name: test/sql/generated_columns/virtual/select.test
# description: Remove generated column
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (price INTEGER, amount_sold INTEGER);
statement ok
INSERT INTO unit VALUES (5,4)
statement error
ALTER TABLE unit ADD COLUMN total_profit INTEGER GENERATED ALWAYS AS (price * amount_sold) VIRTUAL;
----
# CREATE
statement ok
CREATE TABLE tbl (
price INTEGER,
amount_sold INTEGER,
total_profit AS (price * amount_sold),
);
statement ok
INSERT INTO tbl VALUES (5,4);
#Select basic
query I
SELECT total_profit FROM tbl
----
20
#Select *
query III
SELECT * FROM tbl
----
5 4 20
#Select tbl.*
query III
SELECT tbl.* FROM tbl
----
5 4 20

View File

@@ -0,0 +1,41 @@
# name: test/sql/generated_columns/virtual/select_alias.test
# group: [virtual]
statement ok
pragma enable_verification
statement ok
CREATE TABLE tbl (
price INTEGER,
amount_sold INTEGER,
total_profit AS (price * amount_sold),
order_id BIGINT,
y as (order_id + total_profit - amount_sold)
);
foreach table_alias tbl t2 y order_id total_profit amount_sold price "a.b.c"
# Alias all columns
query IIIII nosort scrambled
select * from tbl ${table_alias}(y, order_id, total_profit, amount_sold, price);
----
query IIIII nosort scrambled
select * from tbl ${table_alias};
----
# Alias as completely different things
query IIIII nosort scrambled
select * from tbl ${table_alias}(a, b, c, d, e);
----
query II nosort swapped
select a, b from tbl ${table_alias}(e, c, b, d, a);
----
query II nosort swapped
select y, amount_sold from tbl;
----
#table_alias
endloop

View File

@@ -0,0 +1,26 @@
# name: test/sql/generated_columns/virtual/type_resolution_bug.test
# description: Test the chain reaction effect of deleting a column that is a dependency of a generated column
# group: [virtual]
statement ok
PRAGMA enable_verification
# which itself is also a dependency of a generated column
statement ok
CREATE TABLE unit (
bar TEXT,
foobar as (concat(foo, bar)),
profit_total AS (foobar),
dependent AS (profit_total),
also_dependent AS (concat(profit_total,dependent)),
foo TEXT,
);
statement ok
INSERT INTO unit VALUES ('string','test')
query IIIIII
SELECT * FROM unit;
----
string teststring teststring teststring teststringteststring test

View File

@@ -0,0 +1,47 @@
# name: test/sql/generated_columns/virtual/typechange.test
# description: Change the type of a generated column
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (
price INTEGER,
amount_sold INTEGER,
total_profit AS (price * amount_sold)
);
statement ok
INSERT INTO unit VALUES (5,4)
query I
SELECT total_profit FROM unit;
----
20
# Typechange is not supported yet
statement error
ALTER TABLE unit ALTER COLUMN total_profit TYPE BOOLEAN;
----
<REGEX>:.*Binder Error.*generated columns in alter statement.*
statement ok
INSERT INTO unit VALUES (5,0);
#query I
#SELECT total_profit FROM unit
#----
#True
#False
statement ok
CREATE TABLE tbl (
price INTEGER,
total_profit DATE AS (price * 5),
);
statement error
INSERT INTO tbl VALUES (5);
----
<REGEX>:.*Constraint Error.*Incorrect value for generated column.*

View File

@@ -0,0 +1,35 @@
# name: test/sql/generated_columns/virtual/typechange_dependency.test
# description: Test changing types of dependencies of generated columns
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE unit (
price INTEGER,
amount_sold INTEGER,
total_profit AS (price * amount_sold)
);
statement ok
INSERT INTO unit VALUES (5,4)
query I
SELECT total_profit FROM unit
----
20
# Dependency of 'total_price' - the change is prohibited
statement error
ALTER TABLE unit ALTER COLUMN price TYPE BOOLEAN;
----
# Generated column isn't deleted
statement ok
SELECT total_profit FROM unit;
query III
SELECT * FROM unit
----
5 4 20

View File

@@ -0,0 +1,44 @@
# name: test/sql/generated_columns/virtual/unique.test
# description: Test generated columns with the UNIQUE constraint
# group: [virtual]
statement ok
PRAGMA enable_verification
#Unique constraint not supported yet (SQLite supports it, so it is planned)
# Not supported appended to the column definition
statement error
CREATE TABLE unit (
price INTEGER,
amount_sold AS (price) UNIQUE,
);
----
<REGEX>:.*Binder Error.*not supported yet.*
# Also not as a column list
statement error
CREATE TABLE unit (
price INTEGER,
amount_sold AS (price),
UNIQUE (amount_sold),
);
----
<REGEX>:.*Binder Error.*not supported yet.*
# Show that proper UNIQUE constraints still work when mixed in when generated columns
statement ok
CREATE TABLE tbl (
gen1 AS (price),
gen2 AS (price),
price INTEGER UNIQUE
);
statement ok
INSERT INTO tbl VALUES (5);
# Violates unique constraint
statement error
INSERT INTO tbl VALUES (5);
----
<REGEX>:.*Constraint Error.*Duplicate key.*

View File

@@ -0,0 +1,58 @@
# name: test/sql/generated_columns/virtual/update.test
# description: Test that all types of update statements work
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (
foo INTEGER,
bar INTEGER GENERATED ALWAYS AS (foo+1) VIRTUAL,
foobar INTEGER DEFAULT 5
);
statement ok
INSERT INTO test VALUES (5, DEFAULT)
statement ok
INSERT INTO test VALUES (10, 3);
query III
SELECT * FROM test;
----
5 6 5
10 11 3
# Cant UPDATE a generated column
statement error
UPDATE test SET bar = 5
----
# Correctly formatted UPDATE statements work
statement ok
UPDATE test SET foo=3;
query III
SELECT * FROM test;
----
3 4 5
3 4 3
statement ok
UPDATE test SET foo=9001 WHERE (foobar == 3);
query III
SELECT * FROM test;
----
3 4 5
9001 9002 3
statement ok
UPDATE test SET foo=0 WHERE (bar < 9000);
query III
SELECT * FROM test;
----
0 1 5
9001 9002 3

View File

@@ -0,0 +1,25 @@
# name: test/sql/generated_columns/virtual/update_index.test
# description: Tests updates on a table with virtual columns and indexes
# group: [virtual]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE tbl_comp (
a INT,
gen AS (2 * a),
b INT,
c VARCHAR,
PRIMARY KEY(c));
statement ok
INSERT INTO tbl_comp VALUES (1, 1, 'hello');
statement ok
UPDATE tbl_comp SET c='world'
query IIII
FROM tbl_comp
----
1 2 1 world