forked from apache/hudi
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HUDI-3523] Introduce AddPrimitiveColumnSchemaPostProcessor to suppor…
…t add new primitive column to the end of a schema (apache#6769)
- Loading branch information
1 parent
63aade8
commit 63eaffa
Showing
7 changed files
with
204 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...apache/hudi/utilities/schema/postprocessor/add/AddPrimitiveColumnSchemaPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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.utilities.schema.postprocessor.add; | ||
|
||
import org.apache.hudi.common.config.TypedProperties; | ||
import org.apache.hudi.common.util.StringUtils; | ||
import org.apache.hudi.common.util.ValidationUtils; | ||
import org.apache.hudi.utilities.exception.HoodieSchemaPostProcessException; | ||
import org.apache.hudi.utilities.schema.SchemaPostProcessor; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.spark.api.java.JavaSparkContext; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
/** | ||
* A {@link SchemaPostProcessor} used to add a new column of primitive types to given schema. Only supports adding one | ||
* column at a time. | ||
* <p> | ||
* The new column will be appended to the end. | ||
* <p> | ||
* TODO support complex types. | ||
*/ | ||
public class AddPrimitiveColumnSchemaPostProcessor extends SchemaPostProcessor { | ||
|
||
public AddPrimitiveColumnSchemaPostProcessor(TypedProperties props, JavaSparkContext jssc) { | ||
super(props, jssc); | ||
} | ||
|
||
@Override | ||
public Schema processSchema(Schema schema) { | ||
String newColumnName = this.config.getString(BaseSchemaPostProcessorConfig.SCHEMA_POST_PROCESSOR_ADD_COLUMN_NAME_PROP.key()); | ||
|
||
if (schema.getField(newColumnName) != null) { | ||
throw new HoodieSchemaPostProcessException(String.format("Column %s already exist!", newColumnName)); | ||
} | ||
|
||
List<Schema.Field> sourceFields = schema.getFields(); | ||
List<Schema.Field> targetFields = new ArrayList<>(sourceFields.size() + 1); | ||
|
||
|
||
for (Schema.Field sourceField : sourceFields) { | ||
targetFields.add(new Schema.Field(sourceField.name(), sourceField.schema(), sourceField.doc(), sourceField.defaultVal())); | ||
} | ||
|
||
// add new column to the end | ||
targetFields.add(buildNewColumn()); | ||
|
||
return Schema.createRecord(schema.getName(), schema.getDoc(), schema.getNamespace(), false, targetFields); | ||
} | ||
|
||
private Schema.Field buildNewColumn() { | ||
|
||
String columnName = this.config.getString(BaseSchemaPostProcessorConfig.SCHEMA_POST_PROCESSOR_ADD_COLUMN_NAME_PROP.key()); | ||
String type = this.config.getString(BaseSchemaPostProcessorConfig.SCHEMA_POST_PROCESSOR_ADD_COLUMN_TYPE_PROP.key()).toUpperCase(Locale.ROOT); | ||
String doc = this.config.getString(BaseSchemaPostProcessorConfig.SCHEMA_POST_PROCESSOR_ADD_COLUMN_DOC_PROP.key(), null); | ||
Object defaultValue = this.config.getOrDefault(BaseSchemaPostProcessorConfig.SCHEMA_POST_PROCESSOR_ADD_COLUMN_DEFAULT_PROP.key(), | ||
null); | ||
boolean nullable = this.config.getBoolean(BaseSchemaPostProcessorConfig.SCHEMA_POST_PROCESSOR_ADD_COLUMN_NULLABLE_PROP.key(), | ||
BaseSchemaPostProcessorConfig.SCHEMA_POST_PROCESSOR_ADD_COLUMN_NULLABLE_PROP.defaultValue()); | ||
|
||
ValidationUtils.checkArgument(!StringUtils.isNullOrEmpty(columnName)); | ||
ValidationUtils.checkArgument(!StringUtils.isNullOrEmpty(type)); | ||
ValidationUtils.checkArgument(!Schema.Type.NULL.getName().equals(type)); | ||
|
||
Schema newSchema = createSchema(type, nullable); | ||
|
||
return new Schema.Field(columnName, newSchema, doc, defaultValue); | ||
} | ||
|
||
private Schema createSchema(String type, boolean nullable) { | ||
Schema schema = Schema.create(Schema.Type.valueOf(type)); | ||
if (nullable) { | ||
schema = Schema.createUnion(Schema.create(Schema.Type.NULL), schema); | ||
} | ||
return schema; | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...ava/org/apache/hudi/utilities/schema/postprocessor/add/BaseSchemaPostProcessorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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.utilities.schema.postprocessor.add; | ||
|
||
import org.apache.hudi.common.config.ConfigProperty; | ||
|
||
/** | ||
* Base configs to describe a primitive type column. | ||
*/ | ||
public class BaseSchemaPostProcessorConfig { | ||
|
||
public static final ConfigProperty<String> SCHEMA_POST_PROCESSOR_ADD_COLUMN_NAME_PROP = ConfigProperty | ||
.key("hoodie.deltastreamer.schemaprovider.schema_post_processor.add.column.name") | ||
.noDefaultValue() | ||
.withDocumentation("New column's name"); | ||
|
||
public static final ConfigProperty<String> SCHEMA_POST_PROCESSOR_ADD_COLUMN_TYPE_PROP = ConfigProperty | ||
.key("hoodie.deltastreamer.schemaprovider.schema_post_processor.add.column.type") | ||
.noDefaultValue() | ||
.withDocumentation("New column's type"); | ||
|
||
public static final ConfigProperty<Boolean> SCHEMA_POST_PROCESSOR_ADD_COLUMN_NULLABLE_PROP = ConfigProperty | ||
.key("hoodie.deltastreamer.schemaprovider.schema_post_processor.add.column.nullable") | ||
.defaultValue(true) | ||
.withDocumentation("New column's nullable"); | ||
|
||
public static final ConfigProperty<String> SCHEMA_POST_PROCESSOR_ADD_COLUMN_DEFAULT_PROP = ConfigProperty | ||
.key("hoodie.deltastreamer.schemaprovider.schema_post_processor.add.column.default") | ||
.noDefaultValue() | ||
.withDocumentation("New column's default value"); | ||
|
||
public static final ConfigProperty<String> SCHEMA_POST_PROCESSOR_ADD_COLUMN_DOC_PROP = ConfigProperty | ||
.key("hoodie.deltastreamer.schemaprovider.schema_post_processor.add.column.doc") | ||
.noDefaultValue() | ||
.withDocumentation("Docs about new column"); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters