Skip to content

Commit 10343c1

Browse files
alambadriangbclaudecomphead
authored
Revert apache#17295 (Support from-first SQL syntax) (apache#17520) (apache#17544)
* Add failing test * Fix regression in SELECT FROM syntax with WHERE clause When using 'SELECT FROM table WHERE condition', the query should create an empty projection (no columns) while still filtering rows. This was broken by PR apache#17295 which added FROM-first syntax support. The issue was that both 'FROM table' and 'SELECT FROM table' resulted in empty projection lists, making them indistinguishable. The fix checks for the presence of a WHERE clause to differentiate: - 'FROM table' (no WHERE) -> add wildcard projection (all columns) - 'SELECT FROM table WHERE ...' -> keep empty projection Also updates the test expectation to correctly show the empty Projection node in the query plan. Fixes apache#17513 * Revert * Fix regression: SELECT FROM syntax should return empty projection Removes automatic wildcard projection for empty projections, fixing the regression where `SELECT FROM table` incorrectly returned all columns instead of empty projection. Note: This temporarily breaks FROM-first syntax. A proper fix would require distinguishing between `FROM table` and `SELECT FROM table` at the parser level. Fixes apache#17513 🤖 Generated with [Claude Code](https://claude.ai/code) * add a better regression test * remove comment * fmt * Update datafusion/sqllogictest/test_files/projection.slt * Update datafusion/core/tests/sql/select.rs * revert docs * fmt --------- Co-authored-by: Adrian Garcia Badaracco <[email protected]> Co-authored-by: Claude <[email protected]> Co-authored-by: Oleks V <[email protected]>
1 parent 9e7141f commit 10343c1

File tree

5 files changed

+53
-77
lines changed

5 files changed

+53
-77
lines changed

datafusion/core/tests/sql/select.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,28 @@ async fn test_version_function() {
344344

345345
assert_eq!(version.value(0), expected_version);
346346
}
347+
348+
/// Regression test for https://github.com/apache/datafusion/issues/17513
349+
/// See https://github.com/apache/datafusion/pull/17520
350+
#[tokio::test]
351+
async fn test_select_no_projection() -> Result<()> {
352+
let tmp_dir = TempDir::new()?;
353+
// `create_ctx_with_partition` creates 10 rows per partition and we chose 1 partition
354+
let ctx = create_ctx_with_partition(&tmp_dir, 1).await?;
355+
356+
let results = ctx.sql("SELECT FROM test").await?.collect().await?;
357+
// We should get all of the rows, just without any columns
358+
let total_rows: usize = results.iter().map(|b| b.num_rows()).sum();
359+
assert_eq!(total_rows, 10);
360+
// Check that none of the batches have any columns
361+
for batch in &results {
362+
assert_eq!(batch.num_columns(), 0);
363+
}
364+
// Sanity check the output, should be just empty columns
365+
assert_snapshot!(batches_to_sort_string(&results), @r"
366+
++
367+
++
368+
++
369+
");
370+
Ok(())
371+
}

datafusion/sql/src/select.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -665,14 +665,6 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
665665
let mut prepared_select_exprs = vec![];
666666
let mut error_builder = DataFusionErrorBuilder::new();
667667

668-
// Handle the case where no projection is specified but we have a valid FROM clause
669-
// In this case, implicitly add a wildcard projection (SELECT *)
670-
let projection = if projection.is_empty() && !empty_from {
671-
vec![SelectItem::Wildcard(WildcardAdditionalOptions::default())]
672-
} else {
673-
projection
674-
};
675-
676668
for expr in projection {
677669
match self.sql_select_to_rex(expr, plan, empty_from, planner_context) {
678670
Ok(expr) => prepared_select_exprs.push(expr),

datafusion/sqllogictest/test_files/from-first.slt

Lines changed: 0 additions & 55 deletions
This file was deleted.

datafusion/sqllogictest/test_files/projection.slt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,31 @@ physical_plan
252252

253253
statement ok
254254
drop table t;
255+
256+
# Regression test for
257+
# https://github.com/apache/datafusion/issues/17513
258+
259+
query I
260+
COPY (select 1 as a, 2 as b)
261+
TO 'test_files/scratch/projection/17513.parquet'
262+
STORED AS PARQUET;
263+
----
264+
1
265+
266+
statement ok
267+
create external table t1 stored as parquet location 'test_files/scratch/projection/17513.parquet';
268+
269+
query TT
270+
explain format indent
271+
select from t1 where t1.a > 1;
272+
----
273+
logical_plan
274+
01)Projection:
275+
02)--Filter: t1.a > Int64(1)
276+
03)----TableScan: t1 projection=[a], partial_filters=[t1.a > Int64(1)]
277+
physical_plan
278+
01)ProjectionExec: expr=[]
279+
02)--CoalesceBatchesExec: target_batch_size=8192
280+
03)----FilterExec: a@0 > 1
281+
04)------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
282+
05)--------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/projection/17513.parquet]]}, projection=[a], file_type=parquet, predicate=a@0 > 1, pruning_predicate=a_null_count@1 != row_count@2 AND a_max@0 > 1, required_guarantees=[]

docs/source/user-guide/sql/select.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,6 @@ Example:
7575
SELECT t.a FROM table AS t
7676
```
7777

78-
The `FROM` clause can also come before the `SELECT` clause.
79-
Example:
80-
81-
```sql
82-
FROM table AS t
83-
SELECT t.a
84-
```
85-
86-
If the `SELECT` clause is omitted, the `FROM` clause will return all columns from the table.
87-
88-
```sql
89-
FROM table
90-
```
91-
9278
## WHERE clause
9379

9480
Example:

0 commit comments

Comments
 (0)