Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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 @@ -234,7 +234,13 @@ class V2ExpressionBuilder(e: Expression, isPredicate: Boolean = false) {
if (l.isDefined && r.isDefined) {
b match {
case _: Predicate =>
Some(new V2Predicate(b.sqlOperator, Array[V2Expression](l.get, r.get)))
if (l.get.isInstanceOf[LiteralValue[_]] && r.get.isInstanceOf[FieldReference]) {
Some(new V2Predicate(flipComparisonOperatorName(b.sqlOperator),
Array[V2Expression](r.get, l.get)))
} else {
Some(new V2Predicate(b.sqlOperator, Array[V2Expression](l.get, r.get)))
}

case _ =>
Some(new GeneralScalarExpression(b.sqlOperator, Array[V2Expression](l.get, r.get)))
}
Expand Down Expand Up @@ -408,6 +414,16 @@ class V2ExpressionBuilder(e: Expression, isPredicate: Boolean = false) {
}
case _ => None
}

private def flipComparisonOperatorName(operatorName: String): String = {
operatorName match {
case ">" => "<"
case "<" => ">"
case ">=" => "<="
case "<=" => ">="
case _ => operatorName
}
}
}

object ColumnOrField {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,77 @@
package org.apache.spark.sql.execution.datasources.v2

import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.expressions.Expression
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.connector.expressions.{FieldReference, LiteralValue}
import org.apache.spark.sql.connector.expressions.filter.Predicate
import org.apache.spark.sql.test.SharedSparkSession
import org.apache.spark.sql.types.BooleanType
import org.apache.spark.sql.types.{BooleanType, IntegerType, StringType, StructField, StructType}

class DataSourceV2StrategySuite extends PlanTest with SharedSparkSession {
val attrInts = Seq(
$"cint".int,
$"c.int".int,
GetStructField($"a".struct(StructType(
StructField("cstr", StringType, nullable = true) ::
StructField("cint", IntegerType, nullable = true) :: Nil)), 1, None),
GetStructField($"a".struct(StructType(
StructField("c.int", IntegerType, nullable = true) ::
StructField("cstr", StringType, nullable = true) :: Nil)), 0, None),
GetStructField($"a.b".struct(StructType(
StructField("cstr1", StringType, nullable = true) ::
StructField("cstr2", StringType, nullable = true) ::
StructField("cint", IntegerType, nullable = true) :: Nil)), 2, None),
GetStructField($"a.b".struct(StructType(
StructField("c.int", IntegerType, nullable = true) :: Nil)), 0, None),
GetStructField(GetStructField($"a".struct(StructType(
StructField("cstr1", StringType, nullable = true) ::
StructField("b", StructType(StructField("cint", IntegerType, nullable = true) ::
StructField("cstr2", StringType, nullable = true) :: Nil)) :: Nil)), 1, None), 0, None)
).zip(Seq(
"cint",
"`c.int`", // single level field that contains `dot` in name
"a.cint", // two level nested field
"a.`c.int`", // two level nested field, and nested level contains `dot`
"`a.b`.cint", // two level nested field, and top level contains `dot`
"`a.b`.`c.int`", // two level nested field, and both levels contain `dot`
"a.b.cint" // three level nested field
))

test("translate simple expression") { attrInts
.foreach { case (attrInt, intColName) =>
testTranslateFilter(EqualTo(attrInt, 1),
Some(new Predicate("=", Array(FieldReference(intColName), LiteralValue(1, IntegerType)))))
testTranslateFilter(EqualTo(1, attrInt),
Some(new Predicate("=", Array(FieldReference(intColName), LiteralValue(1, IntegerType)))))

testTranslateFilter(EqualNullSafe(attrInt, 1),
Some(new Predicate("<=>", Array(FieldReference(intColName), LiteralValue(1, IntegerType)))))
testTranslateFilter(EqualNullSafe(1, attrInt),
Some(new Predicate("<=>", Array(FieldReference(intColName), LiteralValue(1, IntegerType)))))

testTranslateFilter(GreaterThan(attrInt, 1),
Some(new Predicate(">", Array(FieldReference(intColName), LiteralValue(1, IntegerType)))))
testTranslateFilter(GreaterThan(1, attrInt),
Some(new Predicate("<", Array(FieldReference(intColName), LiteralValue(1, IntegerType)))))

testTranslateFilter(LessThan(attrInt, 1),
Some(new Predicate("<", Array(FieldReference(intColName), LiteralValue(1, IntegerType)))))
testTranslateFilter(LessThan(1, attrInt),
Some(new Predicate(">", Array(FieldReference(intColName), LiteralValue(1, IntegerType)))))

testTranslateFilter(GreaterThanOrEqual(attrInt, 1),
Some(new Predicate(">=", Array(FieldReference(intColName), LiteralValue(1, IntegerType)))))
testTranslateFilter(GreaterThanOrEqual(1, attrInt),
Some(new Predicate("<=", Array(FieldReference(intColName), LiteralValue(1, IntegerType)))))

testTranslateFilter(LessThanOrEqual(attrInt, 1),
Some(new Predicate("<=", Array(FieldReference(intColName), LiteralValue(1, IntegerType)))))
testTranslateFilter(LessThanOrEqual(1, attrInt),
Some(new Predicate(">=", Array(FieldReference(intColName), LiteralValue(1, IntegerType)))))
}
}

test("SPARK-36644: Push down boolean column filter") {
testTranslateFilter($"col".boolean,
Some(new Predicate("=", Array(FieldReference("col"), LiteralValue(true, BooleanType)))))
Expand Down