|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.comet.exec |
| 21 | + |
| 22 | +import org.apache.spark.SparkConf |
| 23 | +import org.apache.spark.sql.CometTestBase |
| 24 | +import org.apache.spark.sql.comet.CometInMemoryTableScanExec |
| 25 | +import org.apache.spark.sql.comet.execution.shuffle.CometShuffleExchangeExec |
| 26 | +import org.apache.spark.sql.execution.columnar.CometInMemoryRelationHelper |
| 27 | +import org.apache.spark.sql.execution.metric.SQLLastAttemptMetrics |
| 28 | + |
| 29 | +import org.apache.comet.CometConf |
| 30 | + |
| 31 | +/** Spark 4.2's last-attempt metrics over a relation cached in Comet's format. */ |
| 32 | +class CometInMemoryCacheLastAttemptMetricSuite extends CometTestBase { |
| 33 | + |
| 34 | + import testImplicits._ |
| 35 | + |
| 36 | + override protected def beforeAll(): Unit = { |
| 37 | + CometInMemoryRelationHelper.clearSerializer() |
| 38 | + super.beforeAll() |
| 39 | + } |
| 40 | + |
| 41 | + override protected def afterAll(): Unit = { |
| 42 | + try { |
| 43 | + super.afterAll() |
| 44 | + } finally { |
| 45 | + CometInMemoryRelationHelper.clearSerializer() |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + override protected def sparkConf: SparkConf = super.sparkConf |
| 50 | + .set("spark.plugins", "org.apache.spark.CometPlugin") |
| 51 | + .set( |
| 52 | + "spark.sql.cache.serializer", |
| 53 | + "org.apache.spark.sql.comet.execution.arrow.ArrowCachedBatchSerializer") |
| 54 | + |
| 55 | + test("a last-attempt metric outside the cache keeps its value") { |
| 56 | + // Spark finds the metric's stages by walking the plan and its subqueries, and gives up on a |
| 57 | + // shuffle it does not know. A Comet shuffle inside the cached plan must stay out of that walk. |
| 58 | + // https://github.com/apache/datafusion-comet/pull/6577#discussion_r4174502176 |
| 59 | + withSQLConf(CometConf.COMET_EXEC_IN_MEMORY_CACHE_ENABLED.key -> "true") { |
| 60 | + val metric = SQLLastAttemptMetrics.createMetric(spark.sparkContext, "rows") |
| 61 | + val cached = spark.range(0, 100, 1, 2).repartition(2).cache() |
| 62 | + cached.count() |
| 63 | + val df = cached.map { id => metric.add(1); id } |
| 64 | + df.collect() |
| 65 | + val plan = df.queryExecution.executedPlan |
| 66 | + val scans = collect(plan) { case s: CometInMemoryTableScanExec => s } |
| 67 | + assert(scans.size == 1, plan) |
| 68 | + val cachedPlan = scans.head.originalPlan.relation.cachedPlan |
| 69 | + assert(collect(cachedPlan) { case s: CometShuffleExchangeExec => s }.nonEmpty, cachedPlan) |
| 70 | + assert(metric.lastAttemptValueForDataset(df) == Some(100L)) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments