the-other-tim-brown commented on code in PR #8574:
URL: https://github.com/apache/hudi/pull/8574#discussion_r1203094395
##########
hudi-utilities/src/main/java/org/apache/hudi/utilities/UtilHelpers.java:
##########
@@ -191,11 +192,14 @@ public static SchemaPostProcessor
createSchemaPostProcessor(
}
- public static Option<Transformer> createTransformer(Option<List<String>>
classNamesOpt, Boolean isErrorTableWriterEnabled) throws IOException {
+ public static Option<Transformer> createTransformer(Option<List<String>>
classNamesOpt, Option<Schema> sourceSchema,
+ boolean
enableTransformerSchemaValidation, Boolean isErrorTableWriterEnabled) throws
IOException {
+
try {
- return classNamesOpt.map(classNames -> classNames.isEmpty() ? null :
- isErrorTableWriterEnabled ? new
ErrorTableAwareChainedTransformer(classNames) : new
ChainedTransformer(classNames)
- );
+ Function<List<String>, Transformer> chainedTransformerFunction =
classNames ->
+ isErrorTableWriterEnabled ? new
ErrorTableAwareChainedTransformer(classNames, sourceSchema,
enableTransformerSchemaValidation)
Review Comment:
what is the expected behavior here if isErrorTableWriter is null?
##########
hudi-client/hudi-spark-client/src/main/scala/org/apache/hudi/StructUtils.scala:
##########
@@ -0,0 +1,56 @@
+/*
+ * 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;
+
+import org.apache.spark.sql.types.{StructField, StructType}
+
+object StructUtils {
Review Comment:
If this is only used in testing, can we move it under the test folder?
##########
hudi-utilities/src/main/java/org/apache/hudi/utilities/transform/Transformer.java:
##########
@@ -45,4 +47,11 @@ public interface Transformer {
*/
@PublicAPIMethod(maturity = ApiMaturityLevel.STABLE)
Dataset<Row> apply(JavaSparkContext jsc, SparkSession sparkSession,
Dataset<Row> rowDataset, TypedProperties properties);
+
+ @PublicAPIMethod(maturity = ApiMaturityLevel.EVOLVING)
+ default Option<StructType> transformedSchema(JavaSparkContext jsc,
SparkSession sparkSession, StructType incomingStruct, TypedProperties
properties) {
Review Comment:
Won't we always have a schema returned? Do we need Option here?
##########
hudi-utilities/src/main/java/org/apache/hudi/utilities/transform/ChainedTransformer.java:
##########
@@ -93,9 +103,13 @@ public List<String> getTransformersNames() {
@Override
public Dataset<Row> apply(JavaSparkContext jsc, SparkSession sparkSession,
Dataset<Row> rowDataset, TypedProperties properties) {
Dataset<Row> dataset = rowDataset;
+ Option<Schema> incomingSchemaOpt = sourceSchemaOpt;
for (TransformerInfo transformerInfo : transformers) {
Transformer transformer = transformerInfo.getTransformer();
dataset = transformer.apply(jsc, sparkSession, dataset,
transformerInfo.getProperties(properties));
Review Comment:
reminder that we don't need to validate the schema but rather validate that
the transformers will result in a valid plan. if they do not, we want to throw
an easy to understand/debug error with context for a developer to understand
why it's not working.
##########
hudi-utilities/src/main/java/org/apache/hudi/utilities/deltastreamer/DeltaSync.java:
##########
@@ -308,7 +308,9 @@ public DeltaSync(HoodieDeltaStreamer.Config cfg,
SparkSession sparkSession, Sche
UtilHelpers.createSource(cfg.sourceClassName, props, jssc,
sparkSession, schemaProvider, metrics),
this.errorTableWriter, Option.of(props));
- this.transformer =
UtilHelpers.createTransformer(Option.ofNullable(cfg.transformerClassNames),
this.errorTableWriter.isPresent());
+ this.transformer =
UtilHelpers.createTransformer(Option.ofNullable(cfg.transformerClassNames),
+
Option.ofNullable(schemaProvider).map(SchemaProvider::getSourceSchema),
cfg.enableTransformerSchemaValidation,
Review Comment:
The sourceSchema is not necessarily the input to the transformers. Let's
sync on this to clear this up.
##########
hudi-utilities/src/main/java/org/apache/hudi/utilities/transform/ChainedTransformer.java:
##########
@@ -93,9 +105,19 @@ public List<String> getTransformersNames() {
@Override
Review Comment:
This class needs to implement `transformedSchema` as well
##########
hudi-utilities/src/main/java/org/apache/hudi/utilities/deltastreamer/HoodieDeltaStreamer.java:
##########
@@ -289,6 +289,11 @@ public static class Config implements Serializable {
)
public List<String> transformerClassNames = null;
+ @Parameter(names = {"--enable-transformer-schema-validation"},
+ description = "If enabled, schema is validated for the transformed
data against expected schema. "
+ + "Expected schema is provided by the transformer.")
+ public Boolean enableTransformerSchemaValidation = false;
Review Comment:
`boolean` since we don't expect this to be null
##########
hudi-utilities/src/main/java/org/apache/hudi/utilities/transform/ChainedTransformer.java:
##########
@@ -109,6 +131,28 @@ private void validateIdentifier(String id, Set<String>
identifiers, String confi
}
}
+ private Option<StructType> validateAndGetTransformedSchema(TransformerInfo
transformerInfo, Dataset<Row> dataset, Option<StructType> incomingStructOpt,
+ JavaSparkContext
jsc, SparkSession sparkSession, TypedProperties properties) {
+ if (!incomingStructOpt.isPresent()) {
Review Comment:
nit: you can use `orElseThrow` here as a cleaner way of doing something like
this.
`StructType incomingStruct = incomingStructOpt.orElseThrow(() -> new
HoodieSchemaException(String.format("Source schema not available for
transformer %s", transformerInfo));`
##########
hudi-utilities/src/test/java/org/apache/hudi/utilities/deltastreamer/TestTransformer.java:
##########
@@ -92,4 +211,95 @@ public Dataset<Row> apply(JavaSparkContext jsc,
SparkSession sparkSession, Datas
return rowDataset.withColumn("timestamp",
functions.col("timestamp").multiply(multiplier).plus(increment));
}
}
+
+ /**
+ * Provides a transformedSchema implementation for FlatteningTransformer.
+ */
+ public static class FlatteningTransformerWithTransformedSchema extends
FlatteningTransformer {
+
+ @Override
+ public Option<StructType> transformedSchema(JavaSparkContext jsc,
SparkSession sparkSession, StructType incomingStruct, TypedProperties
properties) {
+ String flattenedSelect = flattenSchema(incomingStruct, null);
+ String[] cols = flattenedSelect.split(",");
+ List<Pair<String, String>> replacements = new LinkedList<>();
+ for (String col : cols) {
+ String[] names = col.split(" as ");
+ if (!names[0].equals(names[1])) {
+ replacements.add(Pair.of(names[0], names[1]));
+ }
+ }
+
+ List<StructField> incomingFields =
Arrays.asList(incomingStruct.fields());
+ List<StructField> transformedFields = new LinkedList<>(incomingFields);
+ Set<StructField> fieldsToRemove = new HashSet<>();
+ for (Pair<String, String> replacement : replacements) {
+ String fieldToRemoveName = replacement.getKey().replaceAll("\\..*",
"");
+ StructField fieldToAdd = StructUtils.getField(incomingStruct,
replacement.getKey()).get();
+ StructField fieldToRemove = transformedFields.stream().filter(f ->
f.name().equals(fieldToRemoveName)).findAny().get();
+ fieldsToRemove.add(fieldToRemove);
+ transformedFields.add(transformedFields.indexOf(fieldToRemove), new
StructField(replacement.getKey().replaceAll("\\.", "_"),
+ fieldToAdd.dataType(), fieldToAdd.nullable(),
fieldToAdd.metadata()));
+ }
+ transformedFields.removeAll(fieldsToRemove);
+
+ return Option.of(new StructType(transformedFields.toArray(new
StructField[0])));
+ }
+ }
+
+ /**
+ * Does not provide transformedSchema implementation for
FlatteningTransformer.
+ */
+ public static class FlatteningTransformerWithoutTransformedSchema extends
FlatteningTransformer {
+ @Override
+ public Option<StructType> transformedSchema(JavaSparkContext jsc,
SparkSession sparkSession, StructType incomingStruct, TypedProperties
properties) {
+ return Option.empty();
+ }
+ }
+
+ /**
+ * Adds a new column named random in the dataset.
+ */
+ public static class AddColumnTransformer implements Transformer {
Review Comment:
Let's make a transform that operates on a field that does not actually exist
in the source to make sure that we are throwing the proper exception in this
case. Something like:
`rowDataset.withColumn("random",
functions.lit(5).multiply(functions.col("unknown_column")));`
--
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]