lokeshj1703 commented on code in PR #11816:
URL: https://github.com/apache/hudi/pull/11816#discussion_r1746745451


##########
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/functional/TestSparkSqlWithCustomKeyGenerator.scala:
##########
@@ -442,13 +442,93 @@ class TestSparkSqlWithCustomKeyGenerator extends 
HoodieSparkSqlTestBase {
     }
   }
 
+  test("Test create table with custom key generator") {
+    withTempDir { tmp => {
+      val tableName = generateTableName
+      val tablePath = tmp.getCanonicalPath + "/" + tableName
+      val writePartitionFields = "ts:timestamp"
+      val dateFormat = "yyyy/MM/dd"
+      val tsGenFunc = (ts: Integer) => TS_FORMATTER_FUNC_WITH_FORMAT.apply(ts, 
dateFormat)
+      val customPartitionFunc = (ts: Integer, _: String) => "ts=" + 
tsGenFunc.apply(ts)
+      val keyGenConfigs = TS_KEY_GEN_CONFIGS + 
("hoodie.keygen.timebased.output.dateformat" -> dateFormat)
+
+      spark.sql(
+        s"""
+           |create table ${tableName} (
+           |  `id` INT,
+           |  `name` STRING,
+           |  `price` DECIMAL(5, 1),
+           |  `ts` INT,
+           |  `segment` STRING
+           |) using hudi
+           |tblproperties (
+           |  'primaryKey' = 'id',
+           |  'type' = 'mor',
+           |  'preCombineField'='name',
+           |  'hoodie.datasource.write.keygenerator.class' = 
'$CUSTOM_KEY_GEN_CLASS_NAME',
+           |  'hoodie.datasource.write.partitionpath.field' = 
'$writePartitionFields',
+           |  'hoodie.insert.shuffle.parallelism' = '1',
+           |  'hoodie.upsert.shuffle.parallelism' = '1',
+           |  'hoodie.bulkinsert.shuffle.parallelism' = '1',
+           |  'hoodie.keygen.timebased.timestamp.type' = 'SCALAR',
+           |  'hoodie.keygen.timebased.output.dateformat' = '$dateFormat',
+           |  'hoodie.keygen.timebased.timestamp.scalar.time.unit' = 'seconds'
+           |)
+           location '${tablePath}'
+           """.stripMargin)
+
+      testInserts(tableName, tsGenFunc, customPartitionFunc)
+      assertEquals(7, spark.sql(
+        s"""
+           | SELECT * from $tableName
+           | """.stripMargin).count())
+
+      // Validate ts field is still of type int in the table
+      validateTsFieldSchema(tablePath)
+    }
+    }
+  }
+
   private def validateTsFieldSchema(tablePath: String, fieldName: String, 
expectedType: Schema.Type): Unit = {
     val metaClient = createMetaClient(spark, tablePath)
     val schemaResolver = new TableSchemaResolver(metaClient)
     val nullableSchema = Schema.createUnion(Schema.create(Schema.Type.NULL), 
Schema.create(expectedType))
     assertEquals(nullableSchema, 
schemaResolver.getTableAvroSchema(true).getField(fieldName).schema())
   }
 
+  private def testInserts(tableName: String,
+                                    tsGenFunc: Integer => String,
+                                    partitionGenFunc: (Integer, String) => 
String): Unit = {

Review Comment:
   Addressed



-- 
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]

Reply via email to