vamshikrishnakyatham commented on code in PR #13745: URL: https://github.com/apache/hudi/pull/13745#discussion_r2308163405
########## hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/procedure/TestShowFileGroupHistoryProcedure.scala: ########## @@ -0,0 +1,665 @@ +/* + * 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.procedure + +import org.apache.hudi.HoodieSparkUtils + +import org.apache.spark.sql.hudi.common.HoodieSparkSqlTestBase + +class TestShowFileGroupHistoryProcedure extends HoodieSparkSqlTestBase { + + test("Test show_file_group_history - basic functionality test") { + withTempDir { tmp => + val tableName = generateTableName + val tableLocation = tmp.getCanonicalPath + if (HoodieSparkUtils.isSpark3_4) { + spark.sql("set spark.sql.defaultColumn.enabled = false") + } + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts long + |) using hudi + | location '$tableLocation' + | tblproperties ( + | primaryKey = 'id', + | type = 'cow', + | preCombineField = 'ts' + |) + |""".stripMargin) + spark.sql(s"insert into $tableName values(1, 'a1', 10, 1000)") + spark.sql(s"insert into $tableName values(2, 'a2', 20, 2000)") + + val fileListResult = spark.sql(s"call show_commits(table => '$tableName', limit => 10)").collect() + assert(fileListResult.length >= 2, "Should have at least 2 commits") + + val showFiles = spark.sql(s"select _hoodie_file_name from $tableName limit 1").collect() + assert(showFiles.length > 0, "Should have at least one file") + + val fileName = showFiles.head.getString(0) + val fileGroupId = fileName.split("_")(0) + + val historyResultDf = spark.sql( + s"""call show_file_group_history( + | table => '$tableName', + | file_group_id => '$fileGroupId' + |)""".stripMargin) + historyResultDf.show(false) + val historyResult = historyResultDf.collect() + + assert(historyResult.length == 2, "Should show 2 history entries for file group") + + val headRow = historyResult.head + assert(headRow.length == 27, "Should have 27 columns in result") + assert(headRow.getString(2).equals("commit"), "Action should be commit here") + assert(headRow.getString(7) == "INSERT", "Operation type should be INSERT here") + assert(headRow.getLong(8) == 2, "Small file handling logic coming into play, should have 2 files here") + } + } + + test("Test show_file_group_history - with partition filter test") { + withTempDir { tmp => + val tableName = generateTableName + val tableLocation = tmp.getCanonicalPath + if (HoodieSparkUtils.isSpark3_4) { + spark.sql("set spark.sql.defaultColumn.enabled = false") + } + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | category string, + | ts long + |) using hudi + | location '$tableLocation' + | tblproperties ( + | primaryKey = 'id', + | type = 'cow', + | preCombineField = 'ts' + |) + | partitioned by (category) + |""".stripMargin) + + spark.sql(s"insert into $tableName (id, name, price, category, ts) values(1, 'a1', 10, 'electronics', 1000)") + spark.sql(s"insert into $tableName (id, name, price, category, ts) values(2, 'a2', 20, 'books', 2000)") + spark.sql(s"insert into $tableName (id, name, price, category, ts) values(3, 'a3', 30, 'electronics', 3000)") + + val electronicsFiles = spark.sql( + s"select _hoodie_file_name from $tableName where category = 'electronics' limit 1" + ).collect() + + val fileName = electronicsFiles.head.getString(0) + val fileGroupId = fileName.split("_")(0) + + val historyWithPartition = spark.sql( + s"""call show_file_group_history( + | table => '$tableName', + | file_group_id => '$fileGroupId', + | partition => 'category=electronics' + |)""".stripMargin).collect() + + assert(historyWithPartition.length == 2, "Should find history for electronics partition") + + historyWithPartition.foreach { row => + val partitionPath = row.getString(5) + assert(partitionPath.contains("electronics") || partitionPath.contains("UNKNOWN"), + s"Partition path should contain 'electronics' or be UNKNOWN, got: $partitionPath") + } + + val historyWithoutPartition = spark.sql( + s"""call show_file_group_history( + | table => '$tableName', + | file_group_id => '$fileGroupId' + |)""".stripMargin).collect() + + assert(historyWithoutPartition.length >= historyWithPartition.length, + "History without partition filter should have >= results than with filter") + } + } + + test("Test show_file_group_history - little complex case with updates and limit") { + withTempDir { tmp => + val tableName = generateTableName + val tableLocation = tmp.getCanonicalPath + if (HoodieSparkUtils.isSpark3_4) { + spark.sql("set spark.sql.defaultColumn.enabled = false") + } + spark.sql( + s""" + |create table $tableName ( + | id int, + | name string, + | price double, + | ts long + |) using hudi + | location '$tableLocation' + | tblproperties ( + | primaryKey = 'id', + | type = 'cow', + | preCombineField = 'ts' + |) + |""".stripMargin) + + spark.sql(s"insert into $tableName values(1, 'a1', 10, 1000)") + spark.sql(s"insert into $tableName values(2, 'a2', 20, 2000)") + spark.sql(s"update $tableName set price = 15 where id = 1") + spark.sql(s"insert into $tableName values(3, 'a3', 30, 3000)") + spark.sql(s"update $tableName set price = 25 where id = 2") + spark.sql(s"update $tableName set price = 18 where id = 1") + + val fileInfo = spark.sql(s"select _hoodie_file_name from $tableName where id = 1 limit 1").collect() + val fileName = fileInfo.head.getString(0) + val fileGroupId = fileName.split("_")(0) + + val limitedHistoryDf = spark.sql( + s"""call show_file_group_history( + | table => '$tableName', + | file_group_id => '$fileGroupId', + | limit => 3 + |)""".stripMargin) + limitedHistoryDf.show(false) + val limitedHistory = limitedHistoryDf.collect() + + assert(limitedHistory.length == 3, "Should respect limit parameter") + + val unlimitedHistoryDf = spark.sql( + s"""call show_file_group_history( + | table => '$tableName', + | file_group_id => '$fileGroupId', + | limit => 20 Review Comment: fixed with proper naming convention for the same, my bad -- 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]
