|
| 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.rules |
| 21 | + |
| 22 | +import java.io.File |
| 23 | +import java.nio.file.Files |
| 24 | +import java.util.UUID |
| 25 | + |
| 26 | +import org.apache.commons.io.FileUtils |
| 27 | +import org.apache.spark.SparkConf |
| 28 | +import org.apache.spark.sql.{CometTestBase, SaveMode} |
| 29 | +import org.apache.spark.sql.comet.CometScanExec |
| 30 | +import org.apache.spark.sql.execution.{FileSourceScanExec, SparkPlan} |
| 31 | + |
| 32 | +import org.apache.comet.CometConf |
| 33 | +import org.apache.comet.hadoop.fs.{FakeHDFSFileSystem, FakeHdfsSchemeFileSystem} |
| 34 | + |
| 35 | +/** |
| 36 | + * Comet's native readers go through object_store, which only understands a fixed set of URL |
| 37 | + * schemes. A custom Hadoop FileSystem scheme that object_store can't parse (here `fake://`) must |
| 38 | + * NOT be claimed by the native scan -- it would fail at execution with "Unable to recognise URL". |
| 39 | + * `CometScanRule` must decline it so Spark's Hadoop-FS-aware reader handles the scan. |
| 40 | + * |
| 41 | + * Unlike `ParquetReadFromFakeHadoopFsSuite`, this suite does NOT route the `fake` scheme through |
| 42 | + * libhdfs (`spark.hadoop.fs.comet.libhdfs.schemes`), so it exercises the decline path. The test |
| 43 | + * applies the rule directly to the physical plan and asserts fallback -- no query execution, so |
| 44 | + * it doesn't depend on the native reader actually attempting (and failing on) the scheme. |
| 45 | + */ |
| 46 | +class CometScanSchemeFallbackSuite extends CometTestBase { |
| 47 | + |
| 48 | + private var fakeRootDir: File = _ |
| 49 | + |
| 50 | + override protected def sparkConf: SparkConf = { |
| 51 | + val conf = super.sparkConf |
| 52 | + conf.set("spark.hadoop.fs.fake.impl", "org.apache.comet.hadoop.fs.FakeHDFSFileSystem") |
| 53 | + // Back the `hdfs` scheme with a local FS so we can exercise an `hdfs://` path without a live |
| 54 | + // cluster. `hdfs` is natively readable by default, so this scan must be CLAIMED, not declined. |
| 55 | + conf.set("spark.hadoop.fs.hdfs.impl", "org.apache.comet.hadoop.fs.FakeHdfsSchemeFileSystem") |
| 56 | + conf.set("spark.hadoop.fs.defaultFS", FakeHDFSFileSystem.PREFIX) |
| 57 | + // Intentionally NOT setting CometConf.COMET_LIBHDFS_SCHEMES -- `fake` is not natively readable, |
| 58 | + // and `hdfs` must still be claimed by default (mirrors the native `is_hdfs_scheme` default). |
| 59 | + conf |
| 60 | + } |
| 61 | + |
| 62 | + override def beforeAll(): Unit = { |
| 63 | + fakeRootDir = Files.createTempDirectory(s"comet_scheme_${UUID.randomUUID().toString}").toFile |
| 64 | + super.beforeAll() |
| 65 | + } |
| 66 | + |
| 67 | + protected override def afterAll(): Unit = { |
| 68 | + if (fakeRootDir != null) FileUtils.deleteDirectory(fakeRootDir) |
| 69 | + super.afterAll() |
| 70 | + } |
| 71 | + |
| 72 | + test("native scan declines a filesystem scheme object_store can't read (fake://)") { |
| 73 | + val path = s"${FakeHDFSFileSystem.PREFIX}${fakeRootDir.getAbsolutePath}/data" |
| 74 | + spark.range(0, 10).toDF("id").write.format("parquet").mode(SaveMode.Overwrite).save(path) |
| 75 | + |
| 76 | + // Obtain a clean Spark physical plan (Comet disabled) with the FileSourceScanExec, then apply |
| 77 | + // CometScanRule directly. No execution -- we only check whether the rule claims the scan. |
| 78 | + // Capture via a var inside the block: `SQLTestUtils.withSQLConf` returns Unit on Spark 3.5 |
| 79 | + // but a value on Spark 4.x, so we can't return the plan out of it cross-version. |
| 80 | + var sparkPlan: SparkPlan = null |
| 81 | + withSQLConf(CometConf.COMET_ENABLED.key -> "false") { |
| 82 | + sparkPlan = spark.read.parquet(path).queryExecution.executedPlan |
| 83 | + } |
| 84 | + |
| 85 | + withSQLConf( |
| 86 | + CometConf.COMET_ENABLED.key -> "true", |
| 87 | + CometConf.COMET_NATIVE_SCAN_ENABLED.key -> "true", |
| 88 | + CometConf.COMET_EXEC_ENABLED.key -> "true") { |
| 89 | + val transformed = CometScanRule(spark).apply(stripAQEPlan(sparkPlan)) |
| 90 | + |
| 91 | + val cometScans = transformed.collect { case s: CometScanExec => s } |
| 92 | + val sparkScans = transformed.collect { case s: FileSourceScanExec => s } |
| 93 | + assert( |
| 94 | + cometScans.isEmpty, |
| 95 | + "`fake://` is not object_store-readable; the native scan must fall back to Spark, " + |
| 96 | + s"but Comet claimed it:\n$transformed") |
| 97 | + assert( |
| 98 | + sparkScans.size == 1, |
| 99 | + s"expected the scan to remain a Spark FileSourceScanExec:\n$transformed") |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + test("native scan claims hdfs:// when libhdfs.schemes is unset (native-default lockstep)") { |
| 104 | + // Native's `is_hdfs_scheme` treats `hdfs` as readable when `fs.comet.libhdfs.schemes` is unset, |
| 105 | + // and `create_hdfs_object_store` is in the default build. The JVM gate must agree: with the |
| 106 | + // config unset, an `hdfs://` scan must be CLAIMED by Comet, not declined to Spark. Guards the |
| 107 | + // `case None => Set("hdfs")` default in CometScanRule against the silent-fallback regression |
| 108 | + // Andy flagged in #4525. |
| 109 | + val path = s"${FakeHdfsSchemeFileSystem.PREFIX}${fakeRootDir.getAbsolutePath}/hdfs-data" |
| 110 | + spark.range(0, 10).toDF("id").write.format("parquet").mode(SaveMode.Overwrite).save(path) |
| 111 | + |
| 112 | + var sparkPlan: SparkPlan = null |
| 113 | + withSQLConf(CometConf.COMET_ENABLED.key -> "false") { |
| 114 | + sparkPlan = spark.read.parquet(path).queryExecution.executedPlan |
| 115 | + } |
| 116 | + |
| 117 | + withSQLConf( |
| 118 | + CometConf.COMET_ENABLED.key -> "true", |
| 119 | + CometConf.COMET_NATIVE_SCAN_ENABLED.key -> "true", |
| 120 | + CometConf.COMET_EXEC_ENABLED.key -> "true") { |
| 121 | + val transformed = CometScanRule(spark).apply(stripAQEPlan(sparkPlan)) |
| 122 | + |
| 123 | + val cometScans = transformed.collect { case s: CometScanExec => s } |
| 124 | + val sparkScans = transformed.collect { case s: FileSourceScanExec => s } |
| 125 | + assert( |
| 126 | + cometScans.size == 1, |
| 127 | + "`hdfs://` is natively readable by default; Comet must claim the scan, " + |
| 128 | + s"but it fell back to Spark:\n$transformed") |
| 129 | + assert(sparkScans.isEmpty, s"expected no leftover Spark FileSourceScanExec:\n$transformed") |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments