lokeshj1703 commented on code in PR #11816:
URL: https://github.com/apache/hudi/pull/11816#discussion_r1746745167
##########
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',
Review Comment:
I have updated the test to use primary key with two columns. Also added the
validation for table config.
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/keygen/KeyGenUtils.java:
##########
@@ -67,28 +67,33 @@ public static KeyGeneratorType inferKeyGeneratorType(
Option<String> recordsKeyFields, String partitionFields) {
boolean autoGenerateRecordKeys = !recordsKeyFields.isPresent();
if (autoGenerateRecordKeys) {
- return inferKeyGeneratorTypeForAutoKeyGen(partitionFields);
+ return inferKeyGeneratorTypeFromPartitionFields(partitionFields);
} else {
if (!StringUtils.isNullOrEmpty(partitionFields)) {
- int numPartFields = partitionFields.split(",").length;
int numRecordKeyFields = recordsKeyFields.get().split(",").length;
- if (numPartFields == 1 && numRecordKeyFields == 1) {
- return KeyGeneratorType.SIMPLE;
+ KeyGeneratorType recordKeyGeneratorType = numRecordKeyFields == 1 ?
KeyGeneratorType.SIMPLE : KeyGeneratorType.COMPLEX;
+ KeyGeneratorType partitionKeyGeneratorType =
inferKeyGeneratorTypeFromPartitionFields(partitionFields);
+ if (partitionKeyGeneratorType == KeyGeneratorType.SIMPLE &&
recordKeyGeneratorType == KeyGeneratorType.COMPLEX) {
+ return recordKeyGeneratorType;
+ } else {
+ return partitionKeyGeneratorType;
}
- return KeyGeneratorType.COMPLEX;
}
return KeyGeneratorType.NON_PARTITION;
}
}
// When auto record key gen is enabled, our inference will be based on
partition path only.
- private static KeyGeneratorType inferKeyGeneratorTypeForAutoKeyGen(String
partitionFields) {
+ private static KeyGeneratorType
inferKeyGeneratorTypeFromPartitionFields(String partitionFields) {
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]