@@ -69,7 +69,11 @@ use crate::logical_expr::{
69
69
LogicalPlanBuilder , SetVariable , TableSource , TableType , UNNAMED_TABLE ,
70
70
} ;
71
71
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
+ } ;
73
77
74
78
use crate :: physical_optimizer:: coalesce_batches:: CoalesceBatches ;
75
79
use crate :: physical_optimizer:: repartition:: Repartition ;
@@ -1649,45 +1653,58 @@ impl SessionState {
1649
1653
// table providers for all relations referenced in this query
1650
1654
let mut relations = hashbrown:: HashSet :: with_capacity ( 10 ) ;
1651
1655
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
+ }
1655
1664
1656
- impl < ' a > Visitor for RelationVisitor < ' a > {
1657
- type Break = ( ) ;
1665
+ impl < ' a > Visitor for RelationVisitor < ' a > {
1666
+ type Break = ( ) ;
1658
1667
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
+ }
1666
1672
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)
1680
1680
}
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) => {
1682
1688
let _ = s. as_ref ( ) . visit ( & mut visitor) ;
1683
1689
}
1684
1690
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( ) ) ] ) ) ;
1690
1694
}
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
+ } ,
1691
1708
}
1692
1709
1693
1710
// Always include information_schema if available
0 commit comments