Github user gatorsmile commented on a diff in the pull request: https://github.com/apache/spark/pull/10942#discussion_r51519464 --- Diff: sql/hive/src/test/scala/org/apache/spark/sql/sources/BucketedReadSuite.scala --- @@ -59,6 +61,159 @@ class BucketedReadSuite extends QueryTest with SQLTestUtils with TestHiveSinglet } } + // To verify if the bucket pruning works, this function checks two conditions: + // 1) Check if the pruned buckets (before filtering) are empty. + // 2) Verify the final result is the same as the expected one + private def checkPrunedAnswers( + bucketSpec: BucketSpec, + bucketValues: Seq[Integer], + bucketedDataFrame: DataFrame, + expectedAnswer: DataFrame): Unit = { + + val BucketSpec(numBuckets, bucketColumnNames, _) = bucketSpec + // Limit: bucket pruning only works when the bucket column has one and only one column + assert(bucketColumnNames.length == 1) + val bucketColumnIndex = bucketedDataFrame.schema.fieldIndex(bucketColumnNames.head) + val bucketColumn = bucketedDataFrame.schema.toAttributes.head + val matchedBuckets = new BitSet(numBuckets) + bucketValues.foreach { value => + matchedBuckets.set(DataSourceStrategy.getBucketId(bucketColumn, numBuckets, value)) + } + + // Filter could hide the bug in bucket pruning. Thus, skipping all the filters + val rdd = bucketedDataFrame.queryExecution.executedPlan.find(_.isInstanceOf[PhysicalRDD]) + assert(rdd.isDefined) + + val checkBucketId = rdd.get.execute().map(_.copy()).mapPartitions(iter => { + iter.map(row => + DataSourceStrategy.getBucketId( + bucketColumn, numBuckets, row.get(bucketColumnIndex, bucketColumn.dataType)))}) + // Check if all the returned rows are from the non-pruned buckets + assert(checkBucketId.collect().forall(matchedBuckets.get)) + + checkAnswer( + expectedAnswer + .orderBy(expectedAnswer.logicalPlan.output.map(attr => Column(attr)) : _*), + bucketedDataFrame + .orderBy(bucketedDataFrame.logicalPlan.output.map(attr => Column(attr)) : _*)) + } + + test("read partitioning bucketed tables with bucket pruning filters") { + withTable("bucketed_table") { + val numBuckets = 50 + val bucketSpec = BucketSpec(numBuckets, Seq("j"), Nil) + // json does not support predicate push-down, and thus json is used here + df.write + .format("json") + .partitionBy("i") + .bucketBy(numBuckets, "j") + .saveAsTable("bucketed_table") + + for (j <- 0 until 13) { + // Case 1: EqualTo + checkPrunedAnswers( + bucketSpec, + bucketValues = j :: Nil, + hiveContext.table("bucketed_table") + .select("i", "j", "k").filter($"j" === j), + df.select("i", "j", "k").filter($"j" === j)) + + // Case 2: EqualNullSafe + checkPrunedAnswers( + bucketSpec, + bucketValues = j :: Nil, + hiveContext.table("bucketed_table") + .select("i", "j", "k").filter($"j" <=> j), + df.select("i", "j", "k").filter($"j" <=> j)) + + // Case 3: In + checkPrunedAnswers( + bucketSpec, + bucketValues = Seq(j, j + 1, j + 2, j + 3), + hiveContext.table("bucketed_table") + .select("i", "j", "k").filter($"j".isin(j, j + 1, j + 2, j + 3)), + df.select("i", "j", "k").filter($"j".isin(j, j + 1, j + 2, j + 3))) + } + } + } + + test("read non-partitioning bucketed tables with bucket pruning filters") { + withTable("bucketed_table") { + val numBuckets = 8 + val bucketSpec = BucketSpec(numBuckets, Seq("j"), Nil) + // json does not support predicate push-down, and thus json is used here + df.write + .format("json") + .bucketBy(numBuckets, "j") + .saveAsTable("bucketed_table") + + for (j <- 0 until 13) { + checkPrunedAnswers( + bucketSpec, + bucketValues = j :: Nil, + hiveContext.table("bucketed_table") + .select("i", "j", "k").filter($"j" === j), + df.select("i", "j", "k").filter($"j" === j)) + } + } + } + + test("read partitioning bucketed tables having null in bucketing key") { + withTable("bucketed_table") { + val numBuckets = 8 + val bucketSpec = BucketSpec(numBuckets, Seq("s"), Nil) + // json does not support predicate push-down, and thus json is used here + nullStrings.write + .format("json") + .partitionBy("n") + .bucketBy(numBuckets, "s") + .saveAsTable("bucketed_table") + + // Case 1: isNull + checkPrunedAnswers( + bucketSpec, + bucketValues = null :: Nil, + hiveContext.table("bucketed_table").select("n", "s").filter($"s".isNull), + nullStrings.select("n", "s").filter($"s".isNull)) + + // Case 2: <=> null + checkPrunedAnswers( + bucketSpec, + bucketValues = null :: Nil, + hiveContext.table("bucketed_table").select("n", "s").filter($"s" <=> null), + nullStrings.select("n", "s").filter($"s" <=> null)) + } + } + + test("read partitioning bucketed tables having composite filters") { + withTable("bucketed_table") { + val numBuckets = 8 + val bucketSpec = BucketSpec(numBuckets, Seq("j"), Nil) + // json does not support predicate push-down, and thus json is used here + df.write + .format("json") + .partitionBy("i") + .bucketBy(numBuckets, "j") + .saveAsTable("bucketed_table") + + for (j <- 0 until 13) { + checkPrunedAnswers( + bucketSpec, + bucketValues = j :: Nil, + hiveContext.table("bucketed_table") + .select("i", "j", "k").filter($"j" === j && $"k" > $"j"), + df.select("i", "j", "k").filter($"j" === j && $"k" > $"j")) --- End diff -- : ) Let me address all your comments tonight.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org