Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.polaris.service.it.ext;

import static org.apache.polaris.service.it.env.PolarisClient.polarisClient;
import static org.apache.polaris.service.it.ext.SparkSessionBuilder.CatalogType.ICEBERG;

import com.adobe.testing.s3mock.testcontainers.S3MockContainer;
import java.io.IOException;
Expand Down Expand Up @@ -159,45 +160,19 @@ public void before(

managementApi.createCatalog(externalCatalog);

SparkSession.Builder sessionBuilder =
SparkSession.builder()
.master("local[1]")
.config("spark.hadoop.fs.s3.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem")
.config(
"spark.hadoop.fs.s3.aws.credentials.provider",
"org.apache.hadoop.fs.s3.TemporaryAWSCredentialsProvider")
.config("spark.hadoop.fs.s3.access.key", "foo")
.config("spark.hadoop.fs.s3.secret.key", "bar")
.config(
"spark.sql.extensions",
"org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions")
.config("spark.ui.showConsoleProgress", false)
.config("spark.ui.enabled", "false");
spark =
withCatalog(withCatalog(sessionBuilder, catalogName), externalCatalogName).getOrCreate();
spark = buildSparkSession();

onSpark("USE " + catalogName);
}

protected SparkSession.Builder withCatalog(SparkSession.Builder builder, String catalogName) {
return builder
.config(
String.format("spark.sql.catalog.%s", catalogName),
"org.apache.iceberg.spark.SparkCatalog")
.config("spark.sql.warehouse.dir", warehouseDir.toString())
.config(String.format("spark.sql.catalog.%s.type", catalogName), "rest")
.config(
String.format("spark.sql.catalog.%s.uri", catalogName),
endpoints.catalogApiEndpoint().toString())
.config(String.format("spark.sql.catalog.%s.warehouse", catalogName), catalogName)
.config(String.format("spark.sql.catalog.%s.scope", catalogName), "PRINCIPAL_ROLE:ALL")
.config(
String.format("spark.sql.catalog.%s.header.realm", catalogName), endpoints.realmId())
.config(String.format("spark.sql.catalog.%s.token", catalogName), sparkToken)
.config(String.format("spark.sql.catalog.%s.s3.access-key-id", catalogName), "fakekey")
.config(
String.format("spark.sql.catalog.%s.s3.secret-access-key", catalogName), "fakesecret")
.config(String.format("spark.sql.catalog.%s.s3.region", catalogName), "us-west-2");
protected SparkSession buildSparkSession() {
return SparkSessionBuilder.withTestDefaults()
.withS3MockContainer()
.withExtensions(SparkSessionBuilder.ExtensionType.ICEBERG_ONLY)
.withWarehouse(warehouseDir)
.addCatalog(catalogName, ICEBERG, endpoints, sparkToken)
.addCatalog(externalCatalogName, ICEBERG, endpoints, sparkToken)
.createSession();
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
/*
* 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.service.it.ext;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.polaris.service.it.env.PolarisApiEndpoints;
import org.apache.spark.sql.SparkSession;

/**
* A fluent builder for configuring SparkSession instances with Polaris catalogs.
*
* <p>Example usage:
*
* <pre>
* SparkSession session = SparkSessionBuilder
* .withTestDefaults()
* .addCatalog("catalog1", CatalogType.ICEBERG, endpoints, token)
* .addCatalog("catalog2", CatalogType.ICEBERG, endpoints, token)
* .createSession();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be also useful to have an example about the actual sparkSession build result with in the comment also
like

.config(
            String.format("spark.sql.catalog.%s", catalogName),
            "org.apache.iceberg.spark.SparkCatalog")
        .config("spark.sql.warehouse.dir", warehouseDir.toString())
        .config(String.format("spark.sql.catalog.%s.type", catalogName), "rest")
        .config(
            String.format("spark.sql.catalog.%s.uri", catalogName),
            endpoints.catalogApiEndpoint().toString())
        .config(String.format("spark.sql.catalog.%s.warehouse", catalogName), catalogName)
        .config(String.format("spark.sql.catalog.%s.scope", catalogName), "PRINCIPAL_ROLE:ALL")
        .config(
            String.format("spark.sql.catalog.%s.header.realm", catalogName), endpoints.realmId())
        .config(String.format("spark.sql.catalog.%s.token", catalogName), sparkToken)
        .config(String.format("spark.sql.catalog.%s.s3.access-key-id", catalogName), "fakekey")
        .config(
            String.format("spark.sql.catalog.%s.s3.secret-access-key", catalogName), "fakesecret")
        .config(String.format("spark.sql.catalog.%s.s3.region", catalogName), "us-west-2");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, i just mean in the comment also add description about how the final effect will look like to help illustrate the usage

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add more descriptions as the Java class doc.

* </pre>
*/
public class SparkSessionBuilder {

public enum CatalogType {
ICEBERG("org.apache.iceberg.spark.SparkCatalog"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we can simply have catalogImplClass, which is a string, so the test can choose whatever SparkCatalog they want to sue

POLARIS("org.apache.polaris.spark.SparkCatalog");

private final String implementationClass;

CatalogType(String implementationClass) {
this.implementationClass = implementationClass;
}

public String getImplementationClass() {
return implementationClass;
}
}

public enum ExtensionType {
ICEBERG_ONLY("org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly, let's just take a list of extension class strings, so later when we do hudi test, and possible combination, the test can just pass whatever extension string they want

ICEBERG_AND_DELTA(
"org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions,io.delta.sql.DeltaSparkSessionExtension");

private final String extensionClasses;

ExtensionType(String extensionClasses) {
this.extensionClasses = extensionClasses;
}

public String getExtensionClasses() {
return extensionClasses;
}
}

private static class ConfigPair {
final String key;
final String value;

ConfigPair(String key, String value) {
this.key = key;
this.value = value;
}
}

/** Configuration for a single catalog. */
private static class CatalogConfig {
final String catalogName;
final CatalogType catalogType;
final PolarisApiEndpoints endpoints;
final String token;
final List<ConfigPair> catalogSpecificConfigs;

CatalogConfig(
String catalogName,
CatalogType catalogType,
PolarisApiEndpoints endpoints,
String token,
List<ConfigPair> catalogSpecificConfigs) {
this.catalogName = catalogName;
this.catalogType = catalogType;
this.endpoints = endpoints;
this.token = token;
this.catalogSpecificConfigs =
catalogSpecificConfigs != null ? catalogSpecificConfigs : new ArrayList<>();
}
}

private final SparkSession.Builder builder;
private final List<CatalogConfig> catalogs = new ArrayList<>();
private final List<ConfigPair> additionalConfigs = new ArrayList<>();

private ExtensionType extensionType = ExtensionType.ICEBERG_ONLY;
private URI warehouseDir;
private boolean includeDeltaCatalogConfig = false;

private SparkSessionBuilder(SparkSession.Builder builder) {
this.builder = builder;
}

/**
* Create a SparkSessionBuilder with common test defaults: local master and disabled UI.
*
* @return new builder instance with test defaults
*/
public static SparkSessionBuilder withTestDefaults() {
return new SparkSessionBuilder(SparkSession.builder()).withLocalMaster().withDisabledUI();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are always using testDefaults, right? i don't see a case that we need to use none localMaster without disableUI, let's just remove this configurability

}

public SparkSessionBuilder master(String master) {
this.builder.master(master);
return this;
}

public SparkSessionBuilder appName(String name) {
this.builder.appName(name);
return this;
}

public SparkSessionBuilder withLocalMaster(int cores) {
return master(String.format("local[%d]", cores));
}

public SparkSessionBuilder withLocalMaster() {
return withLocalMaster(1);
}

public SparkSessionBuilder withDisabledUI() {
return withConfig("spark.ui.showConsoleProgress", "false")
Comment thread
flyrain marked this conversation as resolved.
Outdated
.withConfig("spark.ui.enabled", "false");
}

public SparkSessionBuilder withWarehouse(URI warehouseDir) {
this.warehouseDir = warehouseDir;
return this;
}

public SparkSessionBuilder withExtensions(ExtensionType extensionType) {
this.extensionType = extensionType;
return this;
}

public SparkSessionBuilder withDeltaCatalogConfig() {
this.includeDeltaCatalogConfig = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove the delta config, so we don't have to add hudiConfig or other format config later which could make this utility unncessary complicated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, instead of withDeltaCatalogConfig, we can probably take a sparkSessionCatalogImplClass in general for setting up of spark_catalog

return this;
}

public SparkSessionBuilder withS3MockContainer() {
return withS3FileSystem("foo", "bar");
Comment thread
flyrain marked this conversation as resolved.
Outdated
}

public SparkSessionBuilder withS3FileSystem(String accessKey, String secretKey) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we can also take the s3 region as an input to set the following configuration

.config(String.format("spark.sql.catalog.%s.s3.region", catalogName), "us-west-2");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spark.sql.catalog.%s.s3.region is needed only if we config the fileIO like this
spark.sql.catalog.catalog-name.io-impl=xxx, which isn't applicable here.

return withConfig("spark.hadoop.fs.s3.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem")
.withConfig(
"spark.hadoop.fs.s3.aws.credentials.provider",
"org.apache.hadoop.fs.s3.TemporaryAWSCredentialsProvider")
.withConfig("spark.hadoop.fs.s3.access.key", accessKey)
.withConfig("spark.hadoop.fs.s3.secret.key", secretKey);
}

public SparkSessionBuilder addCatalog(
String catalogName, CatalogType catalogType, PolarisApiEndpoints endpoints, String token) {
this.catalogs.add(
new CatalogConfig(catalogName, catalogType, endpoints, token, new ArrayList<>()));
return this;
}

public SparkSessionBuilder withConfig(String key, String value) {
this.additionalConfigs.add(new ConfigPair(key, value));
return this;
}

public SparkSession createSession() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just call this function getOrCreate() to be more precise, since we call getOrCreate for SparkSession

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I thought about it extends from SparkSession.Builder, but it seems a bit over-engineering to me.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, i don't mean to extend the SparkSession.build function, I was suggesting to rename the function name from createSession() to getOrCreate()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed

return build().getOrCreate();
}

/**
* Build the underlying SparkSession.Builder with all configurations applied. Use this if you need
* to do additional configuration before creating the session.
*
* @return configured SparkSession.Builder
*/
public SparkSession.Builder build() {
SparkSession.Builder configuredBuilder = builder;

// Apply core configurations
configuredBuilder = applyExtensions(configuredBuilder);
configuredBuilder = applyDeltaConfig(configuredBuilder);
configuredBuilder = applyWarehouseConfig(configuredBuilder);

// Apply catalog configurations
configuredBuilder = applyCatalogConfigurations(configuredBuilder);

// Apply additional configurations
configuredBuilder = applyAdditionalConfigurations(configuredBuilder);

return configuredBuilder;
}

private SparkSession.Builder applyExtensions(SparkSession.Builder builder) {
return builder.config("spark.sql.extensions", extensionType.getExtensionClasses());
}

private SparkSession.Builder applyDeltaConfig(SparkSession.Builder builder) {
if (includeDeltaCatalogConfig) {
builder =
builder.config(
"spark.sql.catalog.spark_catalog", "org.apache.spark.sql.delta.catalog.DeltaCatalog");
}
return builder;
}

private SparkSession.Builder applyWarehouseConfig(SparkSession.Builder builder) {
if (warehouseDir != null) {
builder = builder.config("spark.sql.warehouse.dir", warehouseDir.toString());
}
return builder;
}

private SparkSession.Builder applyCatalogConfigurations(SparkSession.Builder builder) {
for (CatalogConfig catalog : catalogs) {
builder = applySingleCatalogConfig(builder, catalog);
}
return builder;
}

private SparkSession.Builder applySingleCatalogConfig(

Copilot AI Jul 1, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original manual builder included a spark.sql.catalog.<catalog>.s3.region setting for each catalog, but applySingleCatalogConfig no longer adds this. Consider adding support for catalog-specific S3 region configuration to preserve the prior behavior.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the s3.region isn't needed in the tests.

SparkSession.Builder builder, CatalogConfig catalog) {
// Basic catalog configuration
builder =
builder
.config(
String.format("spark.sql.catalog.%s", catalog.catalogName),
catalog.catalogType.getImplementationClass())
.config(String.format("spark.sql.catalog.%s.type", catalog.catalogName), "rest")
.config(
String.format("spark.sql.catalog.%s.warehouse", catalog.catalogName),
catalog.catalogName)
.config(
String.format("spark.sql.catalog.%s.scope", catalog.catalogName),
"PRINCIPAL_ROLE:ALL");

// Add endpoint configuration
if (catalog.endpoints != null) {
Comment thread
flyrain marked this conversation as resolved.
Outdated
builder =
builder
.config(
String.format("spark.sql.catalog.%s.uri", catalog.catalogName),
catalog.endpoints.catalogApiEndpoint().toString())
.config(
String.format("spark.sql.catalog.%s.header.realm", catalog.catalogName),
catalog.endpoints.realmId());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realmId is set here

}

// Add token configuration
if (catalog.token != null) {
builder =
builder.config(
String.format("spark.sql.catalog.%s.token", catalog.catalogName), catalog.token);
}

// Add catalog-specific configurations
for (ConfigPair config : catalog.catalogSpecificConfigs) {
builder = builder.config(config.key, config.value);
}

return builder;
}

private SparkSession.Builder applyAdditionalConfigurations(SparkSession.Builder builder) {
for (ConfigPair config : additionalConfigs) {
builder = builder.config(config.key, config.value);
}
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,19 @@
package org.apache.polaris.spark.quarkus.it;

import io.quarkus.test.junit.QuarkusIntegrationTest;
import org.apache.polaris.service.it.ext.SparkSessionBuilder;
import org.apache.spark.sql.SparkSession;

@QuarkusIntegrationTest
public class SparkCatalogIcebergIT extends SparkCatalogBaseIT {
/** Initialize the spark catalog to use the iceberg spark catalog. */
@Override
protected SparkSession.Builder withCatalog(SparkSession.Builder builder, String catalogName) {
return builder
.config(
"spark.sql.extensions",
"org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions")
.config(
String.format("spark.sql.catalog.%s", catalogName),
"org.apache.iceberg.spark.SparkCatalog")
.config("spark.sql.warehouse.dir", warehouseDir.toString())
.config(String.format("spark.sql.catalog.%s.type", catalogName), "rest")
.config(
String.format("spark.sql.catalog.%s.uri", catalogName),
endpoints.catalogApiEndpoint().toString())
.config(String.format("spark.sql.catalog.%s.warehouse", catalogName), catalogName)
.config(String.format("spark.sql.catalog.%s.scope", catalogName), "PRINCIPAL_ROLE:ALL")
.config(
String.format("spark.sql.catalog.%s.header.realm", catalogName), endpoints.realmId())
.config(String.format("spark.sql.catalog.%s.token", catalogName), sparkToken)
.config(String.format("spark.sql.catalog.%s.s3.access-key-id", catalogName), "fakekey")
.config(
String.format("spark.sql.catalog.%s.s3.secret-access-key", catalogName), "fakesecret")
.config(String.format("spark.sql.catalog.%s.s3.region", catalogName), "us-west-2");
protected SparkSession buildSparkSession() {
return SparkSessionBuilder.withTestDefaults()
.withS3MockContainer()
.withExtensions(SparkSessionBuilder.ExtensionType.ICEBERG_ONLY)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the realmId set seems missing

 String.format("spark.sql.catalog.%s.header.realm", catalogName), endpoints.realmId())

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.withWarehouse(warehouseDir)
.addCatalog(catalogName, SparkSessionBuilder.CatalogType.ICEBERG, endpoints, sparkToken)
.createSession();
}
}
Loading
Loading