Skip to content

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 6 commits into from
May 19, 2023
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
81 changes: 49 additions & 32 deletions datafusion/core/src/execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> {
Copy link
Contributor Author

@alamb alamb May 15, 2023

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 both DFStatement::DescribeTableStmt and DFStatement::CopyTo

/// Record that `relation` was used in this statement
fn insert(&mut self, relation: &ObjectName) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down
44 changes: 44 additions & 0 deletions datafusion/core/tests/sqllogictests/test_files/copy.slt
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';
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
Loading