forked from apache/iceberg
-
Notifications
You must be signed in to change notification settings - Fork 0
spark: Add support for Variant for Spark with Vortex #34
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
Merged
Changes from all commits
Commits
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
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
114 changes: 114 additions & 0 deletions
114
spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/sql/TestSparkVortexVariantRead.java
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,114 @@ | ||
| /* | ||
| * 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.iceberg.spark.sql; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.util.List; | ||
| import org.apache.iceberg.spark.SparkCatalog; | ||
| import org.apache.iceberg.spark.TestBase; | ||
| import org.apache.spark.sql.Row; | ||
| import org.apache.spark.types.variant.Variant; | ||
| import org.apache.spark.unsafe.types.VariantVal; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Round-trips VARIANT columns through Vortex-format tables via Spark SQL. | ||
| * | ||
| * <p>Vortex reads default to the columnar ({@code ColumnarBatch}) path, but that path cannot | ||
| * surface a variant as Spark's {@link VariantVal}, so {@code SparkBatch} falls back to the | ||
| * row-based reader whenever a variant is projected. Projecting only {@code id} keeps the columnar | ||
| * path; projecting a variant column exercises the row-based reader. | ||
| */ | ||
| public class TestSparkVortexVariantRead extends TestBase { | ||
|
|
||
| private static final String CATALOG = "local"; | ||
| private static final String TABLE = CATALOG + ".default.var_vortex"; | ||
|
|
||
| @BeforeAll | ||
| public static void setupCatalog() { | ||
| // Use a Hadoop catalog to avoid Hive schema conversion (Hive doesn't support VARIANT yet) | ||
| spark.conf().set("spark.sql.catalog." + CATALOG, SparkCatalog.class.getName()); | ||
| spark.conf().set("spark.sql.catalog." + CATALOG + ".type", "hadoop"); | ||
| spark.conf().set("spark.sql.catalog." + CATALOG + ".default-namespace", "default"); | ||
| spark.conf().set("spark.sql.catalog." + CATALOG + ".cache-enabled", "false"); | ||
| String temp = System.getProperty("java.io.tmpdir") + "/iceberg_spark_vortex_variant_warehouse"; | ||
| spark.conf().set("spark.sql.catalog." + CATALOG + ".warehouse", temp); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| public void setupTable() { | ||
| sql("DROP TABLE IF EXISTS %s", TABLE); | ||
| sql( | ||
| "CREATE TABLE %s (id BIGINT, v1 VARIANT, v2 VARIANT) USING iceberg " | ||
| + "TBLPROPERTIES ('format-version'='3', 'write.format.default'='vortex')", | ||
| TABLE); | ||
|
|
||
| sql("INSERT INTO %s SELECT 1, parse_json('{\"a\":1}'), parse_json('{\"x\":10}')", TABLE); | ||
| sql("INSERT INTO %s SELECT 2, parse_json('{\"b\":2}'), parse_json('{\"y\":20}')", TABLE); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void cleanup() { | ||
| sql("DROP TABLE IF EXISTS %s", TABLE); | ||
| } | ||
|
|
||
| @Test | ||
| public void readVariantColumns() { | ||
| List<Row> rows = spark.table(TABLE).select("id", "v1", "v2").orderBy("id").collectAsList(); | ||
| assertThat(rows).hasSize(2); | ||
|
|
||
| assertThat(rows.get(0).getLong(0)).isEqualTo(1L); | ||
| assertVariantField(rows.get(0).get(1), "a", 1L); | ||
| assertVariantField(rows.get(0).get(2), "x", 10L); | ||
|
|
||
| assertThat(rows.get(1).getLong(0)).isEqualTo(2L); | ||
| assertVariantField(rows.get(1).get(1), "b", 2L); | ||
| assertVariantField(rows.get(1).get(2), "y", 20L); | ||
| } | ||
|
|
||
| @Test | ||
| public void readNonVariantProjectionUsesColumnarPath() { | ||
| // Projecting only non-variant columns keeps the columnar Vortex path enabled and must still | ||
| // return correct data after the variant fallback guard was added. | ||
| List<Row> rows = spark.table(TABLE).select("id").orderBy("id").collectAsList(); | ||
| assertThat(rows).extracting(row -> row.getLong(0)).containsExactly(1L, 2L); | ||
| } | ||
|
|
||
| @Test | ||
| public void readNullVariant() { | ||
| sql("INSERT INTO %s SELECT 3, NULL, NULL", TABLE); | ||
|
|
||
| List<Row> rows = spark.table(TABLE).select("id", "v1").where("id = 3").collectAsList(); | ||
| assertThat(rows).hasSize(1); | ||
| assertThat(rows.get(0).get(1)).isNull(); | ||
| } | ||
|
|
||
| private static void assertVariantField(Object value, String key, long expected) { | ||
| assertThat(value).isInstanceOf(VariantVal.class); | ||
| VariantVal variantVal = (VariantVal) value; | ||
| Variant variant = new Variant(variantVal.getValue(), variantVal.getMetadata()); | ||
| Variant field = variant.getFieldByKey(key); | ||
| assertThat(field).isNotNull(); | ||
| assertThat(field.getLong()).isEqualTo(expected); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.