voonhous commented on code in PR #18961: URL: https://github.com/apache/hudi/pull/18961#discussion_r3820919237
########## hudi-client/hudi-spark-client/src/test/java/org/apache/hudi/io/storage/row/TestVariantShreddingInferenceInternalRowFileWriter.java: ########## @@ -0,0 +1,371 @@ +/* + * 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.io.storage.row; + +import org.apache.hudi.common.avro.VariantShreddingSchemaInferrer; +import org.apache.hudi.common.avro.VariantShreddingSchemaInferrer.VariantSample; +import org.apache.hudi.common.schema.HoodieSchema; +import org.apache.hudi.common.schema.HoodieSchemaType; +import org.apache.hudi.common.util.DefaultSizeEstimator; +import org.apache.hudi.core.io.storage.VariantShreddingInferenceFileWriter; + +import org.apache.spark.sql.catalyst.InternalRow; +import org.apache.spark.sql.catalyst.expressions.GenericInternalRow; +import org.apache.spark.sql.catalyst.expressions.UnsafeRow; +import org.apache.spark.sql.types.DataTypes; +import org.apache.spark.sql.types.StructType; +import org.apache.spark.unsafe.types.UTF8String; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static java.util.Collections.singletonList; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * The row-writer sibling of {@code TestVariantShreddingInferenceFileWriter}. The decorator is + * driven with ordinals of -1 (the column absent from the row's StructType), which is the + * sampling branch that needs no Spark adapter on the classpath; sample extraction itself goes + * through {@code SparkAdapter.extractVariantBinary} and is covered by the functional tests. + */ +public class TestVariantShreddingInferenceInternalRowFileWriter { + + private static final int[] ABSENT_COLUMN = {-1}; + + private static InternalRow row(long id) { + return new GenericInternalRow(new Object[] {id}); + } + + /** A row of about a kilobyte, so byte-cap arithmetic on the estimator is not dominated by rounding. */ + private static InternalRow wideRow(long id) { + return new GenericInternalRow(new Object[] {id, UTF8String.fromString(new String(new char[1024]).replace('\0', 'x'))}); + } + + /** A 16-byte UnsafeRow (8-byte null bitset + one long), so byte-cap arithmetic is exact. */ + private static UnsafeRow unsafeRow(long id) { + UnsafeRow row = new UnsafeRow(1); + byte[] buffer = new byte[16]; + row.pointTo(buffer, 16); + row.setLong(0, id); + return row; + } + + /** Records every call so replay order and call kinds can be asserted. */ + private static class RecordingRowWriter implements HoodieInternalRowFileWriter { + private final List<String> calls = new ArrayList<>(); + private final List<InternalRow> rows = new ArrayList<>(); + private int closeCount = 0; + private IOException failWriteWith; + + @Override + public boolean canWrite() { + return true; + } + + @Override + public void writeRow(UTF8String key, InternalRow row) throws IOException { + failIfConfigured(); + calls.add("keyed:" + key); + rows.add(row); + } + + @Override + public void writeRow(InternalRow row) throws IOException { + failIfConfigured(); + calls.add("plain:" + row.getLong(0)); + rows.add(row); + } + + @Override + public void close() { Review Comment: Ported `failCloseWith` and `testThrowingDelegateCloseSurfacesAndIsNotRetried` to the row suite in 0c90b2acb1af; `RecordingRowWriter.close()` now declares `throws IOException`. -- 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]
