luoyuxia commented on code in PR #3395:
URL: https://github.com/apache/fluss/pull/3395#discussion_r3339450432
##########
fluss-flink/fluss-flink-common/src/main/java/org/apache/fluss/flink/lake/LakeFlinkCatalog.java:
##########
@@ -86,6 +87,11 @@ public Catalog getLakeCatalog(
} else if (lakeFormat == ICEBERG) {
catalog = IcebergCatalogFactory.create(catalogName,
catalogProperties);
this.lakeFormat = ICEBERG;
+ } else if (lakeFormat == HUDI) {
Review Comment:
Could you please remove these changes in `LakeFlinkCatalog` and
`LakeTableFactory` in this pr. Let's focus Hudi own catalog instead of flink
catalog in this pr.
We do add them back in another pr which requirs a IT to test we can use
$lake to read hudi table
##########
fluss-lake/fluss-lake-hudi/src/main/java/org/apache/fluss/lake/hudi/HudiLakeCatalog.java:
##########
@@ -0,0 +1,277 @@
+/*
+ * 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.fluss.lake.hudi;
+
+import org.apache.fluss.annotation.VisibleForTesting;
+import org.apache.fluss.config.Configuration;
+import org.apache.fluss.lake.hudi.utils.HudiConversions;
+import org.apache.fluss.lake.hudi.utils.catalog.HudiCatalogUtils;
+import org.apache.fluss.lake.lakestorage.LakeCatalog;
+import org.apache.fluss.metadata.TableChange;
+import org.apache.fluss.metadata.TableDescriptor;
+import org.apache.fluss.metadata.TablePath;
+import org.apache.fluss.utils.IOUtils;
+
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.catalog.Catalog;
+import org.apache.flink.table.catalog.CatalogBaseTable;
+import org.apache.flink.table.catalog.CatalogDatabase;
+import org.apache.flink.table.catalog.CatalogTable;
+import org.apache.flink.table.catalog.Column;
+import org.apache.flink.table.catalog.ObjectPath;
+import org.apache.flink.table.catalog.ResolvedCatalogBaseTable;
+import org.apache.flink.table.catalog.ResolvedSchema;
+import org.apache.flink.table.catalog.exceptions.DatabaseAlreadyExistException;
+import org.apache.flink.table.catalog.exceptions.DatabaseNotExistException;
+import org.apache.flink.table.catalog.exceptions.TableNotExistException;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.hudi.table.catalog.CatalogOptions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Objects;
+
+import static
org.apache.fluss.lake.hudi.utils.catalog.HudiCatalogUtils.HIVE_META_STORE_TYPE;
+import static
org.apache.fluss.lake.hudi.utils.catalog.HudiCatalogUtils.HUDI_CATALOG_DEFAULT_NAME;
+import static org.apache.fluss.metadata.TableDescriptor.BUCKET_COLUMN_NAME;
+import static org.apache.fluss.metadata.TableDescriptor.OFFSET_COLUMN_NAME;
+import static org.apache.fluss.metadata.TableDescriptor.TIMESTAMP_COLUMN_NAME;
+
+/** Implementation of {@link LakeCatalog} for Hudi. */
+public class HudiLakeCatalog implements LakeCatalog {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(HudiLakeCatalog.class);
+
+ public static final LinkedHashMap<String, DataType> SYSTEM_COLUMNS = new
LinkedHashMap<>();
+
+ static {
+ SYSTEM_COLUMNS.put(BUCKET_COLUMN_NAME, DataTypes.INT());
+ SYSTEM_COLUMNS.put(OFFSET_COLUMN_NAME, DataTypes.BIGINT());
+ SYSTEM_COLUMNS.put(TIMESTAMP_COLUMN_NAME, DataTypes.TIMESTAMP(6));
+ }
+
+ private final Catalog hudiCatalog;
+ private final String catalogMode;
+
+ public HudiLakeCatalog(Configuration configuration) {
+ this.catalogMode =
+ configuration.toMap().getOrDefault(CatalogOptions.MODE.key(),
HIVE_META_STORE_TYPE);
+ this.hudiCatalog = HudiCatalogUtils.createHudiCatalog(configuration);
+ this.hudiCatalog.open();
+ }
+
+ @VisibleForTesting
+ protected Catalog getHudiCatalog() {
+ return hudiCatalog;
+ }
+
+ @Override
+ public void createTable(TablePath tablePath, TableDescriptor
tableDescriptor, Context context)
+ throws org.apache.fluss.exception.TableAlreadyExistException {
+ LOG.info("create the lake table for : {} with props: {}", tablePath,
tableDescriptor);
+
+ ObjectPath objectPath = HudiConversions.toHudiObjectPath(tablePath);
+
+ boolean isPkTable =
tableDescriptor.getSchema().getPrimaryKeyIndexes().length > 0;
+
+ // Create Hudi catalog table
Review Comment:
could you please add comment that why convert to flink catalog table and
call flink catalog to create hudi table?
##########
fluss-lake/fluss-lake-hudi/src/main/java/org/apache/fluss/lake/hudi/FlussDataTypeToHudiDataType.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.fluss.lake.hudi;
+
+import org.apache.fluss.types.ArrayType;
+import org.apache.fluss.types.BigIntType;
+import org.apache.fluss.types.BinaryType;
+import org.apache.fluss.types.BooleanType;
+import org.apache.fluss.types.BytesType;
+import org.apache.fluss.types.CharType;
+import org.apache.fluss.types.DataTypeVisitor;
+import org.apache.fluss.types.DateType;
+import org.apache.fluss.types.DecimalType;
+import org.apache.fluss.types.DoubleType;
+import org.apache.fluss.types.FloatType;
+import org.apache.fluss.types.IntType;
+import org.apache.fluss.types.LocalZonedTimestampType;
+import org.apache.fluss.types.MapType;
+import org.apache.fluss.types.RowType;
+import org.apache.fluss.types.SmallIntType;
+import org.apache.fluss.types.StringType;
+import org.apache.fluss.types.TimeType;
+import org.apache.fluss.types.TimestampType;
+import org.apache.fluss.types.TinyIntType;
+
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.types.DataType;
+
+import javax.annotation.concurrent.ThreadSafe;
+
+import static
org.apache.fluss.lake.hudi.utils.catalog.HudiCatalogUtils.FILE_SYSTEM_TYPE;
+import static
org.apache.fluss.lake.hudi.utils.catalog.HudiCatalogUtils.HIVE_META_STORE_TYPE;
+
+/** Convert from Fluss's data type to Hudi's internal data type. */
Review Comment:
nit: not Hudi's internal data type. It's flink internal data type.
Also add comments why convert to flink internal data type intead of Hudi's
internal data type.
##########
fluss-lake/fluss-lake-hudi/src/test/java/org/apache/fluss/lake/hudi/HudiLakeCatalogTest.java:
##########
@@ -0,0 +1,645 @@
+/*
+ * 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.fluss.lake.hudi;
+
+import org.apache.fluss.config.Configuration;
+import org.apache.fluss.exception.InvalidConfigException;
+import org.apache.fluss.exception.InvalidTableException;
+import org.apache.fluss.exception.TableAlreadyExistException;
+import org.apache.fluss.lake.hudi.utils.HudiConversions;
+import org.apache.fluss.lake.lakestorage.TestingLakeCatalogContext;
+import org.apache.fluss.metadata.Schema;
+import org.apache.fluss.metadata.TableDescriptor;
+import org.apache.fluss.metadata.TablePath;
+import org.apache.fluss.types.DataTypes;
+
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.table.catalog.CatalogBaseTable;
+import org.apache.flink.table.catalog.CatalogTable;
+import org.apache.flink.table.catalog.Column;
+import org.apache.flink.table.catalog.ObjectPath;
+import org.apache.flink.table.catalog.ResolvedCatalogTable;
+import org.apache.flink.table.catalog.ResolvedSchema;
+import org.apache.flink.table.catalog.exceptions.TableNotExistException;
+import org.apache.flink.table.types.DataType;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Unit test for {@link HudiLakeCatalog}. */
+class HudiLakeCatalogTest {
+
+ @TempDir private File tempWarehouseDir;
+
+ private HudiLakeCatalog flussHudiLakeCatalog;
+
+ @BeforeEach
+ public void setUp() {
+ Configuration configuration = new Configuration();
+ configuration.setString("catalog.path",
tempWarehouseDir.toURI().toString());
+ configuration.setString("mode", "dfs");
+ this.flussHudiLakeCatalog = new HudiLakeCatalog(configuration);
+ }
+
+ /** Verify property prefix rewriting. */
+ @Test
+ void testPropertyPrefixRewriting() throws TableNotExistException {
+ String database = "test_db";
+ String tableName = "test_table";
+
+ Schema flussSchema =
+ Schema.newBuilder().column("id",
DataTypes.BIGINT()).primaryKey("id").build();
+
+ TableDescriptor tableDescriptor =
+ TableDescriptor.builder()
+ .schema(flussSchema)
+ .distributedBy(3)
+ .property("hudi.precombine.field", "id")
+ .property("table.datalake.freshness", "30s")
+ .build();
+
+ TablePath tablePath = TablePath.of(database, tableName);
+ TestingLakeCatalogContext context = new TestingLakeCatalogContext();
+ flussHudiLakeCatalog.createTable(tablePath, tableDescriptor, context);
+
+ CatalogBaseTable table =
+ flussHudiLakeCatalog
+ .getHudiCatalog()
+ .getTable(HudiConversions.toHudiObjectPath(tablePath));
+
+ // Verify property prefix rewriting
+ assertThat(table.getOptions()).containsEntry("precombine.field", "id");
+
assertThat(table.getOptions()).containsEntry("fluss.table.datalake.freshness",
"30s");
+ assertThat(table.getOptions())
+ .doesNotContainKeys("hudi.precombine.field",
"table.datalake.freshness");
+ }
+
+ @Test
+ void testCreatePrimaryKeyTable() throws TableNotExistException {
+ String database = "test_db";
+ String tableName = "pk_table";
+
+ Schema flussSchema =
+ Schema.newBuilder()
+ .column("id", DataTypes.INT())
+ .column("name", DataTypes.STRING())
+ .withComment("pk_table")
+ .primaryKey("id")
+ .build();
+
+ TableDescriptor tableDescriptor =
+ TableDescriptor.builder().schema(flussSchema).distributedBy(4,
"id").build();
+
+ TablePath tablePath = TablePath.of(database, tableName);
+ TestingLakeCatalogContext context = new TestingLakeCatalogContext();
+ flussHudiLakeCatalog.createTable(tablePath, tableDescriptor, context);
+
+ ObjectPath objectPath = HudiConversions.toHudiObjectPath(tablePath);
+ CatalogBaseTable table =
flussHudiLakeCatalog.getHudiCatalog().getTable(objectPath);
+
+ assertThat(table).isNotNull();
+
+ List<String> primaryKeys = new ArrayList<>();
+ primaryKeys.add("id");
+ TableSchema expectHudiSchema =
Review Comment:
nit: 'org.apache.flink.table.api.TableSchema' is deprecated
##########
fluss-lake/fluss-lake-hudi/src/test/java/org/apache/fluss/lake/hudi/HudiLakeCatalogTest.java:
##########
@@ -0,0 +1,645 @@
+/*
+ * 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.fluss.lake.hudi;
+
+import org.apache.fluss.config.Configuration;
+import org.apache.fluss.exception.InvalidConfigException;
+import org.apache.fluss.exception.InvalidTableException;
+import org.apache.fluss.exception.TableAlreadyExistException;
+import org.apache.fluss.lake.hudi.utils.HudiConversions;
+import org.apache.fluss.lake.lakestorage.TestingLakeCatalogContext;
+import org.apache.fluss.metadata.Schema;
+import org.apache.fluss.metadata.TableDescriptor;
+import org.apache.fluss.metadata.TablePath;
+import org.apache.fluss.types.DataTypes;
+
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.table.catalog.CatalogBaseTable;
+import org.apache.flink.table.catalog.CatalogTable;
+import org.apache.flink.table.catalog.Column;
+import org.apache.flink.table.catalog.ObjectPath;
+import org.apache.flink.table.catalog.ResolvedCatalogTable;
+import org.apache.flink.table.catalog.ResolvedSchema;
+import org.apache.flink.table.catalog.exceptions.TableNotExistException;
+import org.apache.flink.table.types.DataType;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Unit test for {@link HudiLakeCatalog}. */
+class HudiLakeCatalogTest {
+
+ @TempDir private File tempWarehouseDir;
+
+ private HudiLakeCatalog flussHudiLakeCatalog;
+
+ @BeforeEach
+ public void setUp() {
+ Configuration configuration = new Configuration();
+ configuration.setString("catalog.path",
tempWarehouseDir.toURI().toString());
+ configuration.setString("mode", "dfs");
+ this.flussHudiLakeCatalog = new HudiLakeCatalog(configuration);
+ }
+
+ /** Verify property prefix rewriting. */
+ @Test
+ void testPropertyPrefixRewriting() throws TableNotExistException {
+ String database = "test_db";
+ String tableName = "test_table";
+
+ Schema flussSchema =
+ Schema.newBuilder().column("id",
DataTypes.BIGINT()).primaryKey("id").build();
+
+ TableDescriptor tableDescriptor =
+ TableDescriptor.builder()
+ .schema(flussSchema)
+ .distributedBy(3)
+ .property("hudi.precombine.field", "id")
+ .property("table.datalake.freshness", "30s")
+ .build();
+
+ TablePath tablePath = TablePath.of(database, tableName);
+ TestingLakeCatalogContext context = new TestingLakeCatalogContext();
+ flussHudiLakeCatalog.createTable(tablePath, tableDescriptor, context);
+
+ CatalogBaseTable table =
+ flussHudiLakeCatalog
+ .getHudiCatalog()
+ .getTable(HudiConversions.toHudiObjectPath(tablePath));
+
+ // Verify property prefix rewriting
+ assertThat(table.getOptions()).containsEntry("precombine.field", "id");
+
assertThat(table.getOptions()).containsEntry("fluss.table.datalake.freshness",
"30s");
+ assertThat(table.getOptions())
+ .doesNotContainKeys("hudi.precombine.field",
"table.datalake.freshness");
+ }
+
+ @Test
+ void testCreatePrimaryKeyTable() throws TableNotExistException {
+ String database = "test_db";
+ String tableName = "pk_table";
+
+ Schema flussSchema =
+ Schema.newBuilder()
+ .column("id", DataTypes.INT())
+ .column("name", DataTypes.STRING())
+ .withComment("pk_table")
+ .primaryKey("id")
+ .build();
+
+ TableDescriptor tableDescriptor =
+ TableDescriptor.builder().schema(flussSchema).distributedBy(4,
"id").build();
+
+ TablePath tablePath = TablePath.of(database, tableName);
+ TestingLakeCatalogContext context = new TestingLakeCatalogContext();
+ flussHudiLakeCatalog.createTable(tablePath, tableDescriptor, context);
+
+ ObjectPath objectPath = HudiConversions.toHudiObjectPath(tablePath);
+ CatalogBaseTable table =
flussHudiLakeCatalog.getHudiCatalog().getTable(objectPath);
+
+ assertThat(table).isNotNull();
+
+ List<String> primaryKeys = new ArrayList<>();
+ primaryKeys.add("id");
+ TableSchema expectHudiSchema =
+ TableSchema.builder()
+ .field("id",
org.apache.flink.table.api.DataTypes.INT().notNull())
+ .field("name",
org.apache.flink.table.api.DataTypes.STRING())
+ .field("__bucket",
org.apache.flink.table.api.DataTypes.INT())
+ .field("__offset",
org.apache.flink.table.api.DataTypes.BIGINT())
+ .field("__timestamp",
org.apache.flink.table.api.DataTypes.TIMESTAMP(6))
+ .primaryKey("primaryKey", primaryKeys.toArray(new
String[0]))
+ .build();
+
+
assertThat(table.getUnresolvedSchema()).isEqualTo(expectHudiSchema.toSchema());
+ }
+
+ @Test
+ void testCreateLogTable() throws TableNotExistException {
+ String database = "test_db";
+ String tableName = "log_table";
+
+ Schema flussSchema =
+ Schema.newBuilder()
+ .column("id", DataTypes.BIGINT())
+ .column("name", DataTypes.STRING())
+ .build();
+
+ Map<String, String> customProperties = new HashMap<>();
+ customProperties.put("hudi.hoodie.datasource.write.recordkey.field",
"id");
+
+ TableDescriptor td =
+ TableDescriptor.builder()
+ .schema(flussSchema)
+ .distributedBy(3) // no bucket key
+ .customProperties(customProperties)
+ .build();
+
+ TablePath tablePath = TablePath.of(database, tableName);
+ TestingLakeCatalogContext context = new TestingLakeCatalogContext();
+ flussHudiLakeCatalog.createTable(tablePath, td, context);
+
+ ObjectPath objectPath = HudiConversions.toHudiObjectPath(tablePath);
+ CatalogBaseTable table =
flussHudiLakeCatalog.getHudiCatalog().getTable(objectPath);
+
+ List<String> primaryKeys = new ArrayList<>();
+ primaryKeys.add("id");
+ TableSchema expectHudiSchema =
+ TableSchema.builder()
+ .field("id",
org.apache.flink.table.api.DataTypes.BIGINT().notNull())
+ .field("name",
org.apache.flink.table.api.DataTypes.STRING())
+ .field("__bucket",
org.apache.flink.table.api.DataTypes.INT())
+ .field("__offset",
org.apache.flink.table.api.DataTypes.BIGINT())
+ .field("__timestamp",
org.apache.flink.table.api.DataTypes.TIMESTAMP(6))
+ .primaryKey("PK_id", primaryKeys.toArray(new
String[0]))
+ .build();
+
+
assertThat(table.getUnresolvedSchema()).isEqualTo(expectHudiSchema.toSchema());
+ }
+
+ @Test
+ void testCreateLogTableWithoutRecordKeyThrowsException() {
+ Schema flussSchema =
+ Schema.newBuilder()
+ .column("id", DataTypes.BIGINT())
+ .column("name", DataTypes.STRING())
+ .build();
+
+ TableDescriptor tableDescriptor =
+
TableDescriptor.builder().schema(flussSchema).distributedBy(3).build();
+
+ TablePath tablePath = TablePath.of("test_db",
"log_table_without_record_key");
+ TestingLakeCatalogContext context = new TestingLakeCatalogContext();
+
+ assertThatThrownBy(
+ () -> flussHudiLakeCatalog.createTable(tablePath,
tableDescriptor, context))
+ .isInstanceOf(InvalidConfigException.class)
+
.hasMessageContaining("hudi.hoodie.datasource.write.recordkey.field")
+ .hasMessageContaining(tablePath.toString());
+ }
+
+ // ------------------------------------------------------------------
+ // isHudiSchemaCompatible() tests
+ // ------------------------------------------------------------------
+
+ @Test
+ void testIsHudiSchemaCompatibleWithSameSchema() {
Review Comment:
nit: combine testIsHudiSchemaCompatible releated test into single one test
method?
##########
fluss-lake/fluss-lake-hudi/src/test/java/org/apache/fluss/lake/hudi/HudiLakeCatalogTest.java:
##########
@@ -0,0 +1,645 @@
+/*
+ * 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.fluss.lake.hudi;
+
+import org.apache.fluss.config.Configuration;
+import org.apache.fluss.exception.InvalidConfigException;
+import org.apache.fluss.exception.InvalidTableException;
+import org.apache.fluss.exception.TableAlreadyExistException;
+import org.apache.fluss.lake.hudi.utils.HudiConversions;
+import org.apache.fluss.lake.lakestorage.TestingLakeCatalogContext;
+import org.apache.fluss.metadata.Schema;
+import org.apache.fluss.metadata.TableDescriptor;
+import org.apache.fluss.metadata.TablePath;
+import org.apache.fluss.types.DataTypes;
+
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.table.catalog.CatalogBaseTable;
+import org.apache.flink.table.catalog.CatalogTable;
+import org.apache.flink.table.catalog.Column;
+import org.apache.flink.table.catalog.ObjectPath;
+import org.apache.flink.table.catalog.ResolvedCatalogTable;
+import org.apache.flink.table.catalog.ResolvedSchema;
+import org.apache.flink.table.catalog.exceptions.TableNotExistException;
+import org.apache.flink.table.types.DataType;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Unit test for {@link HudiLakeCatalog}. */
+class HudiLakeCatalogTest {
+
+ @TempDir private File tempWarehouseDir;
+
+ private HudiLakeCatalog flussHudiLakeCatalog;
+
+ @BeforeEach
+ public void setUp() {
+ Configuration configuration = new Configuration();
+ configuration.setString("catalog.path",
tempWarehouseDir.toURI().toString());
+ configuration.setString("mode", "dfs");
+ this.flussHudiLakeCatalog = new HudiLakeCatalog(configuration);
+ }
+
+ /** Verify property prefix rewriting. */
+ @Test
+ void testPropertyPrefixRewriting() throws TableNotExistException {
+ String database = "test_db";
+ String tableName = "test_table";
+
+ Schema flussSchema =
+ Schema.newBuilder().column("id",
DataTypes.BIGINT()).primaryKey("id").build();
+
+ TableDescriptor tableDescriptor =
+ TableDescriptor.builder()
+ .schema(flussSchema)
+ .distributedBy(3)
+ .property("hudi.precombine.field", "id")
+ .property("table.datalake.freshness", "30s")
+ .build();
+
+ TablePath tablePath = TablePath.of(database, tableName);
+ TestingLakeCatalogContext context = new TestingLakeCatalogContext();
+ flussHudiLakeCatalog.createTable(tablePath, tableDescriptor, context);
+
+ CatalogBaseTable table =
+ flussHudiLakeCatalog
+ .getHudiCatalog()
+ .getTable(HudiConversions.toHudiObjectPath(tablePath));
+
+ // Verify property prefix rewriting
+ assertThat(table.getOptions()).containsEntry("precombine.field", "id");
+
assertThat(table.getOptions()).containsEntry("fluss.table.datalake.freshness",
"30s");
+ assertThat(table.getOptions())
+ .doesNotContainKeys("hudi.precombine.field",
"table.datalake.freshness");
+ }
+
+ @Test
+ void testCreatePrimaryKeyTable() throws TableNotExistException {
+ String database = "test_db";
+ String tableName = "pk_table";
+
+ Schema flussSchema =
+ Schema.newBuilder()
+ .column("id", DataTypes.INT())
+ .column("name", DataTypes.STRING())
+ .withComment("pk_table")
+ .primaryKey("id")
+ .build();
+
+ TableDescriptor tableDescriptor =
+ TableDescriptor.builder().schema(flussSchema).distributedBy(4,
"id").build();
+
+ TablePath tablePath = TablePath.of(database, tableName);
+ TestingLakeCatalogContext context = new TestingLakeCatalogContext();
+ flussHudiLakeCatalog.createTable(tablePath, tableDescriptor, context);
+
+ ObjectPath objectPath = HudiConversions.toHudiObjectPath(tablePath);
+ CatalogBaseTable table =
flussHudiLakeCatalog.getHudiCatalog().getTable(objectPath);
+
+ assertThat(table).isNotNull();
+
+ List<String> primaryKeys = new ArrayList<>();
+ primaryKeys.add("id");
+ TableSchema expectHudiSchema =
+ TableSchema.builder()
+ .field("id",
org.apache.flink.table.api.DataTypes.INT().notNull())
+ .field("name",
org.apache.flink.table.api.DataTypes.STRING())
+ .field("__bucket",
org.apache.flink.table.api.DataTypes.INT())
+ .field("__offset",
org.apache.flink.table.api.DataTypes.BIGINT())
+ .field("__timestamp",
org.apache.flink.table.api.DataTypes.TIMESTAMP(6))
+ .primaryKey("primaryKey", primaryKeys.toArray(new
String[0]))
+ .build();
+
+
assertThat(table.getUnresolvedSchema()).isEqualTo(expectHudiSchema.toSchema());
+ }
+
+ @Test
+ void testCreateLogTable() throws TableNotExistException {
+ String database = "test_db";
+ String tableName = "log_table";
+
+ Schema flussSchema =
+ Schema.newBuilder()
+ .column("id", DataTypes.BIGINT())
+ .column("name", DataTypes.STRING())
+ .build();
+
+ Map<String, String> customProperties = new HashMap<>();
+ customProperties.put("hudi.hoodie.datasource.write.recordkey.field",
"id");
+
+ TableDescriptor td =
+ TableDescriptor.builder()
+ .schema(flussSchema)
+ .distributedBy(3) // no bucket key
+ .customProperties(customProperties)
+ .build();
+
+ TablePath tablePath = TablePath.of(database, tableName);
+ TestingLakeCatalogContext context = new TestingLakeCatalogContext();
+ flussHudiLakeCatalog.createTable(tablePath, td, context);
+
+ ObjectPath objectPath = HudiConversions.toHudiObjectPath(tablePath);
+ CatalogBaseTable table =
flussHudiLakeCatalog.getHudiCatalog().getTable(objectPath);
+
+ List<String> primaryKeys = new ArrayList<>();
+ primaryKeys.add("id");
+ TableSchema expectHudiSchema =
Review Comment:
dito
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]