-
Notifications
You must be signed in to change notification settings - Fork 265
Add Integration tests for Delta tables for Spark Client #1500
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
HonahX
merged 5 commits into
apache:main
from
gh-yzou:yzou-spark-client-integration-delta
May 6, 2025
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
255 changes: 255 additions & 0 deletions
255
...k/v3.5/integration/src/intTest/java/org/apache/polaris/spark/quarkus/it/SparkDeltaIT.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,255 @@ | ||
/* | ||
* 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.polaris.spark.quarkus.it; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.polaris.service.it.env.IntegrationTestsHelper; | ||
import org.apache.spark.sql.Dataset; | ||
import org.apache.spark.sql.Row; | ||
import org.apache.spark.sql.RowFactory; | ||
import org.apache.spark.sql.delta.DeltaAnalysisException; | ||
import org.apache.spark.sql.types.DataTypes; | ||
import org.apache.spark.sql.types.Metadata; | ||
import org.apache.spark.sql.types.StructField; | ||
import org.apache.spark.sql.types.StructType; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
@QuarkusIntegrationTest | ||
public class SparkDeltaIT extends SparkIntegrationBase { | ||
private String defaultNs; | ||
private String tableRootDir; | ||
|
||
private String getTableLocation(String tableName) { | ||
return String.format("%s/%s", tableRootDir, tableName); | ||
} | ||
|
||
private String getTableNameWithRandomSuffix() { | ||
return generateName("deltatb"); | ||
} | ||
|
||
@BeforeEach | ||
public void createDefaultResources(@TempDir Path tempDir) { | ||
spark.sparkContext().setLogLevel("WARN"); | ||
defaultNs = generateName("delta"); | ||
// create a default namespace | ||
sql("CREATE NAMESPACE %s", defaultNs); | ||
sql("USE NAMESPACE %s", defaultNs); | ||
tableRootDir = | ||
IntegrationTestsHelper.getTemporaryDirectory(tempDir).resolve(defaultNs).getPath(); | ||
} | ||
|
||
@AfterEach | ||
public void cleanupDeltaData() { | ||
// clean up delta data | ||
File dirToDelete = new File(tableRootDir); | ||
FileUtils.deleteQuietly(dirToDelete); | ||
sql("DROP NAMESPACE %s", defaultNs); | ||
} | ||
|
||
@Test | ||
public void testBasicTableOperations() { | ||
// create a regular delta table | ||
String deltatb1 = "deltatb1"; | ||
sql( | ||
"CREATE TABLE %s (id INT, name STRING) USING DELTA LOCATION '%s'", | ||
deltatb1, getTableLocation(deltatb1)); | ||
sql("INSERT INTO %s VALUES (1, 'anna'), (2, 'bob')", deltatb1); | ||
List<Object[]> results = sql("SELECT * FROM %s WHERE id > 1 ORDER BY id DESC", deltatb1); | ||
assertThat(results.size()).isEqualTo(1); | ||
assertThat(results.get(0)).isEqualTo(new Object[] {2, "bob"}); | ||
|
||
// create a detla table with partition | ||
String deltatb2 = "deltatb2"; | ||
sql( | ||
"CREATE TABLE %s (name String, age INT, country STRING) USING DELTA PARTITIONED BY (country) LOCATION '%s'", | ||
deltatb2, getTableLocation(deltatb2)); | ||
sql( | ||
"INSERT INTO %s VALUES ('anna', 10, 'US'), ('james', 32, 'US'), ('yan', 16, 'CHINA')", | ||
deltatb2); | ||
results = sql("SELECT name, country FROM %s ORDER BY age", deltatb2); | ||
assertThat(results.size()).isEqualTo(3); | ||
assertThat(results.get(0)).isEqualTo(new Object[] {"anna", "US"}); | ||
assertThat(results.get(1)).isEqualTo(new Object[] {"yan", "CHINA"}); | ||
assertThat(results.get(2)).isEqualTo(new Object[] {"james", "US"}); | ||
|
||
// verify the partition dir is created | ||
List<String> subDirs = listDirs(getTableLocation(deltatb2)); | ||
assertThat(subDirs).contains("_delta_log", "country=CHINA", "country=US"); | ||
|
||
// test listTables | ||
List<Object[]> tables = sql("SHOW TABLES"); | ||
assertThat(tables.size()).isEqualTo(2); | ||
assertThat(tables) | ||
.contains( | ||
new Object[] {defaultNs, deltatb1, false}, new Object[] {defaultNs, deltatb2, false}); | ||
|
||
sql("DROP TABLE %s", deltatb1); | ||
sql("DROP TABLE %s", deltatb2); | ||
tables = sql("SHOW TABLES"); | ||
assertThat(tables.size()).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
public void testAlterOperations() { | ||
String deltatb = getTableNameWithRandomSuffix(); | ||
sql( | ||
"CREATE TABLE %s (id INT, name STRING) USING DELTA LOCATION '%s'", | ||
deltatb, getTableLocation(deltatb)); | ||
sql("INSERT INTO %s VALUES (1, 'anna'), (2, 'bob')", deltatb); | ||
|
||
// test alter columns | ||
// add two new columns to the table | ||
sql("Alter TABLE %s ADD COLUMNS (city STRING, age INT)", deltatb); | ||
// add one more row to the table | ||
sql("INSERT INTO %s VALUES (3, 'john', 'SFO', 20)", deltatb); | ||
// verify the table now have 4 columns with correct result | ||
List<Object[]> results = sql("SELECT * FROM %s ORDER BY id", deltatb); | ||
assertThat(results.size()).isEqualTo(3); | ||
assertThat(results).contains(new Object[] {1, "anna", null, null}); | ||
assertThat(results).contains(new Object[] {2, "bob", null, null}); | ||
assertThat(results).contains(new Object[] {3, "john", "SFO", 20}); | ||
|
||
// drop and rename column require set the delta.columnMapping property | ||
sql("ALTER TABLE %s SET TBLPROPERTIES ('delta.columnMapping.mode' = 'name')", deltatb); | ||
// drop column age | ||
sql("Alter TABLE %s DROP COLUMN age", deltatb); | ||
// verify the table now have 3 columns with correct result | ||
results = sql("SELECT * FROM %s ORDER BY id", deltatb); | ||
assertThat(results.size()).isEqualTo(3); | ||
assertThat(results).contains(new Object[] {1, "anna", null}); | ||
assertThat(results).contains(new Object[] {2, "bob", null}); | ||
assertThat(results).contains(new Object[] {3, "john", "SFO"}); | ||
|
||
// rename column city to address | ||
sql("Alter TABLE %s RENAME COLUMN city TO address", deltatb); | ||
// verify column address exists | ||
results = sql("SELECT id, address FROM %s ORDER BY id", deltatb); | ||
assertThat(results.size()).isEqualTo(3); | ||
assertThat(results).contains(new Object[] {1, null}); | ||
assertThat(results).contains(new Object[] {2, null}); | ||
assertThat(results).contains(new Object[] {3, "SFO"}); | ||
|
||
// test alter properties | ||
sql( | ||
"ALTER TABLE %s SET TBLPROPERTIES ('description' = 'people table', 'test-owner' = 'test-user')", | ||
deltatb); | ||
List<Object[]> tableInfo = sql("DESCRIBE TABLE EXTENDED %s", deltatb); | ||
// find the table properties result | ||
String properties = null; | ||
for (Object[] info : tableInfo) { | ||
if (info[0].equals("Table Properties")) { | ||
properties = (String) info[1]; | ||
break; | ||
} | ||
} | ||
assertThat(properties).contains("description=people table,test-owner=test-user"); | ||
sql("DROP TABLE %s", deltatb); | ||
} | ||
|
||
@Test | ||
public void testUnsupportedAlterTableOperations() { | ||
String deltatb = getTableNameWithRandomSuffix(); | ||
sql( | ||
"CREATE TABLE %s (name String, age INT, country STRING) USING DELTA PARTITIONED BY (country) LOCATION '%s'", | ||
deltatb, getTableLocation(deltatb)); | ||
|
||
// ALTER TABLE ... RENAME TO ... fails | ||
assertThatThrownBy(() -> sql("ALTER TABLE %s RENAME TO new_delta", deltatb)) | ||
.isInstanceOf(UnsupportedOperationException.class); | ||
|
||
// ALTER TABLE ... SET LOCATION ... fails | ||
assertThatThrownBy(() -> sql("ALTER TABLE %s SET LOCATION '/tmp/new/path'", deltatb)) | ||
.isInstanceOf(DeltaAnalysisException.class); | ||
|
||
sql("DROP TABLE %s", deltatb); | ||
} | ||
|
||
@Test | ||
public void testUnsupportedTableCreateOperations() { | ||
String deltatb = getTableNameWithRandomSuffix(); | ||
// create delta table with no location | ||
assertThatThrownBy(() -> sql("CREATE TABLE %s (id INT, name STRING) USING DELTA", deltatb)) | ||
.isInstanceOf(UnsupportedOperationException.class); | ||
|
||
// CTAS fails | ||
assertThatThrownBy( | ||
() -> | ||
sql( | ||
"CREATE TABLE %s USING DELTA LOCATION '%s' AS SELECT 1 AS id", | ||
deltatb, getTableLocation(deltatb))) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test | ||
public void testDataframeSaveOperations() { | ||
List<Row> data = Arrays.asList(RowFactory.create("Alice", 30), RowFactory.create("Bob", 25)); | ||
StructType schema = | ||
new StructType( | ||
new StructField[] { | ||
new StructField("name", DataTypes.StringType, false, Metadata.empty()), | ||
new StructField("age", DataTypes.IntegerType, false, Metadata.empty()) | ||
}); | ||
Dataset<Row> df = spark.createDataFrame(data, schema); | ||
|
||
String deltatb = getTableNameWithRandomSuffix(); | ||
// saveAsTable requires support for delta requires CTAS support for third party catalog | ||
// in delta catalog, which is currently not supported. | ||
assertThatThrownBy( | ||
() -> | ||
df.write() | ||
.format("delta") | ||
.option("path", getTableLocation(deltatb)) | ||
.saveAsTable(deltatb)) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
|
||
// verify regular dataframe saving still works | ||
df.write().format("delta").save(getTableLocation(deltatb)); | ||
|
||
// verify the partition dir is created | ||
List<String> subDirs = listDirs(getTableLocation(deltatb)); | ||
assertThat(subDirs).contains("_delta_log"); | ||
|
||
// verify we can create a table out of the exising delta location | ||
sql("CREATE TABLE %s USING DELTA LOCATION '%s'", deltatb, getTableLocation(deltatb)); | ||
List<Object[]> tables = sql("SHOW TABLES"); | ||
assertThat(tables.size()).isEqualTo(1); | ||
assertThat(tables).contains(new Object[] {defaultNs, deltatb, false}); | ||
|
||
sql("INSERT INTO %s VALUES ('Anna', 11)", deltatb); | ||
|
||
List<Object[]> results = sql("SELECT * FROM %s ORDER BY name", deltatb); | ||
assertThat(results.size()).isEqualTo(3); | ||
assertThat(results.get(0)).isEqualTo(new Object[] {"Alice", 30}); | ||
assertThat(results.get(1)).isEqualTo(new Object[] {"Anna", 11}); | ||
assertThat(results.get(2)).isEqualTo(new Object[] {"Bob", 25}); | ||
|
||
sql("DROP TABLE %s", deltatb); | ||
} | ||
} |
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.
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.
Q: Is the enforcement for the spark version or the Delta plugin? We might document it for any future changes.
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.
it is for spark, updated the comment to be more clear