yihua commented on code in PR #18379: URL: https://github.com/apache/hudi/pull/18379#discussion_r3088478942
########## hudi-hadoop-common/src/test/java/org/apache/hudi/io/storage/hadoop/TestHoodieAvroParquetConfigInjector.java: ########## @@ -0,0 +1,184 @@ +/* + * 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.io.storage.hadoop; + +import org.apache.hudi.common.config.HoodieConfig; +import org.apache.hudi.common.config.HoodieStorageConfig; +import org.apache.hudi.common.engine.LocalTaskContextSupplier; +import org.apache.hudi.common.schema.HoodieSchema; +import org.apache.hudi.common.testutils.HoodieTestDataGenerator; +import org.apache.hudi.common.testutils.HoodieTestUtils; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.io.HoodieParquetConfigInjector; +import org.apache.hudi.io.storage.HoodieFileWriter; +import org.apache.hudi.storage.HoodieStorage; +import org.apache.hudi.storage.StorageConfiguration; +import org.apache.hudi.storage.StoragePath; + +import org.apache.avro.generic.GenericRecord; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.Path; +import org.apache.parquet.column.Encoding; +import org.apache.parquet.hadoop.ParquetFileReader; +import org.apache.parquet.hadoop.metadata.BlockMetaData; +import org.apache.parquet.hadoop.metadata.ColumnChunkMetaData; +import org.apache.parquet.hadoop.metadata.ParquetMetadata; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests for {@link HoodieParquetConfigInjector} functionality in {@link HoodieAvroFileWriterFactory}. + */ +public class TestHoodieAvroParquetConfigInjector { + + @TempDir + java.nio.file.Path tmpDir; + + /** + * Test implementation that disables dictionary encoding. + */ + public static class DisableDictionaryInjector implements HoodieParquetConfigInjector { + @Override + public Pair<StorageConfiguration, HoodieConfig> injectConfig(StoragePath path, + StorageConfiguration storageConf, + HoodieConfig hoodieConfig) { + hoodieConfig.setValue(HoodieStorageConfig.PARQUET_DICTIONARY_ENABLED, "false"); + return Pair.of(storageConf, hoodieConfig); + } + } Review Comment: _⚠️ Potential issue_ | _🟡 Minor_ <details> <summary>🧩 Analysis chain</summary> 🏁 Script executed: ```shell #!/bin/bash # Search for HoodieConfig constructors ast-grep --pattern 'class HoodieConfig { $$$ HoodieConfig($$$) { $$$ } $$$ }' ``` Repository: yihua/hudi Length of output: 36 --- 🏁 Script executed: ```shell find . -type f -name "HoodieConfig.java" | head -20 ``` Repository: yihua/hudi Length of output: 129 --- 🏁 Script executed: ```shell find . -type f -name "HoodieParquetConfigInjector.java" | head -20 ``` Repository: yihua/hudi Length of output: 133 --- 🏁 Script executed: ```shell # Search for HoodieConfig class definition to understand its structure rg "class HoodieConfig" -A 20 --max-count 1 ``` Repository: yihua/hudi Length of output: 8454 --- 🏁 Script executed: ```shell cat -n ./hudi-common/src/main/java/org/apache/hudi/io/HoodieParquetConfigInjector.java | head -100 ``` Repository: yihua/hudi Length of output: 5597 --- 🏁 Script executed: ```shell # Check if HoodieConfig has a getProps method rg "getProps|getProperties" ./hudi-common/src/main/java/org/apache/hudi/common/config/HoodieConfig.java -A 2 ``` Repository: yihua/hudi Length of output: 513 --- 🏁 Script executed: ```shell # Check the TypedProperties class to understand if it needs deep copying rg "class TypedProperties" -A 30 ``` Repository: yihua/hudi Length of output: 3266 --- 🏁 Script executed: ```shell # Check how HoodieConfig uses the props field and if there's a copy method rg "setValue|putAll" ./hudi-common/src/main/java/org/apache/hudi/common/config/HoodieConfig.java -B 2 -A 2 ``` Repository: yihua/hudi Length of output: 626 --- 🏁 Script executed: ```shell # Check the complete HoodieConfig constructor and its implementation cat -n ./hudi-common/src/main/java/org/apache/hudi/common/config/HoodieConfig.java | head -50 ``` Repository: yihua/hudi Length of output: 2398 --- 🏁 Script executed: ```shell # Verify if there's any copy constructor or method in HoodieConfig rg "copy|clone|Copy|Clone" ./hudi-common/src/main/java/org/apache/hudi/common/config/HoodieConfig.java ``` Repository: yihua/hudi Length of output: 277 --- 🏁 Script executed: ```shell # Get more context around the copy method rg "static HoodieConfig copy|TypedProperties.copy" ./hudi-common/src/main/java/org/apache/hudi/common/config/HoodieConfig.java -B 2 -A 5 ``` Repository: yihua/hudi Length of output: 383 --- 🏁 Script executed: ```shell # Check if TypedProperties has a copy method rg "public static.*copy|public.*copy" ./hudi-common/src/main/java/org/apache/hudi/common/config/TypedProperties.java -A 3 ``` Repository: yihua/hudi Length of output: 161 --- 🏁 Script executed: ```shell # Check the exact constructors in HoodieConfig cat -n ./hudi-common/src/main/java/org/apache/hudi/common/config/HoodieConfig.java | sed -n '50,80p' ``` Repository: yihua/hudi Length of output: 1020 --- 🏁 Script executed: ```shell # Verify the test file and how it's using the DisableDictionaryInjector cat -n ./hudi-hadoop-common/src/test/java/org/apache/hudi/io/storage/hadoop/TestHoodieAvroParquetConfigInjector.java | sed -n '64,127p' ``` Repository: yihua/hudi Length of output: 3617 --- </details> **Test injector violates the documented immutability contract.** The `DisableDictionaryInjector` directly mutates the input `hoodieConfig` (line 69), which violates the interface contract documented in `HoodieParquetConfigInjector`: *"Implementations must NOT mutate the input storageConf or hoodieConfig objects directly."* This could cause unintended side effects and sets a bad example for users implementing their own injectors. <details> <summary>🛠️ Proposed fix: Create a copy before modifying</summary> ```diff public static class DisableDictionaryInjector implements HoodieParquetConfigInjector { `@Override` public Pair<StorageConfiguration, HoodieConfig> injectConfig(StoragePath path, StorageConfiguration storageConf, HoodieConfig hoodieConfig) { - hoodieConfig.setValue(HoodieStorageConfig.PARQUET_DICTIONARY_ENABLED, "false"); - return Pair.of(storageConf, hoodieConfig); + HoodieConfig modifiedConfig = HoodieConfig.copy(hoodieConfig.getProps()); + modifiedConfig.setValue(HoodieStorageConfig.PARQUET_DICTIONARY_ENABLED, "false"); + return Pair.of(storageConf, modifiedConfig); } } ``` </details> <details> <summary>🤖 Prompt for AI Agents</summary> ``` Verify each finding against the current code and only fix it if needed. In `@hudi-hadoop-common/src/test/java/org/apache/hudi/io/storage/hadoop/TestHoodieAvroParquetConfigInjector.java` around lines 64 - 72, The test injector DisableDictionaryInjector currently mutates the input hoodieConfig inside injectConfig; instead, create a new HoodieConfig instance copied from the input (do not modify storageConf) and call setValue on that copy, then return Pair.of(storageConf, copiedHoodieConfig) so the original inputs remain unchanged; update the injectConfig method in DisableDictionaryInjector to clone/copy hoodieConfig before calling setValue. ``` </details> <!-- fingerprinting:phantom:medusa:ocelot --> <!-- This is an auto-generated comment by CodeRabbit --> — *CodeRabbit* ([original](https://github.com/yihua/hudi/pull/13#discussion_r3066960355)) (source:comment#3066960355) -- 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]
