diff --git a/hudi-common/src/main/java/org/apache/hudi/common/config/TypedProperties.java b/hudi-common/src/main/java/org/apache/hudi/common/config/TypedProperties.java index 2688e6454..09671ba2a 100644 --- a/hudi-common/src/main/java/org/apache/hudi/common/config/TypedProperties.java +++ b/hudi-common/src/main/java/org/apache/hudi/common/config/TypedProperties.java @@ -20,6 +20,7 @@ package org.apache.hudi.common.config; import java.io.Serializable; import java.util.Arrays; +import java.util.Enumeration; import java.util.List; import java.util.Objects; import java.util.Properties; @@ -36,12 +37,23 @@ public class TypedProperties extends Properties implements Serializable { public TypedProperties(Properties defaults) { if (Objects.nonNull(defaults)) { - for (String key : defaults.stringPropertyNames()) { - put(key, defaults.getProperty(key)); + for (Enumeration e = defaults.propertyNames(); e.hasMoreElements(); ) { + Object k = e.nextElement(); + Object v = defaults.get(k); + if (v != null) { + put(k, v); + } } } } + @Override + public String getProperty(String key) { + Object oval = super.get(key); + String sval = (oval != null) ? String.valueOf(oval) : null; + return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval; + } + private void checkKey(String property) { if (!containsKey(property)) { throw new IllegalArgumentException("Property " + property + " not found"); diff --git a/hudi-common/src/test/java/org/apache/hudi/common/properties/TestTypedProperties.java b/hudi-common/src/test/java/org/apache/hudi/common/properties/TestTypedProperties.java index f93627525..954b53651 100644 --- a/hudi-common/src/test/java/org/apache/hudi/common/properties/TestTypedProperties.java +++ b/hudi-common/src/test/java/org/apache/hudi/common/properties/TestTypedProperties.java @@ -83,5 +83,27 @@ public class TestTypedProperties { assertTrue(typedProperties.getBoolean("key1")); assertTrue(typedProperties.getBoolean("key1", false)); assertFalse(typedProperties.getBoolean("key2", false)); + // test getBoolean with non-string value for key2 + properties.put("key2", true); + typedProperties = new TypedProperties(properties); + assertTrue(typedProperties.getBoolean("key1", false)); + assertTrue(typedProperties.getBoolean("key2", false)); + // put non-string value in TypedProperties + typedProperties.put("key3", true); + assertTrue(typedProperties.getBoolean("key3", false)); + } + + @Test + public void testTypedPropertiesWithNonStringValue() { + Properties properties = new Properties(); + properties.put("key1", "1"); + properties.put("key2", 2); + + TypedProperties props = new TypedProperties(properties); + assertEquals(1, props.getInteger("key1")); + assertEquals(2, props.getInteger("key2")); + // put non-string value in TypedProperties + props.put("key2", 3); + assertEquals(3, props.getInteger("key2")); } }