Skip to content

Jahangir yelp/fix iceberg database location #5260

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

Closed
Show file tree
Hide file tree
Changes from all 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 @@ -106,21 +106,9 @@ public IcebergCommitCallback(FileStoreTable table, String commitUser) {
this.table = table;
this.commitUser = commitUser;

this.pathFactory = new IcebergPathFactory(catalogTableMetadataPath(table));
IcebergOptions.StorageType storageType =
table.coreOptions().toConfiguration().get(IcebergOptions.METADATA_ICEBERG_STORAGE);
switch (storageType) {
case TABLE_LOCATION:
this.pathFactory = new IcebergPathFactory(new Path(table.location(), "metadata"));
break;
case HADOOP_CATALOG:
case HIVE_CATALOG:
this.pathFactory = new IcebergPathFactory(catalogTableMetadataPath(table));
break;
default:
throw new UnsupportedOperationException(
"Unknown storage type " + storageType.name());
}

IcebergMetadataCommitterFactory metadataCommitterFactory;
try {
metadataCommitterFactory =
Expand All @@ -147,14 +135,26 @@ public static Path catalogTableMetadataPath(FileStoreTable table) {
public static Path catalogDatabasePath(FileStoreTable table) {
Path dbPath = table.location().getParent();
final String dbSuffix = ".db";
if (dbPath.getName().endsWith(dbSuffix)) {
String dbName =
dbPath.getName().substring(0, dbPath.getName().length() - dbSuffix.length());
return new Path(dbPath.getParent(), String.format("iceberg/%s/", dbName));
} else {
if (!dbPath.getName().endsWith(dbSuffix)) {
throw new UnsupportedOperationException(
"Storage type ICEBERG_WAREHOUSE can only be used on Paimon tables in a Paimon warehouse.");
}

IcebergOptions.StorageType storageType =
table.coreOptions().toConfiguration().get(IcebergOptions.METADATA_ICEBERG_STORAGE);
switch (storageType) {
case TABLE_LOCATION:
return dbPath;
case HADOOP_CATALOG:
case HIVE_CATALOG:
String dbName =
dbPath.getName()
.substring(0, dbPath.getName().length() - dbSuffix.length());
return new Path(dbPath.getParent(), String.format("iceberg/%s/", dbName));
default:
throw new UnsupportedOperationException(
"Unknown storage type " + storageType.name());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.paimon.iceberg;

import org.apache.paimon.CoreOptions;
import org.apache.paimon.fs.Path;
import org.apache.paimon.options.Options;
import org.apache.paimon.table.FileStoreTable;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/** Tests for {@link IcebergCommitCallback}. */
public class IcebergCommitCallbackTest {

private FileStoreTable mockTable;
private CoreOptions mockCoreOptions;
private Options mockConfig;

@BeforeEach
void setUp() {
mockTable = mock(FileStoreTable.class);
when(mockTable.location()).thenReturn(new Path("file:/tmp/paimon/mydb.db/mytable"));

mockCoreOptions = mock(CoreOptions.class);
when(mockTable.coreOptions()).thenReturn(mockCoreOptions);

mockConfig = mock(Options.class);
when(mockCoreOptions.toConfiguration()).thenReturn(mockConfig);
}

@ParameterizedTest(name = "StorageType: {0}")
@MethodSource("provideMetadataPaths")
void testCatalogTableMetadataPath(IcebergOptions.StorageType storageType, String expectedPath) {
when(mockConfig.get(IcebergOptions.METADATA_ICEBERG_STORAGE)).thenReturn(storageType);

Path result = IcebergCommitCallback.catalogTableMetadataPath(mockTable);

assertThat(result.toString()).isEqualTo(expectedPath);
}

@ParameterizedTest(name = "StorageType: {0}")
@MethodSource("provideDatabasePaths")
void testCatalogDatabasePath(IcebergOptions.StorageType storageType, String expectedPath) {
when(mockConfig.get(IcebergOptions.METADATA_ICEBERG_STORAGE)).thenReturn(storageType);

Path result = IcebergCommitCallback.catalogDatabasePath(mockTable);

assertThat(result.toString()).isEqualTo(expectedPath);
}

private static Stream<Arguments> provideMetadataPaths() {
return Stream.of(
Arguments.of(
IcebergOptions.StorageType.TABLE_LOCATION,
"file:/tmp/paimon/mydb.db/mytable/metadata"),
Arguments.of(
IcebergOptions.StorageType.HIVE_CATALOG,
"file:/tmp/paimon/iceberg/mydb/mytable/metadata"),
Arguments.of(
IcebergOptions.StorageType.HADOOP_CATALOG,
"file:/tmp/paimon/iceberg/mydb/mytable/metadata"));
}

private static Stream<Arguments> provideDatabasePaths() {
return Stream.of(
Arguments.of(IcebergOptions.StorageType.TABLE_LOCATION, "file:/tmp/paimon/mydb.db"),
Arguments.of(
IcebergOptions.StorageType.HIVE_CATALOG, "file:/tmp/paimon/iceberg/mydb"),
Arguments.of(
IcebergOptions.StorageType.HADOOP_CATALOG,
"file:/tmp/paimon/iceberg/mydb"));
}
}