SEPURI-SAI-KRISHNA commented on code in PR #19648: URL: https://github.com/apache/hudi/pull/19648#discussion_r3815194737
########## hudi-client/hudi-spark-client/src/test/java/org/apache/hudi/keygen/TestPartitionPathFormatter.java: ########## @@ -0,0 +1,115 @@ +/* + * 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.keygen; + +import org.apache.spark.unsafe.types.UTF8String; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.apache.hudi.common.util.PartitionPathEncodeUtils.DEFAULT_PARTITION_PATH; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests the partition-path formatters backing the key generators, making sure that both the + * {@link String} (Avro/{@link org.apache.spark.sql.Row} write path) and the {@link UTF8String} + * (row-writer/{@link org.apache.spark.sql.catalyst.InternalRow} write path) flavors produce + * identical partition paths. + */ +class TestPartitionPathFormatter { + + private static final List<String> SINGLE_FIELD = Collections.singletonList("date_col"); + private static final List<String> TWO_FIELDS = Arrays.asList("date_col", "city"); + + private String combine(boolean useRowWriterPath, + boolean hiveStylePartitioning, + boolean encode, + boolean slashSeparatedDatePartitioning, + List<String> fields, + Object... parts) { + if (useRowWriterPath) { + return new UTF8StringPartitionPathFormatter( + UTF8StringPartitionPathFormatter.UTF8StringBuilder::new, hiveStylePartitioning, encode, + slashSeparatedDatePartitioning).combine(fields, parts).toString(); + } + return new StringPartitionPathFormatter( + StringPartitionPathFormatter.JavaStringBuilder::new, hiveStylePartitioning, encode, + slashSeparatedDatePartitioning).combine(fields, parts); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void testSlashSeparatedDatePartitioningSingleField(boolean useRowWriterPath) { + assertEquals("2026/01/05", + combine(useRowWriterPath, false, false, true, SINGLE_FIELD, "2026-01-05")); + // Input that is already slash-separated is left untouched + assertEquals("2026/01/05", + combine(useRowWriterPath, false, false, true, SINGLE_FIELD, "2026/01/05")); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void testSlashSeparatedDatePartitioningOnlyAppliesToSingleFieldPartitioning(boolean useRowWriterPath) { + // NOTE: This mirrors [[KeyGenUtils#getRecordPartitionPath]] driving the Avro write-path + assertEquals("2026-01-05/san-francisco", + combine(useRowWriterPath, false, false, true, TWO_FIELDS, "2026-01-05", "san-francisco")); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void testSlashSeparatedDatePartitioningHandlesNullAndEmptyValues(boolean useRowWriterPath) { + assertEquals(DEFAULT_PARTITION_PATH, + combine(useRowWriterPath, false, false, true, SINGLE_FIELD, new Object[] {null})); + assertEquals(DEFAULT_PARTITION_PATH, + combine(useRowWriterPath, false, false, true, SINGLE_FIELD, "")); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void testSlashSeparatedDatePartitioningEncodesValues(boolean useRowWriterPath) { + // '?' has to be escaped, while the date separators are turned into directory separators + assertEquals("2026/01/05", combine(useRowWriterPath, false, true, true, SINGLE_FIELD, "2026-01-05")); + assertEquals("a%3Fb", combine(useRowWriterPath, false, true, true, SINGLE_FIELD, "a?b")); Review Comment: Applied. The `a%3Fb` assert stays and the pinned one is added below it, with the comment explaining why: ```java assertEquals("a%3Fb", combine(useRowWriterPath, false, true, true, SINGLE_FIELD, "a?b")); // Encoding runs before the substitution (parity with KeyGenUtils), so an already slash-separated // value is escaped rather than turned into directories assertEquals("2026%2F01%2F05", combine(useRowWriterPath, false, true, true, SINGLE_FIELD, "2026/01/05")); ``` You are right that the two pre-existing asserts were no-ops for this change: a date value has nothing for `escapePathName` to escape, and `a?b` contains no dash for the substitution to act on. The new line is the one that actually pins the ordering, since it is the only input where encode-then-substitute and substitute-then-encode differ. Verified on both formatter flavors. -- 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]
