Repository navigation
test: compare in-memory caches with independent answers #6262
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
Merged
andygrove
merged 3 commits into
apache:main
from
LinSimon-901101:fix/6203-cache-test-oracles
Sep 29, 2026
+420
−103
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
256 changes: 256 additions & 0 deletions
256
spark/src/test/scala/org/apache/comet/exec/CometInMemoryCachePruningSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.comet.exec | ||
|
|
||
| import java.sql.Timestamp | ||
| import java.time.Instant | ||
|
|
||
| import org.apache.spark.SparkConf | ||
| import org.apache.spark.sql.{CometTestBase, DataFrame, Row} | ||
| import org.apache.spark.sql.comet.{CometInMemoryTableScanExec, CometNativeScanExec} | ||
| import org.apache.spark.sql.execution.FileSourceScanExec | ||
| import org.apache.spark.sql.execution.columnar.CometInMemoryRelationHelper | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| import org.apache.comet.CometConf | ||
|
|
||
| class CometInMemoryCachePruningSuite extends CometTestBase { | ||
|
|
||
| override protected def beforeAll(): Unit = { | ||
| CometInMemoryRelationHelper.clearSerializer() | ||
| super.beforeAll() | ||
| } | ||
|
|
||
| override protected def afterAll(): Unit = { | ||
| try { | ||
| super.afterAll() | ||
| } finally { | ||
| CometInMemoryRelationHelper.clearSerializer() | ||
| } | ||
| } | ||
|
|
||
| override protected def sparkConf: SparkConf = super.sparkConf | ||
| .set("spark.plugins", "org.apache.spark.CometPlugin") | ||
| .set( | ||
| "spark.sql.cache.serializer", | ||
| "org.apache.spark.sql.comet.execution.arrow.ArrowCachedBatchSerializer") | ||
|
|
||
| private val schema = StructType( | ||
| Seq( | ||
| StructField("id", IntegerType, nullable = false), | ||
| StructField("d", DoubleType), | ||
| StructField("f", FloatType), | ||
| StructField("n", IntegerType), | ||
| StructField("s", StringType), | ||
| StructField("dec", DecimalType(20, 3)), | ||
| StructField("ts", TimestampType), | ||
| StructField("b", BooleanType))) | ||
|
|
||
| private def fixture(): DataFrame = { | ||
| def repeated(d: Double): Seq[Double] = Seq.fill(4)(d) | ||
| // Every four rows form one batch in all three writers. Keep NaN-only, mixed finite/NaN, | ||
| // signed-zero-only, infinity and all-null batches separate so incorrect bounds lose rows. | ||
| val values = Seq( | ||
| repeated(Double.NegativeInfinity), | ||
| repeated(-100.0), | ||
| repeated(-2.0), | ||
| repeated(-0.0), | ||
| repeated(0.0), | ||
| repeated(0.25), | ||
| repeated(1.0), | ||
| repeated(2.0), | ||
| repeated(100.0), | ||
| repeated(Double.PositiveInfinity), | ||
| repeated(Double.NaN), | ||
| Seq(1.0, Double.NaN, 3.0, 2.0), | ||
| repeated(0.0), // all-null batch | ||
| Seq(-0.0, 0.0, -0.0, 0.0), | ||
| Seq(-3.0, -2.0, -1.0, 0.0), | ||
| Seq(Double.PositiveInfinity, Double.NaN, Double.PositiveInfinity, Double.NaN)) | ||
| val strings = Seq( | ||
| "", | ||
| "a", | ||
| "ab", | ||
| "b", | ||
| "\u007f", | ||
| "\u0080", | ||
| "\ue000", | ||
| "\ud800\udc00", | ||
| "é", | ||
| "中", | ||
| "prefix-a", | ||
| "prefix-z", | ||
| null, | ||
| "z", | ||
| "e\u0301", | ||
| "😀") | ||
| val rows = values.zipWithIndex.flatMap { case (batch, group) => | ||
| batch.zipWithIndex.map { case (d, offset) => | ||
| val isNull = group == 12 || (group == 11 && offset == 2) | ||
| Row( | ||
| group * 4 + offset, | ||
| if (isNull) null else Double.box(d), | ||
| if (isNull) null else Float.box(d.toFloat), | ||
| if (isNull) null else Int.box(group), | ||
| strings(group), | ||
| if (group == 12) null else new java.math.BigDecimal(s"${group - 8}.125"), | ||
| if (group == 12) null | ||
| else | ||
| Timestamp.from( | ||
| Instant | ||
| .parse("1960-01-01T00:00:00Z") | ||
| .plusSeconds(group * 86400L) | ||
| .plusNanos(offset * 1000L)), | ||
| if (group == 12) null else Boolean.box(group % 2 == 0)) | ||
| } | ||
| } | ||
| spark.createDataFrame(spark.sparkContext.parallelize(rows, 1), schema) | ||
| } | ||
|
|
||
| private val predicates = Seq( | ||
| "d = CAST('NaN' AS DOUBLE)", | ||
| "f = CAST('NaN' AS FLOAT)", | ||
| "d > CAST('Infinity' AS DOUBLE)", | ||
| "f < CAST('NaN' AS FLOAT)", | ||
| "d = 0.0D", | ||
| "f = CAST('-0.0' AS FLOAT)", | ||
| "d >= CAST('-0.0' AS DOUBLE) AND d <= 0.0D", | ||
| "f >= CAST(0.0 AS FLOAT) AND f <= CAST('-0.0' AS FLOAT)", | ||
| "d = CAST('-Infinity' AS DOUBLE)", | ||
| "f >= CAST('Infinity' AS FLOAT)", | ||
| "d > -2.0D AND d < 2.0D", | ||
| "d IS NULL", | ||
| "d IS NOT NULL", | ||
| "n IS NULL", | ||
| "s = '中'", | ||
| "s >= '\ue000'", | ||
| "s < '\u0080'", | ||
| "s LIKE 'prefix%'", | ||
| "dec >= -1.125 AND dec < 2.125", | ||
| "ts < TIMESTAMP '1960-01-05 00:00:00'", | ||
| "b <=> true", | ||
| "id IN (1, 9, 49)", | ||
| "d = -100.0D OR s = 'prefix-z'") | ||
|
|
||
| Seq("native Arrow", "Spark columnar", "row").foreach { writer => | ||
| test(s"cache pruning matches uncached Spark with $writer input") { | ||
| withSQLConf( | ||
| SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false", | ||
| SQLConf.SESSION_LOCAL_TIMEZONE.key -> "UTC", | ||
| SQLConf.IN_MEMORY_PARTITION_PRUNING.key -> "true", | ||
| SQLConf.CACHE_VECTORIZED_READER_ENABLED.key -> "true", | ||
| SQLConf.PARQUET_VECTORIZED_READER_ENABLED.key -> "true", | ||
| SQLConf.PARQUET_VECTORIZED_READER_BATCH_SIZE.key -> "4", | ||
| SQLConf.COLUMN_BATCH_SIZE.key -> "4", | ||
| CometConf.COMET_BATCH_SIZE.key -> "4", | ||
| CometConf.COMET_SHUFFLE_JVM_BATCH_SIZE.key -> "4", | ||
| CometConf.COMET_EXEC_IN_MEMORY_CACHE_ENABLED.key -> "true", | ||
| CometConf.COMET_SPARK_TO_ARROW_ENABLED.key -> "false", | ||
| CometConf.COMET_NATIVE_SCAN_ENABLED.key -> (writer == "native Arrow").toString) { | ||
| spark.catalog.clearCache() | ||
| val source = fixture() | ||
| // Collect every expected value and predicate result before registering any cache. A | ||
| // second query against the cached table, even with Comet disabled, is not an oracle: | ||
| // Spark still decodes the same Comet payload and applies the same cached statistics. | ||
| // Use the original in-memory data: Parquet row-group pruning can itself mishandle | ||
| // signed zero, which would make a file-based oracle hide a cache pruning regression. | ||
| var oracle = Seq.empty[Row] | ||
| withSQLConf(CometConf.COMET_ENABLED.key -> "false") { | ||
| oracle = source | ||
| .selectExpr((Seq("*") ++ predicates.zipWithIndex.map { case (p, i) => | ||
| s"($p) AS predicate_$i" | ||
| }): _*) | ||
| .collect() | ||
| .toSeq | ||
| } | ||
| val expectedRows = oracle.map(row => Row.fromSeq(row.toSeq.take(schema.length))) | ||
|
|
||
| withTempPath { path => | ||
| val input = if (writer == "row") { | ||
| source | ||
| } else { | ||
| withSQLConf(CometConf.COMET_ENABLED.key -> "false") { | ||
| source.write.option("parquet.enable.dictionary", "false").parquet(path.toString) | ||
| } | ||
| spark.read.parquet(path.toString) | ||
| } | ||
| input.createOrReplaceTempView("pruning_cache") | ||
| val cached = spark.table("pruning_cache").cache() | ||
| try { | ||
| val relation = | ||
| spark.sharedState.cacheManager.lookupCachedData(cached).get.cachedRepresentation | ||
| val plan = relation.cacheBuilder.cachedPlan | ||
| withClue(s"$writer cache writer:\n$plan\n") { | ||
| writer match { | ||
| case "native Arrow" => | ||
| assert(plan.supportsColumnar) | ||
| assert(plan.collect { case s: CometNativeScanExec => s }.nonEmpty) | ||
| case "Spark columnar" => | ||
| assert(plan.supportsColumnar) | ||
| assert(plan.collect { case s: FileSourceScanExec => s }.nonEmpty) | ||
| assert(plan.collect { case s: CometNativeScanExec => s }.isEmpty) | ||
| case "row" => assert(!plan.supportsColumnar) | ||
| } | ||
| } | ||
| checkCometAnswer(cached, expectedRows) | ||
| val batches = relation.cacheBuilder.cachedColumnBuffers.collect() | ||
| assert(batches.length == 16, "the fixture must produce many distinct small batches") | ||
| assert(batches.forall(_.numRows == 4)) | ||
| assert( | ||
| batches.forall(_.getClass.getName == | ||
| "org.apache.spark.sql.comet.execution.arrow.CometCachedBatch")) | ||
|
|
||
| predicates.zipWithIndex.foreach { case (predicate, i) => | ||
| def matches(row: Row): Boolean = | ||
| !row.isNullAt(schema.length + i) && row.getBoolean(schema.length + i) | ||
| val expected = oracle | ||
| .filter(matches) | ||
| .map(_.getInt(0)) | ||
| .sorted | ||
| val expectedScannedRows = oracle.grouped(4).count(_.exists(matches)) * 4 | ||
| assert(expected.nonEmpty && expected.length < expectedRows.length) | ||
| val query = cached.where(predicate).select("id") | ||
| val actual = query.collect().map(_.getInt(0)).sorted.toSeq | ||
| val scans = query.queryExecution.executedPlan.collect { | ||
| case scan: CometInMemoryTableScanExec => scan | ||
| } | ||
| withClue(s"$writer input, predicate: $predicate\n") { | ||
| assert(actual == expected) | ||
| assert(scans.length == 1) | ||
| assert(scans.head.originalPlan.predicates.nonEmpty) | ||
| val scannedRows = scans.head.metrics("numOutputRows").value | ||
| // Counting eligible fixture batches also rejects a filter that only drops the | ||
| // all-null batch, without applying the predicate's actual bounds. | ||
| assert( | ||
| scannedRows == expectedScannedRows, | ||
| s"expected $expectedScannedRows rows from eligible batches, decoded $scannedRows") | ||
| } | ||
| } | ||
| } finally { | ||
| cached.unpersist(blocking = true) | ||
| spark.catalog.clearCache() | ||
| spark.catalog.dropTempView("pruning_cache") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No predicate here can catch an off-by-one bound on an int column.
id IN (1, 9, 49)lands on the second row of each of its batches, andnonly appears inn IS NULL, which prunes on the null count. If every int batch reports its upper bound one lower, this suite still passes on all three writers, and only the typed-bounds tests inCometInMemoryCacheSuitenotice. Could we addn = 5?nis constant within a batch, so that one predicate sits on both bounds of batch 5.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andygrove Thanks for pointing this out. I'll add the
n = 5coverage in a follow-up issue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened #6378 to track adding
n = 5coverage for integer bounds.