Skip to content

Commit 0f91ba9

Browse files
committed
clean up syntax
1 parent cb2cee2 commit 0f91ba9

File tree

2 files changed

+62
-85
lines changed

2 files changed

+62
-85
lines changed

datafusion/core/src/execution/context.rs

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ use crate::logical_expr::{
6969
LogicalPlanBuilder, SetVariable, TableSource, TableType, UNNAMED_TABLE,
7070
};
7171
use crate::optimizer::OptimizerRule;
72-
use datafusion_sql::{planner::ParserOptions, ResolvedTableReference, TableReference};
72+
use datafusion_sql::{
73+
parser::{CopyToSource, CopyToStatement},
74+
planner::ParserOptions,
75+
ResolvedTableReference, TableReference,
76+
};
7377

7478
use crate::physical_optimizer::coalesce_batches::CoalesceBatches;
7579
use crate::physical_optimizer::repartition::Repartition;
@@ -1649,45 +1653,58 @@ impl SessionState {
16491653
// table providers for all relations referenced in this query
16501654
let mut relations = hashbrown::HashSet::with_capacity(10);
16511655

1652-
match statement {
1653-
DFStatement::Statement(s) => {
1654-
struct RelationVisitor<'a>(&'a mut hashbrown::HashSet<ObjectName>);
1656+
struct RelationVisitor<'a>(&'a mut hashbrown::HashSet<ObjectName>);
1657+
1658+
impl<'a> RelationVisitor<'a> {
1659+
/// Record that `relation` was used in this statement
1660+
fn insert(&mut self, relation: &ObjectName) {
1661+
self.0.get_or_insert_with(relation, |_| relation.clone());
1662+
}
1663+
}
16551664

1656-
impl<'a> Visitor for RelationVisitor<'a> {
1657-
type Break = ();
1665+
impl<'a> Visitor for RelationVisitor<'a> {
1666+
type Break = ();
16581667

1659-
fn pre_visit_relation(
1660-
&mut self,
1661-
relation: &ObjectName,
1662-
) -> ControlFlow<()> {
1663-
self.0.get_or_insert_with(relation, |_| relation.clone());
1664-
ControlFlow::Continue(())
1665-
}
1668+
fn pre_visit_relation(&mut self, relation: &ObjectName) -> ControlFlow<()> {
1669+
self.insert(relation);
1670+
ControlFlow::Continue(())
1671+
}
16661672

1667-
fn pre_visit_statement(
1668-
&mut self,
1669-
statement: &Statement,
1670-
) -> ControlFlow<()> {
1671-
if let Statement::ShowCreate {
1672-
obj_type: ShowCreateObject::Table | ShowCreateObject::View,
1673-
obj_name,
1674-
} = statement
1675-
{
1676-
self.0.get_or_insert_with(obj_name, |_| obj_name.clone());
1677-
}
1678-
ControlFlow::Continue(())
1679-
}
1673+
fn pre_visit_statement(&mut self, statement: &Statement) -> ControlFlow<()> {
1674+
if let Statement::ShowCreate {
1675+
obj_type: ShowCreateObject::Table | ShowCreateObject::View,
1676+
obj_name,
1677+
} = statement
1678+
{
1679+
self.insert(obj_name)
16801680
}
1681-
let mut visitor = RelationVisitor(&mut relations);
1681+
ControlFlow::Continue(())
1682+
}
1683+
}
1684+
1685+
let mut visitor = RelationVisitor(&mut relations);
1686+
match statement {
1687+
DFStatement::Statement(s) => {
16821688
let _ = s.as_ref().visit(&mut visitor);
16831689
}
16841690
DFStatement::CreateExternalTable(table) => {
1685-
relations.insert(ObjectName(vec![Ident::from(table.name.as_str())]));
1686-
}
1687-
DFStatement::DescribeTableStmt(table) => {
1688-
relations
1689-
.get_or_insert_with(&table.table_name, |_| table.table_name.clone());
1691+
visitor
1692+
.0
1693+
.insert(ObjectName(vec![Ident::from(table.name.as_str())]));
16901694
}
1695+
DFStatement::DescribeTableStmt(table) => visitor.insert(&table.table_name),
1696+
DFStatement::CopyTo(CopyToStatement {
1697+
source,
1698+
target: _,
1699+
options: _,
1700+
}) => match source {
1701+
CopyToSource::Relation(table_name) => {
1702+
visitor.insert(table_name);
1703+
}
1704+
CopyToSource::Query(query) => {
1705+
query.visit(&mut visitor);
1706+
}
1707+
},
16911708
}
16921709

16931710
// Always include information_schema if available

datafusion/core/tests/sqllogictests/test_files/copy.slt

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,61 +20,21 @@
2020
statement ok
2121
create table source_table(col1 integer, col2 varchar) as values (1, 'Foo'), (2, 'Bar');
2222

23-
# create an external parquet file
24-
# TODO figure out how to use a real tempfile name
25-
#statement ok
26-
#COPY source_table to '/tmp/table.parquet';
23+
# Copy from table
24+
statement error DataFusion error: This feature is not implemented: `COPY \.\. TO \.\.` statement is yet supported
25+
COPY source_table to '/tmp/table.parquet';
2726

27+
# Copy from table with options
28+
statement error DataFusion error: This feature is not implemented: `COPY \.\. TO \.\.` statement is yet supported
29+
COPY source_table to '/tmp/table.parquet' (row_group_size 55);
2830

29-
# create an external parquet file from a query
30-
# TODO figure out how to use a real tempfile name
31-
statement ok
32-
COPY (select col2, sum(col1) from source_table group by col2 order by col2) to '/tmp/table.parquet';
33-
34-
# Read the data back in
35-
statement ok
36-
CREATE external table new_table STORED as PARQUET LOCATION '/tmp/table.parquet';
37-
38-
# Read the data back in
39-
query IT
40-
select * from new_table;
41-
----
42-
1 Foo
43-
2 Bar
44-
45-
statement ok
46-
drop table new_table;
47-
48-
49-
# TODO error cases:
50-
# incomplete statement
51-
# pass non literal to COPY options
52-
53-
54-
55-
56-
57-
# TODO add support for reading directly for files without needing to create an external table
58-
# (TODO find ticket)
5931

32+
# Error cases:
6033

34+
# Incomplete statement
35+
statement error DataFusion error: SQL error: ParserError\("Expected \), found: EOF"\)
36+
COPY (select col2, sum(col1) from source_table
6137

62-
# TODO test copying to partitioned target (directory)
63-
64-
65-
# TODO test copying with options
66-
67-
68-
69-
# TODO test query source
70-
71-
72-
73-
# TODO: support CSV, JSON and AVRO
74-
75-
# TODO: document different options
76-
77-
78-
79-
statement ok
80-
drop table source_table;
38+
# Copy from table with non literal
39+
statement error DataFusion error: SQL error: ParserError\("Expected ',' or '\)' after option definition, found: \+"\)
40+
COPY source_table to '/tmp/table.parquet' (row_group_size 55 + 102);

0 commit comments

Comments
 (0)