-
Couldn't load subscription status.
- Fork 28.9k
[SPARK-24361][SQL] Polish code block manipulation API #21405
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
Changes from all commits
1df9943
5fe425c
00bef6b
5d9c454
162deb2
d138ee0
ee9a4c0
e7cfa28
5945c15
2b30654
aff411b
53b329a
72faac3
ffbf4ab
d040676
c378ce2
2ca9741
d91f111
5c40233
8d0b1b9
2d77332
e30be7a
21411f5
ce8d9ed
707dd6f
c9dee44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,7 +65,9 @@ class CodeBlockSuite extends SparkFunSuite { | |
| |boolean $isNull = false; | ||
| |int $value = -1; | ||
| """.stripMargin | ||
| val exprValues = code.exprValues | ||
| val exprValues = code.asInstanceOf[CodeBlock].blockInputs.collect { | ||
| case e: ExprValue => e | ||
| }.toSet | ||
| assert(exprValues.size == 2) | ||
| assert(exprValues === Set(value, isNull)) | ||
| } | ||
|
|
@@ -94,7 +96,9 @@ class CodeBlockSuite extends SparkFunSuite { | |
|
|
||
| assert(code.toString == expected) | ||
|
|
||
| val exprValues = code.exprValues | ||
| val exprValues = code.children.flatMap(_.asInstanceOf[CodeBlock].blockInputs.collect { | ||
| case e: ExprValue => e | ||
| }).toSet | ||
| assert(exprValues.size == 5) | ||
| assert(exprValues === Set(isNull1, value1, isNull2, value2, literal)) | ||
| } | ||
|
|
@@ -107,7 +111,7 @@ class CodeBlockSuite extends SparkFunSuite { | |
| assert(e.getMessage().contains(s"Can not interpolate ${obj.getClass.getName}")) | ||
| } | ||
|
|
||
| test("replace expr values in code block") { | ||
| test("transform expr in code block") { | ||
| val expr = JavaCode.expression("1 + 1", IntegerType) | ||
| val isNull = JavaCode.isNullVariable("expr1_isNull") | ||
| val exprInFunc = JavaCode.variable("expr1", IntegerType) | ||
|
|
@@ -120,11 +124,11 @@ class CodeBlockSuite extends SparkFunSuite { | |
| |}""".stripMargin | ||
|
|
||
| val aliasedParam = JavaCode.variable("aliased", expr.javaType) | ||
| val aliasedInputs = code.asInstanceOf[CodeBlock].blockInputs.map { | ||
| case _: SimpleExprValue => aliasedParam | ||
| case other => other | ||
|
|
||
| // We want to replace all occurrences of `expr` with the variable `aliasedParam`. | ||
| val aliasedCode = code.transformExprValues { | ||
| case SimpleExprValue("1 + 1", java.lang.Integer.TYPE) => aliasedParam | ||
| } | ||
| val aliasedCode = CodeBlock(code.asInstanceOf[CodeBlock].codeParts, aliasedInputs).stripMargin | ||
| val expected = | ||
| code""" | ||
| |callFunc(int $aliasedParam) { | ||
|
|
@@ -133,4 +137,61 @@ class CodeBlockSuite extends SparkFunSuite { | |
| |}""".stripMargin | ||
| assert(aliasedCode.toString == expected.toString) | ||
| } | ||
|
|
||
| test ("transform expr in nested blocks") { | ||
| val expr = JavaCode.expression("1 + 1", IntegerType) | ||
| val isNull = JavaCode.isNullVariable("expr1_isNull") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not related to this PR, shall we remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is fine and maybe more clear. I was thinking the name of |
||
| val exprInFunc = JavaCode.variable("expr1", IntegerType) | ||
|
|
||
| val funcs = Seq("callFunc1", "callFunc2", "callFunc3") | ||
| val subBlocks = funcs.map { funcName => | ||
| code""" | ||
| |$funcName(int $expr) { | ||
| | boolean $isNull = false; | ||
| | int $exprInFunc = $expr + 1; | ||
| |}""".stripMargin | ||
| } | ||
|
|
||
| val aliasedParam = JavaCode.variable("aliased", expr.javaType) | ||
|
|
||
| val block = code"${subBlocks(0)}\n${subBlocks(1)}\n${subBlocks(2)}" | ||
| val transformedBlock = block.transform { | ||
| case b: Block => b.transformExprValues { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmm, I'd image There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so the this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then I may call it |
||
| case SimpleExprValue("1 + 1", java.lang.Integer.TYPE) => aliasedParam | ||
| } | ||
| }.asInstanceOf[CodeBlock] | ||
|
|
||
| val expected1 = | ||
| code""" | ||
| |callFunc1(int aliased) { | ||
| | boolean expr1_isNull = false; | ||
| | int expr1 = aliased + 1; | ||
| |}""".stripMargin | ||
|
|
||
| val expected2 = | ||
| code""" | ||
| |callFunc2(int aliased) { | ||
| | boolean expr1_isNull = false; | ||
| | int expr1 = aliased + 1; | ||
| |}""".stripMargin | ||
|
|
||
| val expected3 = | ||
| code""" | ||
| |callFunc3(int aliased) { | ||
| | boolean expr1_isNull = false; | ||
| | int expr1 = aliased + 1; | ||
| |}""".stripMargin | ||
|
|
||
| val exprValues = transformedBlock.children.flatMap { block => | ||
| block.asInstanceOf[CodeBlock].blockInputs.collect { | ||
| case e: ExprValue => e | ||
| } | ||
| }.toSet | ||
|
|
||
| assert(transformedBlock.children(0).toString == expected1.toString) | ||
| assert(transformedBlock.children(1).toString == expected2.toString) | ||
| assert(transformedBlock.children(2).toString == expected3.toString) | ||
| assert(transformedBlock.toString == (expected1 + expected2 + expected3).toString) | ||
| assert(exprValues === Set(isNull, exprInFunc, aliasedParam)) | ||
| } | ||
| } | ||
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.
am I wrong or we are not updating the
exprValueshere?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.
transformExprValueswill create a new instanceBlockbecause a block is immutable. So once there are change, new block should take newexprValues.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, can we add some tests for this?
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.
Ok. Will add them in next commit. Thanks.
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.
Added test for it.