-
Notifications
You must be signed in to change notification settings - Fork 0
Depend on Spark-3.0.0-palantir.18 (cont'd) #1
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
Changes from all commits
ef7af82
81f87d9
f8ee9cd
0cbf39b
2149ba0
019b796
9b2f3e0
cd76f66
f3bfada
a486536
e74ed5e
4a821e4
f4018da
c732fd8
f727e6e
eb08e1c
d04c3e4
4b327ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,32 +20,18 @@ | |
| package com.netflix.iceberg.spark.source; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import com.netflix.iceberg.FileFormat; | ||
| import com.netflix.iceberg.Schema; | ||
| import com.netflix.iceberg.Table; | ||
| import com.netflix.iceberg.hadoop.HadoopTables; | ||
| import com.netflix.iceberg.spark.SparkSchemaUtil; | ||
| import com.netflix.iceberg.types.CheckCompatibility; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.spark.sql.SaveMode; | ||
| import org.apache.spark.sql.SparkSession; | ||
| import org.apache.spark.sql.sources.DataSourceRegister; | ||
| import org.apache.spark.sql.sources.v2.DataSourceV2; | ||
| import org.apache.spark.sql.sources.v2.DataSourceOptions; | ||
| import org.apache.spark.sql.sources.v2.ReadSupport; | ||
| import org.apache.spark.sql.sources.v2.WriteSupport; | ||
| import org.apache.spark.sql.sources.v2.reader.DataSourceReader; | ||
| import org.apache.spark.sql.sources.v2.writer.DataSourceWriter; | ||
| import org.apache.spark.sql.sources.v2.TableProvider; | ||
| import org.apache.spark.sql.types.StructType; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import static com.netflix.iceberg.TableProperties.DEFAULT_FILE_FORMAT; | ||
| import static com.netflix.iceberg.TableProperties.DEFAULT_FILE_FORMAT_DEFAULT; | ||
|
|
||
| public class IcebergSource implements DataSourceV2, ReadSupport, WriteSupport, DataSourceRegister { | ||
| public class IcebergSource implements TableProvider, DataSourceRegister { | ||
|
|
||
| private SparkSession lazySpark = null; | ||
| private Configuration lazyConf = null; | ||
|
|
@@ -55,45 +41,6 @@ public String shortName() { | |
| return "iceberg"; | ||
| } | ||
|
|
||
| @Override | ||
| public DataSourceReader createReader(DataSourceOptions options) { | ||
| Configuration conf = new Configuration(lazyBaseConf()); | ||
| Table table = getTableAndResolveHadoopConfiguration(options, conf); | ||
| return new Reader(table); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<DataSourceWriter> createWriter(String jobId, StructType dfStruct, SaveMode mode, | ||
| DataSourceOptions options) { | ||
| Preconditions.checkArgument(mode == SaveMode.Append, "Save mode %s is not supported", mode); | ||
| Configuration conf = new Configuration(lazyBaseConf()); | ||
| Table table = getTableAndResolveHadoopConfiguration(options, conf); | ||
|
|
||
| Schema dfSchema = SparkSchemaUtil.convert(table.schema(), dfStruct); | ||
| List<String> errors = CheckCompatibility.writeCompatibilityErrors(table.schema(), dfSchema); | ||
| if (!errors.isEmpty()) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append("Cannot write incompatible dataframe to table with schema:\n") | ||
| .append(table.schema()).append("\nProblems:"); | ||
| for (String error : errors) { | ||
| sb.append("\n* ").append(error); | ||
| } | ||
| throw new IllegalArgumentException(sb.toString()); | ||
| } | ||
|
|
||
| Optional<String> formatOption = options.get("iceberg.write.format"); | ||
| FileFormat format; | ||
| if (formatOption.isPresent()) { | ||
| format = FileFormat.valueOf(formatOption.get().toUpperCase(Locale.ENGLISH)); | ||
| } else { | ||
| format = FileFormat.valueOf(table.properties() | ||
| .getOrDefault(DEFAULT_FILE_FORMAT, DEFAULT_FILE_FORMAT_DEFAULT) | ||
| .toUpperCase(Locale.ENGLISH)); | ||
| } | ||
|
|
||
| return Optional.of(new Writer(table, format)); | ||
| } | ||
|
|
||
| protected Table findTable(DataSourceOptions options, Configuration conf) { | ||
| Optional<String> location = options.get("path"); | ||
| Preconditions.checkArgument(location.isPresent(), | ||
|
|
@@ -136,4 +83,16 @@ private static void mergeIcebergHadoopConfs( | |
| .filter(key -> key.startsWith("iceberg.hadoop")) | ||
| .forEach(key -> baseConf.set(key.replaceFirst("iceberg.hadoop", ""), options.get(key))); | ||
| } | ||
|
|
||
| @Override | ||
| public org.apache.spark.sql.sources.v2.Table getTable(DataSourceOptions options) { | ||
| Configuration conf = new Configuration(lazyBaseConf()); | ||
| Table table = getTableAndResolveHadoopConfiguration(options, conf); | ||
| return new IcebergSparkTable(table); | ||
| } | ||
|
|
||
| @Override | ||
| public org.apache.spark.sql.sources.v2.Table getTable(DataSourceOptions options, StructType schema) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be no need to implement this method. Iceberg tables always have a schema so it makes no sense to supply one. That's for cases like CSV where a schema is required to interpret the data (column names, and types). Otherwise, normal projection works just fine.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you still need it for write? Sort of analogous to the previous There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In DSv2, the writer is passed the physical plan and schema, then calls TableCatalog.createTable with that schema. After it creates the table, it uses the instance returned by create. These methods are called by the DataFrameReader and DataFrameWriter and assume that the table already exists. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throwing |
||
| throw new UnsupportedOperationException("Schema should never be passed into an iceberg table"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| /* | ||
| * 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 com.netflix.iceberg.spark.source; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import com.google.common.collect.Lists; | ||
| import com.netflix.iceberg.FileFormat; | ||
| import com.netflix.iceberg.Table; | ||
| import com.netflix.iceberg.expressions.Expression; | ||
| import com.netflix.iceberg.spark.SparkFilters; | ||
| import com.netflix.iceberg.spark.SparkSchemaUtil; | ||
| import org.apache.spark.sql.SaveMode; | ||
| import org.apache.spark.sql.sources.Filter; | ||
| import org.apache.spark.sql.sources.v2.DataSourceOptions; | ||
| import org.apache.spark.sql.sources.v2.SupportsBatchRead; | ||
| import org.apache.spark.sql.sources.v2.SupportsBatchWrite; | ||
| import org.apache.spark.sql.sources.v2.reader.*; | ||
| import org.apache.spark.sql.sources.v2.writer.BatchWrite; | ||
| import org.apache.spark.sql.sources.v2.writer.SupportsSaveMode; | ||
| import org.apache.spark.sql.sources.v2.writer.WriteBuilder; | ||
| import org.apache.spark.sql.types.StructType; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Optional; | ||
|
|
||
| import static com.netflix.iceberg.TableProperties.DEFAULT_FILE_FORMAT; | ||
| import static com.netflix.iceberg.TableProperties.DEFAULT_FILE_FORMAT_DEFAULT; | ||
|
|
||
| public class IcebergSparkTable implements SupportsBatchRead, SupportsBatchWrite { | ||
|
|
||
| private final Table table; | ||
|
|
||
| public IcebergSparkTable(Table table) { | ||
| this.table = table; | ||
| } | ||
|
|
||
| @Override | ||
| public ScanBuilder newScanBuilder(DataSourceOptions options) { | ||
| return new IcebergReaderBuilder(table); | ||
| } | ||
|
|
||
| @Override | ||
| public WriteBuilder newWriteBuilder(DataSourceOptions options) { | ||
| Optional<String> formatOption = options.get("iceberg.write.format"); | ||
| if (formatOption.isPresent()) { | ||
| return new IcebergWriterBuilder(table, FileFormat.valueOf(formatOption.get().toUpperCase(Locale.ENGLISH))); | ||
| } | ||
| return new IcebergWriterBuilder( | ||
| table, | ||
| FileFormat.valueOf(table.properties() | ||
| .getOrDefault(DEFAULT_FILE_FORMAT, DEFAULT_FILE_FORMAT_DEFAULT) | ||
| .toUpperCase(Locale.ENGLISH))); | ||
| } | ||
|
|
||
| @Override | ||
| public String name() { | ||
| return table.location(); | ||
| } | ||
|
|
||
| @Override | ||
| public StructType schema() { | ||
| return SparkSchemaUtil.convert(table.schema()); | ||
| } | ||
|
|
||
|
|
||
| private static class IcebergWriterBuilder implements WriteBuilder, | ||
| SupportsSaveMode { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should just say that we don't support SaveMode here, since we're specifically checking that only appending is allowed anyways. We'll discuss how to get Iceberg connected to table catalogs and the V2 logical plans when the time comes. |
||
| private final Table table; | ||
| private final FileFormat fileFormat; | ||
|
|
||
| public IcebergWriterBuilder(Table table, FileFormat fileFormat) { | ||
| this.table = table; | ||
| this.fileFormat = fileFormat; | ||
| } | ||
|
|
||
| @Override | ||
| public BatchWrite buildForBatch() { | ||
| return new Writer(table, fileFormat); | ||
| } | ||
|
|
||
| @Override | ||
| public WriteBuilder mode(SaveMode mode) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should just say that we don't support
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok yup, the reason I did that was because some tests relied on it, but I can update those tests too. |
||
| Preconditions.checkArgument(mode == SaveMode.Append, "Save mode %s is not supported", mode); | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| private static class IcebergReaderBuilder implements ScanBuilder, | ||
| SupportsPushDownFilters, | ||
| SupportsPushDownRequiredColumns { | ||
|
|
||
| private static final Filter[] NO_FILTERS = new Filter[0]; | ||
|
|
||
| private final Table table; | ||
| private Filter[] pushedFilters = NO_FILTERS; | ||
| private List<Expression> filterExpressions = Lists.newArrayList(); | ||
| private StructType requestedSchema = null; | ||
|
|
||
| public IcebergReaderBuilder(Table table) { | ||
| this.table = table; | ||
| } | ||
|
|
||
| @Override | ||
| public Filter[] pushFilters(Filter[] filters) { | ||
| List<Expression> expressions = Lists.newArrayListWithExpectedSize(filters.length); | ||
| List<Filter> pushed = Lists.newArrayListWithExpectedSize(filters.length); | ||
|
|
||
| for (Filter filter : filters) { | ||
| Expression expr = SparkFilters.convert(filter); | ||
| if (expr != null) { | ||
| expressions.add(expr); | ||
| pushed.add(filter); | ||
| } | ||
| } | ||
|
|
||
| this.filterExpressions = expressions; | ||
| this.pushedFilters = pushed.toArray(new Filter[0]); | ||
|
|
||
| // Spark doesn't support residuals per task, so return all filters | ||
| // to get Spark to handle record-level filtering | ||
| return filters; | ||
| } | ||
|
|
||
| @Override | ||
| public Filter[] pushedFilters() { | ||
| return pushedFilters; | ||
| } | ||
|
|
||
| @Override | ||
| public void pruneColumns(StructType requiredSchema) { | ||
| this.requestedSchema = requiredSchema; | ||
| } | ||
|
|
||
| @Override | ||
| public Scan build() { | ||
| return new Reader(table, filterExpressions, requestedSchema); | ||
| } | ||
| } | ||
| } | ||
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.
Did we need to use
commons-lang3? Not entirely opposed to it, just wondering if we can minimize the diff just a tiny bit.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.
Hmm the original import doesn't seem to be part of the natural dependency tree anymore :/
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.
That's fine then, actually we probably want to be exclusively using
lang3in upstream as well. We'll catch this if we introduce Baseline and linting has a rule for it.