Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions datafusion/expr/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1931,10 +1931,12 @@ fn project_with_validation(
expr: impl IntoIterator<Item = (impl Into<SelectExpr>, bool)>,
) -> Result<LogicalPlan> {
let mut projected_expr = vec![];
let mut has_wildcard = false;
for (e, validate) in expr {
let e = e.into();
match e {
SelectExpr::Wildcard(opt) => {
has_wildcard = true;
let expanded = expand_wildcard(plan.schema(), &plan, Some(&opt))?;

// If there is a REPLACE statement, replace that column with the given
Expand All @@ -1955,6 +1957,7 @@ fn project_with_validation(
}
}
SelectExpr::QualifiedWildcard(table_ref, opt) => {
has_wildcard = true;
let expanded =
expand_qualified_wildcard(&table_ref, plan.schema(), Some(&opt))?;

Expand Down Expand Up @@ -1984,6 +1987,12 @@ fn project_with_validation(
}
}
}
if has_wildcard && projected_expr.is_empty() && !plan.schema().fields().is_empty() {
return plan_err!(
"SELECT list is empty after resolving * expressions, \
the wildcard expanded to zero columns"
);
}
validate_unique_names("Projections", projected_expr.iter())?;

Projection::try_new(projected_expr, Arc::new(plan)).map(LogicalPlan::Projection)
Expand Down
28 changes: 14 additions & 14 deletions datafusion/sqllogictest/test_files/select.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1318,25 +1318,25 @@ statement error
SELECT * EXCLUDE(a, a)
FROM table1

# if EXCEPT all the columns, query should still succeed but return empty
statement ok
# if EXCEPT all the columns, query should return an error
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, postgres rejects this syntax

postgres=# CREATE TABLE table1 (
  a int,
  b int,
  c int,
  d int
) ;
CREATE TABLE
postgres=# SELECT * EXCEPT(a, b, c, d)
FROM table1;
ERROR:  syntax error at or near "a"
LINE 1: SELECT * EXCEPT(a, b, c, d)
                        ^

Thus I agree we should be following the duckdb behavior

memory D CREATE TABLE table1 (
           a int,
           b int,
           c int,
           d int
         );

However, duckdb seems to reject this syntax entirely

How did

memory D SELECT * EXCEPT a, b, c, d FROM table1;
Parser Error:
syntax error at or near "a"

LINE 1: SELECT * EXCEPT a, b, c, d FROM table1;
                        ^
memory D SELECT * EXCEPT(a, b, c, d) FROM table1;
Parser Error:
syntax error at or near "a"

LINE 1: SELECT * EXCEPT(a, b, c, d) FROM table1;
                        ^

It looks like the actual syntax is EXCLUDE rather than EXCEPT...

memory D SELECT * EXCLUDE(a, b, c, d) FROM table1;
Binder Error:
SELECT list is empty after resolving * expressions!

🤔

statement error DataFusion error: Error during planning: SELECT list is empty after resolving \* expressions, the wildcard expanded to zero columns
SELECT * EXCEPT(a, b, c, d)
FROM table1

# try zero column with LIMIT, 1 row but empty
statement ok
# try zero column with LIMIT, should error
statement error DataFusion error: Error during planning: SELECT list is empty after resolving \* expressions, the wildcard expanded to zero columns
SELECT * EXCEPT (a, b, c, d)
FROM table1
LIMIT 1

# try zero column with GROUP BY, 2 row but empty
statement ok
# try zero column with GROUP BY, should error
statement error DataFusion error: Error during planning: SELECT list is empty after resolving \* expressions, the wildcard expanded to zero columns
SELECT * EXCEPT (a, b, c, d)
FROM table1
GROUP BY a

# try zero column with WHERE, 1 row but empty
statement ok
# try zero column with WHERE, should error
statement error DataFusion error: Error during planning: SELECT list is empty after resolving \* expressions, the wildcard expanded to zero columns
SELECT * EXCEPT (a, b, c, d)
FROM table1
WHERE a = 1
Expand All @@ -1352,15 +1352,15 @@ CREATE TABLE table2 (
(1, 10, 100, 1000),
(2, 20, 200, 2000);

# try zero column with inner JOIN, 2 row but empty
statement ok
# try zero column with inner JOIN, should error
statement error DataFusion error: Error during planning: SELECT list is empty after resolving \* expressions, the wildcard expanded to zero columns
WITH t1 AS (SELECT a AS t1_a FROM table1), t2 AS (SELECT a AS t2_a FROM table2)
SELECT * EXCEPT (t1_a, t2_a)
FROM t1
JOIN t2 ON (t1_a = t2_a)

# try zero column with more JOIN, 2 row but empty
statement ok
# try zero column with more JOIN, should error
statement error DataFusion error: Error during planning: SELECT list is empty after resolving \* expressions, the wildcard expanded to zero columns
SELECT * EXCEPT (b1, b2)
FROM (
SELECT b AS b1 FROM table1
Expand All @@ -1369,8 +1369,8 @@ JOIN (
SELECT b AS b2 FROM table2
) ON b1 = b2

# try zero column with Window, 2 row but empty
statement ok
# try zero column with Window, should error
statement error DataFusion error: Error during planning: SELECT list is empty after resolving \* expressions, the wildcard expanded to zero columns
SELECT * EXCEPT (a, b, row_num)
FROM (
SELECT
Expand Down
Loading