Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -181,9 +181,11 @@ private static DataType toFlinkPrimitiveType(PrimitiveTypeInfo hiveType) {
*/
public static List<FieldSchema> toHiveFieldSchema(TableSchema schema, boolean withOperationField) {
List<FieldSchema> columns = new ArrayList<>();
Collection<String> metaFields = withOperationField
? HoodieRecord.HOODIE_META_COLUMNS_WITH_OPERATION // caution that the set may break sequence
: HoodieRecord.HOODIE_META_COLUMNS;
Collection<String> metaFields = new ArrayList<>(HoodieRecord.HOODIE_META_COLUMNS);
if (withOperationField) {
metaFields.add(HoodieRecord.OPERATION_METADATA_FIELD);
}

for (String metaField : metaFields) {
columns.add(new FieldSchema(metaField, "string", null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.hudi.client.HoodieFlinkWriteClient;
import org.apache.hudi.common.fs.FSUtils;
import org.apache.hudi.common.model.HoodieFileFormat;
import org.apache.hudi.common.model.HoodieRecord;
import org.apache.hudi.common.table.HoodieTableConfig;
import org.apache.hudi.common.table.HoodieTableMetaClient;
import org.apache.hudi.common.table.timeline.HoodieActiveTimeline;
Expand Down Expand Up @@ -438,8 +439,10 @@ public CatalogBaseTable getTable(ObjectPath tablePath) throws TableNotExistExcep
LOG.warn("{} does not have any hoodie schema, and use hive table schema to infer the table schema", tablePath);
schema = HiveSchemaUtils.convertTableSchema(hiveTable);
}
org.apache.flink.table.api.Schema resultSchema = DataTypeUtils.dropIfExistsColumns(schema, HoodieRecord.HOODIE_META_COLUMNS_WITH_OPERATION);

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.

In line419, we already ignore the metadata column, so why drop it again?

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.

Indeed, I have removed.

Map<String, String> options = supplementOptions(tablePath, parameters);
return CatalogTable.of(schema, parameters.get(COMMENT),
return CatalogTable.of(resultSchema, parameters.get(COMMENT),
HiveSchemaUtils.getFieldNames(hiveTable.getPartitionKeys()), options);
}

Expand Down Expand Up @@ -819,7 +822,7 @@ public void dropPartition(
try (HoodieFlinkWriteClient<?> writeClient = createWriteClient(tablePath, table)) {
boolean hiveStylePartitioning = Boolean.parseBoolean(table.getOptions().get(FlinkOptions.HIVE_STYLE_PARTITIONING.key()));
writeClient.deletePartitions(
Collections.singletonList(HoodieCatalogUtil.inferPartitionPath(hiveStylePartitioning, partitionSpec)),
Collections.singletonList(HoodieCatalogUtil.inferPartitionPath(hiveStylePartitioning, partitionSpec)),
HoodieActiveTimeline.createNewInstantTime())
.forEach(writeStatus -> {
if (writeStatus.hasErrors()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.hudi.table.catalog;

import org.apache.hudi.common.fs.FSUtils;
import org.apache.hudi.common.model.HoodieRecord;
import org.apache.hudi.common.model.HoodieTableType;
import org.apache.hudi.common.table.TableSchemaResolver;
import org.apache.hudi.configuration.FlinkOptions;
Expand All @@ -28,6 +29,8 @@

import org.apache.avro.Schema;
import org.apache.flink.table.catalog.CatalogTable;
import org.apache.flink.table.types.logical.RowType;
import org.apache.flink.table.types.logical.VarCharType;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
Expand All @@ -39,7 +42,9 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
Expand All @@ -49,6 +54,7 @@
import java.util.stream.Collectors;

import static org.apache.flink.table.factories.FactoryUtil.CONNECTOR;
import static org.apache.hudi.common.model.HoodieRecord.OPERATION_METADATA_FIELD;
import static org.apache.hudi.common.table.HoodieTableMetaClient.AUXILIARYFOLDER_NAME;

/**
Expand Down Expand Up @@ -169,7 +175,8 @@ public static Map<String, String> translateFlinkTableProperties2Spark(
Configuration hadoopConf,
Map<String, String> properties,
List<String> partitionKeys) {
Schema schema = AvroSchemaConverter.convertToSchema(catalogTable.getSchema().toPhysicalRowDataType().getLogicalType());
RowType containMetaFields = getContainMetaFields(catalogTable);
Schema schema = AvroSchemaConverter.convertToSchema(containMetaFields);
MessageType messageType = TableSchemaResolver.convertAvroSchemaToParquet(schema, hadoopConf);
String sparkVersion = catalogTable.getOptions().getOrDefault(SPARK_VERSION, DEFAULT_SPARK_VERSION);
Map<String, String> sparkTableProperties = SparkDataSourceTableUtils.getSparkTableProperties(
Expand All @@ -184,6 +191,21 @@ public static Map<String, String> translateFlinkTableProperties2Spark(
e -> e.getKey().equalsIgnoreCase(FlinkOptions.TABLE_TYPE.key()) ? VALUE_MAPPING.get(e.getValue()) : e.getValue()));
}

private static RowType getContainMetaFields(CatalogTable catalogTable) {
RowType rowType = (RowType) catalogTable.getSchema().toPhysicalRowDataType().getLogicalType().copy();
boolean withOperationField = Boolean.parseBoolean(catalogTable.getOptions().getOrDefault(FlinkOptions.CHANGELOG_ENABLED.key(), "false"));
Collection<String> metaFields = new ArrayList<>(HoodieRecord.HOODIE_META_COLUMNS);
if (withOperationField) {
metaFields.add(OPERATION_METADATA_FIELD);
}
ArrayList<RowType.RowField> rowFields = new ArrayList<>();
for (String metaField : metaFields) {
rowFields.add(new RowType.RowField(metaField, new VarCharType(10000)));
}
rowFields.addAll(rowType.getFields());
return new RowType(false, rowFields);
}

public static Map<String, String> translateSparkTableProperties2Flink(Map<String, String> options) {
if (options.containsKey(CONNECTOR.key())) {
return options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
Expand Down Expand Up @@ -167,4 +168,27 @@ public static DataType ensureColumnsAsNonNullable(DataType dataType, @Nullable L
return DataTypes.ROW(fields.stream().toArray(DataTypes.Field[]::new)).notNull();
}

/**
* drop some columns.
*
* @param schema
* @param dropCols
* @return
*/
public static org.apache.flink.table.api.Schema dropIfExistsColumns(org.apache.flink.table.api.Schema schema, Collection<String> dropCols) {
ArrayList<org.apache.flink.table.api.Schema.UnresolvedColumn> unresolvedColumns = new ArrayList<>(schema.getColumns());
unresolvedColumns.removeIf(c -> dropCols.contains(c.getName()));
org.apache.flink.table.api.Schema.Builder builder =
org.apache.flink.table.api.Schema.newBuilder()
.fromColumns(unresolvedColumns);
if (schema.getPrimaryKey().isPresent()) {
builder.primaryKey(schema.getPrimaryKey().get().getColumnNames());
}
if (schema.getWatermarkSpecs() != null) {
schema.getWatermarkSpecs().forEach(w -> builder.watermark(w.getColumnName(), w.getWatermarkExpression()));
}
org.apache.flink.table.api.Schema resultSchema = builder.build();
return resultSchema;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ public void testCreateAndGetHoodieTable(HoodieTableType tableType) throws Except
.map(f -> f.getName() + ":" + f.getType())
.collect(Collectors.joining(","));
assertEquals("par1:string", partitionSchema);

String avroSchemaStr = hiveTable.getParameters().get("spark.sql.sources.schema.part.0");
String expectAvroSchemaStr =
"{\"type\":\"struct\",\"fields\":[{\"name\":\"_hoodie_commit_time\",\"type\":\"string\",\"nullable\":true,\"metadata\":{}},{\"name\":\"_hoodie_commit_seqno\",\"type\":\"string\",\"nullable\":true,\"metadata\":{}},{\"name\":\"_hoodie_record_key\",\"type\":\"string\",\"nullable\":true,\"metadata\":{}},{\"name\":\"_hoodie_partition_path\",\"type\":\"string\",\"nullable\":true,\"metadata\":{}},{\"name\":\"_hoodie_file_name\",\"type\":\"string\",\"nullable\":true,\"metadata\":{}},{\"name\":\"uuid\",\"type\":\"integer\",\"nullable\":false,\"metadata\":{}},{\"name\":\"name\",\"type\":\"string\",\"nullable\":true,\"metadata\":{}},{\"name\":\"age\",\"type\":\"integer\",\"nullable\":true,\"metadata\":{}},{\"name\":\"ts\",\"type\":\"long\",\"nullable\":true,\"metadata\":{}},{\"name\":\"par1\",\"type\":\"string\",\"nullable\":true,\"metadata\":{}}]}";
assertEquals(avroSchemaStr, expectAvroSchemaStr);

// validate catalog table
CatalogBaseTable table1 = hoodieCatalog.getTable(tablePath);
assertEquals("hudi", table1.getOptions().get(CONNECTOR.key()));
Expand Down