-
Notifications
You must be signed in to change notification settings - Fork 200
Coral-Spark: Migrate 'CAST(ROW: RECORD_TYPE)' and 'extract_union' transformations from RelNode to SqlNode layer #354
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
Merged
ljfgem
merged 3 commits into
linkedin:master
from
ljfgem:coral_spark_transformation_migration
Feb 14, 2023
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
53 changes: 53 additions & 0 deletions
53
...ark/src/main/java/com/linkedin/coral/spark/transformers/CastToNamedStructTransformer.java
This file contains hidden or 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 @@ | ||
| /** | ||
| * Copyright 2023 LinkedIn Corporation. All rights reserved. | ||
| * Licensed under the BSD-2 Clause license. | ||
| * See LICENSE in the project root for license information. | ||
| */ | ||
| package com.linkedin.coral.spark.transformers; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.calcite.sql.SqlCall; | ||
| import org.apache.calcite.sql.SqlIdentifier; | ||
| import org.apache.calcite.sql.SqlKind; | ||
| import org.apache.calcite.sql.SqlNode; | ||
| import org.apache.calcite.sql.SqlRowTypeNameSpec; | ||
| import org.apache.calcite.sql.SqlRowTypeSpec; | ||
| import org.apache.calcite.sql.parser.SqlParserPos; | ||
|
|
||
| import com.linkedin.coral.common.transformers.SqlCallTransformer; | ||
| import com.linkedin.coral.hive.hive2rel.functions.HiveNamedStructFunction; | ||
|
|
||
|
|
||
| /** | ||
| * This transformer transforms `CAST(ROW: RECORD_TYPE)` to `named_struct` in Spark. | ||
| * For example, the SqlCall `CAST(ROW(123, 'xyz') AS ROW(`abc` INTEGER, `def` CHAR(3) CHARACTER SET `ISO-8859-1`))` | ||
| * will be transformed to `named_struct('abc', 123, 'def', 'xyz')` | ||
| */ | ||
| public class CastToNamedStructTransformer extends SqlCallTransformer { | ||
| @Override | ||
| protected boolean condition(SqlCall sqlCall) { | ||
| if (sqlCall.getOperator().getKind() == SqlKind.CAST) { | ||
| final SqlNode firstOperand = sqlCall.getOperandList().get(0); | ||
| final SqlNode secondOperand = sqlCall.getOperandList().get(1); | ||
| return firstOperand instanceof SqlCall && ((SqlCall) firstOperand).getOperator().getKind() == SqlKind.ROW | ||
| && secondOperand instanceof SqlRowTypeSpec; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| protected SqlCall transform(SqlCall sqlCall) { | ||
| List<SqlNode> newOperands = new ArrayList<>(); | ||
| final SqlCall rowCall = (SqlCall) sqlCall.getOperandList().get(0); // like `ROW(123, 'xyz')` in above example | ||
| final SqlRowTypeSpec sqlRowTypeSpec = (SqlRowTypeSpec) sqlCall.getOperandList().get(1); // like `ROW(`abc` INTEGER, `def` CHAR(3) CHARACTER SET `ISO-8859-1`))` in above example | ||
| for (int i = 0; i < rowCall.getOperandList().size(); ++i) { | ||
| final String fieldName = | ||
| ((SqlRowTypeNameSpec) sqlRowTypeSpec.getTypeNameSpec()).getFieldNames().get(i).names.get(0); | ||
| newOperands.add(new SqlIdentifier("'" + fieldName + "'", SqlParserPos.ZERO)); // need to single-quote the field name | ||
| newOperands.add(rowCall.getOperandList().get(i)); | ||
| } | ||
| return HiveNamedStructFunction.NAMED_STRUCT.createCall(sqlCall.getParserPosition(), newOperands); | ||
ljfgem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
69 changes: 69 additions & 0 deletions
69
.../src/main/java/com/linkedin/coral/spark/transformers/ExtractUnionFunctionTransformer.java
This file contains hidden or 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,69 @@ | ||
| /** | ||
| * Copyright 2023 LinkedIn Corporation. All rights reserved. | ||
| * Licensed under the BSD-2 Clause license. | ||
| * See LICENSE in the project root for license information. | ||
| */ | ||
| package com.linkedin.coral.spark.transformers; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import org.apache.calcite.sql.SqlCall; | ||
| import org.apache.calcite.sql.SqlNode; | ||
| import org.apache.calcite.sql.SqlNumericLiteral; | ||
| import org.apache.calcite.sql.SqlOperator; | ||
| import org.apache.calcite.sql.parser.SqlParserPos; | ||
|
|
||
| import com.linkedin.coral.com.google.common.collect.ImmutableList; | ||
| import com.linkedin.coral.common.transformers.SqlCallTransformer; | ||
| import com.linkedin.coral.hive.hive2rel.functions.CoalesceStructUtility; | ||
| import com.linkedin.coral.spark.containers.SparkUDFInfo; | ||
|
|
||
|
|
||
| /** | ||
| * This transformer transforms `extract_union` to `coalesce_struct`. | ||
| * Instead of leaving `extract_union` visible to Spark, since we adopted the new exploded struct schema (a.k.a struct_tr) | ||
| * that is different from extract_union's output (a.k.a struct_ex) to interpret union in Coral IR, | ||
| * we need to swap the reference of `extract_union` to a new UDF that is coalescing the difference between | ||
| * struct_tr and struct_ex. | ||
| * See {@link CoalesceStructUtility#COALESCE_STRUCT_FUNCTION_RETURN_STRATEGY} and its comments for more details. | ||
ljfgem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * Check `CoralSparkTest#testUnionExtractUDF` for examples. | ||
| */ | ||
| public class ExtractUnionFunctionTransformer extends SqlCallTransformer { | ||
| private static final String EXTRACT_UNION = "extract_union"; | ||
| private static final String COALESCE_STRUCT = "coalesce_struct"; | ||
|
|
||
| private final Set<SparkUDFInfo> sparkUDFInfos; | ||
|
|
||
| public ExtractUnionFunctionTransformer(Set<SparkUDFInfo> sparkUDFInfos) { | ||
| this.sparkUDFInfos = sparkUDFInfos; | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean condition(SqlCall sqlCall) { | ||
| return EXTRACT_UNION.equalsIgnoreCase(sqlCall.getOperator().getName()); | ||
| } | ||
|
|
||
| @Override | ||
| protected SqlCall transform(SqlCall sqlCall) { | ||
| sparkUDFInfos.add(new SparkUDFInfo("com.linkedin.coalescestruct.GenericUDFCoalesceStruct", "coalesce_struct", | ||
| ImmutableList.of(URI.create("ivy://com.linkedin.coalesce-struct:coalesce-struct-impl:+")), | ||
| SparkUDFInfo.UDFTYPE.HIVE_CUSTOM_UDF)); | ||
| final List<SqlNode> operandList = sqlCall.getOperandList(); | ||
| final SqlOperator coalesceStructFunction = | ||
| createSqlOperator(COALESCE_STRUCT, CoalesceStructUtility.COALESCE_STRUCT_FUNCTION_RETURN_STRATEGY); | ||
| if (operandList.size() == 1) { | ||
| // one arg case: extract_union(field_name) | ||
| return coalesceStructFunction.createCall(sqlCall.getParserPosition(), operandList); | ||
| } else if (operandList.size() == 2) { | ||
| // two arg case: extract_union(field_name, ordinal) | ||
| final int newOrdinal = ((SqlNumericLiteral) operandList.get(1)).getValueAs(Integer.class) + 1; | ||
| return coalesceStructFunction.createCall(sqlCall.getParserPosition(), ImmutableList.of(operandList.get(0), | ||
| SqlNumericLiteral.createExactNumeric(String.valueOf(newOrdinal), SqlParserPos.ZERO))); | ||
| } else { | ||
| return sqlCall; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: transforms Coral IR function ..... to Spark compatible operator
named_struct....There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's suitable to call it
Coral IRfunction, I think it's a function in CoralSqlNode converted from Coral IR?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the path:
source SQL -> sourceSqlNode1 -> CoralSqlNode1 -> CoralRelNode -> CoralSqlNode2 -> TargetLangSqlNode -> target SQLif an operator is present in CoralSqlNode1, CoralRelNode, CoralSqlNode2 and we transform this operator when we go to LanguageSqlNode, such operators can be called Coral IR functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as per the current status of our code base -
hive SQL -> HiveSqlNode (same as CoralSqlNode1) -> CoralRelNode -> CoralSqlNode2 -> SparkSqlNode -> Spark SQLI think this operator qualifies as Coral Operator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarification, do you mean an operator is a Coral IR function if it appears in
CoralSqlNode1 / CoralRelNode / CoralSqlNode2orCoralSqlNode1 & CoralRelNode & CoralSqlNode2?Not sure because it doesn't appear in
CoralSqlNode1.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for a corresponding input hive SQL, what is the operator we use in the SqlNode representation? how do we represent the named_struct on LHS?
but generally yes, an operator present in CoralSqlNode1 would be present in CoralRelNode and then generated back in CoralSqlNode2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's still represented as
named_structin CoralSqlNode1, and Calcite converts it toCAST(ROW: RECORD_TYPE)in RelNode.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. In that case, could you investigate if it's possible to add a lightweight override in
CoralRelNodeToCoralSqlNodeto such that the coralSqlNode2 generated uses the same operatornamed_structas coralSqlNode1?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened #357 for tracking.