-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add parent project ID test to BigQuery #24912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ebyhr
merged 1 commit into
trinodb:master
from
SemionPar:semionpar/bigquery-parent-project-id-test
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...in/trino-bigquery/src/test/java/io/trino/plugin/bigquery/TestBigQueryParentProjectId.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.plugin.bigquery; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import io.trino.testing.AbstractTestQueryFramework; | ||
| import io.trino.testing.MaterializedRow; | ||
| import io.trino.testing.QueryRunner; | ||
| import io.trino.testing.sql.TestTable; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static io.trino.testing.MaterializedResult.DEFAULT_PRECISION; | ||
| import static io.trino.testing.TestingNames.randomNameSuffix; | ||
| import static io.trino.testing.TestingProperties.requiredNonEmptySystemProperty; | ||
| import static io.trino.tpch.TpchTable.NATION; | ||
| import static java.lang.String.format; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class TestBigQueryParentProjectId | ||
| extends AbstractTestQueryFramework | ||
| { | ||
| private final String projectId; | ||
| private final String parentProjectId; | ||
|
|
||
| TestBigQueryParentProjectId() | ||
| { | ||
| projectId = requiredNonEmptySystemProperty("testing.bigquery-project-id"); | ||
| parentProjectId = requiredNonEmptySystemProperty("testing.bigquery-parent-project-id"); | ||
| } | ||
|
|
||
| @Override | ||
| protected QueryRunner createQueryRunner() | ||
| throws Exception | ||
| { | ||
| return BigQueryQueryRunner.builder() | ||
| .setConnectorProperties(ImmutableMap.<String, String>builder() | ||
| .put("bigquery.parent-project-id", parentProjectId) | ||
| .buildOrThrow()) | ||
| .setInitialTables(ImmutableList.of(NATION)) | ||
| .build(); | ||
| } | ||
|
|
||
| @Test | ||
| void testQueriesWithParentProjectId() | ||
| throws Exception | ||
| { | ||
| // tpch schema is available in both projects | ||
| assertThat(computeScalar("SELECT name FROM bigquery.tpch.nation WHERE nationkey = 0")).isEqualTo("ALGERIA"); | ||
| assertThat(computeScalar("SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT name FROM tpch.nation WHERE nationkey = 0'))")).isEqualTo("ALGERIA"); | ||
| assertThat(computeScalar(format("SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT name FROM %s.tpch.nation WHERE nationkey = 0'))", projectId))) | ||
| .isEqualTo("ALGERIA"); | ||
| assertThat(computeScalar(format("SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT name FROM %s.tpch.nation WHERE nationkey = 0'))", parentProjectId))) | ||
| .isEqualTo("ALGERIA"); | ||
|
|
||
| String trinoSchema = "someschema_" + randomNameSuffix(); | ||
| try (AutoCloseable ignored = withSchema(trinoSchema); TestTable table = newTrinoTable("%s.table".formatted(trinoSchema), "(col1 INT)")) { | ||
| String tableName = table.getName().split("\\.")[1]; | ||
| // schema created in parentProjectId by default | ||
| assertThat(computeActual(format( | ||
| "SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT schema_name FROM `%s.region-us.INFORMATION_SCHEMA.SCHEMATA`'))", | ||
| projectId))) | ||
| .doesNotContain(row(trinoSchema)); | ||
| // If Parent project ID is not provided, PTF calls to unprefixed datasets go to credentials JSON default project ID. | ||
| // In this configuration, credentials JSON project ID is equal to "testing.bigquery-project-id" | ||
| assertThat(computeActual("SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT schema_name FROM INFORMATION_SCHEMA.SCHEMATA'))")) | ||
| .contains(row(trinoSchema)); | ||
| assertThat(computeActual(format( | ||
| "SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT schema_name FROM `%s.region-us.INFORMATION_SCHEMA.SCHEMATA`'))", | ||
| parentProjectId))) | ||
| .contains(row(trinoSchema)); | ||
| // table created in parentProjectId by default | ||
| assertThat(computeActual("SHOW TABLES FROM " + trinoSchema).getOnlyColumn()).contains(tableName); | ||
| assertThat(computeActual(format( | ||
| "SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT table_name FROM `%s.region-us.INFORMATION_SCHEMA.TABLES` WHERE table_schema = \"%s\"'))", | ||
| projectId, | ||
| trinoSchema))) | ||
| .doesNotContain(row(tableName)); | ||
| assertThat(query(format( | ||
| "SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = \"%s\"'))", | ||
| trinoSchema))) | ||
| .failure() | ||
| .hasMessageContaining("Table \"INFORMATION_SCHEMA.TABLES\" must be qualified with a dataset (e.g. dataset.table)"); | ||
| assertThat(computeActual(format( | ||
|
SemionPar marked this conversation as resolved.
Outdated
|
||
| "SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT table_name FROM `%s.region-us.INFORMATION_SCHEMA.TABLES` WHERE table_schema = \"%s\"'))", | ||
| parentProjectId, | ||
| trinoSchema))) | ||
| .contains(row(tableName)); | ||
| assertThat(query("SELECT * FROM " + table.getName())).returnsEmptyResult(); | ||
| assertThat(query("SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT * FROM `%s.%s`'))".formatted(parentProjectId, table.getName()))).returnsEmptyResult(); | ||
| assertThat(query("SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT * FROM `%s.%s`'))".formatted(projectId, table.getName()))) | ||
| .failure() | ||
| .hasMessageContaining("Failed to get destination table for query"); | ||
| } | ||
| } | ||
|
|
||
| private AutoCloseable withSchema(String schemaName) | ||
| { | ||
| QueryRunner queryRunner = getQueryRunner(); | ||
| queryRunner.execute("DROP SCHEMA IF EXISTS " + schemaName); | ||
| queryRunner.execute("CREATE SCHEMA " + schemaName); | ||
| return () -> queryRunner.execute("DROP SCHEMA IF EXISTS " + schemaName); | ||
| } | ||
|
|
||
| private static MaterializedRow row(String value) | ||
| { | ||
| return new MaterializedRow(DEFAULT_PRECISION, value); | ||
| } | ||
| } | ||
122 changes: 122 additions & 0 deletions
122
...no-bigquery/src/test/java/io/trino/plugin/bigquery/TestBigQueryWithBothProjectIdsSet.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.plugin.bigquery; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import io.trino.testing.AbstractTestQueryFramework; | ||
| import io.trino.testing.MaterializedRow; | ||
| import io.trino.testing.QueryRunner; | ||
| import io.trino.testing.sql.TestTable; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static io.trino.testing.MaterializedResult.DEFAULT_PRECISION; | ||
| import static io.trino.testing.TestingNames.randomNameSuffix; | ||
| import static io.trino.testing.TestingProperties.requiredNonEmptySystemProperty; | ||
| import static io.trino.tpch.TpchTable.NATION; | ||
| import static java.lang.String.format; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class TestBigQueryWithBothProjectIdsSet | ||
| extends AbstractTestQueryFramework | ||
| { | ||
| private final String projectId; | ||
| private final String parentProjectId; | ||
|
|
||
| TestBigQueryWithBothProjectIdsSet() | ||
| { | ||
| projectId = requiredNonEmptySystemProperty("testing.bigquery-project-id"); | ||
| parentProjectId = requiredNonEmptySystemProperty("testing.bigquery-parent-project-id"); | ||
| } | ||
|
|
||
| @Override | ||
| protected QueryRunner createQueryRunner() | ||
| throws Exception | ||
| { | ||
| return BigQueryQueryRunner.builder() | ||
| .setConnectorProperties(ImmutableMap.<String, String>builder() | ||
| .put("bigquery.project-id", projectId) | ||
| .put("bigquery.parent-project-id", parentProjectId) | ||
| .buildOrThrow()) | ||
| .setInitialTables(ImmutableList.of(NATION)) | ||
| .build(); | ||
| } | ||
|
|
||
| @Test | ||
| void testQueriesWithBothProjectIdAndParentProjectId() | ||
| throws Exception | ||
| { | ||
| // tpch schema is available in both projects | ||
| assertThat(computeScalar("SELECT name FROM bigquery.tpch.nation WHERE nationkey = 0")).isEqualTo("ALGERIA"); | ||
| assertThat(computeScalar("SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT name FROM tpch.nation WHERE nationkey = 0'))")).isEqualTo("ALGERIA"); | ||
| assertThat(computeScalar(format("SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT name FROM %s.tpch.nation WHERE nationkey = 0'))", projectId))) | ||
| .isEqualTo("ALGERIA"); | ||
| assertThat(computeScalar(format("SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT name FROM %s.tpch.nation WHERE nationkey = 0'))", parentProjectId))) | ||
| .isEqualTo("ALGERIA"); | ||
|
|
||
| String trinoSchema = "someschema_" + randomNameSuffix(); | ||
| try (AutoCloseable ignored = withSchema(trinoSchema); TestTable table = newTrinoTable("%s.table".formatted(trinoSchema), "(col1 INT)")) { | ||
| String tableName = table.getName().split("\\.")[1]; | ||
| // schema created in projectId is present in projectId and NOT present in parentProjectId | ||
| assertThat(computeActual(format( | ||
| "SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT schema_name FROM `%s.region-us.INFORMATION_SCHEMA.SCHEMATA`'))", | ||
| projectId))) | ||
| .contains(row(trinoSchema)); | ||
| // confusion point: this implicitly points to Parent project! | ||
| // If Parent project ID is provided, then it is set as default credentials project ID overriding the value in the JSON. | ||
| // PTF calls to unprefixed datasets go to credentials default project ID. | ||
| assertThat(computeActual("SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT schema_name FROM INFORMATION_SCHEMA.SCHEMATA'))")) | ||
| .doesNotContain(row(trinoSchema)); | ||
| assertThat(computeActual(format( | ||
| "SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT schema_name FROM `%s.region-us.INFORMATION_SCHEMA.SCHEMATA`'))", | ||
| parentProjectId))) | ||
| .doesNotContain(row(trinoSchema)); | ||
| // table created in projectId is present in projectId and NOT present in parentProjectId | ||
| assertThat(computeActual("SHOW TABLES FROM " + trinoSchema).getOnlyColumn()).contains(tableName); | ||
| assertThat(computeActual(format( | ||
| "SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT table_name FROM `%s.region-us.INFORMATION_SCHEMA.TABLES` WHERE table_schema = \"%s\"'))", | ||
| projectId, | ||
| trinoSchema))) | ||
| .contains(row(tableName)); | ||
| assertThat(query(format( | ||
| "SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = \"%s\"'))", | ||
| trinoSchema))) | ||
| .failure() | ||
| .hasMessageContaining("Table \"INFORMATION_SCHEMA.TABLES\" must be qualified with a dataset (e.g. dataset.table)"); | ||
| assertThat(computeActual(format( | ||
| "SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT table_name FROM `%s.region-us.INFORMATION_SCHEMA.TABLES` WHERE table_schema = \"%s\"'))", | ||
| parentProjectId, | ||
| trinoSchema))) | ||
| .doesNotContain(row(tableName)); | ||
| assertThat(query("SELECT * FROM " + table.getName())).returnsEmptyResult(); | ||
| assertThat(query("SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT * FROM `%s.%s`'))".formatted(projectId, table.getName()))).returnsEmptyResult(); | ||
| assertThat(query("SELECT * FROM TABLE(bigquery.system.query(query => 'SELECT * FROM `%s.%s`'))".formatted(parentProjectId, table.getName()))) | ||
| .failure() | ||
| .hasMessageContaining("Failed to get destination table for query"); | ||
| } | ||
| } | ||
|
|
||
| private AutoCloseable withSchema(String schemaName) | ||
| { | ||
| QueryRunner queryRunner = getQueryRunner(); | ||
| queryRunner.execute("DROP SCHEMA IF EXISTS " + schemaName); | ||
| queryRunner.execute("CREATE SCHEMA " + schemaName); | ||
| return () -> queryRunner.execute("DROP SCHEMA IF EXISTS " + schemaName); | ||
| } | ||
|
|
||
| private static MaterializedRow row(String value) | ||
| { | ||
| return new MaterializedRow(DEFAULT_PRECISION, value); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.