Skip to content

Commit

Permalink
Fix parent project id handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pajaks authored and ebyhr committed Aug 29, 2024
1 parent da2eb4b commit 74cce25
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,15 @@ jobs:
env:
BIGQUERY_CREDENTIALS_KEY: ${{ secrets.BIGQUERY_CREDENTIALS_KEY }}
GCP_STORAGE_BUCKET: ${{ vars.GCP_STORAGE_BUCKET }}
BIGQUERY_TESTING_PROJECT_ID: ${{ vars.BIGQUERY_TESTING_PROJECT_ID }}
BIGQUERY_TESTING_PARENT_PROJECT_ID: ${{ vars.BIGQUERY_TESTING_PARENT_PROJECT_ID }}
if: matrix.modules == 'plugin/trino-bigquery' && !contains(matrix.profile, 'cloud-tests-2') && (env.CI_SKIP_SECRETS_PRESENCE_CHECKS != '' || env.BIGQUERY_CREDENTIALS_KEY != '')
run: |
$MAVEN test ${MAVEN_TEST} -pl :trino-bigquery -Pcloud-tests-1 \
-Dbigquery.credentials-key="${BIGQUERY_CREDENTIALS_KEY}" \
-Dtesting.gcp-storage-bucket="${GCP_STORAGE_BUCKET}"
-Dtesting.gcp-storage-bucket="${GCP_STORAGE_BUCKET}" \
-Dtesting.bigquery-project-id="${BIGQUERY_TESTING_PROJECT_ID}" \
-Dtesting.bigquery-parent-project-id="${BIGQUERY_TESTING_PARENT_PROJECT_ID}"
- name: Cloud BigQuery Smoke Tests
id: tests-bq-smoke
env:
Expand Down
2 changes: 2 additions & 0 deletions plugin/trino-bigquery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@
<exclude>**/TestBigQueryCaseInsensitiveMapping.java</exclude>
<exclude>**/TestBigQuery*FailureRecoveryTest.java</exclude>
<exclude>**/TestBigQueryWithProxyTest.java</exclude>
<exclude>**/TestBigQueryParentProjectId.java</exclude>
</excludes>
</configuration>
</plugin>
Expand All @@ -557,6 +558,7 @@
<configuration>
<includes>
<include>**/TestBigQueryAvroConnectorTest.java</include>
<include>**/TestBigQueryParentProjectId.java</include>
</includes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public TableInfo getCachedTable(Duration viewExpiration, TableInfo remoteTableId
*/
public String getParentProjectId()
{
return bigQuery.getOptions().getProjectId();
return Optional.ofNullable(bigQuery.getOptions().getQuotaProjectId()).orElse(bigQuery.getOptions().getProjectId());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public class CredentialsOptionsConfigurer
implements BigQueryOptionsConfigurer
{
private final BigQueryCredentialsSupplier credentialsSupplier;
private final Optional<String> configProjectId;
private final Optional<String> configParentProjectId;

@Inject
public CredentialsOptionsConfigurer(BigQueryConfig bigQueryConfig, BigQueryCredentialsSupplier credentialsSupplier)
{
this.configProjectId = bigQueryConfig.getProjectId();
this.configParentProjectId = bigQueryConfig.getParentProjectId();
this.credentialsSupplier = requireNonNull(credentialsSupplier, "credentialsSupplier is null");
}
Expand All @@ -44,9 +46,11 @@ public CredentialsOptionsConfigurer(BigQueryConfig bigQueryConfig, BigQueryCrede
public BigQueryOptions.Builder configure(BigQueryOptions.Builder builder, ConnectorSession session)
{
Optional<Credentials> credentials = credentialsSupplier.getCredentials(session);
String parentProjectId = resolveProjectId(configParentProjectId, credentials);
String projectId = resolveProjectId(configProjectId, credentials);
credentials.ifPresent(builder::setCredentials);
builder.setProjectId(parentProjectId);
builder.setProjectId(projectId);
// Quota project id is different name for parent project id, both indicates project used for quota and billing purposes.
configParentProjectId.ifPresent(builder::setQuotaProjectId);
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.QueryRunner;
import org.junit.jupiter.api.Test;

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 testingProjectId;
private final String testingParentProjectId;

TestBigQueryParentProjectId()
{
testingProjectId = requiredNonEmptySystemProperty("testing.bigquery-project-id");
testingParentProjectId = requiredNonEmptySystemProperty("testing.bigquery-parent-project-id");
}

@Override
protected QueryRunner createQueryRunner()
throws Exception
{
return BigQueryQueryRunner.builder()
.setConnectorProperties(ImmutableMap.<String, String>builder()
.put("bigquery.project-id", testingProjectId)
.put("bigquery.parent-project-id", testingParentProjectId)
.buildOrThrow())
.setInitialTables(ImmutableList.of(NATION))
.build();
}

@Test
void testQueriesWithParentProjectId()
{
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'))", testingProjectId)))
.isEqualTo("ALGERIA");
}
}

0 comments on commit 74cce25

Please sign in to comment.