Skip to content

[SPARK-33538][SQL] Directly push IN/NOT predicates to the Hive Metastore #51132

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

Closed
wants to merge 11 commits into from

Conversation

yuexing
Copy link
Contributor

@yuexing yuexing commented Jun 9, 2025

What changes were proposed in this pull request?

This PR refactors the way IN and NOT IN predicates are converted in HiveShim. Previously, these predicates were expanded into chains of OR/AND comparisons (e.g., a = 1 or a = 2). This change updates the logic to use SQL-native IN and NOT IN syntax (e.g., a in (1, 2)), improving readability and possibly query performance.

Why are the changes needed?

Efficiency:
Many SQL engines optimize IN/NOT IN natively, which can improve performance and reduce query complexity.
Maintainability:
Simplifies predicate generation logic, making the codebase easier to maintain and extend.

Does this PR introduce any user-facing change?

No user-facing changes; this is an internal refactor that affects query generation logic.

How was this patch tested?

Existing unit tests for predicate conversion and query generation should be sufficient to cover the updated logic.

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

No.

@github-actions github-actions bot added the SQL label Jun 9, 2025
@@ -749,12 +749,14 @@ private[client] class Shim_v2_0 extends Shim with Logging {
}
}

def convertInToOr(name: String, values: Seq[String]): String = {
values.map(value => s"$name = $value").mkString("(", " or ", ")")
def convertInDirectly(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def convertInDirectly(
def convertIn(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

}
}


Copy link
Member

Choose a reason for hiding this comment

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

Revert this change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@pan3793
Copy link
Member

pan3793 commented Jun 10, 2025

@yuexing do you have perf numbers for this change? with which kind/version of backend RDBMS of HMS?

@Tagar
Copy link

Tagar commented Jun 10, 2025

Blindly replacing = to IN (...) even for a single-element lookups can actually cause regressions, depending on HMS backend.

Worse performance: IN (...) can skip indexes or change query plans
probably affects pgsql, Oracle, SQL Server, MySQL etc. Needs to be properly tested for regressions!

NULL behavior differences: col = NULL vs col IN (NULL) behave differently.

Plan caching / cursor sharing changes - affects at least Oracle, SQL Server. May be a lesser issue, but could add up for metastores with high qps.

Safer Strategy - only rewrite to IN (...): If there's more than one value.

@Tagar
Copy link

Tagar commented Jun 10, 2025

Separately, spark.sql.hive.metastorePartitionPruningDirectInEnabled is too long? I would not understand what this means.
Perthaps spark.sql.hive.metastore.directInPruning or spark.sql.hive.hms.directIn is better? Just as an option

@yuexing
Copy link
Contributor Author

yuexing commented Jun 11, 2025

Separately, spark.sql.hive.metastorePartitionPruningDirectInEnabled is too long? I would not understand what this means. Perthaps spark.sql.hive.metastore.directInPruning or spark.sql.hive.hms.directIn is better? Just as an option

hi @Tagar Thanks for commenting, let me reply to those questions;

1st, 'in' predicate with null is already skipped in old code, see

def convert(expr: Expression): Option[String] = expr match {
      case Not(InSet(_, values)) if values.size > inSetThreshold =>
        None

      case Not(In(_, list)) if hasNullLiteral(list) => None
      case Not(InSet(_, list)) if list.contains(null) => None
...

2nd, the conf is already removed following the code review comment. please see newest patch

@@ -749,12 +749,12 @@ private[client] class Shim_v2_0 extends Shim with Logging {
}
}

def convertInToOr(name: String, values: Seq[String]): String = {
values.map(value => s"$name = $value").mkString("(", " or ", ")")
def convertIn(name: String, values: Seq[String]): String = {
Copy link
Contributor

Choose a reason for hiding this comment

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

all test passed, friendly ping @wangyum

Copy link
Contributor

@LuciferYang LuciferYang left a comment

Choose a reason for hiding this comment

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

+1, LGTM
@wangyum Do you need to take another look?

@wangyum
Copy link
Member

wangyum commented Jun 16, 2025

Safer Strategy - only rewrite to IN (...): If there's more than one value.

@Tagar OptimizeIn will rewrite In to EqualTo If there's only one value:

if (newList.length == 1
// TODO: `EqualTo` for structural types are not working. Until SPARK-24443 is addressed,
// TODO: we exclude them in this rule.
&& !v.isInstanceOf[CreateNamedStruct]
&& !newList.head.isInstanceOf[CreateNamedStruct]) {
EqualTo(v, newList.head)

@wangyum wangyum changed the title [SPARK-33538][SQL]Directly push IN predicates to the Hive Metastore [SPARK-33538][SQL] Directly push IN/NOT predicates to the Hive Metastore Jun 16, 2025
@LuciferYang
Copy link
Contributor

Merged into master.Thanks @yuexing @wangyum @pan3793 and @Tagar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants