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/binder/alias_error_10057.test
# description: Test old_implicit_cast setting
# group: [binder]
statement ok
PRAGMA enable_verification
statement error
with test_data as (
select 'foo' as a
)
select test_data.foobar as new_column from test_data where new_column is not null;
----
foobar

View File

@@ -0,0 +1,11 @@
# name: test/sql/binder/alias_where_side_effects.test
# description: Issue #10099: Incorrect query result when using random value in where clause
# group: [binder]
statement ok
PRAGMA enable_verification
statement error
select count(*) from (select random() as num from range(20) where num > 0.9) where num <= 0.9
----
expression has side effects

View File

@@ -0,0 +1,42 @@
# name: test/sql/binder/column_value_alias_group.test
# description: Test using column value aliases as groups
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
create table test(a int);
statement ok
insert into test values (2), (1), (3);
# group by user
query I rowsort
select a as "user" from test group by "user";
----
1
2
3
# having
query I
select a as "user" from test group by "user" having "user"=1;
----
1
# order by user
query I
select a as "user" from test order by "user";
----
1
2
3
# group by AND order by user
query I
select a as "user" from test group by "user" order by "user";
----
1
2
3

View File

@@ -0,0 +1,40 @@
# name: test/sql/binder/duplicate_alias.test
# description: Duplicate table aliases
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
create table t(i int);
statement ok
INSERT INTO t VALUES (42);
# this works - since no column is referenced there is no ambiguity
query I
SELECT COUNT(*) FROM t, t
----
1
# this works - all columns can be uniquely identified - no ambiguity
query II
SELECT * FROM (SELECT 42 x) t, (SELECT 84 y) t
----
42 84
query II
SELECT t.x, t.y FROM (SELECT 42 x) t, (SELECT 84 y) t
----
42 84
statement error
SELECT t.z FROM (SELECT 42 x) t, (SELECT 84 y) t
----
does not have a column named
# this does not work - "t" is ambiguous
statement error
SELECT t.i FROM t, t
----
duplicate alias "t"

View File

@@ -0,0 +1,43 @@
# name: test/sql/binder/function_chaining_19035.test
# description: Lambda expression with macro function chaining
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
CREATE MACRO list_contains_macro(x, y) AS (SELECT list_contains(x, y))
statement error
SELECT list_filter([[1, 2, 1], [1, 2, 3], [1, 1, 1]], lambda x: list_contains_macro(x, 3))
----
<REGEX>:Binder Error.*subqueries in lambda expressions are not supported.*
statement ok
CREATE TABLE tbl(a int[]);
statement ok
INSERT INTO tbl VALUES ([5, 4, 3]), ([1, 2, 3]), (NULL), ([NULL, 101, 12]);
query I
SELECT list_transform(a, lambda x, i: x + i + list_any_value(a)) FROM tbl;
----
[11, 11, 11]
[3, 5, 7]
NULL
[NULL, 204, 116]
query I
select ['a a ', ' b ', ' cc'].list_transform(lambda x: nullif(trim(x), '')) as trimmed_and_nulled
----
[a a, b, cc]
query I
select ['a a ', ' b ', ' cc'].list_transform(lambda x: x.trim().nullif('')) as trimmed_and_nulled;
----
[a a, b, cc]
query I
select ['a a ', ' b ', ' cd'].list_transform(lambda x: x.trim().nullif('').reverse()) as trimmed_and_nulled;
----
[a a, b, dc]

View File

@@ -0,0 +1,18 @@
# name: test/sql/binder/group_by_incremental_alias.test
# description: Issue #7880: Group By incremental alias issues
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
create table my_functions as select 'my_name' as function_name;
query II
select
function_name as raw,
replace(raw, '_', ' ') as prettier
from my_functions
group by all;
----
my_name my name

View File

@@ -0,0 +1,30 @@
# name: test/sql/binder/group_by_qualification.test
# description: Issue #8740 - Error when not using fully qualified field name on select AND group by
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
CREATE SCHEMA IF NOT EXISTS prd;
statement ok
CREATE TABLE prd.ad_stats_v(event_date DATE, app VARCHAR, country VARCHAR, platform VARCHAR);
# mix of column, table.column, schema.table.column qualifications
statement ok
select event_date, country
FROM prd.ad_stats_v
GROUP by event_date, prd.ad_stats_v.country
order by country
statement ok
select event_date, ad_stats_v.country
FROM prd.ad_stats_v
GROUP by event_date, prd.ad_stats_v.country
order by ad_stats_v.country
statement ok
select event_date, prd.ad_stats_v.country
FROM prd.ad_stats_v
GROUP by event_date, prd.ad_stats_v.country;

View File

@@ -0,0 +1,44 @@
# name: test/sql/binder/integer_literal_binding.test
# description: Test integer literal binding
# group: [binder]
statement ok
PRAGMA enable_verification
# integer literal binding for arithmetic
foreach type UTINYINT USMALLINT UINTEGER UBIGINT TINYINT SMALLINT INTEGER BIGINT
query I
SELECT typeof(100::${type} + 1) == '${type}';
----
true
query I
SELECT typeof(100 + 1::${type}) == '${type}';
----
true
endloop
# integer literals out of range for type
query I
SELECT typeof(1::TINYINT + 100);
----
TINYINT
query I
SELECT typeof(1::TINYINT + 10000);
----
INTEGER
# integer literals with decimals
query I
SELECT typeof(1.05 + 1)
----
DECIMAL(13,2)
query I
SELECT typeof(1 + 1);
----
INTEGER

View File

@@ -0,0 +1,26 @@
# name: test/sql/binder/missing_column_error.test
# description: Correctly report missing column errors instead of lateral joins
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
create table customers (name varchar, age integer, something_easy_to_type_wrong integer, city varchar);
statement ok
create table zipcodes (city varchar, zipcode varchar);
statement error
with cte as (
select *,
rank() over (order by something_easy_to_typo_wrong) as rk
from customers
where age <= 42
)
select *
from zipcodes
join cte
using (city);
----
something_easy_to_type_wrong

View File

@@ -0,0 +1,65 @@
# name: test/sql/binder/not_similar_to.test
# description: Correctly return all columns NOT similar to pattern
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
create or replace table foo as select 'd' as a, 'e' as b, 'f' as c;
# should select the column a and c
query II
select * similar to '(a|c)' from foo;
----
d f
# should select the column b
query I
select * not similar to '(a|c)' from foo;
----
e
# should select the column b
query I
SELECT * similar to 'b' FROM (select * not similar to '(a|c)' from foo);
----
e
statement error
SELECT * similar to 'a' FROM (select * not similar to '(a|c)' from foo);
----
Binder Error: No matching columns found that match regex "a"
statement ok
CREATE TABLE t0(c0 VARCHAR);
statement ok
INSERT INTO t0(c0) VALUES (0.1);
query T
SELECT * FROM t0 WHERE REGEXP_MATCHES(t0.c0, '1');
----
0.1
query T
SELECT * FROM t0 WHERE NOT REGEXP_MATCHES(t0.c0, '1');
----
query I
SELECT 'aaa' NOT SIMILAR TO '[b-z]{3}';
----
1
statement ok
CREATE TABLE integers(col1 INTEGER, col2 INTEGER, k INTEGER)
statement ok
INSERT INTO integers VALUES (1, 2, 3)
query II
SELECT * LIKE 'col%' FROM integers
----
1 2

View File

@@ -0,0 +1,39 @@
# name: test/sql/binder/old_implicit_cast.test
# description: Test old_implicit_cast setting
# group: [binder]
statement ok
CREATE TABLE integers AS SELECT 42 AS i, '5' AS v;
statement error
SELECT i >= v FROM integers
----
an explicit cast is required
statement error
SELECT i[1] FROM integers
----
No function matches
statement error
SELECT [i, v] FROM integers
----
Binder Error: Cannot deduce template type
statement ok
SET old_implicit_casting=true
query I
SELECT i[1] FROM integers
----
4
query I
SELECT i >= v FROM integers
----
true
query I
SELECT [i, v] FROM integers
----
[42, 5]

View File

@@ -0,0 +1,43 @@
# name: test/sql/binder/order_by_view.test
# description: Test ORDER BY a view with DISTINCT ON
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE src("Name" VARCHAR, CreatedAt TIMESTAMP, userID VARCHAR, "Version" VARCHAR, Clients BIGINT, HasDocumentation BOOLEAN, HasCustomAddress BOOLEAN, HasHostname BOOLEAN, RunningContainers BIGINT, HasActions BOOLEAN);
statement ok
CREATE VIEW model AS
SELECT DISTINCT on(userID, date_trunc('day', CreatedAt))
date_trunc('day', CreatedAt) AS CreatedAt,
"Version",
Clients,
HasCustomAddress,
HasHostname,
RunningContainers,
HasDocumentation,
HasActions
FROM src
WHERE name = 'events'
ORDER BY userID, CreatedAt DESC;
statement ok
SELECT HasCustomAddress, count(*) AS total_records
FROM model
WHERE
1 = 1 AND
CreatedAt >= '2023-12-01' AND
CreatedAt < '2023-12-13'
GROUP BY HasCustomAddress ;
statement ok
SELECT HasCustomAddress, count(*) AS total_records
FROM model
WHERE 1 = 1 AND
CreatedAt >= '2023-12-01' AND
CreatedAt < '2023-12-13'
GROUP BY HasCustomAddress
ORDER BY true, total_records DESC NULLS LAST
LIMIT 250 OFFSET 0;

View File

@@ -0,0 +1,163 @@
# name: test/sql/binder/separate_schema_tables.test
# description: Test tables in different schemas with the same name
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
CREATE SCHEMA IF NOT EXISTS s1;
statement ok
CREATE SCHEMA IF NOT EXISTS s2;
statement ok
CREATE SCHEMA IF NOT EXISTS s3;
statement ok
CREATE TABLE s1.tbl(i INT);
statement ok
CREATE TABLE s2.tbl(i INT);
statement ok
CREATE TABLE s3.tbl(i INT);
statement ok
CREATE TABLE tbl(i INT);
statement ok
INSERT INTO s1.tbl VALUES (10);
statement ok
INSERT INTO s2.tbl VALUES (100);
statement ok
INSERT INTO s3.tbl VALUES (1000);
statement ok
INSERT INTO tbl VALUES (1);
query III
SELECT * FROM tbl, s1.tbl, s2.tbl
----
1 10 100
query IIII
SELECT * FROM tbl, s1.tbl, s2.tbl, s3.tbl
----
1 10 100 1000
statement error
SELECT tbl.i FROM s1.tbl, s2.tbl
----
s1.tbl or s2.tbl
statement error
SELECT tbl.i FROM s1.tbl, s2.tbl, s3.tbl
----
s1.tbl, s2.tbl or s3.tbl
# struct pack
query III
SELECT s1.tbl, s2.tbl, s3.tbl FROM s1.tbl, s2.tbl, s3.tbl
----
{'i': 10} {'i': 100} {'i': 1000}
# test joins
statement ok
CREATE TABLE s1.t AS SELECT 1 id, 's1.t' payload UNION ALL SELECT 10 id, 'AAA' payload
statement ok
CREATE TABLE s2.t AS SELECT 1 id, 's2.t' payload2 UNION ALL SELECT 100 id, 'BBB' payload2
statement ok
CREATE TABLE s3.t AS SELECT 1 id, 's3.t' payload3 UNION ALL SELECT 1000 id, 'CCC' payload3
# USING
query IIII
SELECT * FROM s1.t JOIN s2.t USING (id) JOIN s3.t USING (id)
----
1 s1.t s2.t s3.t
# explicit column reference to using column
query I
SELECT id FROM s1.t JOIN s2.t USING (id) JOIN s3.t USING (id)
----
1
# natural join
query IIII
SELECT * FROM s1.t NATURAL JOIN s2.t NATURAL JOIN s3.t
----
1 s1.t s2.t s3.t
# left join
query IIIIIII
SELECT id, s1.t.id, s2.t.id, s3.t.id, s1.t.payload, s2.t.payload2, s3.t.payload3
FROM s1.t LEFT JOIN s2.t USING (id) LEFT JOIN s3.t USING (id)
ORDER BY ALL
----
1 1 1 1 s1.t s2.t s3.t
10 10 NULL NULL AAA NULL NULL
# right join
query IIIIIII
SELECT id, s1.t.id, s2.t.id, s3.t.id, s1.t.payload, s2.t.payload2, s3.t.payload3
FROM s1.t RIGHT JOIN s2.t USING (id) RIGHT JOIN s3.t USING (id)
ORDER BY ALL
----
1 1 1 1 s1.t s2.t s3.t
1000 NULL NULL 1000 NULL NULL CCC
# full outer join
query IIIIIII
SELECT id, s1.t.id, s2.t.id, s3.t.id, s1.t.payload, s2.t.payload2, s3.t.payload3
FROM s1.t FULL OUTER JOIN s2.t USING (id) FULL OUTER JOIN s3.t USING (id)
ORDER BY ALL
----
1 1 1 1 s1.t s2.t s3.t
10 10 NULL NULL AAA NULL NULL
100 NULL 100 NULL NULL BBB NULL
1000 NULL NULL 1000 NULL NULL CCC
# now do the same with identifiers that differ in case only
statement ok
CREATE OR REPLACE TABLE s1.tbl(col INT);
statement ok
CREATE OR REPLACE TABLE s2.TBL(COL INT);
statement ok
CREATE OR REPLACE TABLE s3.Tbl(Col INT);
statement ok
INSERT INTO s1.tbl VALUES (10);
statement ok
INSERT INTO s2.tbl VALUES (100);
statement ok
INSERT INTO s3.tbl VALUES (1000);
query IIII
SELECT * FROM tbl, s1.tbl, s2.tbl, s3.tbl
----
1 10 100 1000
statement error
SELECT tbl.col FROM s1.tbl, s2.tbl
----
s1.tbl or s2.TBL
statement error
SELECT tbl.col FROM s1.tbl, s2.tbl, s3.tbl
----
s1.tbl, s2.TBL or s3.Tbl
# struct pack
query III
SELECT s1.tbl, s2.tbl, s3.tbl FROM s1.tbl, s2.tbl, s3.tbl
----
{'col': 10} {'COL': 100} {'Col': 1000}

View File

@@ -0,0 +1,64 @@
# name: test/sql/binder/similar_to.test
# description: Test similar to suggestions
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE depdelay_minutes(depdelay_minutes INTEGER);
statement error
SELECT * FROM depdelay
----
depdelay_minutes
statement error
SELECT depdelay FROM depdelay_minutes
----
depdelay_minutes
statement ok
CREATE TABLE lineitem(i INTEGER);
statement error
SELECT * FROM li
----
lineitem
statement error
SELECT * FROM lineitem_long
----
lineitem
statement error
select jaro_winkler_('x', 'y');
----
jaro_winkler_similarity
# check entries in other schemas
statement ok
create schema s1;
statement ok
create table s1.my_lineitem(i int);
# exact match: show it
statement error
select * from my_lineitem;
----
s1.my_lineitem
# we prefer the current schema even if another schema has a slightly better match
statement error
select * from m_lineitem;
----
lineitem
statement ok
create table s1.orders(i int)
statement error
select * from ord
----
s1.orders

View File

@@ -0,0 +1,112 @@
# name: test/sql/binder/string_literal_binding.test
# description: Test string literal binding
# group: [binder]
statement ok
PRAGMA enable_verification
# string literals bind differently from strings
# they can be auto-cast to everything
# this is an integer comparison
query I
select '01'=1;
----
true
query I
SELECT cos('0')
----
1
# date comparison
query I
select date '1992-01-01'>'1991-01-01';
----
true
# Issue #9948 - this is a date comparison, and (2023-12-11 < 2023-12-11) = false
query I
select date '2023-12-11' < '2023-12-11 15:54:45.119';
----
false
# Issue #8529 - Inconsistent Date Partition Handling Between DuckDB and PostgreSQL
statement ok
CREATE TABLE test (
"date" DATE,
value VARCHAR
);
statement ok
INSERT INTO test VALUES ('2023-08-01', 1), ('2023-08-02', 2), ('2023-08-03', 3), ('2023-08-04', 4), ('2023-08-05', 5), ('2023-08-06', 6), ('2023-08-07', 7);
query II
SELECT * FROM test WHERE date >= '2023-08-05 00:00:00' AND date < '2023-08-06 00:00:00';
----
2023-08-05 5
# literals prefer to be strings
query I
SELECT '[hello]'[1];
----
[
query I
SELECT list('hello world')
----
[hello world]
# literals in IN clause work correctly
query I
select 1 IN ('1', '2');
----
true
# as does COALESCE with string literals
query I
SELECT COALESCE(1, '1');
----
1
# we can do equality comparison with string columns
query I
select i=1 from (values ('01')) t(i);
----
true
# or with an IN clause
query I
select i IN (1) from (values ('01')) t(i);
----
true
# consistency between IN and equality
query I
WITH cte AS (SELECT '01' AS s)
SELECT 1=s AS in_res FROM cte;
----
true
query I
WITH cte AS (SELECT '01' AS s)
SELECT 1 IN (s) AS in_res FROM cte;
----
true
# but not >
statement error
select i>1 from (values ('01')) t(i);
----
an explicit cast is required
# we cannot do the same comparison with string columns
statement error
select date '1992-01-01'>i from (values ('1991-01-01')) t(i);
----
Cannot compare values of type
# we cannot subscript dates or other types
statement error
select d[1] from (values (date '1992-01-01')) t(d);
----
No function matches the given name and argument types

View File

@@ -0,0 +1,31 @@
# name: test/sql/binder/table_alias_single_quotes.test
# description: Test table alias with single quotes
# group: [binder]
statement ok
SET default_null_order='nulls_first';
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE integers(i INTEGER);
statement ok
INSERT INTO integers VALUES (1), (2), (3), (NULL)
query I
SELECT t.k FROM integers AS 't'('k') ORDER BY ALL
----
NULL
1
2
3
query I
SELECT t.k FROM integers t('k') ORDER BY ALL
----
NULL
1
2
3

View File

@@ -0,0 +1,46 @@
# name: test/sql/binder/table_view_alias.test
# description: Test table/view aliasing
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
CREATE SCHEMA s1;
statement ok
CREATE VIEW s1.v AS SELECT 42 c;
statement ok
CREATE TABLE s1.t AS SELECT 42 c
query III
SELECT s1.v.c, v.c, c FROM s1.v
----
42 42 42
query III
SELECT s1.t.c, t.c, c FROM s1.t
----
42 42 42
# explicitly aliasing the table, even if it is to the same name, should make the schema reference no longer work
statement error
SELECT s1.t.c, t.c, c FROM s1.t AS t
----
Referenced table "s1.t" not found
statement error
SELECT s1.x.c FROM s1.v AS x
----
Referenced table "s1.x" not found
statement error
SELECT s1.v.c FROM s1.v AS x
----
Referenced table "s1.v" not found
statement error
SELECT s1.v.c FROM s1.v AS v
----
Referenced table "s1.v" not found

View File

@@ -0,0 +1,75 @@
# name: test/sql/binder/test_alias.test
# description: Test that aliases work properly in renaming columns
# group: [binder]
statement ok
SET default_null_order='nulls_first';
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE integers(i INTEGER);
statement ok
INSERT INTO integers VALUES (1), (2), (3), (NULL)
query IR
SELECT i % 2 AS p, SUM(i) AS sum_i FROM integers GROUP BY p ORDER BY 1
----
NULL NULL
0 2.000000
1 4.000000
query TT
SELECT alias(i % 2) AS p, alias(SUM(i)) AS sum_i FROM integers GROUP BY p ORDER BY 1
----
p sum_i
query II
SELECT i + 1 + 1 + 1 AS k, abs(i) AS l FROM integers WHERE i=1 ORDER BY 1
----
4 1
query TT
SELECT alias(i + 1 + 1 + 1) AS k, alias(abs(i)) AS l FROM integers WHERE i=1 ORDER BY 1
----
k l
query TTTT
SELECT alias(i) AS k, alias(i IN (1)) AS l, alias(i >= 10) AS m, alias(1=0) AS n FROM integers WHERE i=1 ORDER BY 1
----
k l m n
query TT
SELECT alias(CASE WHEN i=1 THEN 19 ELSE 0 END) AS k, alias(i::VARCHAR) AS l FROM integers WHERE i=1 ORDER BY 1
----
k l
statement ok
CREATE TABLE test (a INTEGER, b INTEGER)
statement ok
INSERT INTO test VALUES (42, 10), (43, 100);
# check column names for simple projections and aliases
query IIII
SELECT a, b, a * 2 AS c, b * (a * 2) AS d FROM test ORDER BY a
----
42 10 84 840
43 100 86 8600
query TTTT
SELECT alias(a), alias(b), alias(a * 2) AS c, alias(b * (a * 2)) AS d FROM test ORDER BY a
----
a b c d
a b c d
# test nested alias in where clause
query IIII
select i as b, b as c, c as d, d as e from integers where e = 3;
----
3 3 3 3

View File

@@ -0,0 +1,88 @@
# name: test/sql/binder/test_alias_map_in_subquery.test
# description: Test binding an alias in a subquery
# group: [binder]
# Simple reproduction.
statement ok
CREATE OR REPLACE TABLE tbl (example VARCHAR);
statement ok
INSERT INTO tbl VALUES ('hello');
query I
SELECT (WITH keys AS (
SELECT example AS k
), nonNull AS (
SELECT keys.k, example AS v
FROM keys
WHERE v IS NOT NULL
)
SELECT nonNull.v
FROM nonNull
)
FROM tbl;
----
hello
# With the JSON extension and lambdas.
require json
statement ok
CREATE OR REPLACE TABLE testjson (example JSON);
statement ok
INSERT INTO testjson VALUES ('{ "location" : { "address" : "123 Main St" }, "sampleField" : null, "anotherField" : 123, "yetAnotherField" : "abc" }');
query I
SELECT (WITH keys AS (SELECT unnest(json_keys(example)) AS k), nonNull AS (
SELECT keys.k, example->keys.k AS v
FROM keys WHERE nullif(v, 'null') IS NOT NULL
)
SELECT json_group_object(nonNull.k, nonNull.v)
FROM nonNull
)
FROM testjson;
----
{"location":{"address":"123 Main St"},"anotherField":123,"yetAnotherField":"abc"}
# With a MACRO (issue #10491).
statement ok
CREATE OR REPLACE MACRO strip_null_value(jsonValue) AS (
WITH keys AS (SELECT UNNEST(json_keys(jsonValue)) AS k),
nonNull AS (
SELECT keys.k, jsonValue->keys.k AS v
FROM keys WHERE nullif(v, 'null') IS NOT NULL
)
SELECT json_group_object(nonNull.k, nonNull.v)
FROM nonNull
);
query I
SELECT strip_null_value('{ "location" : { "address" : "123 Main St" }, "sampleField" : null, "anotherField" : 123, "yetAnotherField" : "abc" }')
AS example;
----
{"location":{"address":"123 Main St"},"anotherField":123,"yetAnotherField":"abc"}
# Testing with a table.
statement ok
CREATE OR REPLACE TABLE testjson (example JSON);
statement ok
INSERT INTO testjson
VALUES ('{ "location" : { "address" : "123 Main St" }, "sampleField" : null, "anotherField" : 123, "yetAnotherField" : "abc" }');
query I
SELECT strip_null_value(example) FROM testjson;
----
{"location":{"address":"123 Main St"},"anotherField":123,"yetAnotherField":"abc"}
# Testing with a CTE.
query I
WITH x AS (
SELECT '{ "location" : { "address" : "123 Main St" }, "sampleField" : null, "anotherField" : 123, "yetAnotherField" : "abc" }'
AS example)
SELECT strip_null_value(x.example) AS test
FROM x;
----
{"location":{"address":"123 Main St"},"anotherField":123,"yetAnotherField":"abc"}

View File

@@ -0,0 +1,143 @@
# name: test/sql/binder/test_case_insensitive_binding.test
# description: Test case insensitive binding of columns
# group: [binder]
statement ok
PRAGMA enable_verification
# we can bind case insensitive column names
statement ok
CREATE TABLE test ("HeLlO" INTEGER)
statement ok
INSERT INTO test VALUES (1)
# lowercase names are aliased
statement ok
SELECT HeLlO FROM test
statement ok
SELECT hello FROM test
statement ok
SELECT "HeLlO" FROM test
statement ok
SELECT "HELLO" FROM test
statement ok
SELECT "HELLo" FROM test
# verify that the column name of the original column is returned
query I
SELECT alias(HeLlO) FROM test
----
HeLlO
query I
SELECT alias(hello) FROM test
----
HeLlO
# verify that an alias here still works
query I
SELECT alias(x) FROM (SELECT HeLlO as x FROM test) tbl;
----
x
# verify that it also works when we specify the table-name explicitly
statement ok
SELECT test.HeLlO FROM test
statement ok
SELECT test.hello FROM test
statement ok
SELECT test."HeLlO" FROM test
statement ok
SELECT test."HELLO" FROM test
statement ok
SELECT test."HELLo" FROM test
statement ok
UPDATE test SET hello=3
statement ok
UPDATE test SET HeLlO=3
statement ok
DROP TABLE test
# this counts as a duplicate column error
statement error
CREATE TABLE test("HeLlO" INTEGER, "HELLO" INTEGER)
----
# conflicts can come from different sources!
statement ok
CREATE TABLE test1("HeLlO" INTEGER)
statement ok
CREATE TABLE test2("HELLO" INTEGER)
statement error
SELECT HeLlO FROM test1, test2
----
statement error
SELECT hello FROM test1, test2
----
statement error
SELECT "HeLlO" FROM test1, test2
----
statement error
SELECT "HELLO" FROM test1, test2
----
statement error
SELECT "HELLo" FROM test1, test2
----
# in this case we can eliminate the conflict by specifically selecting the source
statement ok
SELECT test1.HeLlO FROM test1, test2
statement ok
SELECT test1.hello FROM test1, test2
statement ok
SELECT test1."HeLlO" FROM test1, test2
statement ok
SELECT test1."HELLO" FROM test1, test2
statement ok
SELECT test1."HELLo" FROM test1, test2
statement ok
SELECT test2.HeLlO FROM test1, test2
statement ok
SELECT test2.hello FROM test1, test2
statement ok
SELECT test2."HeLlO" FROM test1, test2
statement ok
SELECT test2."HELLO" FROM test1, test2
statement ok
SELECT test2."HELLo" FROM test1, test2
statement ok
SELECT * FROM test1 JOIN test2 USING (hello)
query I
SELECT hello FROM (SELECT 42) tbl("HeLlO")
----
42

View File

@@ -0,0 +1,140 @@
# name: test/sql/binder/test_function_chaining_alias.test
# description: Test referencing an alias or a function chaining alias
# group: [binder]
statement ok
PRAGMA enable_verification
query II
SELECT 'test' || ' more testing' AS added, added.substr(5) AS my_substr
----
test more testing more testing
statement ok
CREATE TABLE varchars(v VARCHAR);
statement ok
INSERT INTO varchars VALUES ('>>%Test<<'), ('%FUNCTION%'), ('Chaining')
query I
SELECT v.lower() FROM varchars
----
>>%test<<
%function%
chaining
# Use only_alphabet before it is defined.
statement error
SELECT
v.trim('><') AS trim_inequality,
only_alphabet.lower() AS lower,
trim_inequality.replace('%', '') AS only_alphabet,
FROM varchars
----
<REGEX>:Binder Error.*column cannot be referenced before it is defined.*
query III
SELECT
v.trim('><') AS trim_inequality,
trim_inequality.replace('%', '') AS only_alphabet,
only_alphabet.lower() AS lower
FROM varchars
----
%Test Test test
%FUNCTION% FUNCTION function
Chaining Chaining chaining
# Test a column with a table name.
query III
SELECT
varchars.v.trim('><') AS trim_inequality,
trim_inequality.replace('%', '') AS only_alphabet,
only_alphabet.lower() AS lower
FROM varchars
----
%Test Test test
%FUNCTION% FUNCTION function
Chaining Chaining chaining
statement ok
DELETE FROM varchars
statement ok
INSERT INTO varchars VALUES ('Test Function Chaining Alias');
query III
SELECT
v.split(' ')::VARCHAR strings,
strings.lower() lower,
lower.upper() upper
FROM varchars
----
[Test, Function, Chaining, Alias] [test, function, chaining, alias] [TEST, FUNCTION, CHAINING, ALIAS]
query IIII
SELECT
v.split(' ') strings,
strings.apply(lambda x: x.lower()).filter(lambda x: x[1] == 't') lower,
strings.apply(lambda x: x.upper()).filter(lambda x: x[1] == 'T') upper,
lower + upper AS mix_case_srings
FROM varchars
----
[Test, Function, Chaining, Alias] [test] [TEST] [test, TEST]
# Test prepared statements.
statement ok
PREPARE v1 AS
SELECT
(?.split(' ')::VARCHAR).lower() lstrings,
(?.split(' ')::VARCHAR).upper() ustrings,
list_concat(lstrings::VARCHAR[], ustrings::VARCHAR[]) AS mix_case_srings
query III
EXECUTE v1('Hello World', 'test function chaining')
----
[hello, world] [TEST, FUNCTION, CHAINING] [hello, world, TEST, FUNCTION, CHAINING]
statement ok
INSERT INTO varchars VALUES ('Another longggggg String');
# Use an alias in a WHERE clause.
query IIII
SELECT
v.split(' ') strings,
strings.apply(lambda x: x.lower()).filter(lambda x: x[1] == 't' OR x[1] == 'a') lower,
strings.apply(lambda x: x.upper()).filter(lambda x: x[1] == 'T' OR x[1] == 'A') upper,
lower + upper AS mix_case_srings
FROM varchars
WHERE mix_case_srings[1] = 'test'
----
[Test, Function, Chaining, Alias] [test, alias] [TEST, ALIAS] [test, alias, TEST, ALIAS]
query IIII
SELECT
v.split(' ') strings,
strings.apply(lambda x: x.lower()).filter(lambda x: x[1] == 't' OR x[1] == 'a') lower,
strings.apply(lambda x: x.upper()).filter(lambda x: x[1] == 'T' OR x[1] == 'A') upper,
lower + upper AS mix_case_srings
FROM varchars
WHERE mix_case_srings[1] = 'another'
----
[Another, longggggg, String] [another] [ANOTHER] [another, ANOTHER]
# CTE with function chaining alias.
query II
WITH test AS (
SELECT 'woot' AS my_column
)
FROM test
SELECT
my_column.substr(2) AS partial_woot,
partial_woot.substr(2) AS more_partially_woot
WHERE
more_partially_woot = 'ot';
----
oot ot

View File

@@ -0,0 +1,87 @@
# name: test/sql/binder/test_having_alias.test
# description: Test that aliases can be used in the HAVING clause
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE integers AS SELECT * FROM range(5) tbl(i);
# use an alias to an aggregate in the having clause
query II
SELECT i, COUNT(*) AS k FROM integers GROUP BY i HAVING k=1 ORDER BY i;
----
0 1
1 1
2 1
3 1
4 1
# alias cannot be qualified
statement error
SELECT i, COUNT(*) AS k FROM integers GROUP BY i HAVING integers.k=1 ORDER BY i;
----
Binder Error: column k must appear in the GROUP BY clause
# column name wins over the alias
query II
SELECT 1 AS i, COUNT(*) FROM integers GROUP BY i HAVING i=2;
----
1 1
# as if qualified
query II
SELECT i AS j, COUNT(*) AS i FROM integers GROUP BY j HAVING integers.i=1 ORDER BY i;
----
1 1
# or we use the group by alias
query II
SELECT i AS j, COUNT(*) AS i FROM integers GROUP BY j HAVING j=1 ORDER BY i;
----
1 1
query I
SELECT COUNT(i) AS j FROM integers HAVING j=5;
----
5
query I
SELECT COUNT(i) AS i FROM integers HAVING i=5;
----
5
query I
SELECT COUNT(i) AS i FROM integers GROUP BY i HAVING i=5;
----
query I
SELECT COUNT(i) AS i FROM integers HAVING i=5 ORDER BY i;
----
5
# use the same alias multiple times
query I
SELECT COUNT(i) AS j FROM integers HAVING j=j;
----
5
# multiple alias in HAVING to expression with side-effects
query I
SELECT COUNT(*) FROM (SELECT i, SUM(RANDOM()) AS k FROM integers GROUP BY i HAVING k=k) tbl(i, k);
----
5
# if this is qualified we get an error
statement error
SELECT COUNT(i) AS i FROM integers HAVING integers.i=5 ORDER BY i;
----
Binder Error: column i must appear in the GROUP BY clause
# recursive alias without aggregate
statement error
SELECT i + i AS i FROM integers HAVING i=5 ORDER BY i;
----
Binder Error: column i must appear in the GROUP BY clause

View File

@@ -0,0 +1,80 @@
# name: test/sql/binder/test_implicit_struct_pack.test
# description: Test implicit struct pack when selecting table alias
# group: [binder]
statement ok
pragma enable_verification
statement ok
create table test as select range i from range(3)
query T
select test from test
----
{'i': 0}
{'i': 1}
{'i': 2}
# equivalent
query T nosort q0
select test from main.test
query T nosort q0
select main.test from main.test
query T nosort q0
SELECT t FROM test AS t
query T nosort q0
select t from (SELECT * FROM test) AS t
# shouldn't work
statement error
select main.test from main.test t
----
statement error
select main.t from main.test t
----
# example query in feature request
query T
WITH data AS (
SELECT 1 as a, 2 as b, 3 as c
)
SELECT d FROM data d
----
{'a': 1, 'b': 2, 'c': 3}
# schema conflict
# create ambiguity: main.test can refer to column main.test, or to table main.test
statement ok
create table main as select 3 test
# selecting the column has precedence over selecting the implicit struct_pack
query T
select main.test from main, test
----
3
3
3
query T
select test from main, test
----
3
3
3
# struct conflict
# more ambiguity: main.test can now also refer to struct field "test" of struct "main"
# selecting the struct field has precedence over selecting the implicit struct_pack
statement ok
create table structs as select {test: 4} main
query T
select main.test from structs, test
----
4
4
4

View File

@@ -0,0 +1,75 @@
# name: test/sql/binder/test_in_with_collate.test
# description: Test that we can use string collation in IN and NOT IN operator
# group: [binder]
# create table with collation
statement ok
create table tbl (str varchar);
# insert data with nocase
statement ok
insert into tbl values ('ABCDE'), ('aBcDe');
# insert data with noaccent
statement ok
insert into tbl values ('àbcdë');
# insert data with noaccent and nocase
statement ok
insert into tbl values ('ÀbCdÈ');
# query IN without collation
query I
select * from tbl where str in ('AbCdE');
----
# query IN with nocase collation
query I
select * from tbl where str collate nocase in ('abcde');
----
ABCDE
aBcDe
# query IN with noaccent collation
query I
select * from tbl where str collate noaccent in ('abcde');
----
àbcdë
# query IN with nocase.noaccent collation
query I
select * from tbl where str collate nocase.noaccent in ('abcde');
----
ABCDE
aBcDe
àbcdë
ÀbCdÈ
# query NOT IN without collation
query I
select * from tbl where str not in ('abcde');
----
ABCDE
aBcDe
àbcdë
ÀbCdÈ
# query NOT IN with nocase collation
query I
select * from tbl where str collate nocase not in ('abcde');
----
àbcdë
ÀbCdÈ
# query NOT IN with noaccent collation
query I
select * from tbl where str collate noaccent not in ('abcde');
----
ABCDE
aBcDe
ÀbCdÈ
# query NOT IN with nocase.noaccent collation
query I
select * from tbl where str collate nocase.noaccent not in ('abcde');
----

View File

@@ -0,0 +1,210 @@
# name: test/sql/binder/test_null_type_propagation.test
# description: Test binding of NULL type
# group: [binder]
statement ok
PRAGMA enable_verification
# regular binding
query I
SELECT NULL
----
NULL
# union
query I
SELECT NULL UNION ALL SELECT CAST(1 AS BOOLEAN)
----
NULL
True
query I
SELECT NULL UNION ALL SELECT NULL
----
NULL
NULL
# mutliple unions
query I
SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT NULL UNION ALL SELECT CAST(1 AS BOOLEAN)
----
NULL
NULL
NULL
NULL
True
# scalar subquery
query I
SELECT (SELECT NULL) UNION ALL SELECT CAST(1 AS BOOLEAN)
----
NULL
True
# table subquery
query I
SELECT * FROM (SELECT NULL) tbl(i) UNION ALL SELECT CAST(1 AS BOOLEAN)
----
NULL
True
query I
SELECT * FROM (SELECT (SELECT NULL) UNION ALL SELECT CAST(1 AS BOOLEAN)) tbl(i)
----
NULL
True
query I
SELECT * FROM (SELECT NULL) tbl(i) UNION ALL SELECT NULL
----
NULL
NULL
# bool_and
query I
SELECT bool_and(i) FROM (SELECT * FROM (SELECT NULL) tbl(i) UNION ALL SELECT CAST(1 as BOOLEAN)) tbl(i)
----
True
# this wouldn't work if we would upcast to integer
statement error
SELECT bool_and(i) FROM (SELECT * FROM (SELECT NULL::INTEGER) tbl(i) UNION ALL SELECT CAST(1 as BOOLEAN)) tbl(i)
----
# cross product
query II
SELECT * FROM (SELECT NULL) tbl(i), (SELECT NULL) tbl2(j)
----
NULL NULL
query II
SELECT bool_and(i), bool_and(j) FROM (SELECT NULL) tbl(i), (SELECT NULL) tbl2(j)
----
NULL NULL
# queries with UNION (not UNION ALL) need the 'sort' because UNION is an aggregate - no order-preserving guarantees
# Issue #1049: Should a UNION with untyped NULL preserve the type?
query III sort
SELECT NULL as a, NULL as b, 1 as id UNION SELECT CAST(1 AS BOOLEAN) as a, CAST(0 AS BOOLEAN) as b, 2 as id
----
True False 2
NULL NULL 1
query III sort
SELECT CAST(1 AS BOOLEAN) as a, CAST(0 AS BOOLEAN) as b, 1 as id UNION SELECT NULL as a, NULL as b, 2 as id
----
True False 1
NULL NULL 2
# old behavior: cast NULL to integer explicitly
query III sort
SELECT NULL::INTEGER as a, NULL::INTEGER as b, 1 as id UNION SELECT CAST(1 AS BOOLEAN) as a, CAST(0 AS BOOLEAN) as b, 2 as id
----
1 0 2
NULL NULL 1
query III sort
SELECT CAST(1 AS BOOLEAN) as a, CAST(0 AS BOOLEAN) as b, 1 as id UNION SELECT NULL::INTEGER as a, NULL::INTEGER as b, 2 as id
----
1 0 1
NULL NULL 2
# NULL is auto-cast to integer for storage purposes
statement ok
CREATE TABLE tbl AS SELECT NULL UNION ALL SELECT NULL
query I
SELECT * FROM tbl
----
NULL
NULL
query I
SELECT typeof(#1) FROM tbl LIMIT 1
----
INTEGER
# views preserve NULLs
statement ok
CREATE VIEW v1 AS SELECT NULL
query I
SELECT * FROM v1
----
NULL
query I
SELECT * FROM v1 UNION ALL SELECT CAST(1 AS BOOLEAN)
----
NULL
True
query I
SELECT typeof(#1) FROM v1
----
"NULL"
# also with unions
statement ok
CREATE VIEW v2 AS SELECT NULL UNION ALL SELECT NULL
query I
SELECT * FROM v2
----
NULL
NULL
query I
SELECT * FROM v2 UNION ALL SELECT CAST(1 AS BOOLEAN)
----
NULL
NULL
True
query I
SELECT typeof(#1) FROM v2 LIMIT 1
----
"NULL"
# nulls in lists
query I
SELECT [NULL]
----
[NULL]
query I
SELECT [NULL] UNION ALL SELECT [True]
----
[NULL]
[true]
# nulls in structs
query I
SELECT {'x': NULL}
----
{'x': NULL}
query I
SELECT {'x': NULL} UNION ALL SELECT {'x': True}
----
{'x': NULL}
{'x': true}
# ctes
query I
WITH cte AS (SELECT NULL)
SELECT * FROM cte
----
NULL
require no_alternative_verify
# This does not work like this anymore, because CTE inlining happens in the optimizer.
# NULL type propagation for UNION ALL does not work as this test intends for materialized CTEs
# query I
# WITH cte AS NOT MATERIALIZED (SELECT NULL)
# SELECT * FROM cte UNION ALL SELECT CAST(1 AS BOOLEAN)
# ----
# NULL
# True

View File

@@ -0,0 +1,20 @@
# name: test/sql/binder/test_postitional_ref_order_binding.test
# description: Test that we can use string aliases with an AS clause
# group: [binder]
require tpch
statement ok
PRAGMA enable_verification
#statement ok
#call dbgen(sf=0.01)
statement ok
create table test as select * from (values (42, 43), (44, 45)) v(i, j);
query II
select i, sum(j) as s from test group by i order by #1;
----
42 43
44 45

View File

@@ -0,0 +1,64 @@
# name: test/sql/binder/test_select_clause_alias.test
# description: Test referencing an alias that exists earlier on in the SELECT clause
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE integers(i INTEGER);
statement ok
INSERT INTO integers VALUES (1), (2), (3);
# we can refer to aliases that occur earlier-on in the SELECT clause
query II
SELECT i + 1 AS a, a + 1 AS b FROM integers ORDER BY b
----
2 3
3 4
4 5
# we can also do this in a composite manner
query IIII
SELECT i + 1 AS a, a + a AS b, b + b AS c, c + c AS d FROM integers ORDER BY b
----
2 4 8 16
3 6 12 24
4 8 16 32
# the original columns take priority over aliases
query II
SELECT i + 1 AS i, i + 1 AS b FROM integers ORDER BY b
----
2 2
3 3
4 4
# we cannot refer to aliases that are defined LATER ON in the select clause
statement error
SELECT a + 1 AS b, i + 1 AS a FROM integers
----
# expressions with side effects are not supported yet
statement error
SELECT RANDOM() AS a, a + 1 AS b FROM integers
----
This is not yet supported
statement ok
create table orders as
select
cast(random()*100 as integer) + 1 as customer_id,
date '2020-01-01' + interval (cast(random()*365*10 as integer)) days as order_date,
cast(random()*1000 as integer) as order_amount,
from range(0, 1000)
order by order_date;
# expressions with subqueries are not supported yet
statement error
select
(select distinct date_trunc('month', order_date) from orders) as month,
(select sum(order_amount) from orders where date_trunc('month', order_date) = month) as revenue;
----
This is not yet supported

View File

@@ -0,0 +1,36 @@
# name: test/sql/binder/test_string_alias.test
# description: Test that we can use string aliases with an AS clause
# group: [binder]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE integers(i INTEGER);
# we can use strings as long as there is an AS clause
statement ok
SELECT i AS 'hello world' FROM integers
# without an AS clause this does not work
statement error
SELECT i 'hello world' FROM integers
----
<REGEX>:.*Catalog Error.*does not exist.*
# double quotes work everywhere
statement ok
SELECT i "hello world" FROM integers
statement ok
SELECT i AS "hello world" FROM integers
# also in table aliases we can use strings
statement ok
SELECT "hello world".i FROM integers AS 'hello world'
# but not without the AS clause
statement error
SELECT "hello world".i FROM integers 'hello world'
----
Parser Error: syntax error