Skip to content
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

[HUDI-8582] Adding support for nested fields with col stats #12623

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -438,9 +438,8 @@ private void processAppendResult(AppendResult result, List<HoodieRecord> recordL
.getColumnsToIndex(hoodieTable.getMetaClient().getTableConfig(),
config.getMetadataConfig(), Lazy.eagerly(Option.of(writeSchemaWithMetaFields)),
Option.of(this.recordMerger.getRecordType())));
final List<Schema.Field> fieldsToIndex = writeSchemaWithMetaFields.getFields().stream()
.filter(field -> columnsToIndexSet.contains(field.name()))
.collect(Collectors.toList());
final List<Pair<String, Schema.Field>> fieldsToIndex = columnsToIndexSet.stream()
.map(fieldName -> HoodieTableMetadataUtil.getSchemaForField(writeSchemaWithMetaFields, fieldName)).collect(Collectors.toList());
try {
Map<String, HoodieColumnRangeMetadata<Comparable>> columnRangeMetadataMap =
collectColumnRangeMetadata(recordList, fieldsToIndex, stat.getPath(), writeSchemaWithMetaFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
import org.apache.hudi.common.table.timeline.TimelineMetadataUtils;
import org.apache.hudi.common.table.view.HoodieTableFileSystemView;
import org.apache.hudi.common.util.CollectionUtils;
import org.apache.hudi.common.util.Either;
import org.apache.hudi.common.util.FileFormatUtils;
import org.apache.hudi.common.util.FileIOUtils;
import org.apache.hudi.common.util.HoodieRecordUtils;
Expand Down Expand Up @@ -226,7 +225,7 @@ public static boolean isFilesPartitionAvailable(HoodieTableMetaClient metaClient
* the collection of provided records
*/
public static Map<String, HoodieColumnRangeMetadata<Comparable>> collectColumnRangeMetadata(
List<HoodieRecord> records, List<Schema.Field> targetFields, String filePath, Schema recordSchema) {
List<HoodieRecord> records, List<Pair<String, Schema.Field>> targetFields, String filePath, Schema recordSchema) {
// Helper class to calculate column stats
class ColumnStats {
Object minValue;
Expand All @@ -242,18 +241,18 @@ class ColumnStats {
records.forEach((record) -> {
// For each column (field) we have to index update corresponding column stats
// with the values from this record
targetFields.forEach(field -> {
ColumnStats colStats = allColumnStats.computeIfAbsent(field.name(), ignored -> new ColumnStats());
Schema fieldSchema = getNestedFieldSchemaFromWriteSchema(recordSchema, field.name());
targetFields.forEach(fieldNameFieldPair -> {
ColumnStats colStats = allColumnStats.computeIfAbsent(fieldNameFieldPair.getKey(), ignored -> new ColumnStats());
Schema fieldSchema = fieldNameFieldPair.getValue().schema();
Object fieldValue;
if (record.getRecordType() == HoodieRecordType.AVRO) {
fieldValue = HoodieAvroUtils.getRecordColumnValues(record, new String[]{field.name()}, recordSchema, false)[0];
fieldValue = HoodieAvroUtils.getRecordColumnValues(record, new String[]{fieldNameFieldPair.getKey()}, recordSchema, false)[0];
if (fieldSchema.getType() == Schema.Type.INT && fieldSchema.getLogicalType() != null && fieldSchema.getLogicalType() == LogicalTypes.date()) {
fieldValue = java.sql.Date.valueOf(fieldValue.toString());
}

} else if (record.getRecordType() == HoodieRecordType.SPARK) {
fieldValue = record.getColumnValues(recordSchema, new String[]{field.name()}, false)[0];
fieldValue = record.getColumnValues(recordSchema, new String[]{fieldNameFieldPair.getKey()}, false)[0];
if (fieldSchema.getType() == Schema.Type.INT && fieldSchema.getLogicalType() != null && fieldSchema.getLogicalType() == LogicalTypes.date()) {
fieldValue = java.sql.Date.valueOf(LocalDate.ofEpochDay((Integer) fieldValue).toString());
}
Expand All @@ -279,13 +278,13 @@ class ColumnStats {
});

Stream<HoodieColumnRangeMetadata<Comparable>> hoodieColumnRangeMetadataStream =
targetFields.stream().map(field -> {
ColumnStats colStats = allColumnStats.get(field.name());
targetFields.stream().map(fieldNameFieldPair -> {
ColumnStats colStats = allColumnStats.get(fieldNameFieldPair.getKey());
HoodieColumnRangeMetadata<Comparable> hcrm = HoodieColumnRangeMetadata.<Comparable>create(
filePath,
field.name(),
colStats == null ? null : coerceToComparable(field.schema(), colStats.minValue),
colStats == null ? null : coerceToComparable(field.schema(), colStats.maxValue),
fieldNameFieldPair.getKey(),
colStats == null ? null : coerceToComparable(fieldNameFieldPair.getValue().schema(), colStats.minValue),
colStats == null ? null : coerceToComparable(fieldNameFieldPair.getValue().schema(), colStats.maxValue),
colStats == null ? 0L : colStats.nullCount,
colStats == null ? 0L : colStats.valueCount,
// NOTE: Size and compressed size statistics are set to 0 to make sure we're not
Expand Down Expand Up @@ -1501,32 +1500,25 @@ public static List<String> getColumnsToIndex(HoodieCommitMetadata commitMetadata
@VisibleForTesting
public static List<String> getColumnsToIndex(HoodieTableConfig tableConfig,
HoodieMetadataConfig metadataConfig,
List<String> columnNames) {
return getColumnsToIndex(tableConfig, metadataConfig, Either.left(columnNames), false, Option.empty());
}

@VisibleForTesting
public static List<String> getColumnsToIndex(HoodieTableConfig tableConfig,
HoodieMetadataConfig metadataConfig,
Lazy<Option<Schema>> tableSchema,
Lazy<Option<Schema>> tableSchemaLazyOpt,
Option<HoodieRecordType> recordType) {
return getColumnsToIndex(tableConfig, metadataConfig, Either.right(tableSchema), false, recordType);
return getColumnsToIndexInternal(tableConfig, metadataConfig, tableSchemaLazyOpt, false, recordType);
}

@VisibleForTesting
public static List<String> getColumnsToIndex(HoodieTableConfig tableConfig,
HoodieMetadataConfig metadataConfig,
Lazy<Option<Schema>> tableSchema,
Lazy<Option<Schema>> tableSchemaLazyOpt,
boolean isTableInitializing) {
return getColumnsToIndex(tableConfig, metadataConfig, Either.right(tableSchema), isTableInitializing, Option.empty());
return getColumnsToIndexInternal(tableConfig, metadataConfig, tableSchemaLazyOpt, isTableInitializing, Option.empty());
}

private static List<String> getColumnsToIndex(HoodieTableConfig tableConfig,
private static List<String> getColumnsToIndexInternal(HoodieTableConfig tableConfig,
HoodieMetadataConfig metadataConfig,
Either<List<String>, Lazy<Option<Schema>>> tableSchema,
boolean isTableInitializing,
Option<HoodieRecordType> recordType) {
Stream<String> columnsToIndexWithoutRequiredMetas = getColumnsToIndexWithoutRequiredMetaFields(metadataConfig, tableSchema, isTableInitializing, recordType);
Lazy<Option<Schema>> tableSchemaLazyOpt,
boolean isTableInitializing,
Option<HoodieRecordType> recordType) {
Stream<String> columnsToIndexWithoutRequiredMetas = getColumnsToIndexWithoutRequiredMetaFields(metadataConfig, tableSchemaLazyOpt, isTableInitializing, recordType);
if (!tableConfig.populateMetaFields()) {
return columnsToIndexWithoutRequiredMetas.collect(Collectors.toList());
}
Expand All @@ -1541,39 +1533,38 @@ private static List<String> getColumnsToIndex(HoodieTableConfig tableConfig,
* required meta cols
*
* @param metadataConfig metadata config
* @param tableSchema either a list of the columns in the table, or a lazy option of the table schema
* @param tableSchemaLazyOpt lazy option of the table schema
* @param isTableInitializing true if table is being initialized.
* @param recordType Option of record type. Used to determine which types are valid to index
* @return list of columns that should be indexed
*/
private static Stream<String> getColumnsToIndexWithoutRequiredMetaFields(HoodieMetadataConfig metadataConfig,
Either<List<String>, Lazy<Option<Schema>>> tableSchema,
Lazy<Option<Schema>> tableSchemaLazyOpt,
boolean isTableInitializing,
Option<HoodieRecordType> recordType) {
List<String> columnsToIndex = metadataConfig.getColumnsEnabledForColumnStatsIndex();
if (!columnsToIndex.isEmpty()) {
if (isTableInitializing) {
return columnsToIndex.stream();
}
// filter for top level fields here.
List<String> topLevelEligibleFields = tableSchema.isLeft() ? tableSchema.asLeft() :
(tableSchema.asRight().get().map(schema -> schema.getFields().stream()
.filter(field -> isColumnTypeSupported(field.schema(), recordType))
.map(field -> field.name()).collect(toList()))
.orElse(new ArrayList<String>()));
return columnsToIndex.stream().filter(fieldName -> !META_COL_SET_TO_INDEX.contains(fieldName) && !fieldName.contains(".")
&& (topLevelEligibleFields.isEmpty() || topLevelEligibleFields.contains(fieldName)));
}
if (tableSchema.isLeft()) {
return getFirstNFieldNames(tableSchema.asLeft().stream(), metadataConfig.maxColumnsToIndexForColStats());
// filter for eligible fields
Option<Schema> tableSchema = tableSchemaLazyOpt.get();
return columnsToIndex.stream().filter(fieldName -> !META_COL_SET_TO_INDEX.contains(fieldName))
.filter(fieldName -> {
if (tableSchema.isPresent()) {
return isColumnTypeSupported(getSchemaForField(tableSchema.get(), fieldName).getValue().schema(), recordType);
} else {
return true;
}
});
}
// if not overridden
if (tableSchemaLazyOpt.get().isPresent()) {
return tableSchemaLazyOpt.get().map(schema -> getFirstNSupportedFieldNames(schema, metadataConfig.maxColumnsToIndexForColStats(), recordType)).orElse(Stream.empty());
} else if (isTableInitializing) {
return Stream.empty();
} else {
if (tableSchema.asRight().get().isPresent()) {
return tableSchema.asRight().get().map(schema -> getFirstNSupportedFieldNames(schema, metadataConfig.maxColumnsToIndexForColStats(), recordType)).orElse(Stream.empty());
} else if (isTableInitializing) {
return Stream.empty();
} else {
throw new HoodieMetadataException("Cannot initialize col stats with empty list of cols");
}
throw new HoodieMetadataException("Cannot initialize col stats with empty list of cols");
}
}

Expand All @@ -1586,6 +1577,25 @@ private static Stream<String> getFirstNFieldNames(Stream<String> fieldNames, int
return fieldNames.filter(fieldName -> !HOODIE_META_COLUMNS_WITH_OPERATION.contains(fieldName)).limit(n);
}

@VisibleForTesting
public static Pair<String, Schema.Field> getSchemaForField(Schema schema, String fieldName) {
return getSchemaForField(schema, fieldName, StringUtils.EMPTY_STRING);
}

@VisibleForTesting
public static Pair<String, Schema.Field> getSchemaForField(Schema schema, String fieldName, String prefix) {
if (!fieldName.contains(".")) {
return Pair.of(prefix + fieldName, schema.getField(fieldName));
} else {
int rootFieldIndex = fieldName.indexOf(".");
Schema.Field rootField = schema.getField(fieldName.substring(0, rootFieldIndex));
if (rootField == null) {
throw new HoodieException("Failed to find " + fieldName + " in the table schema ");
}
return getSchemaForField(rootField.schema(), fieldName.substring(rootFieldIndex + 1), prefix + fieldName.substring(0, rootFieldIndex + 1));
}
}

private static Stream<HoodieRecord> translateWriteStatToColumnStats(HoodieWriteStat writeStat,
HoodieTableMetaClient datasetMetaClient,
List<String> columnsToIndex) {
Expand Down Expand Up @@ -1663,8 +1673,7 @@ public static List<HoodieColumnRangeMetadata<Comparable>> getLogFileColumnRangeM
List<String> columnsToIndex, Option<Schema> writerSchemaOpt,
int maxBufferSize) throws IOException {
if (writerSchemaOpt.isPresent()) {
List<Schema.Field> fieldsToIndex = writerSchemaOpt.get().getFields().stream()
.filter(field -> columnsToIndex.contains(field.name()))
List<Pair<String, Schema.Field>> fieldsToIndex = columnsToIndex.stream().map(fieldName -> getSchemaForField(writerSchemaOpt.get(), fieldName))
.collect(Collectors.toList());
// read log file records without merging
List<HoodieRecord> records = new ArrayList<>();
Expand Down
Loading
Loading