yihua commented on code in PR #10167: URL: https://github.com/apache/hudi/pull/10167#discussion_r1430200489
########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/common/table/read/TestSpark35RecordPositionMetadataColumn.scala: ########## @@ -0,0 +1,112 @@ +/* + * 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.read + +import org.apache.hadoop.conf.Configuration +import org.apache.hudi.SparkAdapterSupport.sparkAdapter +import org.apache.hudi.common.config.HoodieStorageConfig +import org.apache.hudi.common.model.HoodieTableType +import org.apache.hudi.common.testutils.HoodieTestTable +import org.apache.hudi.config.HoodieWriteConfig +import org.apache.hudi.testutils.SparkClientFunctionalTestHarness +import org.apache.hudi.util.CloseableInternalRowIterator +import org.apache.hudi.{DataSourceWriteOptions, HoodieSparkUtils} +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.execution.datasources.parquet.{HoodieFileGroupReaderBasedParquetFileFormat, ParquetFileFormat} +import org.apache.spark.sql.types.{IntegerType, LongType, StringType, StructField, StructType} +import org.junit.jupiter.api.Assertions.{assertEquals, assertTrue} +import org.junit.jupiter.api.Test + +class TestSpark35RecordPositionMetadataColumn extends SparkClientFunctionalTestHarness { + private val HOOD_FORMAT = "hudi" Review Comment: ```suggestion ``` ########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/common/table/read/TestSpark35RecordPositionMetadataColumn.scala: ########## @@ -0,0 +1,112 @@ +/* + * 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.read + +import org.apache.hadoop.conf.Configuration +import org.apache.hudi.SparkAdapterSupport.sparkAdapter +import org.apache.hudi.common.config.HoodieStorageConfig +import org.apache.hudi.common.model.HoodieTableType +import org.apache.hudi.common.testutils.HoodieTestTable +import org.apache.hudi.config.HoodieWriteConfig +import org.apache.hudi.testutils.SparkClientFunctionalTestHarness +import org.apache.hudi.util.CloseableInternalRowIterator +import org.apache.hudi.{DataSourceWriteOptions, HoodieSparkUtils} +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.execution.datasources.parquet.{HoodieFileGroupReaderBasedParquetFileFormat, ParquetFileFormat} +import org.apache.spark.sql.types.{IntegerType, LongType, StringType, StructField, StructType} +import org.junit.jupiter.api.Assertions.{assertEquals, assertTrue} +import org.junit.jupiter.api.Test + +class TestSpark35RecordPositionMetadataColumn extends SparkClientFunctionalTestHarness { + private val HOOD_FORMAT = "hudi" + private val PARQUET_FORMAT = "parquet" + private val ROW_INDEX_COLUMN = "_tmp_metadata_row_index" + private val SPARK_MERGER = "org.apache.hudi.HoodieSparkRecordMerger" + + @Test + def testRecordPositionColumn(): Unit = { + val _spark = spark + import _spark.implicits._ + + val userToCountryDF = Seq( + (1, "US", "1000"), + (2, "US", "1000"), + (3, "China", "1000"), + (4, "Singapore", "1000") + ).toDF("userid", "country", "ts") + val userToCountryTblPath = s"$basePath/user_to_country_table" + + userToCountryDF.write.format(HOOD_FORMAT) Review Comment: ```suggestion userToCountryDF.write.format("hudi") ``` ########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/common/table/read/TestIfRecordPositionColumnWorkForSpark3_5.scala: ########## @@ -0,0 +1,105 @@ +/* + * 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.read + +import org.apache.hadoop.conf.Configuration +import org.apache.hudi.SparkAdapterSupport.sparkAdapter +import org.apache.hudi.common.config.HoodieStorageConfig +import org.apache.hudi.common.testutils.HoodieTestTable +import org.apache.hudi.config.HoodieWriteConfig +import org.apache.hudi.testutils.SparkClientFunctionalTestHarness +import org.apache.hudi.util.CloseableInternalRowIterator +import org.apache.hudi.{DataSourceWriteOptions, HoodieSparkUtils} +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.execution.datasources.parquet.{HoodieFileGroupReaderBasedParquetFileFormat, ParquetFileFormat} +import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType} +import org.junit.jupiter.api.Assertions.{assertEquals, assertTrue} +import org.junit.jupiter.api.Test + +class TestIfRecordPositionColumnWorkForSpark3_5 extends SparkClientFunctionalTestHarness { + private val HOOD_FORMAT = "org.apache.hudi" + private val MOR_TABLE_TYPE = "MERGE_ON_READ" + private val PARQUET_FORMAT = "parquet" + private val ROW_INDEX_COLUMN = "_tmp_metadata_row_index" + private val SPARK_MERGER = "org.apache.hudi.HoodieSparkRecordMerger" + + @Test + def testConfirmIfRowIndexColumnWorks(): Unit = { + val _spark = spark + import _spark.implicits._ + + val userToCountryDF = Seq( + (1, "US", "1000"), + (2, "US", "1000"), + (3, "China", "1000"), + (4, "Singapore", "1000") + ).toDF("userid", "country", "ts") + val userToCountryTblPath = s"$basePath/user_to_country_table" + + userToCountryDF.write.format(HOOD_FORMAT) + .option(DataSourceWriteOptions.RECORDKEY_FIELD.key, "userid") + .option(DataSourceWriteOptions.PRECOMBINE_FIELD.key, "ts") + .option(HoodieWriteConfig.TBL_NAME.key, "user_to_country") + .option(HoodieWriteConfig.RECORD_MERGER_IMPLS.key, SPARK_MERGER) + .option(HoodieWriteConfig.WRITE_RECORD_POSITIONS.key, "true") + .option(HoodieStorageConfig.LOGFILE_DATA_BLOCK_FORMAT.key, PARQUET_FORMAT) + .option(DataSourceWriteOptions.TABLE_TYPE.key(), MOR_TABLE_TYPE) + .save(userToCountryTblPath) + + val dataSchema = new StructType( + Array( + StructField("userid", IntegerType, nullable = false), + StructField("country", StringType, nullable = false), + StructField("ts", StringType, nullable = false) + ) + ) + val requiredSchema = HoodieFileGroupReaderBasedParquetFileFormat.getAppliedRequiredSchema( + dataSchema, shouldUseRecordPosition = true, ROW_INDEX_COLUMN) + if (HoodieSparkUtils.gteqSpark3_5) { + assertEquals(4, requiredSchema.fields.length) + } + + _spark.conf.set("spark.sql.parquet.enableVectorizedReader", "false") Review Comment: We need to make this automatic. ########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/common/table/read/TestSpark35RecordPositionMetadataColumn.scala: ########## @@ -0,0 +1,112 @@ +/* + * 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.read + +import org.apache.hadoop.conf.Configuration +import org.apache.hudi.SparkAdapterSupport.sparkAdapter +import org.apache.hudi.common.config.HoodieStorageConfig +import org.apache.hudi.common.model.HoodieTableType +import org.apache.hudi.common.testutils.HoodieTestTable +import org.apache.hudi.config.HoodieWriteConfig +import org.apache.hudi.testutils.SparkClientFunctionalTestHarness +import org.apache.hudi.util.CloseableInternalRowIterator +import org.apache.hudi.{DataSourceWriteOptions, HoodieSparkUtils} +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.execution.datasources.parquet.{HoodieFileGroupReaderBasedParquetFileFormat, ParquetFileFormat} +import org.apache.spark.sql.types.{IntegerType, LongType, StringType, StructField, StructType} +import org.junit.jupiter.api.Assertions.{assertEquals, assertTrue} +import org.junit.jupiter.api.Test + +class TestSpark35RecordPositionMetadataColumn extends SparkClientFunctionalTestHarness { + private val HOOD_FORMAT = "hudi" + private val PARQUET_FORMAT = "parquet" + private val ROW_INDEX_COLUMN = "_tmp_metadata_row_index" + private val SPARK_MERGER = "org.apache.hudi.HoodieSparkRecordMerger" + + @Test + def testRecordPositionColumn(): Unit = { + val _spark = spark + import _spark.implicits._ + + val userToCountryDF = Seq( + (1, "US", "1000"), + (2, "US", "1000"), + (3, "China", "1000"), + (4, "Singapore", "1000") + ).toDF("userid", "country", "ts") + val userToCountryTblPath = s"$basePath/user_to_country_table" + + userToCountryDF.write.format(HOOD_FORMAT) + .option(DataSourceWriteOptions.RECORDKEY_FIELD.key, "userid") + .option(DataSourceWriteOptions.PRECOMBINE_FIELD.key, "ts") + .option(HoodieWriteConfig.TBL_NAME.key, "user_to_country") + .option(HoodieWriteConfig.RECORD_MERGER_IMPLS.key, SPARK_MERGER) + .option(HoodieWriteConfig.WRITE_RECORD_POSITIONS.key, "true") + .option(HoodieStorageConfig.LOGFILE_DATA_BLOCK_FORMAT.key, PARQUET_FORMAT) + .option(DataSourceWriteOptions.TABLE_TYPE.key(), HoodieTableType.MERGE_ON_READ.name()) + .save(userToCountryTblPath) + + val dataSchema = new StructType( + Array( + StructField("userid", IntegerType, nullable = false), + StructField("country", StringType, nullable = false), + StructField("ts", StringType, nullable = false) + ) + ) + val requiredSchema = HoodieFileGroupReaderBasedParquetFileFormat + .getAppliedRequiredSchema( + dataSchema, + shouldUseRecordPosition = true, + ROW_INDEX_COLUMN) + if (HoodieSparkUtils.gteqSpark3_5) { + assertEquals(4, requiredSchema.fields.length) + assertEquals( + "StructField(_tmp_metadata_row_index,LongType,false)", + requiredSchema.fields(3).toString) + } + + _spark.conf.set("spark.sql.parquet.enableVectorizedReader", "false") + val metaClient = getHoodieMetaClient( + spark().sparkContext.hadoopConfiguration, userToCountryTblPath) + val fileReader = new ParquetFileFormat().buildReaderWithPartitionValues( + spark(), + dataSchema, + StructType(Nil), + requiredSchema, + Nil, + Map.empty, + new Configuration(spark().sparkContext.hadoopConfiguration)) + val allBaseFiles = HoodieTestTable.of(metaClient).listAllBaseFiles + assertTrue(allBaseFiles.nonEmpty) + + if (HoodieSparkUtils.gteqSpark3_5) { + val fileInfo = sparkAdapter.getSparkPartitionedFileUtils + .createPartitionedFile(InternalRow.empty, allBaseFiles.head.getPath, 0, allBaseFiles.head.getLen) + val iterator = new CloseableInternalRowIterator(fileReader.apply(fileInfo)) + var rowIndices: Set[Long] = Set() + while (iterator.hasNext) { + val row = iterator.next() + rowIndices += row.getLong(3) + } + val expectedRowIndices: Set[Long] = Set(0L, 1L, 2L, 3L) Review Comment: Could you add another test case where the some rows are filtered out (e.g., 0 and 1 are filtered out) and the row positions returned are still correct? -- 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]
