the-other-tim-brown commented on code in PR #9743: URL: https://github.com/apache/hudi/pull/9743#discussion_r1349818445
########## hudi-client/hudi-client-common/src/main/java/org/apache/hudi/common/table/log/CachingIterator.java: ########## @@ -0,0 +1,41 @@ +/* + * 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.hudi.common.table.log; + +import java.util.Iterator; + +public abstract class CachingIterator<T> implements Iterator<T> { Review Comment: Can you add a javadoc explaining what the CachingIterator should be used for? ########## hudi-client/hudi-client-common/src/main/java/org/apache/hudi/common/table/log/HoodieFileSliceReader.java: ########## @@ -19,47 +19,80 @@ package org.apache.hudi.common.table.log; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.model.HoodiePayloadProps; import org.apache.hudi.common.model.HoodieRecord; +import org.apache.hudi.common.model.HoodieRecordMerger; import org.apache.hudi.common.util.Option; import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.exception.HoodieClusteringException; import org.apache.hudi.io.storage.HoodieFileReader; import org.apache.avro.Schema; import java.io.IOException; import java.util.Iterator; +import java.util.Map; import java.util.Properties; -/** - * Reads records from base file and merges any updates from log files and provides iterable over all records in the file slice. - */ -public class HoodieFileSliceReader<T> implements Iterator<HoodieRecord<T>> { +public class HoodieFileSliceReader<T> extends LogFileIterator<T> { + private Option<Iterator<HoodieRecord>> baseFileIterator; + private HoodieMergedLogRecordScanner scanner; + private Schema schema; + private Properties props; - private final Iterator<HoodieRecord<T>> recordsIterator; + private TypedProperties payloadProps = new TypedProperties(); + private Option<Pair<String, String>> simpleKeyGenFieldsOpt; + Map<String, HoodieRecord> records; + HoodieRecordMerger merger; - public static HoodieFileSliceReader getFileSliceReader( - Option<HoodieFileReader> baseFileReader, HoodieMergedLogRecordScanner scanner, Schema schema, Properties props, Option<Pair<String, String>> simpleKeyGenFieldsOpt) throws IOException { + public HoodieFileSliceReader(Option<HoodieFileReader> baseFileReader, + HoodieMergedLogRecordScanner scanner, Schema schema, String preCombineField, HoodieRecordMerger merger, + Properties props, Option<Pair<String, String>> simpleKeyGenFieldsOpt) throws IOException { + super(scanner); if (baseFileReader.isPresent()) { - Iterator<HoodieRecord> baseIterator = baseFileReader.get().getRecordIterator(schema); - while (baseIterator.hasNext()) { - scanner.processNextRecord(baseIterator.next().wrapIntoHoodieRecordPayloadWithParams(schema, props, - simpleKeyGenFieldsOpt, scanner.isWithOperationField(), scanner.getPartitionNameOverride(), false, Option.empty())); - } + this.baseFileIterator = Option.of(baseFileReader.get().getRecordIterator(schema)); + } else { + this.baseFileIterator = Option.empty(); } - return new HoodieFileSliceReader(scanner.iterator()); + this.scanner = scanner; + this.schema = schema; + this.merger = merger; + if (preCombineField != null) { + payloadProps.setProperty(HoodiePayloadProps.PAYLOAD_ORDERING_FIELD_PROP_KEY, preCombineField); + } + this.props = props; + this.simpleKeyGenFieldsOpt = simpleKeyGenFieldsOpt; + this.records = scanner.getRecords(); } - private HoodieFileSliceReader(Iterator<HoodieRecord<T>> recordsItr) { - this.recordsIterator = recordsItr; + private Boolean hasNextInternal() { Review Comment: can you add some context on why all this extra logic is needed now? ########## hudi-common/src/main/java/org/apache/hudi/common/table/log/block/HoodieAvroDataBlock.java: ########## @@ -153,11 +156,14 @@ protected <T> ClosableIterator<T> deserializeRecords(HoodieReaderContext<T> read } private static class RecordIterator implements ClosableIterator<IndexedRecord> { + + private final Boolean whichImplementation = false; Review Comment: can we use primitive here as well? ########## hudi-common/src/main/java/org/apache/hudi/avro/AvroCastingGenericRecord.java: ########## @@ -0,0 +1,259 @@ +/* + * 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.hudi.avro; + +import org.apache.hudi.common.util.ValidationUtils; + +import org.apache.avro.AvroRuntimeException; +import org.apache.avro.Schema; +import org.apache.avro.generic.GenericData; +import org.apache.avro.generic.GenericRecord; +import org.apache.avro.generic.IndexedRecord; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.UnaryOperator; + +import static org.apache.avro.Schema.Type.ARRAY; +import static org.apache.avro.Schema.Type.MAP; +import static org.apache.avro.Schema.Type.STRING; +import static org.apache.avro.Schema.Type.UNION; +import static org.apache.hudi.common.util.StringUtils.getUTF8Bytes; + + +/** + * Implementation of avro generic record that uses casting for implicit schema evolution + */ +public class AvroCastingGenericRecord implements GenericRecord { + private final IndexedRecord record; + private final Schema readerSchema; + private final Map<Integer, UnaryOperator<Object>> fieldConverters; + + private AvroCastingGenericRecord(IndexedRecord record, Schema readerSchema, Map<Integer, UnaryOperator<Object>> fieldConverters) { + this.record = record; + this.readerSchema = readerSchema; + this.fieldConverters = fieldConverters; + } + + @Override + public void put(int i, Object v) { + throw new UnsupportedOperationException(); + } + + @Override + public Object get(int i) { + if (fieldConverters.containsKey(i)) { + return fieldConverters.get(i).apply(record.get(i)); + } + return record.get(i); + } + + @Override + public Schema getSchema() { + return readerSchema; + } + + @Override + public void put(String key, Object v) { + Schema.Field field = getSchema().getField(key); + if (field == null) { + throw new AvroRuntimeException("Not a valid schema field: " + key); + } + put(field.pos(), v); + } + + @Override + public Object get(String key) { + Schema.Field field = getSchema().getField(key); + if (field == null) { + throw new AvroRuntimeException("Not a valid schema field: " + key); + } + return get(field.pos()); + } + + + /** + * Avro schema evolution does not support promotion from numbers to string. This function returns true if + * it will be necessary to rewrite the record to support the evolution. + * NOTE: this does not determine whether the schema evolution is valid. It is just trying to find if the schema evolves from + * a number to string, as quick as possible. + */ + public static Boolean recordNeedsRewriteForExtendedAvroSchemaEvolution(Schema writerSchema, Schema readerSchema) { Review Comment: return primitive boolean ########## hudi-client/hudi-client-common/src/main/java/org/apache/hudi/common/table/log/CachingIterator.java: ########## @@ -0,0 +1,41 @@ +/* + * 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.hudi.common.table.log; + +import java.util.Iterator; + +public abstract class CachingIterator<T> implements Iterator<T> { + + protected T nextRecord; + + protected abstract Boolean doHasNext(); Review Comment: Do we intend to return `null` from this method? if not, we should use a primitive to reduce auto-boxing overhead as we return a primitive for `hasNext` -- 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]
