diff --git a/query/src/sql/planner/binder/project.rs b/query/src/sql/planner/binder/project.rs index 8727499cb8b85..da14e92e9325c 100644 --- a/query/src/sql/planner/binder/project.rs +++ b/query/src/sql/planner/binder/project.rs @@ -14,6 +14,7 @@ use common_ast::ast::Indirection; use common_ast::ast::SelectTarget; +use common_ast::ast::TableAlias; use common_exception::ErrorCode; use common_exception::Result; diff --git a/query/src/sql/planner/binder/select.rs b/query/src/sql/planner/binder/select.rs index ec51c3a75f576..a1292f4a2e7ec 100644 --- a/query/src/sql/planner/binder/select.rs +++ b/query/src/sql/planner/binder/select.rs @@ -21,6 +21,7 @@ use common_ast::ast::Query; use common_ast::ast::SelectStmt; use common_ast::ast::SelectTarget; use common_ast::ast::SetExpr; +use common_ast::ast::TableAlias; use common_ast::ast::TableReference; use common_datavalues::DataTypeImpl; use common_exception::ErrorCode; @@ -43,11 +44,20 @@ use crate::storages::ToReadDataSourcePlan; use crate::table_functions::TableFunction; impl Binder { - #[async_recursion] pub(super) async fn bind_query( &mut self, query: &Query, bind_context: &BindContext, + ) -> Result { + self.bind_query_with_alias(query, &None, bind_context).await + } + + #[async_recursion] + async fn bind_query_with_alias( + &mut self, + query: &Query, + alias: &Option, + bind_context: &BindContext, ) -> Result { let mut has_order_by = false; if !query.order_by.is_empty() { @@ -55,10 +65,10 @@ impl Binder { }; let mut bind_context = match &query.body { SetExpr::Select(stmt) => { - self.bind_select_stmt(stmt, has_order_by, bind_context) + self.bind_select_stmt(stmt, has_order_by, alias, bind_context) .await } - SetExpr::Query(stmt) => self.bind_query(stmt, bind_context).await, + SetExpr::Query(stmt) => self.bind_query_with_alias(stmt, alias, bind_context).await, _ => Err(ErrorCode::UnImplement("Unsupported query type")), }?; @@ -84,6 +94,7 @@ impl Binder { &mut self, stmt: &SelectStmt, has_order_by: bool, + _alias: &Option, bind_context: &BindContext, ) -> Result { let mut input_context = if let Some(from) = &stmt.from { @@ -221,7 +232,10 @@ impl Binder { Ok(result) } TableReference::Join(join) => self.bind_join(bind_context, join).await, - _ => Err(ErrorCode::UnImplement("Unsupported table reference type")), + TableReference::Subquery { subquery, alias } => { + self.bind_query_with_alias(subquery, alias, bind_context) + .await + } } }