|
| 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 |
| 21 | + |
| 22 | +import org.apache.spark.SparkConf |
| 23 | +import org.apache.spark.sql.comet.CometIcebergNativeScanExec |
| 24 | +import org.apache.spark.sql.execution.SparkPlan |
| 25 | + |
| 26 | +class IcebergReadFromS3Suite extends CometS3TestBase { |
| 27 | + |
| 28 | + override protected val testBucketName = "test-iceberg-bucket" |
| 29 | + |
| 30 | + private def icebergAvailable: Boolean = { |
| 31 | + try { |
| 32 | + Class.forName("org.apache.iceberg.catalog.Catalog") |
| 33 | + true |
| 34 | + } catch { |
| 35 | + case _: ClassNotFoundException => false |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + override protected def sparkConf: SparkConf = { |
| 40 | + val conf = super.sparkConf |
| 41 | + |
| 42 | + conf.set("spark.sql.catalog.s3_catalog", "org.apache.iceberg.spark.SparkCatalog") |
| 43 | + conf.set("spark.sql.catalog.s3_catalog.type", "hadoop") |
| 44 | + conf.set("spark.sql.catalog.s3_catalog.warehouse", s"s3a://$testBucketName/warehouse") |
| 45 | + |
| 46 | + conf.set(CometConf.COMET_ENABLED.key, "true") |
| 47 | + conf.set(CometConf.COMET_EXEC_ENABLED.key, "true") |
| 48 | + conf.set(CometConf.COMET_ICEBERG_NATIVE_ENABLED.key, "true") |
| 49 | + |
| 50 | + conf |
| 51 | + } |
| 52 | + |
| 53 | + /** Collects all CometIcebergNativeScanExec nodes from a plan */ |
| 54 | + private def collectIcebergNativeScans(plan: SparkPlan): Seq[CometIcebergNativeScanExec] = { |
| 55 | + collect(plan) { case scan: CometIcebergNativeScanExec => |
| 56 | + scan |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Helper to verify query correctness and that exactly one CometIcebergNativeScanExec is used. |
| 62 | + */ |
| 63 | + private def checkIcebergNativeScan(query: String): Unit = { |
| 64 | + val (_, cometPlan) = checkSparkAnswer(query) |
| 65 | + val icebergScans = collectIcebergNativeScans(cometPlan) |
| 66 | + assert( |
| 67 | + icebergScans.length == 1, |
| 68 | + s"Expected exactly 1 CometIcebergNativeScanExec but found ${icebergScans.length}. Plan:\n$cometPlan") |
| 69 | + } |
| 70 | + |
| 71 | + test("create and query simple Iceberg table from MinIO") { |
| 72 | + assume(icebergAvailable, "Iceberg not available in classpath") |
| 73 | + |
| 74 | + spark.sql(""" |
| 75 | + CREATE TABLE s3_catalog.db.simple_table ( |
| 76 | + id INT, |
| 77 | + name STRING, |
| 78 | + value DOUBLE |
| 79 | + ) USING iceberg |
| 80 | + """) |
| 81 | + |
| 82 | + spark.sql(""" |
| 83 | + INSERT INTO s3_catalog.db.simple_table |
| 84 | + VALUES (1, 'Alice', 10.5), (2, 'Bob', 20.3), (3, 'Charlie', 30.7) |
| 85 | + """) |
| 86 | + |
| 87 | + checkIcebergNativeScan("SELECT * FROM s3_catalog.db.simple_table ORDER BY id") |
| 88 | + |
| 89 | + spark.sql("DROP TABLE s3_catalog.db.simple_table") |
| 90 | + } |
| 91 | + |
| 92 | + test("read partitioned Iceberg table from MinIO") { |
| 93 | + assume(icebergAvailable, "Iceberg not available in classpath") |
| 94 | + |
| 95 | + spark.sql(""" |
| 96 | + CREATE TABLE s3_catalog.db.partitioned_table ( |
| 97 | + id INT, |
| 98 | + category STRING, |
| 99 | + value DOUBLE |
| 100 | + ) USING iceberg |
| 101 | + PARTITIONED BY (category) |
| 102 | + """) |
| 103 | + |
| 104 | + spark.sql(""" |
| 105 | + INSERT INTO s3_catalog.db.partitioned_table VALUES |
| 106 | + (1, 'A', 10.5), (2, 'B', 20.3), (3, 'C', 30.7), |
| 107 | + (4, 'A', 15.2), (5, 'B', 25.8), (6, 'C', 35.0) |
| 108 | + """) |
| 109 | + |
| 110 | + checkIcebergNativeScan("SELECT * FROM s3_catalog.db.partitioned_table ORDER BY id") |
| 111 | + checkIcebergNativeScan( |
| 112 | + "SELECT * FROM s3_catalog.db.partitioned_table WHERE category = 'A' ORDER BY id") |
| 113 | + |
| 114 | + spark.sql("DROP TABLE s3_catalog.db.partitioned_table") |
| 115 | + } |
| 116 | + |
| 117 | + test("filter pushdown to S3-backed Iceberg table") { |
| 118 | + assume(icebergAvailable, "Iceberg not available in classpath") |
| 119 | + |
| 120 | + spark.sql(""" |
| 121 | + CREATE TABLE s3_catalog.db.filter_test ( |
| 122 | + id INT, |
| 123 | + name STRING, |
| 124 | + value DOUBLE |
| 125 | + ) USING iceberg |
| 126 | + """) |
| 127 | + |
| 128 | + spark.sql(""" |
| 129 | + INSERT INTO s3_catalog.db.filter_test VALUES |
| 130 | + (1, 'Alice', 10.5), (2, 'Bob', 20.3), (3, 'Charlie', 30.7), |
| 131 | + (4, 'Diana', 15.2), (5, 'Eve', 25.8) |
| 132 | + """) |
| 133 | + |
| 134 | + checkIcebergNativeScan("SELECT * FROM s3_catalog.db.filter_test WHERE id = 3") |
| 135 | + checkIcebergNativeScan("SELECT * FROM s3_catalog.db.filter_test WHERE value > 20.0") |
| 136 | + checkIcebergNativeScan("SELECT * FROM s3_catalog.db.filter_test WHERE name = 'Alice'") |
| 137 | + |
| 138 | + spark.sql("DROP TABLE s3_catalog.db.filter_test") |
| 139 | + } |
| 140 | + |
| 141 | + test("multiple files in S3 - verify no duplicates") { |
| 142 | + assume(icebergAvailable, "Iceberg not available in classpath") |
| 143 | + |
| 144 | + withSQLConf("spark.sql.files.maxRecordsPerFile" -> "50") { |
| 145 | + spark.sql(""" |
| 146 | + CREATE TABLE s3_catalog.db.multifile_test ( |
| 147 | + id INT, |
| 148 | + data STRING |
| 149 | + ) USING iceberg |
| 150 | + """) |
| 151 | + |
| 152 | + spark.sql(""" |
| 153 | + INSERT INTO s3_catalog.db.multifile_test |
| 154 | + SELECT id, CONCAT('data_', CAST(id AS STRING)) as data |
| 155 | + FROM range(200) |
| 156 | + """) |
| 157 | + |
| 158 | + checkIcebergNativeScan("SELECT COUNT(DISTINCT id) FROM s3_catalog.db.multifile_test") |
| 159 | + checkIcebergNativeScan( |
| 160 | + "SELECT * FROM s3_catalog.db.multifile_test WHERE id < 10 ORDER BY id") |
| 161 | + |
| 162 | + spark.sql("DROP TABLE s3_catalog.db.multifile_test") |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + test("MOR table with deletes in S3") { |
| 167 | + assume(icebergAvailable, "Iceberg not available in classpath") |
| 168 | + |
| 169 | + spark.sql(""" |
| 170 | + CREATE TABLE s3_catalog.db.mor_delete_test ( |
| 171 | + id INT, |
| 172 | + name STRING, |
| 173 | + value DOUBLE |
| 174 | + ) USING iceberg |
| 175 | + TBLPROPERTIES ( |
| 176 | + 'write.delete.mode' = 'merge-on-read', |
| 177 | + 'write.merge.mode' = 'merge-on-read' |
| 178 | + ) |
| 179 | + """) |
| 180 | + |
| 181 | + spark.sql(""" |
| 182 | + INSERT INTO s3_catalog.db.mor_delete_test VALUES |
| 183 | + (1, 'Alice', 10.5), (2, 'Bob', 20.3), (3, 'Charlie', 30.7), |
| 184 | + (4, 'Diana', 15.2), (5, 'Eve', 25.8) |
| 185 | + """) |
| 186 | + |
| 187 | + spark.sql("DELETE FROM s3_catalog.db.mor_delete_test WHERE id IN (2, 4)") |
| 188 | + |
| 189 | + checkIcebergNativeScan("SELECT * FROM s3_catalog.db.mor_delete_test ORDER BY id") |
| 190 | + |
| 191 | + spark.sql("DROP TABLE s3_catalog.db.mor_delete_test") |
| 192 | + } |
| 193 | +} |
0 commit comments