Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
14 changes: 11 additions & 3 deletions docs/docs/spark-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ spark.read
.table("catalog.db.table")
```

Iceberg 1.8.0 and later support setting read options by Spark session configuration `spark.datasource.iceberg.<key>=<value>`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is good, but was also thinking of adding a section for priority as well as mentioned.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This can be in its own section, like "session level configuration"?

@szehon-ho szehon-ho Nov 21, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I still think we need new section like 'Configuration Priority' where we can explain the order of precedence:
DataFrame Writes:

  • explicit dataframeWriter option
  • dataframe session default
  • if table exists, explicit table option
  • if table exists, table default

DataFrame Reads:

  • explicit dataFrameReader option
  • dataframe session default
  • if table exists, explicit table option
  • if table exists, table default

(please double check)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I hesitate to write such a section because the situation looks more complex, some configurations are allowed to be set by dedicated session configuration, for example

  public boolean localityEnabled() {
    boolean defaultValue = Util.mayHaveBlockLocations(table.io(), table.location());
    return confParser
        .booleanConf()
        .option(SparkReadOptions.LOCALITY)
        .sessionConf(SparkSQLProperties.LOCALITY)
        .defaultValue(defaultValue)
        .parse();
  }

when using DataFrame to read Iceberg tables, for example: `spark.datasource.iceberg.split-size=512m`, it has lower priority
than options explicitly passed to DataFrameReader.

| Spark option | Default | Description |
| --------------- | --------------------- | ----------------------------------------------------------------------------------------- |
| snapshot-id | (latest) | Snapshot ID of the table snapshot to read |
Expand All @@ -167,16 +171,20 @@ spark.read

### Write options

Spark write options are passed when configuring the DataFrameWriter, like this:
Spark write options are passed when configuring the DataFrameWriterV2, like this:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I replaced the example with DataFrameWriterV2 because

The v1 DataFrame `write` API is still supported, but is not recommended.


```scala
// write with Avro instead of Parquet
df.write
df.writeTo("catalog.db.table")
.option("write-format", "avro")
.option("snapshot-property.key", "value")
.insertInto("catalog.db.table")
.append()
```

Iceberg 1.8.0 and later support setting write options by Spark session configuration `spark.datasource.iceberg.<key>=<value>`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we extract to its own section, no need to repeat it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I write it here because it's "Write options", actually, Spark has different concepts to allow the format/extensions to control the behavior, i.e. table properties, session configurations, options.

when using DataFrame to write Iceberg tables, for example: `spark.datasource.iceberg.write-format=orc`, it has lower priority
than options explicitly passed to DataFrameWriterV2.

| Spark option | Default | Description |
| ---------------------- | -------------------------- | ------------------------------------------------------------ |
| write-format | Table write.format.default | File format to use for this write operation; parquet, avro, or orc |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.spark.sql.connector.catalog.CatalogManager;
import org.apache.spark.sql.connector.catalog.CatalogPlugin;
import org.apache.spark.sql.connector.catalog.Identifier;
import org.apache.spark.sql.connector.catalog.SessionConfigSupport;
import org.apache.spark.sql.connector.catalog.SupportsCatalogOptions;
import org.apache.spark.sql.connector.catalog.Table;
import org.apache.spark.sql.connector.catalog.TableCatalog;
Expand All @@ -61,7 +62,8 @@
* <p>The above list is in order of priority. For example: a matching catalog will take priority
* over any namespace resolution.
*/
public class IcebergSource implements DataSourceRegister, SupportsCatalogOptions {
public class IcebergSource

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Another comment, is it now multiple ways to configure properties (including #4011), it may be confusing to user. Worth to add a documentation about it, listing the precedence, ie:

I guess using dataframe API (to be double-checked)

  • explicit dataframe option
  • dataframe session default
  • if table exists, explicit table option
  • if table exists, table default

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I updated the docs and hope it's clear now.

implements DataSourceRegister, SupportsCatalogOptions, SessionConfigSupport {
private static final String DEFAULT_CATALOG_NAME = "default_iceberg";
private static final String DEFAULT_CACHE_CATALOG_NAME = "default_cache_iceberg";
private static final String DEFAULT_CATALOG = "spark.sql.catalog." + DEFAULT_CATALOG_NAME;
Expand All @@ -80,6 +82,11 @@ public String shortName() {
return "iceberg";
}

@Override
public String keyPrefix() {
return shortName();
}

@Override
public StructType inferSchema(CaseInsensitiveStringMap options) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2184,6 +2184,51 @@ private void testWithFilter(String filterExpr, TableIdentifier tableIdentifier)
assertThat(actual).as("Rows must match").containsExactlyInAnyOrderElementsOf(expected);
}

@Test
public void testSessionConfigSupport() {
PartitionSpec spec = PartitionSpec.builderFor(SCHEMA).identity("id").build();
TableIdentifier tableIdentifier = TableIdentifier.of("db", "session_config_table");
Table table = createTable(tableIdentifier, SCHEMA, spec);

List<SimpleRecord> initialRecords =
Lists.newArrayList(
new SimpleRecord(1, "a"), new SimpleRecord(2, "b"), new SimpleRecord(3, "c"));

Dataset<Row> df = spark.createDataFrame(initialRecords, SimpleRecord.class);

df.select("id", "data")
.write()
.format("iceberg")
.mode(SaveMode.Append)
.save(loadLocation(tableIdentifier));

long s1 = table.currentSnapshot().snapshotId();

withSQLConf(
// set write option through session configuration
ImmutableMap.of("spark.datasource.iceberg.snapshot-property.foo", "bar"),
() -> {
df.select("id", "data")
.write()
.format("iceberg")
.mode(SaveMode.Append)
.save(loadLocation(tableIdentifier));
});

table.refresh();
Assert.assertEquals("bar", table.currentSnapshot().summary().get("foo"));

withSQLConf(
// set read option through session configuration
ImmutableMap.of("spark.datasource.iceberg.snapshot-id", String.valueOf(s1)),
() -> {
Dataset<Row> result = spark.read().format("iceberg").load(loadLocation(tableIdentifier));
List<SimpleRecord> actual = result.as(Encoders.bean(SimpleRecord.class)).collectAsList();
Assert.assertEquals("Number of rows should match", initialRecords.size(), actual.size());
Assert.assertEquals("Result rows should match", initialRecords, actual);
});
}

private GenericData.Record manifestRecord(
Table manifestTable, Long referenceSnapshotId, ManifestFile manifest) {
GenericRecordBuilder builder =
Expand Down