This is an automated email from the ASF dual-hosted git repository.
akurmustafa pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 444f0c42c4 Fix Select Into with Order By and Limit (#6442)
444f0c42c4 is described below
commit 444f0c42c4f37103c51eb8e0e92ad140727ca16f
Author: Berkay Şahin <[email protected]>
AuthorDate: Thu May 25 18:01:41 2023 +0300
Fix Select Into with Order By and Limit (#6442)
* DDL LogicalPlan is built after order by and limit checks
* Test added for the cases with order by and limit
---
.../core/tests/sqllogictests/test_files/ddl.slt | 16 ++++++++++++
datafusion/sql/src/query.rs | 30 +++++++++++++++++++---
datafusion/sql/src/select.rs | 19 ++------------
3 files changed, 44 insertions(+), 21 deletions(-)
diff --git a/datafusion/core/tests/sqllogictests/test_files/ddl.slt
b/datafusion/core/tests/sqllogictests/test_files/ddl.slt
index d75d35e6f0..1cf67be3a2 100644
--- a/datafusion/core/tests/sqllogictests/test_files/ddl.slt
+++ b/datafusion/core/tests/sqllogictests/test_files/ddl.slt
@@ -345,6 +345,22 @@ SELECT * FROM new_table;
----
1 2 hello
+statement ok
+DROP TABLE new_table
+
+# create_table_with_schema_as_multiple_values
+statement ok
+CREATE TABLE test_table(c1 int, c2 float, c3 varchar) AS VALUES(1, 2,
'hello'),(2, 1, 'there'),(3, 0, '!');
+
+statement ok
+SELECT * INTO new_table FROM test_table ORDER BY c1 DESC LIMIT 2
+
+query IRT
+SELECT * FROM new_table
+----
+3 0 !
+2 1 there
+
statement ok
DROP TABLE my_table;
diff --git a/datafusion/sql/src/query.rs b/datafusion/sql/src/query.rs
index 27235a050d..2d7771d8c7 100644
--- a/datafusion/sql/src/query.rs
+++ b/datafusion/sql/src/query.rs
@@ -15,11 +15,17 @@
// specific language governing permissions and limitations
// under the License.
+use std::sync::Arc;
+
use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
use datafusion_common::{DataFusionError, Result, ScalarValue};
-use datafusion_expr::{Expr, LogicalPlan, LogicalPlanBuilder};
-use sqlparser::ast::{Expr as SQLExpr, Offset as SQLOffset, OrderByExpr, Query,
Value};
+use datafusion_expr::{
+ CreateMemoryTable, DdlStatement, Expr, LogicalPlan, LogicalPlanBuilder,
+};
+use sqlparser::ast::{
+ Expr as SQLExpr, Offset as SQLOffset, OrderByExpr, Query, SetExpr, Value,
+};
use sqlparser::parser::ParserError::ParserError;
@@ -71,9 +77,25 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
planner_context.insert_cte(cte_name, logical_plan);
}
}
- let plan = self.set_expr_to_plan(*set_expr, planner_context)?;
+ let plan = self.set_expr_to_plan(*(set_expr.clone()),
planner_context)?;
let plan = self.order_by(plan, query.order_by, planner_context)?;
- self.limit(plan, query.offset, query.limit)
+ let plan = self.limit(plan, query.offset, query.limit)?;
+
+ let plan = match *set_expr {
+ SetExpr::Select(select) if select.into.is_some() => {
+ let select_into = select.into.unwrap();
+
LogicalPlan::Ddl(DdlStatement::CreateMemoryTable(CreateMemoryTable {
+ name:
self.object_name_to_table_reference(select_into.name)?,
+ primary_key: Vec::new(),
+ input: Arc::new(plan),
+ if_not_exists: false,
+ or_replace: false,
+ }))
+ }
+ _ => plan,
+ };
+
+ Ok(plan)
}
/// Wrap a plan in a limit
diff --git a/datafusion/sql/src/select.rs b/datafusion/sql/src/select.rs
index 01bd740bf2..612a38d6f0 100644
--- a/datafusion/sql/src/select.rs
+++ b/datafusion/sql/src/select.rs
@@ -31,8 +31,7 @@ use datafusion_expr::utils::{
};
use datafusion_expr::Expr::Alias;
use datafusion_expr::{
- CreateMemoryTable, DdlStatement, Expr, Filter, GroupingSet, LogicalPlan,
- LogicalPlanBuilder, Partitioning,
+ Expr, Filter, GroupingSet, LogicalPlan, LogicalPlanBuilder, Partitioning,
};
use sqlparser::ast::{Distinct, Expr as SQLExpr, WildcardAdditionalOptions,
WindowType};
@@ -240,21 +239,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
plan
};
- if let Some(select_into) = select.into {
- Ok(LogicalPlan::Ddl(DdlStatement::CreateMemoryTable(
- CreateMemoryTable {
- name:
self.object_name_to_table_reference(select_into.name)?,
- // SELECT INTO statement does not copy constraints such as
primary key
- primary_key: Vec::new(),
- input: Arc::new(plan),
- // These are not applicable with SELECT INTO
- if_not_exists: false,
- or_replace: false,
- },
- )))
- } else {
- Ok(plan)
- }
+ Ok(plan)
}
fn plan_selection(