From c92d29a42e95c2f4946ed65f15b7fc3ec7364ec4 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Fri, 22 Aug 2025 22:17:25 +0200 Subject: [PATCH 1/3] Support from-first syntax I noticed in https://github.com/apache/datafusion/pull/17278 that `from` without an explicit projection did not work. This should fix it --- datafusion/sql/src/select.rs | 9 ++++ .../sqllogictest/test_files/from-first.slt | 41 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 datafusion/sqllogictest/test_files/from-first.slt diff --git a/datafusion/sql/src/select.rs b/datafusion/sql/src/select.rs index c7ccda7232e2a..1a90e5e09b778 100644 --- a/datafusion/sql/src/select.rs +++ b/datafusion/sql/src/select.rs @@ -664,6 +664,15 @@ impl SqlToRel<'_, S> { ) -> Result> { let mut prepared_select_exprs = vec![]; let mut error_builder = DataFusionErrorBuilder::new(); + + // Handle the case where no projection is specified but we have a valid FROM clause + // In this case, implicitly add a wildcard projection (SELECT *) + let projection = if projection.is_empty() && !empty_from { + vec![SelectItem::Wildcard(WildcardAdditionalOptions::default())] + } else { + projection + }; + for expr in projection { match self.sql_select_to_rex(expr, plan, empty_from, planner_context) { Ok(expr) => prepared_select_exprs.push(expr), diff --git a/datafusion/sqllogictest/test_files/from-first.slt b/datafusion/sqllogictest/test_files/from-first.slt new file mode 100644 index 0000000000000..e35029b7768dc --- /dev/null +++ b/datafusion/sqllogictest/test_files/from-first.slt @@ -0,0 +1,41 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +query I +FROM range(2) +---- +0 +1 + +query I +FROM range(2) +SELECT * +---- +0 +1 + +query I +FROM (SELECT * FROM range(2)) +---- +0 +1 + +query I +FROM (FROM range(2)) +---- +0 +1 From fe284b6f655deccf040cc4a65387032f83de6f6e Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sat, 23 Aug 2025 13:47:04 +0200 Subject: [PATCH 2/3] add more tests --- datafusion/sqllogictest/test_files/from-first.slt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/datafusion/sqllogictest/test_files/from-first.slt b/datafusion/sqllogictest/test_files/from-first.slt index e35029b7768dc..c4a305e85ea77 100644 --- a/datafusion/sqllogictest/test_files/from-first.slt +++ b/datafusion/sqllogictest/test_files/from-first.slt @@ -39,3 +39,17 @@ FROM (FROM range(2)) ---- 0 1 + +query I +FROM range(2) +SELECT 1 +---- +1 +1 + +query I +FROM range(2) as r +SELECT r.value +---- +0 +1 From 9f1b436a543dc1c34ed016e54678218ac9e73228 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sat, 23 Aug 2025 13:51:58 +0200 Subject: [PATCH 3/3] document in user guide --- docs/source/user-guide/sql/select.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/source/user-guide/sql/select.md b/docs/source/user-guide/sql/select.md index 39163cf492a4a..eb8bca7a75ef0 100644 --- a/docs/source/user-guide/sql/select.md +++ b/docs/source/user-guide/sql/select.md @@ -75,6 +75,20 @@ Example: SELECT t.a FROM table AS t ``` +The `FROM` clause can also come before the `SELECT` clause. +Example: + +```sql +FROM table AS t +SELECT t.a +``` + +If the `SELECT` clause is omitted, the `FROM` clause will return all columns from the table. + +```sql +FROM table +``` + ## WHERE clause Example: