-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-298] Fix issue with incorrect column mapping casusing bad data, during on-the-fly merge of Real Time tables #956
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
| import org.apache.avro.generic.GenericRecord; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants; | ||
| import org.apache.hadoop.hive.serde2.ColumnProjectionUtils; | ||
| import org.apache.hadoop.hive.serde2.io.DoubleWritable; | ||
| import org.apache.hadoop.io.ArrayWritable; | ||
|
|
@@ -89,13 +90,14 @@ public abstract class AbstractRealtimeRecordReader { | |
| // Schema handles | ||
| private Schema readerSchema; | ||
| private Schema writerSchema; | ||
| private Schema hiveSchema; | ||
|
|
||
| public AbstractRealtimeRecordReader(HoodieRealtimeFileSplit split, JobConf job) { | ||
| this.split = split; | ||
| this.jobConf = job; | ||
| LOG.info("cfg ==> " + job.get(ColumnProjectionUtils.READ_COLUMN_NAMES_CONF_STR)); | ||
| LOG.info("columnIds ==> " + job.get(ColumnProjectionUtils.READ_COLUMN_IDS_CONF_STR)); | ||
| LOG.info("partitioningColumns ==> " + job.get("partition_columns", "")); | ||
| LOG.info("partitioningColumns ==> " + job.get(hive_metastoreConstants.META_TABLE_PARTITION_COLUMNS, "")); | ||
| try { | ||
| this.usesCustomPayload = usesCustomPayload(); | ||
| LOG.info("usesCustomPayload ==> " + this.usesCustomPayload); | ||
|
|
@@ -179,7 +181,8 @@ private static List<String> orderFields(String fieldNameCsv, String fieldOrderCs | |
| /** | ||
| * Generate a reader schema off the provided writeSchema, to just project out the provided columns | ||
| */ | ||
| public static Schema generateProjectionSchema(Schema writeSchema, List<String> fieldNames) { | ||
| public static Schema generateProjectionSchema(Schema writeSchema, Map<String, Field> schemaFieldsMap, | ||
| List<String> fieldNames) { | ||
| /** | ||
| * Avro & Presto field names seems to be case sensitive (support fields differing only in case) whereas | ||
| * Hive/Impala/SparkSQL(default) are case-insensitive. Spark allows this to be configurable using | ||
|
|
@@ -191,8 +194,6 @@ public static Schema generateProjectionSchema(Schema writeSchema, List<String> f | |
| * | ||
| */ | ||
| List<Schema.Field> projectedFields = new ArrayList<>(); | ||
| Map<String, Schema.Field> schemaFieldsMap = writeSchema.getFields().stream() | ||
| .map(r -> Pair.of(r.name().toLowerCase(), r)).collect(Collectors.toMap(Pair::getLeft, Pair::getRight)); | ||
| for (String fn : fieldNames) { | ||
| Schema.Field field = schemaFieldsMap.get(fn.toLowerCase()); | ||
| if (field == null) { | ||
|
|
@@ -209,6 +210,11 @@ public static Schema generateProjectionSchema(Schema writeSchema, List<String> f | |
| return projectedSchema; | ||
| } | ||
|
|
||
| public static Map<String, Field> getNameToFieldMap(Schema schema) { | ||
| return schema.getFields().stream().map(r -> Pair.of(r.name().toLowerCase(), r)) | ||
| .collect(Collectors.toMap(Pair::getLeft, Pair::getRight)); | ||
| } | ||
|
|
||
| /** | ||
| * Convert the projected read from delta record into an array writable | ||
| */ | ||
|
|
@@ -321,20 +327,48 @@ private void init() throws IOException { | |
| LOG.debug("Writer Schema From Log => " + writerSchema.getFields()); | ||
| } | ||
| // Add partitioning fields to writer schema for resulting row to contain null values for these fields | ||
| String partitionFields = jobConf.get("partition_columns", ""); | ||
| String partitionFields = jobConf.get(hive_metastoreConstants.META_TABLE_PARTITION_COLUMNS, ""); | ||
| List<String> partitioningFields = | ||
| partitionFields.length() > 0 ? Arrays.stream(partitionFields.split(",")).collect(Collectors.toList()) | ||
| : new ArrayList<>(); | ||
| writerSchema = addPartitionFields(writerSchema, partitioningFields); | ||
| List<String> projectionFields = orderFields(jobConf.get(ColumnProjectionUtils.READ_COLUMN_NAMES_CONF_STR), | ||
| jobConf.get(ColumnProjectionUtils.READ_COLUMN_IDS_CONF_STR), partitioningFields); | ||
|
|
||
| Map<String, Field> schemaFieldsMap = getNameToFieldMap(writerSchema); | ||
| hiveSchema = constructHiveOrderedSchema(writerSchema, schemaFieldsMap); | ||
| // TODO(vc): In the future, the reader schema should be updated based on log files & be able | ||
| // to null out fields not present before | ||
| readerSchema = generateProjectionSchema(writerSchema, projectionFields); | ||
|
|
||
| readerSchema = generateProjectionSchema(writerSchema, schemaFieldsMap, projectionFields); | ||
| LOG.info(String.format("About to read compacted logs %s for base split %s, projecting cols %s", | ||
| split.getDeltaFilePaths(), split.getPath(), projectionFields)); | ||
| } | ||
|
|
||
| private Schema constructHiveOrderedSchema(Schema writerSchema, Map<String, Field> schemaFieldsMap) { | ||
| // Get all column names of hive table | ||
| String hiveColumnString = jobConf.get(hive_metastoreConstants.META_TABLE_COLUMNS); | ||
| String[] hiveColumns = hiveColumnString.split(","); | ||
| List<Field> hiveSchemaFields = new ArrayList<>(); | ||
|
|
||
| for (String columnName : hiveColumns) { | ||
| Field field = schemaFieldsMap.get(columnName.toLowerCase()); | ||
|
|
||
| if (field != null) { | ||
| hiveSchemaFields.add(new Schema.Field(field.name(), field.schema(), field.doc(), field.defaultValue())); | ||
| } else { | ||
| // Hive has some extra virtual columns like BLOCK__OFFSET__INSIDE__FILE which do not exist in table schema. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :) hive |
||
| // They will get skipped as they won't be found in the original schema. | ||
| LOG.debug("Skipping Hive Column => " + columnName); | ||
| } | ||
| } | ||
|
|
||
| Schema hiveSchema = Schema.createRecord(writerSchema.getName(), writerSchema.getDoc(), writerSchema.getNamespace(), | ||
| writerSchema.isError()); | ||
| hiveSchema.setFields(hiveSchemaFields); | ||
| return hiveSchema; | ||
| } | ||
|
|
||
| public Schema getReaderSchema() { | ||
| return readerSchema; | ||
| } | ||
|
|
@@ -343,6 +377,10 @@ public Schema getWriterSchema() { | |
| return writerSchema; | ||
| } | ||
|
|
||
| public Schema getHiveSchema() { | ||
| return hiveSchema; | ||
| } | ||
|
|
||
| public long getMaxCompactionMemoryInBytes() { | ||
| // jobConf.getMemoryForMapTask() returns in MB | ||
| return (long) Math | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need
toLowerCase(). avro is case sensitive?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep looks like Avro is case sensitive. https://avro.apache.org/docs/1.8.2/spec.html#names
Possibly thats also the reason why this code was doing
lowercaseconversion in the first place:https://github.com/apache/incubator-hudi/blob/master/hudi-hadoop-mr/src/main/java/org/apache/hudi/hadoop/realtime/AbstractRealtimeRecordReader.java#L195
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand this is not related to the change here. can you just add a todo to consider case sensitivity down the line or may be file a JIRA. May be we need to understand what happens when fields just differ in case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created a Jira https://issues.apache.org/jira/browse/HUDI-303 where we can follow up on.