the-other-tim-brown commented on code in PR #18098: URL: https://github.com/apache/hudi/pull/18098#discussion_r3068572520
########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/blob/TestBatchedBlobReader.scala: ########## @@ -0,0 +1,435 @@ +/* + * 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.blob + +import org.apache.hudi.blob.BlobTestHelpers._ +import org.apache.hudi.common.schema.HoodieSchema +import org.apache.hudi.testutils.HoodieClientTestBase + +import org.apache.spark.sql.Row +import org.apache.spark.sql.functions._ +import org.apache.spark.sql.hudi.blob.BatchedBlobReader +import org.apache.spark.sql.types._ +import org.junit.jupiter.api.Assertions._ +import org.junit.jupiter.api.Test + +import java.util.Collections + +/** + * Tests for BatchedByteRangeReader. + * + * These tests verify the batching behavior and effectiveness of the + * BatchedByteRangeReader compared to non-batched approaches. + */ +class TestBatchedBlobReader extends HoodieClientTestBase { + + @Test + def testBasicBatchedRead(): Unit = { + val filePath = createTestFile(tempDir, "basic.bin", 10000) + + // Create input with struct column + val inputDF = sparkSession.createDataFrame(Seq( + (filePath, 0L, 100L), + (filePath, 100L, 100L), + (filePath, 200L, 100L) + )).toDF("external_path", "offset", "length") + .withColumn("data", blobStructCol("data", col("external_path"), col("offset"), col("length"))) + .select("data") + + // Read with batching + val resultDF = BatchedBlobReader.readBatched(inputDF, storageConf) + + // Verify schema + assertTrue(resultDF.columns.contains("data")) + assertEquals(1, resultDF.columns.length) // data + + // Verify results + val results = resultDF.collect() + assertEquals(3, results.length) + + // Check data content + results.zipWithIndex.foreach { case (row, i) => + val data = row.getAs[Array[Byte]]("data") + assertEquals(100, data.length) + + // Verify content matches expected pattern + assertBytesContent(data, expectedOffset = i * 100) + } Review Comment: This is addressed in the latest commit ########## hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/spark/sql/hudi/blob/ScalarFunctions.scala: ########## @@ -0,0 +1,95 @@ +/* + * 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.spark.sql.hudi.blob + +import org.apache.spark.sql.catalyst.FunctionIdentifier +import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionInfo} + +/** + * Registry of scalar functions for Hudi SQL integration. + * + * These functions are registered as built-in functions that can be used + * in SQL queries. They integrate with Spark's function registry and are + * available in both SQL and DataFrame API contexts. + * + * <h3>Function Registration:</h3> + * Functions are registered via [[SparkAdapter.injectScalarFunctions]] which is + * called during [[HoodieSparkSessionExtension]] initialization. + * + * <h3>Adding New Functions:</h3> + * To add a new scalar function: + * <ol> + * <li>Create a marker expression class (extends Unevaluable)</li> + * <li>Add function definition tuple to [[funcs]] below</li> + * <li>Create a logical plan rule to handle the expression</li> + * <li>Register the rule in [[HoodieAnalysis.customPostHocResolutionRules]]</li> + * </ol> + */ +object ScalarFunctions { + + private val READ_BLOB_FUNC_NAME = "read_blob" + + /** + * Function definitions as tuples of: + * <ul> + * <li>FunctionIdentifier - function name</li> + * <li>ExpressionInfo - metadata for DESCRIBE FUNCTION</li> + * <li>Builder function - (Seq[Expression] => Expression)</li> + * </ul> + */ + val funcs: Seq[(FunctionIdentifier, ExpressionInfo, Seq[Expression] => Expression)] = Seq( + ( + FunctionIdentifier(READ_BLOB_FUNC_NAME), + new ExpressionInfo( + classOf[ReadBlobExpression].getCanonicalName, + READ_BLOB_FUNC_NAME, + """ + |Usage: read_blob(blob_column) - Reads blob data from storage + | + |Reads byte ranges from files referenced in a blob column. The column must have + |metadata hudi_blob=true. Review Comment: This is addressed in the latest commit -- 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]
