diff --git a/alphasql/alphacheck.cc b/alphasql/alphacheck.cc index da7dab75..a86117ea 100644 --- a/alphasql/alphacheck.cc +++ b/alphasql/alphacheck.cc @@ -143,7 +143,6 @@ absl::Status Run(const std::string& sql_file_path, const AnalyzerOptions& option auto* create_table_stmt = resolved_statement->GetAs(); std::cout << "DDL analyzed, adding table to catalog..." << std::endl; std::string table_name = absl::StrJoin(create_table_stmt->name_path(), "."); - // TODO(Matts966): raise error for duplicated table_names. std::unique_ptr table(new zetasql::SimpleTable(table_name)); for (const auto& column_definition : create_table_stmt->column_definition_list()) { std::unique_ptr column(new SimpleColumn(table_name, column_definition->column().name_id().ToString(), diff --git a/alphasql/identifier_resolver.cc b/alphasql/identifier_resolver.cc index 3e90ecf7..f6a4a2ba 100644 --- a/alphasql/identifier_resolver.cc +++ b/alphasql/identifier_resolver.cc @@ -81,7 +81,18 @@ zetasql_base::StatusOr GetIdentifierInformation(const std::stri // &resolver.identifier_information.table_information.referenced)); table_name_resolver::GetTables(sql_file_path, options, &resolver.identifier_information.table_information.referenced); - // TODO: Filter temporary tables from referenced tables. + + // Filter temporary tables from referenced tables because they are local. + for (const auto& temporary_table : resolver.temporary_tables) { + auto referenced_it = resolver.identifier_information.table_information.referenced.begin(); + while (referenced_it != resolver.identifier_information.table_information.referenced.end()) { + if (absl::StrJoin(*referenced_it, ".") == temporary_table) { + referenced_it = resolver.identifier_information.table_information.referenced.erase(referenced_it); + } else { + ++referenced_it; + } + } + } return resolver.identifier_information; } @@ -94,10 +105,16 @@ void IdentifierResolver::visitASTDropStatement(const ASTDropStatement* node, voi } void IdentifierResolver::visitASTCreateTableStatement(const ASTCreateTableStatement* node, - void* data) { - if (node->scope() != ASTCreateStatement::TEMPORARY) { - identifier_information.table_information.created.insert(node->name()->ToIdentifierVector()); + void* data) { + const auto& name_vector = node->name()->ToIdentifierVector(); + if (node->scope() == ASTCreateStatement::TEMPORARY) { + const std::string path_str = absl::StrJoin(name_vector, "."); + temporary_tables.insert(path_str); + visitASTChildren(node, data); + return; } + + identifier_information.table_information.created.insert(name_vector); visitASTChildren(node, data); } @@ -112,6 +129,7 @@ void IdentifierResolver::visitASTInsertStatement(const ASTInsertStatement* node, if (!status_or_path.ok()) { std::cout << "Path expression can't be extracted" << std::endl; std::cout << status_or_path.status() << std::endl; + visitASTChildren(node, data); return; } const auto path_expr = status_or_path.value(); @@ -132,6 +150,7 @@ void IdentifierResolver::visitASTUpdateStatement(const ASTUpdateStatement* node, if (!status_or_path.ok()) { std::cout << "Path expression can't be extracted!" << std::endl; std::cout << status_or_path.status() << std::endl; + visitASTChildren(node, data); return; } const auto path_expr = status_or_path.value(); diff --git a/alphasql/identifier_resolver.h b/alphasql/identifier_resolver.h index 09d7de84..6ace6db4 100644 --- a/alphasql/identifier_resolver.h +++ b/alphasql/identifier_resolver.h @@ -63,6 +63,7 @@ class IdentifierResolver : public DefaultParseTreeVisitor { ~IdentifierResolver() override {} identifier_info identifier_information; + std::set temporary_tables; void defaultVisit(const ASTNode* node, void* data) override { visitASTChildren(node, data); diff --git a/samples/create-temp-table-test/external_tables.txt b/samples/create-temp-table-test/external_tables.txt index a9a5aecf..e69de29b 100644 --- a/samples/create-temp-table-test/external_tables.txt +++ b/samples/create-temp-table-test/external_tables.txt @@ -1 +0,0 @@ -tmp diff --git a/samples/sample-ci/alphacheck_stdout.txt b/samples/sample-ci/alphacheck_stdout.txt index fc56779a..174dc736 100644 --- a/samples/sample-ci/alphacheck_stdout.txt +++ b/samples/sample-ci/alphacheck_stdout.txt @@ -1,6 +1,6 @@ Analyzing "samples/sample-ci/sample/create_datawarehouse3.sql" ERROR: INVALID_ARGUMENT: Table not found: `bigquery-public-data.samples.gsod` [at samples/sample-ci/sample/create_datawarehouse3.sql:5:3] catalog: + dataset.main tablename2 tablename1 - dataset.main