Skip to content

Commit

Permalink
Merge pull request #57 from Matts966/feature/56-filter-temp-tables-fr…
Browse files Browse the repository at this point in the history
…om-reference

Filter temporary tables from referenced tables list because they are local
  • Loading branch information
Matts966 authored Nov 28, 2020
2 parents 0ca36f4 + d9bf222 commit 87dd474
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
1 change: 0 additions & 1 deletion alphasql/alphacheck.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ absl::Status Run(const std::string& sql_file_path, const AnalyzerOptions& option
auto* create_table_stmt = resolved_statement->GetAs<ResolvedCreateTableStmt>();
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<zetasql::SimpleTable> table(new zetasql::SimpleTable(table_name));
for (const auto& column_definition : create_table_stmt->column_definition_list()) {
std::unique_ptr<zetasql::SimpleColumn> column(new SimpleColumn(table_name, column_definition->column().name_id().ToString(),
Expand Down
27 changes: 23 additions & 4 deletions alphasql/identifier_resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,18 @@ zetasql_base::StatusOr<identifier_info> 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;
}
Expand All @@ -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);
}

Expand All @@ -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();
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions alphasql/identifier_resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class IdentifierResolver : public DefaultParseTreeVisitor {
~IdentifierResolver() override {}

identifier_info identifier_information;
std::set<std::string> temporary_tables;

void defaultVisit(const ASTNode* node, void* data) override {
visitASTChildren(node, data);
Expand Down
1 change: 0 additions & 1 deletion samples/create-temp-table-test/external_tables.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
tmp
2 changes: 1 addition & 1 deletion samples/sample-ci/alphacheck_stdout.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 87dd474

Please sign in to comment.