Skip to content

Commit bf4bc6d

Browse files
author
Piyush Narang
committed
Add support for Config setting in ValuesWriter factory
1 parent 8a852a3 commit bf4bc6d

File tree

5 files changed

+110
-4
lines changed

5 files changed

+110
-4
lines changed

parquet-column/src/test/java/org/apache/parquet/column/values/factory/DefaultValuesWriterFactoryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ public void testFloat_V2_NoDict() {
305305

306306
private void doTestValueWriter(PrimitiveTypeName typeName, WriterVersion version, boolean enableDictionary, Class<? extends ValuesWriter> expectedValueWriterClass) {
307307
ColumnDescriptor mockPath = getMockColumn(typeName);
308-
ValuesWriterFactory selectionStrategy = getDefaultFactory(version, enableDictionary);
309-
ValuesWriter writer = selectionStrategy.newValuesWriter(mockPath);
308+
ValuesWriterFactory factory = getDefaultFactory(version, enableDictionary);
309+
ValuesWriter writer = factory.newValuesWriter(mockPath);
310310

311311
validateWriterType(writer, expectedValueWriterClass);
312312
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.parquet.column.values.factory;
20+
21+
import org.apache.hadoop.conf.Configuration;
22+
23+
/**
24+
* Used with {@link ValuesWriterFactory} to indicate a factory that has Hadoop config included.
25+
*/
26+
public interface ConfigurableFactory {
27+
28+
void setConfiguration(Configuration config);
29+
30+
Configuration getConfiguration();
31+
}

parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ParquetOutputFormat.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.apache.parquet.Log;
3939
import org.apache.parquet.column.ParquetProperties;
4040
import org.apache.parquet.column.ParquetProperties.WriterVersion;
41-
import org.apache.parquet.column.values.factory.DefaultValuesWriterFactory;
41+
import org.apache.parquet.column.values.factory.ConfigurableFactory;
4242
import org.apache.parquet.column.values.factory.ValuesWriterFactory;
4343
import org.apache.parquet.hadoop.ParquetFileWriter.Mode;
4444
import org.apache.parquet.hadoop.api.WriteSupport;
@@ -340,7 +340,13 @@ public static ValuesWriterFactory getValuesWriterFactory(Configuration conf) {
340340
}
341341

342342
try {
343-
return factoryOverride.newInstance();
343+
ValuesWriterFactory factory = factoryOverride.newInstance();
344+
if (factory instanceof ConfigurableFactory) {
345+
ConfigurableFactory configurableFactory = (ConfigurableFactory) factory;
346+
configurableFactory.setConfiguration(conf);
347+
}
348+
349+
return factory;
344350
} catch (Exception e) {
345351
throw new BadConfigurationException("Unable to instantiate ValuesWriterFactory: " + factoryOverride, e);
346352
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.parquet.hadoop;
20+
21+
import org.apache.hadoop.conf.Configuration;
22+
import org.apache.parquet.column.ColumnDescriptor;
23+
import org.apache.parquet.column.values.ValuesWriter;
24+
import org.apache.parquet.column.values.factory.ConfigurableFactory;
25+
import org.apache.parquet.column.values.factory.ValuesWriterFactory;
26+
import org.apache.parquet.column.values.factory.ValuesWriterFactoryParams;
27+
28+
/**
29+
* ValuesWriter factory test class for verifying overrides and configuration
30+
*/
31+
public class StubConfigurableValuesWriterFactory implements ValuesWriterFactory, ConfigurableFactory {
32+
33+
private Configuration config;
34+
35+
@Override
36+
public void setConfiguration(Configuration config) {
37+
this.config = config;
38+
}
39+
40+
@Override
41+
public Configuration getConfiguration() {
42+
return config;
43+
}
44+
45+
@Override
46+
public void initialize(ValuesWriterFactoryParams params) {
47+
}
48+
49+
@Override
50+
public ValuesWriter newValuesWriter(ColumnDescriptor descriptor) {
51+
return null;
52+
}
53+
}

parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestParquetOutputFormatFactoryOverrides.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
package org.apache.parquet.hadoop;
2020

2121
import org.apache.hadoop.conf.Configuration;
22+
import org.apache.parquet.column.values.factory.ConfigurableFactory;
2223
import org.apache.parquet.column.values.factory.DefaultValuesWriterFactory;
2324
import org.apache.parquet.column.values.factory.ValuesWriterFactory;
2425
import org.junit.Test;
2526

2627
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.assertNotNull;
2729

2830
public class TestParquetOutputFormatFactoryOverrides {
2931

@@ -59,4 +61,18 @@ public void testValidFactoryOverride() {
5961
ValuesWriterFactory factory = ParquetOutputFormat.getValuesWriterFactory(conf);
6062
assertEquals("Incorrect factory override chosen", StubValuesWriterFactory.class, factory.getClass());
6163
}
64+
65+
@Test
66+
public void testFactoryOverrideWithCfg() {
67+
Configuration conf = new Configuration();
68+
conf.set("foo", "bar");
69+
conf.set(ParquetOutputFormat.WRITER_FACTORY_OVERRIDE, "org.apache.parquet.hadoop.StubConfigurableValuesWriterFactory");
70+
71+
ValuesWriterFactory factory = ParquetOutputFormat.getValuesWriterFactory(conf);
72+
assertEquals("Incorrect factory override chosen", StubConfigurableValuesWriterFactory.class, factory.getClass());
73+
74+
ConfigurableFactory configurable = (ConfigurableFactory) factory;
75+
assertNotNull("Not a ConfigurableFactory", configurable);
76+
assertEquals("Incorrect config value found", "bar", configurable.getConfiguration().get("foo"));
77+
}
6278
}

0 commit comments

Comments
 (0)