|
18 | 18 | package org.apache.spark.sql.execution.datasources.v2 |
19 | 19 |
|
20 | 20 | import org.apache.spark.sql.catalyst.dsl.expressions._ |
21 | | -import org.apache.spark.sql.catalyst.expressions.Expression |
| 21 | +import org.apache.spark.sql.catalyst.expressions._ |
22 | 22 | import org.apache.spark.sql.catalyst.plans.PlanTest |
23 | 23 | import org.apache.spark.sql.connector.expressions.{FieldReference, LiteralValue} |
24 | 24 | import org.apache.spark.sql.connector.expressions.filter.Predicate |
25 | 25 | import org.apache.spark.sql.test.SharedSparkSession |
26 | | -import org.apache.spark.sql.types.BooleanType |
| 26 | +import org.apache.spark.sql.types.{BooleanType, IntegerType, StringType, StructField, StructType} |
27 | 27 |
|
28 | 28 | class DataSourceV2StrategySuite extends PlanTest with SharedSparkSession { |
| 29 | + val attrInts = Seq( |
| 30 | + $"cint".int, |
| 31 | + $"c.int".int, |
| 32 | + GetStructField($"a".struct(StructType( |
| 33 | + StructField("cstr", StringType, nullable = true) :: |
| 34 | + StructField("cint", IntegerType, nullable = true) :: Nil)), 1, None), |
| 35 | + GetStructField($"a".struct(StructType( |
| 36 | + StructField("c.int", IntegerType, nullable = true) :: |
| 37 | + StructField("cstr", StringType, nullable = true) :: Nil)), 0, None), |
| 38 | + GetStructField($"a.b".struct(StructType( |
| 39 | + StructField("cstr1", StringType, nullable = true) :: |
| 40 | + StructField("cstr2", StringType, nullable = true) :: |
| 41 | + StructField("cint", IntegerType, nullable = true) :: Nil)), 2, None), |
| 42 | + GetStructField($"a.b".struct(StructType( |
| 43 | + StructField("c.int", IntegerType, nullable = true) :: Nil)), 0, None), |
| 44 | + GetStructField(GetStructField($"a".struct(StructType( |
| 45 | + StructField("cstr1", StringType, nullable = true) :: |
| 46 | + StructField("b", StructType(StructField("cint", IntegerType, nullable = true) :: |
| 47 | + StructField("cstr2", StringType, nullable = true) :: Nil)) :: Nil)), 1, None), 0, None) |
| 48 | + ).zip(Seq( |
| 49 | + "cint", |
| 50 | + "`c.int`", // single level field that contains `dot` in name |
| 51 | + "a.cint", // two level nested field |
| 52 | + "a.`c.int`", // two level nested field, and nested level contains `dot` |
| 53 | + "`a.b`.cint", // two level nested field, and top level contains `dot` |
| 54 | + "`a.b`.`c.int`", // two level nested field, and both levels contain `dot` |
| 55 | + "a.b.cint" // three level nested field |
| 56 | + )) |
| 57 | + |
| 58 | + test("SPARK-39784: translate binary expression") { attrInts |
| 59 | + .foreach { case (attrInt, intColName) => |
| 60 | + testTranslateFilter(EqualTo(attrInt, 1), |
| 61 | + Some(new Predicate("=", Array(FieldReference(intColName), LiteralValue(1, IntegerType))))) |
| 62 | + testTranslateFilter(EqualTo(1, attrInt), |
| 63 | + Some(new Predicate("=", Array(FieldReference(intColName), LiteralValue(1, IntegerType))))) |
| 64 | + |
| 65 | + testTranslateFilter(EqualNullSafe(attrInt, 1), |
| 66 | + Some(new Predicate("<=>", Array(FieldReference(intColName), LiteralValue(1, IntegerType))))) |
| 67 | + testTranslateFilter(EqualNullSafe(1, attrInt), |
| 68 | + Some(new Predicate("<=>", Array(FieldReference(intColName), LiteralValue(1, IntegerType))))) |
| 69 | + |
| 70 | + testTranslateFilter(GreaterThan(attrInt, 1), |
| 71 | + Some(new Predicate(">", Array(FieldReference(intColName), LiteralValue(1, IntegerType))))) |
| 72 | + testTranslateFilter(GreaterThan(1, attrInt), |
| 73 | + Some(new Predicate("<", Array(FieldReference(intColName), LiteralValue(1, IntegerType))))) |
| 74 | + |
| 75 | + testTranslateFilter(LessThan(attrInt, 1), |
| 76 | + Some(new Predicate("<", Array(FieldReference(intColName), LiteralValue(1, IntegerType))))) |
| 77 | + testTranslateFilter(LessThan(1, attrInt), |
| 78 | + Some(new Predicate(">", Array(FieldReference(intColName), LiteralValue(1, IntegerType))))) |
| 79 | + |
| 80 | + testTranslateFilter(GreaterThanOrEqual(attrInt, 1), |
| 81 | + Some(new Predicate(">=", Array(FieldReference(intColName), LiteralValue(1, IntegerType))))) |
| 82 | + testTranslateFilter(GreaterThanOrEqual(1, attrInt), |
| 83 | + Some(new Predicate("<=", Array(FieldReference(intColName), LiteralValue(1, IntegerType))))) |
| 84 | + |
| 85 | + testTranslateFilter(LessThanOrEqual(attrInt, 1), |
| 86 | + Some(new Predicate("<=", Array(FieldReference(intColName), LiteralValue(1, IntegerType))))) |
| 87 | + testTranslateFilter(LessThanOrEqual(1, attrInt), |
| 88 | + Some(new Predicate(">=", Array(FieldReference(intColName), LiteralValue(1, IntegerType))))) |
| 89 | + } |
| 90 | + } |
| 91 | + |
29 | 92 | test("SPARK-36644: Push down boolean column filter") { |
30 | 93 | testTranslateFilter($"col".boolean, |
31 | 94 | Some(new Predicate("=", Array(FieldReference("col"), LiteralValue(true, BooleanType))))) |
|
0 commit comments