@@ -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 ;
@@ -1686,45 +1690,58 @@ impl SessionState {
1686
1690
// table providers for all relations referenced in this query
1687
1691
let mut relations = hashbrown:: HashSet :: with_capacity ( 10 ) ;
1688
1692
1689
- match statement {
1690
- DFStatement :: Statement ( s) => {
1691
- struct RelationVisitor < ' a > ( & ' a mut hashbrown:: HashSet < ObjectName > ) ;
1693
+ struct RelationVisitor < ' a > ( & ' a mut hashbrown:: HashSet < ObjectName > ) ;
1694
+
1695
+ impl < ' a > RelationVisitor < ' a > {
1696
+ /// Record that `relation` was used in this statement
1697
+ fn insert ( & mut self , relation : & ObjectName ) {
1698
+ self . 0 . get_or_insert_with ( relation, |_| relation. clone ( ) ) ;
1699
+ }
1700
+ }
1692
1701
1693
- impl < ' a > Visitor for RelationVisitor < ' a > {
1694
- type Break = ( ) ;
1702
+ impl < ' a > Visitor for RelationVisitor < ' a > {
1703
+ type Break = ( ) ;
1695
1704
1696
- fn pre_visit_relation (
1697
- & mut self ,
1698
- relation : & ObjectName ,
1699
- ) -> ControlFlow < ( ) > {
1700
- self . 0 . get_or_insert_with ( relation, |_| relation. clone ( ) ) ;
1701
- ControlFlow :: Continue ( ( ) )
1702
- }
1705
+ fn pre_visit_relation ( & mut self , relation : & ObjectName ) -> ControlFlow < ( ) > {
1706
+ self . insert ( relation) ;
1707
+ ControlFlow :: Continue ( ( ) )
1708
+ }
1703
1709
1704
- fn pre_visit_statement (
1705
- & mut self ,
1706
- statement : & Statement ,
1707
- ) -> ControlFlow < ( ) > {
1708
- if let Statement :: ShowCreate {
1709
- obj_type : ShowCreateObject :: Table | ShowCreateObject :: View ,
1710
- obj_name,
1711
- } = statement
1712
- {
1713
- self . 0 . get_or_insert_with ( obj_name, |_| obj_name. clone ( ) ) ;
1714
- }
1715
- ControlFlow :: Continue ( ( ) )
1716
- }
1710
+ fn pre_visit_statement ( & mut self , statement : & Statement ) -> ControlFlow < ( ) > {
1711
+ if let Statement :: ShowCreate {
1712
+ obj_type : ShowCreateObject :: Table | ShowCreateObject :: View ,
1713
+ obj_name,
1714
+ } = statement
1715
+ {
1716
+ self . insert ( obj_name)
1717
1717
}
1718
- let mut visitor = RelationVisitor ( & mut relations) ;
1718
+ ControlFlow :: Continue ( ( ) )
1719
+ }
1720
+ }
1721
+
1722
+ let mut visitor = RelationVisitor ( & mut relations) ;
1723
+ match statement {
1724
+ DFStatement :: Statement ( s) => {
1719
1725
let _ = s. as_ref ( ) . visit ( & mut visitor) ;
1720
1726
}
1721
1727
DFStatement :: CreateExternalTable ( table) => {
1722
- relations. insert ( ObjectName ( vec ! [ Ident :: from( table. name. as_str( ) ) ] ) ) ;
1723
- }
1724
- DFStatement :: DescribeTableStmt ( table) => {
1725
- relations
1726
- . get_or_insert_with ( & table. table_name , |_| table. table_name . clone ( ) ) ;
1728
+ visitor
1729
+ . 0
1730
+ . insert ( ObjectName ( vec ! [ Ident :: from( table. name. as_str( ) ) ] ) ) ;
1727
1731
}
1732
+ DFStatement :: DescribeTableStmt ( table) => visitor. insert ( & table. table_name ) ,
1733
+ DFStatement :: CopyTo ( CopyToStatement {
1734
+ source,
1735
+ target : _,
1736
+ options : _,
1737
+ } ) => match source {
1738
+ CopyToSource :: Relation ( table_name) => {
1739
+ visitor. insert ( table_name) ;
1740
+ }
1741
+ CopyToSource :: Query ( query) => {
1742
+ query. visit ( & mut visitor) ;
1743
+ }
1744
+ } ,
1728
1745
}
1729
1746
1730
1747
// Always include information_schema if available
0 commit comments