-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add COPY .. TO ..
syntax support
#6355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5865162
Add `COPY .. TO ..` syntax support
alamb 23fd692
fix rustdocs
alamb 18c3f97
Apply suggestions from code review
alamb b3caedf
Merge remote-tracking branch 'apache/main' into alamb/copy_syntax
alamb 08e9c25
Update message
alamb 17c6960
Clarify error
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,11 @@ use crate::logical_expr::{ | |
LogicalPlanBuilder, SetVariable, TableSource, TableType, UNNAMED_TABLE, | ||
}; | ||
use crate::optimizer::OptimizerRule; | ||
use datafusion_sql::{planner::ParserOptions, ResolvedTableReference, TableReference}; | ||
use datafusion_sql::{ | ||
parser::{CopyToSource, CopyToStatement}, | ||
planner::ParserOptions, | ||
ResolvedTableReference, TableReference, | ||
}; | ||
|
||
use crate::physical_optimizer::coalesce_batches::CoalesceBatches; | ||
use crate::physical_optimizer::repartition::Repartition; | ||
|
@@ -1686,45 +1690,58 @@ impl SessionState { | |
// table providers for all relations referenced in this query | ||
let mut relations = hashbrown::HashSet::with_capacity(10); | ||
|
||
match statement { | ||
DFStatement::Statement(s) => { | ||
struct RelationVisitor<'a>(&'a mut hashbrown::HashSet<ObjectName>); | ||
struct RelationVisitor<'a>(&'a mut hashbrown::HashSet<ObjectName>); | ||
|
||
impl<'a> RelationVisitor<'a> { | ||
/// Record that `relation` was used in this statement | ||
fn insert(&mut self, relation: &ObjectName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice refactor |
||
self.0.get_or_insert_with(relation, |_| relation.clone()); | ||
} | ||
} | ||
|
||
impl<'a> Visitor for RelationVisitor<'a> { | ||
type Break = (); | ||
impl<'a> Visitor for RelationVisitor<'a> { | ||
type Break = (); | ||
|
||
fn pre_visit_relation( | ||
&mut self, | ||
relation: &ObjectName, | ||
) -> ControlFlow<()> { | ||
self.0.get_or_insert_with(relation, |_| relation.clone()); | ||
ControlFlow::Continue(()) | ||
} | ||
fn pre_visit_relation(&mut self, relation: &ObjectName) -> ControlFlow<()> { | ||
self.insert(relation); | ||
ControlFlow::Continue(()) | ||
} | ||
|
||
fn pre_visit_statement( | ||
&mut self, | ||
statement: &Statement, | ||
) -> ControlFlow<()> { | ||
if let Statement::ShowCreate { | ||
obj_type: ShowCreateObject::Table | ShowCreateObject::View, | ||
obj_name, | ||
} = statement | ||
{ | ||
self.0.get_or_insert_with(obj_name, |_| obj_name.clone()); | ||
} | ||
ControlFlow::Continue(()) | ||
} | ||
fn pre_visit_statement(&mut self, statement: &Statement) -> ControlFlow<()> { | ||
if let Statement::ShowCreate { | ||
obj_type: ShowCreateObject::Table | ShowCreateObject::View, | ||
obj_name, | ||
} = statement | ||
{ | ||
self.insert(obj_name) | ||
} | ||
let mut visitor = RelationVisitor(&mut relations); | ||
ControlFlow::Continue(()) | ||
} | ||
} | ||
|
||
let mut visitor = RelationVisitor(&mut relations); | ||
match statement { | ||
DFStatement::Statement(s) => { | ||
let _ = s.as_ref().visit(&mut visitor); | ||
} | ||
DFStatement::CreateExternalTable(table) => { | ||
relations.insert(ObjectName(vec![Ident::from(table.name.as_str())])); | ||
} | ||
DFStatement::DescribeTableStmt(table) => { | ||
relations | ||
.get_or_insert_with(&table.table_name, |_| table.table_name.clone()); | ||
visitor | ||
.0 | ||
.insert(ObjectName(vec![Ident::from(table.name.as_str())])); | ||
} | ||
DFStatement::DescribeTableStmt(table) => visitor.insert(&table.table_name), | ||
DFStatement::CopyTo(CopyToStatement { | ||
source, | ||
target: _, | ||
options: _, | ||
}) => match source { | ||
CopyToSource::Relation(table_name) => { | ||
visitor.insert(table_name); | ||
} | ||
CopyToSource::Query(query) => { | ||
query.visit(&mut visitor); | ||
} | ||
}, | ||
} | ||
|
||
// Always include information_schema if available | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# 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. | ||
|
||
# tests for copy command | ||
|
||
statement ok | ||
create table source_table(col1 integer, col2 varchar) as values (1, 'Foo'), (2, 'Bar'); | ||
|
||
# Copy from table | ||
statement error DataFusion error: This feature is not implemented: `COPY \.\. TO \.\.` statement is not yet supported | ||
COPY source_table to '/tmp/table.parquet'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what I really want -- it is so close |
||
|
||
# Copy from table with options | ||
statement error DataFusion error: This feature is not implemented: `COPY \.\. TO \.\.` statement is not yet supported | ||
COPY source_table to '/tmp/table.parquet' (row_group_size 55); | ||
|
||
# Copy from table with options (and trailing comma) | ||
statement error DataFusion error: This feature is not implemented: `COPY \.\. TO \.\.` statement is not yet supported | ||
COPY source_table to '/tmp/table.parquet' (row_group_size 55, row_group_limit_bytes 9,); | ||
|
||
|
||
# Error cases: | ||
|
||
# Incomplete statement | ||
statement error DataFusion error: SQL error: ParserError\("Expected \), found: EOF"\) | ||
COPY (select col2, sum(col1) from source_table | ||
|
||
# Copy from table with non literal | ||
statement error DataFusion error: SQL error: ParserError\("Expected ',' or '\)' after option definition, found: \+"\) | ||
COPY source_table to '/tmp/table.parquet' (row_group_size 55 + 102); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this simply pulls the
RelationVisitor
up a level so it can be used for bothDFStatement::DescribeTableStmt
andDFStatement::CopyTo