Skip to content

Commit

Permalink
[zetasql-toolkit] fix: ensure BigQueryCatalog.addAllTablesUsedInQuery…
Browse files Browse the repository at this point in the history
… ignores unqualified tables (GoogleCloudPlatform#1016)
  • Loading branch information
Pablo Paglilla authored Apr 21, 2023
1 parent 3f33a54 commit c950001
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
2 changes: 1 addition & 1 deletion tools/zetasql-helper/zetasql-toolkit-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.google.zetasql.toolkit</groupId>
<artifactId>zetasql-toolkit-core</artifactId>
<version>0.2.0</version>
<version>0.2.1</version>

<properties>
<zetasql.toolkit.version>${project.version}</zetasql.toolkit.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.google.zetasql.toolkit.catalog;

import com.google.zetasql.Analyzer;
import com.google.zetasql.AnalyzerOptions;
import com.google.zetasql.SimpleCatalog;
import com.google.zetasql.SimpleTable;
import com.google.zetasql.resolvedast.ResolvedCreateStatementEnums.CreateMode;
Expand All @@ -26,8 +24,6 @@
import com.google.zetasql.toolkit.catalog.bigquery.ProcedureInfo;
import com.google.zetasql.toolkit.catalog.bigquery.TVFInfo;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Interface for an object that wraps a ZetaSQL SimpleCatalog and allows adding resources to it by
Expand Down Expand Up @@ -209,24 +205,6 @@ default void addProcedure(String procedure) {
this.addProcedures(List.of(procedure));
}

/**
* Adds all the tables used in the provided query to this catalog.
*
* <p>Uses Analyzer.extractTableNamesFromScript to extract the table names and later uses
* this.addTables to add them.
*
* @param query The SQL query from which to get the tables that should be added to the catalog
* @param options The ZetaSQL AnalyzerOptions to use when extracting the table names from the
* query
*/
default void addAllTablesUsedInQuery(String query, AnalyzerOptions options) {
Set<String> tables =
Analyzer.extractTableNamesFromScript(query, options).stream()
.map(tablePath -> String.join(".", tablePath))
.collect(Collectors.toSet());
this.addTables(List.copyOf(tables));
}

/**
* Creates a copy of this CatalogWrapper.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,25 @@ public void addAllTablesInProject(String projectId) {
table, CreateMode.CREATE_OR_REPLACE, CreateScope.CREATE_DEFAULT_SCOPE));
}

/**
* Adds all the tables used in the provided query to this catalog.
*
* <p>Uses Analyzer.extractTableNamesFromScript to extract the table names and later uses
* this.addTables to add them.
*
* @param query The SQL query from which to get the tables that should be added to the catalog
* @param options The ZetaSQL AnalyzerOptions to use when extracting the table names from the
* query
*/
public void addAllTablesUsedInQuery(String query, AnalyzerOptions options) {
Set<String> tables =
Analyzer.extractTableNamesFromScript(query, options).stream()
.map(tablePath -> String.join(".", tablePath))
.filter(BigQueryReference::isQualified) // Remove non-qualified tables
.collect(Collectors.toSet());
this.addTables(List.copyOf(tables));
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.Spanner;
import com.google.zetasql.Analyzer;
import com.google.zetasql.AnalyzerOptions;
import com.google.zetasql.SimpleCatalog;
import com.google.zetasql.SimpleTable;
import com.google.zetasql.ZetaSQLBuiltinFunctionOptions;
Expand All @@ -32,6 +34,8 @@
import com.google.zetasql.toolkit.catalog.spanner.exceptions.InvalidSpannerTableName;
import com.google.zetasql.toolkit.options.SpannerLanguageOptions;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/** {@link CatalogWrapper} implementation that follows Cloud Spanner semantics */
public class SpannerCatalog implements CatalogWrapper {
Expand Down Expand Up @@ -225,6 +229,24 @@ public void addAllTablesInDatabase() {
table, CreateMode.CREATE_OR_REPLACE, CreateScope.CREATE_DEFAULT_SCOPE));
}

/**
* Adds all the tables used in the provided query to this catalog.
*
* <p>Uses Analyzer.extractTableNamesFromScript to extract the table names and later uses
* this.addTables to add them.
*
* @param query The SQL query from which to get the tables that should be added to the catalog
* @param options The ZetaSQL AnalyzerOptions to use when extracting the table names from the
* query
*/
public void addAllTablesUsedInQuery(String query, AnalyzerOptions options) {
Set<String> tables =
Analyzer.extractTableNamesFromScript(query, options).stream()
.map(tablePath -> String.join(".", tablePath))
.collect(Collectors.toSet());
this.addTables(List.copyOf(tables));
}

@Override
public void addFunctions(List<String> functions) {
throw new UnsupportedOperationException(
Expand Down
2 changes: 1 addition & 1 deletion tools/zetasql-helper/zetasql-toolkit-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<zetasql.toolkit.version>0.2.0</zetasql.toolkit.version>
<zetasql.toolkit.version>0.2.1</zetasql.toolkit.version>
<google.cloud.jib.version>3.3.1</google.cloud.jib.version>
<container.mainClass/>
</properties>
Expand Down

0 comments on commit c950001

Please sign in to comment.