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

[SPARK-44930][SQL] Deterministic ApplyFunctionExpression should be foldable #42629

Closed
wants to merge 4 commits into from

Conversation

ConeyLiu
Copy link
Contributor

What changes were proposed in this pull request?

Currently, ApplyFunctionExpression is unfoldable because inherits the default value from Expression. However, it should be foldable for a deterministic ApplyFunctionExpression.

Why are the changes needed?

This could help optimize the usage for V2 UDF applying to constant expressions.

Does this PR introduce any user-facing change?

No

How was this patch tested?

New UT.

Was this patch authored or co-authored using generative AI tooling?

No.

@github-actions github-actions bot added the SQL label Aug 23, 2023
@ConeyLiu
Copy link
Contributor Author

Hi, @cloud-fan @rdblue @beliefer @LuciferYang @sunchao could you help to review this? Thanks a lot.

@@ -425,3 +447,14 @@ case class SerializableBoxedInt(intVal: Int) {
class NotSerializableBoxedInt(intVal: Int) {
def addAsInt(other: Int): Int = intVal + other
}

case object CharLength extends ScalarFunction[Int] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we reuse the CharLength defined at JDBCV2Suite?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is located in sql/core module.

Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't matter. You can move it from catalyst to core.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The CharLength is defined in sql/core. Do you mean to move it from sql/core to sql/catalyst?

Copy link
Contributor

Choose a reason for hiding this comment

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

We can just add a new test case in DataSourceV2FunctionSuite to check ApplyFunctionExpression#foldable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved the UT in DataSourceV2FunctionSuite

errorClass = null,
parameters = Map.empty
)
intercept[UnsupportedOperationException](sql("SELECT testcat.ns.strlen('abc')").collect())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exception throws in constant folding stage instead of execution stage.

Copy link
Contributor

Choose a reason for hiding this comment

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

shall we still use checkError?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

checkError require an exception with the type of SparkThrowable

@@ -341,7 +333,7 @@ class DataSourceV2FunctionSuite extends DatasourceV2SQLBase {
test("scalar function: no implementation found") {
catalog("testcat").asInstanceOf[SupportsNamespaces].createNamespace(Array("ns"), emptyProps)
addFunction(Identifier.of(Array("ns"), "strlen"), StrLen(StrLenNoImpl))
intercept[SparkException](sql("SELECT testcat.ns.strlen('abc')").collect())
intercept[UnsupportedOperationException](sql("SELECT testcat.ns.strlen('abc')").collect())
Copy link
Contributor Author

@ConeyLiu ConeyLiu Aug 23, 2023

Choose a reason for hiding this comment

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

same here

Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Member

@sunchao sunchao left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -415,6 +417,26 @@ class ConstantFoldingSuite extends PlanTest {
}
}
}

test("Fold deterministic ApplyFunctionExpression") {
Copy link
Member

Choose a reason for hiding this comment

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

nit: can we put JIRA name here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

@cloud-fan
Copy link
Contributor

thanks, mering to master/3.5!

@cloud-fan cloud-fan closed this in 994389f Aug 25, 2023
cloud-fan pushed a commit that referenced this pull request Aug 25, 2023
…ldable

### What changes were proposed in this pull request?

Currently, ApplyFunctionExpression is unfoldable because inherits the default value from Expression.  However, it should be foldable for a deterministic ApplyFunctionExpression.

### Why are the changes needed?

This could help optimize the usage for V2 UDF applying to constant expressions.

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

New UT.

### Was this patch authored or co-authored using generative AI tooling?

No.

Closes #42629 from ConeyLiu/constant-fold-v2-udf.

Authored-by: xianyangliu <xianyangliu@tencent.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
(cherry picked from commit 994389f)
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
@ConeyLiu
Copy link
Contributor Author

Thanks @cloud-fan @beliefer @sunchao

@ConeyLiu ConeyLiu deleted the constant-fold-v2-udf branch August 25, 2023 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants