|
| 1 | +/* |
| 2 | + * Copyright (2021) The Delta Lake Project Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.apache.spark.sql.delta.stats |
| 18 | + |
| 19 | +// scalastyle:off import.ordering.noEmptyLine |
| 20 | +import org.apache.spark.sql.delta.sources.DeltaSQLConf |
| 21 | + |
| 22 | +import org.apache.spark.sql.{DataFrame, QueryTest} |
| 23 | +import org.apache.spark.sql.functions._ |
| 24 | +import org.apache.spark.sql.test.SharedSparkSession |
| 25 | + |
| 26 | +/** |
| 27 | + * Test suite to verify when preparedScan.scanned.rows is populated in PreparedDeltaFileIndex, |
| 28 | + * and the behavior of the DELTA_ALWAYS_COLLECT_STATS flag. |
| 29 | + */ |
| 30 | +class PreparedDeltaFileIndexRowCountSuite |
| 31 | + extends QueryTest |
| 32 | + with SharedSparkSession |
| 33 | + |
| 34 | + import testImplicits._ |
| 35 | + |
| 36 | + private def getDeltaScan(df: DataFrame): DeltaScan = { |
| 37 | + val scans = df.queryExecution.optimizedPlan.collect { |
| 38 | + case DeltaTable(prepared: PreparedDeltaFileIndex) => prepared.preparedScan |
| 39 | + } |
| 40 | + assert(scans.size == 1, s"Expected 1 DeltaScan, found ${scans.size}") |
| 41 | + scans.head |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Test utility that creates a partitioned Delta table and verifies scanned.rows behavior. |
| 46 | + * |
| 47 | + * @param alwaysCollectStats value of the DELTA_ALWAYS_COLLECT_STATS flag |
| 48 | + * @param queryTransform function to transform the base DataFrame (apply filters) |
| 49 | + * @param expectedRowsDefined whether scanned.rows should be defined |
| 50 | + * @param expectedRowCount expected row count if defined (None to skip validation) |
| 51 | + */ |
| 52 | + private def testRowCountBehavior( |
| 53 | + alwaysCollectStats: Boolean, |
| 54 | + queryTransform: DataFrame => DataFrame, |
| 55 | + expectedRowsDefined: Boolean, |
| 56 | + expectedRowCount: Option[Long] = None): Unit = { |
| 57 | + withTempDir { dir => |
| 58 | + withSQLConf(DeltaSQLConf.DELTA_COLLECT_STATS.key -> "true") { |
| 59 | + spark.range(100).toDF("id") |
| 60 | + .withColumn("part", $"id" % 4) |
| 61 | + .repartition(4) |
| 62 | + .write.format("delta").partitionBy("part").save(dir.getAbsolutePath) |
| 63 | + } |
| 64 | + |
| 65 | + DeltaLog.clearCache() |
| 66 | + |
| 67 | + withSQLConf(DeltaSQLConf.DELTA_ALWAYS_COLLECT_STATS.key -> alwaysCollectStats.toString) { |
| 68 | + val df = spark.read.format("delta").load(dir.getAbsolutePath) |
| 69 | + val scan = getDeltaScan(queryTransform(df)) |
| 70 | + |
| 71 | + if (expectedRowsDefined) { |
| 72 | + assert(scan.scanned.rows.isDefined, "scanned.rows should be defined") |
| 73 | + expectedRowCount.foreach { expected => |
| 74 | + assert(scan.scanned.rows.get == expected, |
| 75 | + s"Expected $expected rows, got ${scan.scanned.rows.get}") |
| 76 | + } |
| 77 | + } else { |
| 78 | + assert(scan.scanned.rows.isEmpty, "scanned.rows should be None") |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Define query cases: (name, transform function, always collects rows) |
| 85 | + // Note: In the Edge code path, DataSkippingReaderEdge.filterOnPartitions always collects |
| 86 | + // row counts for partition filter cases (see keepNumRecords = true in that method). |
| 87 | + // So only "no filter" and "TrueLiteral filter" depend on the alwaysCollectStats flag. |
| 88 | + private val queryCases: Seq[(String, DataFrame => DataFrame, Boolean)] = Seq( |
| 89 | + ("no filter", identity[DataFrame], false), |
| 90 | + ("TrueLiteral filter", _.where(lit(true)), false), |
| 91 | + ("partition filter only", _.where($"part" === 1), false), |
| 92 | + ("data filter", _.where($"id" === 50), true), |
| 93 | + ("partition + data filter", _.where($"part" === 1).where($"id" === 50), true) |
| 94 | + ) |
| 95 | + |
| 96 | + // Grid test: all query cases x flag values |
| 97 | + for { |
| 98 | + (caseName, queryTransform, alwaysCollectsRows) <- queryCases |
| 99 | + alwaysCollectStats <- Seq(false, true) |
| 100 | + } { |
| 101 | + val flagDesc = s"alwaysCollectStats=$alwaysCollectStats" |
| 102 | + // If the query type always collects rows, rows is always defined; otherwise depends on flag |
| 103 | + val expectedRowsDefined = alwaysCollectsRows || alwaysCollectStats |
| 104 | + |
| 105 | + test(s"$caseName - $flagDesc") { |
| 106 | + testRowCountBehavior( |
| 107 | + alwaysCollectStats = alwaysCollectStats, |
| 108 | + queryTransform = queryTransform, |
| 109 | + expectedRowsDefined = expectedRowsDefined |
| 110 | + ) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + test("alwaysCollectStats with missing stats returns None") { |
| 115 | + withTempDir { dir => |
| 116 | + // Create table without stats |
| 117 | + withSQLConf(DeltaSQLConf.DELTA_COLLECT_STATS.key -> "false") { |
| 118 | + spark.range(100).toDF("id") |
| 119 | + .write.format("delta").save(dir.getAbsolutePath) |
| 120 | + } |
| 121 | + |
| 122 | + DeltaLog.clearCache() |
| 123 | + |
| 124 | + withSQLConf(DeltaSQLConf.DELTA_ALWAYS_COLLECT_STATS.key -> "true") { |
| 125 | + val df = spark.read.format("delta").load(dir.getAbsolutePath) |
| 126 | + val scan = getDeltaScan(df) |
| 127 | + assert(scan.scanned.rows.isEmpty, "scanned.rows should be None when stats are missing") |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments