Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CDAP-21075] Support DP 2.2(spark 3.5.1) by checking if the new method is present or not. #15808

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import io.cdap.cdap.etl.spark.function.FunctionCache;
import io.cdap.cdap.etl.spark.join.JoinExpressionRequest;
import io.cdap.cdap.etl.spark.join.JoinRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.MapFunction;
Expand Down Expand Up @@ -108,12 +110,44 @@ public SparkCollection<T> join(JoinExpressionRequest joinRequest) {
@Override
public DataframeCollection toDataframeCollection(Schema schema) {
StructType sparkSchema = DataFrames.toDataType(schema);
ExpressionEncoder<Row> encoder = RowEncoder.apply(sparkSchema);
ExpressionEncoder<Row> encoder = getRowEncoder(sparkSchema);
Dataset<StructuredRecord> ds = (Dataset<StructuredRecord>) getDataset();
MapFunction<StructuredRecord, Row> converter = r -> DataFrames.toRow(r, sparkSchema);
return new DataframeCollection(schema, ds.map(converter, encoder),
sec, jsc, sqlContext, datasetContext, sinkFactory, functionCacheFactory);
}

/**
* This is required to handle breaking changes between spark 3.3.2 (Dataproc 2.1) to 3.5.1 (Dataproc 2.2).
* And we need to support both.
* Here we are trying to check if the new method introduced in 3.5.1 exists or not, and based on that we
* invoke the new method or the old one.
*/
public static ExpressionEncoder<Row> getRowEncoder(StructType sparkSchema) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please cache the Method objects retrieved? Instrospection is a pretty heavy operation, we can do it once and then simply do "invoke".


StringBuilder errorStrBuilder = new StringBuilder("Failed to load a suitable Encoder dynamically. Errors : ");

try {
// Check if the `RowEncoder` class has the `encoderFor` method. This is for spark 3.5
Method encoderForMethod = RowEncoder.class.getMethod("encoderFor", StructType.class);
Object agnosticEncoderObj = encoderForMethod.invoke(null, sparkSchema);

Method applyMethod = ExpressionEncoder.class.getMethod("apply",
Class.forName("org.apache.spark.sql.catalyst.encoders.AgnosticEncoder"));

return (ExpressionEncoder<Row>)applyMethod.invoke(null, agnosticEncoderObj);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
errorStrBuilder.append(System.lineSeparator()).append(e.getMessage());
}

try {
// If it reaches here, meaning it should have the older method for 3.3.2 and lower.
Method applyMethod = RowEncoder.class.getMethod("apply", StructType.class);
return (ExpressionEncoder<Row>) applyMethod.invoke(null, sparkSchema);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
errorStrBuilder.append(System.lineSeparator()).append(e.getMessage());
}

throw new RuntimeException(errorStrBuilder.toString());
}
}
Loading