|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC and Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.spark.bigquery.integration; |
| 17 | + |
| 18 | +import static com.google.common.truth.Truth.assertThat; |
| 19 | + |
| 20 | +import com.google.cloud.bigquery.JobInfo; |
| 21 | +import com.google.cloud.bigquery.QueryJobConfiguration; |
| 22 | +import com.google.cloud.bigquery.TableId; |
| 23 | +import com.google.cloud.spark.bigquery.ReadSessionCreator; |
| 24 | +import java.io.StringWriter; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.List; |
| 27 | +import java.util.UUID; |
| 28 | +import java.util.stream.Collectors; |
| 29 | +import org.apache.log4j.Level; |
| 30 | +import org.apache.log4j.Logger; |
| 31 | +import org.apache.log4j.SimpleLayout; |
| 32 | +import org.apache.log4j.WriterAppender; |
| 33 | +import org.apache.spark.sql.Dataset; |
| 34 | +import org.apache.spark.sql.Row; |
| 35 | +import org.junit.After; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Test; |
| 38 | + |
| 39 | +public class MaterializedViewReadIT extends SparkBigQueryIntegrationTestBase { |
| 40 | + |
| 41 | + private final Logger logger = Logger.getLogger(ReadSessionCreator.class); |
| 42 | + private WriterAppender appender; |
| 43 | + private StringWriter stringWriter; |
| 44 | + private String testDataset; |
| 45 | + private String mvName; |
| 46 | + private TableId sourceTableId; |
| 47 | + private String testServiceAccount; |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() throws InterruptedException { |
| 51 | + // Set up a custom log appender to capture logs |
| 52 | + stringWriter = new StringWriter(); |
| 53 | + appender = new WriterAppender(new SimpleLayout(), stringWriter); |
| 54 | + appender.setThreshold(Level.WARN); |
| 55 | + logger.addAppender(appender); |
| 56 | + |
| 57 | + // Create a dedicated service account for this test |
| 58 | + String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 59 | + assertThat(projectId).isNotNull(); |
| 60 | + testServiceAccount = |
| 61 | + String.format( |
| 62 | + "mv-test-%s@%s.iam.gserviceaccount.com", |
| 63 | + UUID.randomUUID().toString().substring(0, 8), projectId); |
| 64 | + IntegrationTestUtils.createServiceAccount(testServiceAccount); |
| 65 | + |
| 66 | + // Create a temporary dataset and grant the new SA the BQ User role |
| 67 | + // This provides permissions to run jobs (for the fallback) but not to read table data directly |
| 68 | + testDataset = "mv_read_it_" + System.nanoTime(); |
| 69 | + IntegrationTestUtils.createDataset(testDataset); |
| 70 | + IntegrationTestUtils.grantServiceAccountRole( |
| 71 | + testServiceAccount, "roles/bigquery.user", testDataset); |
| 72 | + IntegrationTestUtils.grantServiceAccountRole( |
| 73 | + testServiceAccount, "roles/bigquery.metadataViewer", testDataset); |
| 74 | + |
| 75 | + // Create a source table |
| 76 | + sourceTableId = TableId.of(testDataset, "source_table_" + System.nanoTime()); |
| 77 | + IntegrationTestUtils.createTable(sourceTableId, "name:string, value:integer", "name,value"); |
| 78 | + |
| 79 | + // Create a Materialized View |
| 80 | + mvName = "test_mv_" + System.nanoTime(); |
| 81 | + String createMvSql = |
| 82 | + String.format( |
| 83 | + "CREATE MATERIALIZED VIEW `%s.%s` AS SELECT name, value FROM `%s.%s`", |
| 84 | + testDataset, mvName, testDataset, sourceTableId.getTable()); |
| 85 | + QueryJobConfiguration createMvJob = QueryJobConfiguration.newBuilder(createMvSql).build(); |
| 86 | + bigquery.create(JobInfo.of(createMvJob)).waitFor(); |
| 87 | + } |
| 88 | + |
| 89 | + @After |
| 90 | + public void tearDown() { |
| 91 | + logger.removeAppender(appender); |
| 92 | + if (testDataset != null) { |
| 93 | + IntegrationTestUtils.deleteDatasetAndTables(testDataset); |
| 94 | + } |
| 95 | + if (testServiceAccount != null) { |
| 96 | + IntegrationTestUtils.deleteServiceAccount(testServiceAccount); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testReadMaterializedView_lackingPermission_logsWarningAndFallsBack() { |
| 102 | + // This test confirms that when reading a Materialized View with a service account |
| 103 | + // that lacks `bigquery.tables.getData` permission, the connector: |
| 104 | + // 1. Logs a specific WARN message indicating the permission issue and the fallback. |
| 105 | + // 2. Successfully reads the data by falling back to materializing the view's query. |
| 106 | + |
| 107 | + // Arrange: Use the dedicated service account with insufficient permissions for a direct read. |
| 108 | + String mvToRead = testDataset + "." + mvName; |
| 109 | + |
| 110 | + // Act: Read the materialized view, impersonating the test service account |
| 111 | + Dataset<Row> df = |
| 112 | + spark |
| 113 | + .read() |
| 114 | + .format("bigquery") |
| 115 | + .option("viewsEnabled", "true") // Required to read any kind of view |
| 116 | + .option("impersonationServiceAccount", testServiceAccount) |
| 117 | + .load(mvToRead); |
| 118 | + |
| 119 | + List<Row> result = df.collectAsList(); |
| 120 | + |
| 121 | + // Assert |
| 122 | + // 1. Assert that the read was successful via fallback |
| 123 | + assertThat(result).hasSize(2); |
| 124 | + List<String> names = result.stream().map(row -> row.getString(0)).collect(Collectors.toList()); |
| 125 | + assertThat(names).containsExactlyElementsIn(Arrays.asList("name1", "name2")); |
| 126 | + |
| 127 | + // 2. Assert that the specific warning was logged |
| 128 | + String logOutput = stringWriter.toString(); |
| 129 | + assertThat(logOutput).contains("Failed to initiate a direct read from Materialized View"); |
| 130 | + assertThat(logOutput) |
| 131 | + .contains("The service account likely lacks 'bigquery.tables.getData' permission"); |
| 132 | + assertThat(logOutput).contains("Falling back to re-executing the view's underlying query"); |
| 133 | + } |
| 134 | +} |
0 commit comments