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] Do not cast data type when not needed in DML commands #4158

Merged
merged 1 commit into from
Feb 14, 2025

Conversation

xupefei
Copy link
Collaborator

@xupefei xupefei commented Feb 14, 2025

Which Delta project/connector is this regarding?

  • Spark
  • Standalone
  • Flink
  • Kernel
  • Other (fill in here)

Description

This PR changes the logic of data type casting in DML operations, so that when the expression has the desired output type no cast will be performed. This is to avoid a performance issue when the expression is slow to evaluate, such as for array functions on an array of structs, i.e., array_*(array<struct<...>>, ...).

For example, given array_distinct(physical_ids) inside a WHEN UPDATE clause:

CASE WHEN (size(physical_ids#270, false) = 0) THEN array_except(physical_ids#1611, []) ELSE [] END

It will be first transformed into

array_distinct(transform(physical_ids, (_, idx) -> physical_ids[idx]))

and then to

transform(
  when(size(to_add) != 0, array_distinct(physical_ids), to_remove),
  (_, idx) ->
    when(size(to_add) != 0, array_distinct(physical_ids), to_remove)[idx]

which triples the amount of calculations.

After this PR the transformation will not happen again.

How was this patch tested?

Manually inspecting the query plan.

Does this PR introduce any user-facing changes?

No.

@xupefei xupefei requested a review from johanl-db February 14, 2025 13:55
@xupefei xupefei changed the title Do not cast data type when not needed Do not cast data type when not needed in DML commands Feb 14, 2025
@xupefei xupefei changed the title Do not cast data type when not needed in DML commands [Spark] Do not cast data type when not needed in DML commands Feb 14, 2025
@allisonport-db allisonport-db merged commit 41a4e81 into delta-io:master Feb 14, 2025
16 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants