@@ -5,7 +5,6 @@ use datafusion::arrow::datatypes::SchemaRef;
55use datafusion:: error:: Result ;
66use datafusion:: logical_expr:: TableSource ;
77use datafusion:: logical_expr:: TableType ;
8- use datafusion:: sql:: sqlparser:: ast:: TableFunctionArgs ;
98use datafusion:: sql:: TableReference ;
109use std:: any:: Any ;
1110use std:: sync:: Arc ;
@@ -15,16 +14,22 @@ use super::executor::LogicalOptimizer;
1514use super :: AstAnalyzer ;
1615use super :: RemoteTableRef ;
1716
17+ /// Trait to represent a SQL remote table inside [`SQLTableSource`].
18+ /// A remote table provides information such as schema, table reference, and
19+ /// provides hooks for rewriting the logical plan and AST before execution.
20+ /// This crate provides [`RemoteTable`] as a default ready-to-use type.
1821pub trait SQLTable : std:: fmt:: Debug + Send + Sync {
1922 /// Returns a reference as a trait object.
2023 fn as_any ( & self ) -> & dyn Any ;
21- /// Table reference for the table, which can be used to identify the table in the SQL query.
22- /// This method should return a unique identifier for the table.
23- /// Used for registering the table to sql schema provider.
24+ /// Provides the [`TableReference`](`datafusion::sql::TableReference`) used to identify the table in SQL queries.
25+ /// This TableReference is used for registering the table with the [`SQLSchemaProvider`](`super::SQLSchemaProvider`).
26+ /// If the table provider is registered in the Datafusion context under a different name,
27+ /// the logical plan will be rewritten to use this table reference during execution.
28+ /// Therefore, any AST analyzer should match against this table reference.
2429 fn table_reference ( & self ) -> TableReference ;
2530 /// Schema of the remote table
2631 fn schema ( & self ) -> SchemaRef ;
27- /// Returns an logical optimizer specific to this table, will be used to modify the logical plan before execution
32+ /// Returns a logical optimizer specific to this table, will be used to modify the logical plan before execution
2833 fn logical_optimizer ( & self ) -> Option < LogicalOptimizer > {
2934 None
3035 }
@@ -34,20 +39,33 @@ pub trait SQLTable: std::fmt::Debug + Send + Sync {
3439 }
3540}
3641
37- #[ derive( Debug ) ]
42+ /// Represents a remote table with a reference and schema.
43+ #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
3844pub struct RemoteTable {
3945 table_ref : RemoteTableRef ,
4046 schema : SchemaRef ,
4147}
4248
4349impl RemoteTable {
50+ /// Creates a new `RemoteTable` instance.
51+ ///
52+ /// Examples:
53+ /// ```rust
54+ /// RemoteTable::new("myschema.table", schema);
55+ /// RemoteTable::new(r#"myschema."Table""#, schema);
56+ /// RemoteTable::new("myschema.view('obj')", schema);
57+ /// RemoteTable::new("myschema.view(name => 'obj')", schema);
58+ /// RemoteTable::new("myschema.view(name = 'obj')", schema);
59+ /// ```
4460 pub fn new ( table_ref : impl Into < RemoteTableRef > , schema : SchemaRef ) -> Self {
4561 Self {
4662 table_ref : table_ref. into ( ) ,
4763 schema,
4864 }
4965 }
5066
67+ /// Return table reference of this remote table.
68+ /// Only returns the object name, ignoring functional params if any
5169 pub fn table_reference ( & self ) -> TableReference {
5270 TableReference :: from ( & self . table_ref )
5371 }
@@ -70,17 +88,12 @@ impl SQLTable for RemoteTable {
7088 None
7189 }
7290
91+ /// Returns ast analyzer that modifies table that contains functional args after table ident
7392 fn ast_analyzer ( & self ) -> Option < AstAnalyzer > {
7493 if let RemoteTableRef :: TableReferenceWithArgs ( name, args) = & self . table_ref {
7594 Some (
7695 ast_analyzer:: TableArgReplace :: default ( )
77- . with (
78- name. clone ( ) ,
79- TableFunctionArgs {
80- args : args. iter ( ) . cloned ( ) . collect ( ) ,
81- settings : None ,
82- } ,
83- )
96+ . with ( name. clone ( ) , args. iter ( ) . cloned ( ) . collect ( ) )
8497 . into_analyzer ( ) ,
8598 )
8699 } else {
@@ -107,6 +120,7 @@ impl SQLTableSource {
107120 Ok ( Self :: new_with_schema ( provider, remote_table_ref, schema) )
108121 }
109122
123+ /// Create a SQLTableSource with a table reference and schema
110124 pub fn new_with_schema (
111125 provider : Arc < SQLFederationProvider > ,
112126 table_ref : impl Into < RemoteTableRef > ,
@@ -118,10 +132,12 @@ impl SQLTableSource {
118132 }
119133 }
120134
135+ /// Create new with a custom SQLtable instance.
121136 pub fn new_with_table ( provider : Arc < SQLFederationProvider > , table : Arc < dyn SQLTable > ) -> Self {
122137 Self { provider, table }
123138 }
124139
140+ /// Return associated table reference of stored remote table
125141 pub fn table_reference ( & self ) -> TableReference {
126142 self . table . table_reference ( )
127143 }
0 commit comments