Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@ public Map<String, String> loadNamespaceMetadata(Namespace namespace) {
}
}

@Override
protected boolean isValidIdentifier(TableIdentifier identifier) {
try {
validateNamespace(identifier.namespace());
} catch (IllegalArgumentException e) {
return false;
}
return true;
}

@Override
public String name() {
return catalogName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@
import static org.apache.iceberg.CatalogUtil.ICEBERG_CATALOG_TYPE;
import static org.apache.iceberg.CatalogUtil.ICEBERG_CATALOG_TYPE_BIGQUERY;
import static org.apache.iceberg.gcp.bigquery.BigQueryMetastoreCatalog.PROJECT_ID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
Comment thread
nastra marked this conversation as resolved.
Outdated

import java.io.File;
import java.util.List;
import java.util.Map;
import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.MetadataTableUtils;
import org.apache.iceberg.Table;
import org.apache.iceberg.TableMetadata;
import org.apache.iceberg.TableOperations;
Comment thread
nastra marked this conversation as resolved.
Outdated
import org.apache.iceberg.catalog.CatalogTests;
import org.apache.iceberg.catalog.Namespace;
import org.apache.iceberg.catalog.TableIdentifier;
Expand All @@ -39,6 +47,8 @@
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
Comment thread
nastra marked this conversation as resolved.
Outdated

public class TestBigQueryCatalog extends CatalogTests<BigQueryMetastoreCatalog> {
@TempDir private File tempFolder;
Expand Down Expand Up @@ -171,4 +181,85 @@ public void renameTableNamespaceMissing() {
public void testRenameTableMissingSourceTable() {
super.testRenameTableMissingSourceTable();
}

@Test
public void testIsValidIdentifierWithValidSingleLevelNamespace() {
TableIdentifier validIdentifier = TableIdentifier.of("dataset1", "table1");
assertThat(catalog.isValidIdentifier(validIdentifier)).isTrue();
}

@Test
public void testIsValidIdentifierWithInvalidMultiLevelNamespace() {
TableIdentifier invalidIdentifier =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: all of these can be inlined

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, formatting creates multiple lines for a few of the more verbose cases.

TableIdentifier.of(Namespace.of("level1", "level2"), "table1");
assertThat(catalog.isValidIdentifier(invalidIdentifier)).isFalse();
}

@Test
public void testIsValidIdentifierWithThreeLevelNamespace() {
TableIdentifier invalidIdentifier =
TableIdentifier.of(Namespace.of("level1", "level2", "level3"), "table1");
assertThat(catalog.isValidIdentifier(invalidIdentifier)).isFalse();
}

@Test
public void testIsValidIdentifierWithEmptyNamespace() {
TableIdentifier invalidIdentifier = TableIdentifier.of(Namespace.empty(), "table1");
assertThat(catalog.isValidIdentifier(invalidIdentifier)).isFalse();
}

@Test
public void testLoadMetadataTableIsCalled() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I follow why this is being tested here, can you elaborate please?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to test that loading a metadata table will actually happen with the fix in place, since that's technically a valid case of specifying an invalid table name.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am bit confused too, if the goal is that that the metadata table loading works correctly post this fix, shouldnt we enable this test instead ?

@Disabled("BigQuery Metastore does not support multi layer namespaces")
@Test
public void testLoadMetadataTable() {}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will move the test case there!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason why can't re-use the same test ? as if just enable this test from the base class it test if we can read the Files metadata table ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I agree, we should be able to remove this test method entirely and rely on the testing behavior defined in CatalogTests. The reason this method existed here is so that it could be disabled

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, heres the test with the same name in CatalogTests.java

@Test
public void testLoadMetadataTable() {
C catalog = catalog();
TableIdentifier tableIdent = TableIdentifier.of("ns", "tbl");
TableIdentifier metaIdent = TableIdentifier.of("ns", "tbl", "files");
if (requiresNamespaceCreate()) {
catalog.createNamespace(tableIdent.namespace());
}
catalog.buildTable(tableIdent, SCHEMA).create();
Table table = catalog.loadTable(metaIdent);
assertThat(table).isNotNull();
assertThat(table).isInstanceOf(FilesTable.class);
// check that the table metadata can be refreshed
table.refresh();
assertThat(table.name()).isEqualTo(catalog.name() + "." + metaIdent);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good thank you.

// Create a spy of the catalog to verify method calls
BigQueryMetastoreCatalog spyCatalog = spy(catalog);

// Create mock objects
TableOperations mockOps = Mockito.mock(TableOperations.class);
TableMetadata mockMetadata = Mockito.mock(TableMetadata.class);
Table mockMetadataTable = Mockito.mock(Table.class);

// Mock the table operations to return metadata (indicating base table exists)
when(mockOps.current()).thenReturn(mockMetadata);

// Create the expected base table identifier that will be extracted from the metadata table
// identifier
TableIdentifier expectedBaseTableId = TableIdentifier.of("dataset1", "table1");
when(spyCatalog.newTableOps(expectedBaseTableId)).thenReturn(mockOps);

// Use MockedStatic to mock the static MetadataTableUtils.createMetadataTableInstance call
try (MockedStatic<MetadataTableUtils> mockedUtils =
Mockito.mockStatic(MetadataTableUtils.class)) {
mockedUtils
.when(
() ->
MetadataTableUtils.createMetadataTableInstance(
any(TableOperations.class),
any(String.class),
any(TableIdentifier.class),
any(TableIdentifier.class),
any()))
.thenReturn(mockMetadataTable);

// Create a metadata table identifier
TableIdentifier metadataTableId =
TableIdentifier.of(Namespace.of("dataset1", "table1"), "partitions");

// Call loadTable which should trigger the metadata table loading path
Table result = spyCatalog.loadTable(metadataTableId);

// Verify that MetadataTableUtils.createMetadataTableInstance was called
// This confirms that loadMetadataTable path was taken
mockedUtils.verify(
() ->
MetadataTableUtils.createMetadataTableInstance(
any(TableOperations.class),
any(String.class),
any(TableIdentifier.class),
any(TableIdentifier.class),
any()));

// Verify the result is the mocked metadata table
assertThat(result).isSameAs(mockMetadataTable);
}
}
}