reading from view
use std::sync::Arc;
+use common_ast::parser::Parser;
use common_exception::ErrorCode;
use common_exception::Result;
use sqlparser::ast::FunctionArg;
@@ -24,9 +25,12 @@ use sqlparser::ast::Query;
use sqlparser::ast::TableAlias;
use sqlparser::ast::TableFactor;
use sqlparser::ast::TableWithJoins;
+use sqlparser::dialect::GenericDialect;
use crate::catalogs::Catalog;
use crate::sessions::QueryContext;
+use crate::sql::DfParser;
+use crate::sql::DfStatement;
use crate::sql::statements::analyzer_expr::ExpressionAnalyzer;
use crate::sql::statements::query::query_schema_joined::JoinedSchema;
use crate::sql::statements::AnalyzableStatement;
@@ -98,6 +102,28 @@ impl JoinedSchemaAnalyzer {
let (database, table) = self.resolve_table(&item.name)?;
let read_table = self.ctx.get_table(&database, &table).await?;
+ let tbl_info = read_table.get_table_info();
+ // TODO(veeupup) make view use subquery logic
+ if let Some(view) = &tbl_info.meta.view { // view, not table
+ // parse and make it subquery
+ let sql = view.subquery.clone();
+ let (statements, _) = DfParser::parse_sql(&sql)?;
+ if statements.len() == 1 {
+ if let DfStatement::Query(subquery) = &statements[0] {
+ match subquery.analyze(self.ctx.clone()).await? {
+ AnalyzedResult::SelectQuery(state) => {
+ let viewname = tbl_info.name.clone();
+ let alias = vec![viewname];
+ return JoinedSchema::from_subquery(state, alias);
+ },
+ _ => {}
+ }
+ }
+ }
+ return Err(ErrorCode::LogicalError(
+ "Logical error, subquery analyzed data must be SelectQuery, it's a bug.",
+ ));
+ }else {
match &item.alias {
None => {
let name_prefix = vec![database, table];
@@ -109,6 +135,7 @@ impl JoinedSchemaAnalyzer {
}
}
}
+ }
async fn table_function(&self, item: &TableFunctionRPNItem) -> Result<JoinedSchema> {
if item.name.0.len() >= 2 {
