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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

s"($name) in (${values.mkString(", ")})"
}

def convertNotInToAnd(name: String, values: Seq[String]): String = {
values.map(value => s"$name != $value").mkString("(", " and ", ")")
def convertNotIn(name: String, values: Seq[String]): String = {
s"($name) not in (${values.mkString(", ")})"
}

def hasNullLiteral(list: Seq[Expression]): Boolean = list.exists {
Expand Down Expand Up @@ -786,11 +786,11 @@ private[client] class Shim_v2_0 extends Shim with Logging {

case In(ExtractAttribute(SupportedAttribute(name)), ExtractableLiterals(values))
if useAdvanced =>
Some(convertInToOr(name, values))
Some(convertIn(name, values))

case Not(In(ExtractAttribute(SupportedAttribute(name)), ExtractableLiterals(values)))
if useAdvanced =>
Some(convertNotInToAnd(name, values))
Some(convertNotIn(name, values))

case InSet(child, values) if useAdvanced && values.size > inSetThreshold =>
val dataType = child.dataType
Expand All @@ -802,19 +802,19 @@ private[client] class Shim_v2_0 extends Shim with Logging {

case InSet(child @ ExtractAttribute(SupportedAttribute(name)), ExtractableDateValues(values))
if useAdvanced && child.dataType == DateType =>
Some(convertInToOr(name, values))
Some(convertIn(name, values))

case Not(InSet(child @ ExtractAttribute(SupportedAttribute(name)),
ExtractableDateValues(values))) if useAdvanced && child.dataType == DateType =>
Some(convertNotInToAnd(name, values))
Some(convertNotIn(name, values))

case InSet(ExtractAttribute(SupportedAttribute(name)), ExtractableValues(values))
if useAdvanced =>
Some(convertInToOr(name, values))
Some(convertIn(name, values))

case Not(InSet(ExtractAttribute(SupportedAttribute(name)), ExtractableValues(values)))
if useAdvanced =>
Some(convertNotInToAnd(name, values))
Some(convertNotIn(name, values))

case op @ SpecialBinaryComparison(
ExtractAttribute(SupportedAttribute(name)), ExtractableLiteral(value)) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class FiltersSuite extends SparkFunSuite with PlanTest {
filterTest("date filter with IN predicate",
(a("datecol", DateType) in
(Literal(Date.valueOf("2019-01-01")), Literal(Date.valueOf("2019-01-07")))) :: Nil,
"(datecol = \"2019-01-01\" or datecol = \"2019-01-07\")")
"(datecol) in (\"2019-01-01\", \"2019-01-07\")")

filterTest("date and string filter",
(Literal(Date.valueOf("2019-01-01")) === a("datecol", DateType)) ::
Expand All @@ -84,7 +84,7 @@ class FiltersSuite extends SparkFunSuite with PlanTest {

filterTest("string filter with InSet predicate",
InSet(a("strcol", StringType), Set("1", "2").map(s => UTF8String.fromString(s))) :: Nil,
"(strcol = \"1\" or strcol = \"2\")")
"(strcol) in (\"1\", \"2\")")

filterTest("skip varchar",
(Literal("") === a("varchar", StringType)) :: Nil,
Expand All @@ -97,7 +97,7 @@ class FiltersSuite extends SparkFunSuite with PlanTest {

filterTest("SPARK-24879 null literals should be ignored for IN constructs",
(a("intcol", IntegerType) in (Literal(1), Literal(null))) :: Nil,
"(intcol = 1)")
"(intcol) in (1)")

filterTest("NOT: int and string filters",
(a("intcol", IntegerType) =!= Literal(1)) :: (Literal("a") =!= a("strcol", IntegerType)) :: Nil,
Expand All @@ -109,7 +109,7 @@ class FiltersSuite extends SparkFunSuite with PlanTest {

filterTest("not-in, string filter",
(Not(In(a("strcol", StringType), Seq(Literal("a"), Literal("b"))))) :: Nil,
"""(strcol != "a" and strcol != "b")""")
"""(strcol) not in ("a", "b")""")

filterTest("not-in, string filter with null",
(Not(In(a("strcol", StringType), Seq(Literal("a"), Literal("b"), Literal(null))))) :: Nil,
Expand All @@ -118,7 +118,7 @@ class FiltersSuite extends SparkFunSuite with PlanTest {
filterTest("not-in, date filter",
(Not(In(a("datecol", DateType),
Seq(Literal(Date.valueOf("2021-01-01")), Literal(Date.valueOf("2021-01-02")))))) :: Nil,
"""(datecol != "2021-01-01" and datecol != "2021-01-02")""")
"""(datecol) not in ("2021-01-01", "2021-01-02")""")

filterTest("not-in, date filter with null",
(Not(In(a("datecol", DateType),
Expand All @@ -128,7 +128,7 @@ class FiltersSuite extends SparkFunSuite with PlanTest {

filterTest("not-inset, string filter",
(Not(InSet(a("strcol", StringType), Set(Literal("a").eval(), Literal("b").eval())))) :: Nil,
"""(strcol != "a" and strcol != "b")""")
"""(strcol) not in ("a", "b")""")

filterTest("not-inset, string filter with null",
(Not(InSet(a("strcol", StringType),
Expand All @@ -139,7 +139,7 @@ class FiltersSuite extends SparkFunSuite with PlanTest {
(Not(InSet(a("datecol", DateType),
Set(Literal(Date.valueOf("2020-01-01")).eval(),
Literal(Date.valueOf("2020-01-02")).eval())))) :: Nil,
"""(datecol != "2020-01-01" and datecol != "2020-01-02")""")
"""(datecol) not in ("2020-01-01", "2020-01-02")""")

filterTest("not-inset, date filter with null",
(Not(InSet(a("datecol", DateType),
Expand Down Expand Up @@ -239,14 +239,14 @@ class FiltersSuite extends SparkFunSuite with PlanTest {
withSQLConf(SQLConf.HIVE_METASTORE_PARTITION_PRUNING_INSET_THRESHOLD.key -> "3") {
val intFilter = InSet(a("p", IntegerType), Set(null, 1, 2))
val intConverted = shim.convertFilters(testTable, Seq(intFilter))
assert(intConverted == "(p = 1 or p = 2)")
assert(intConverted == "(p) in (1, 2)")
}

withSQLConf(SQLConf.HIVE_METASTORE_PARTITION_PRUNING_INSET_THRESHOLD.key -> "3") {
val dateFilter = InSet(a("p", DateType), Set(null,
Literal(Date.valueOf("2020-01-01")).eval(), Literal(Date.valueOf("2021-01-01")).eval()))
val dateConverted = shim.convertFilters(testTable, Seq(dateFilter))
assert(dateConverted == "(p = \"2020-01-01\" or p = \"2021-01-01\")")
assert(dateConverted == "(p) in (\"2020-01-01\", \"2021-01-01\")")
}
}

Expand Down