-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Kernel: Add a conversion utility to convert Iceberg schema to Delta schema #4075
base: master
Are you sure you want to change the base?
Changes from all commits
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,5 +20,8 @@ from the Apache Spark project (www.github.com/apache/spark) | |
Apache Spark | ||
Copyright 2014 and onwards The Apache Software Foundation. | ||
|
||
Apache Iceberg | ||
Copyright 2017-2024 The Apache Software Foundation | ||
Comment on lines
+23
to
+24
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. Same as above, I think this change to the notice is correct, currently the kernel module doesn't have its own License/Notice so I added this to the root level. |
||
|
||
This product includes software developed at | ||
The Apache Software Foundation (http://www.apache.org/). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -577,7 +577,8 @@ lazy val kernelApi = (project in file("kernel/kernel-api")) | |
"com.fasterxml.jackson.core" % "jackson-core" % "2.13.5", | ||
"com.fasterxml.jackson.core" % "jackson-annotations" % "2.13.5", | ||
"com.fasterxml.jackson.datatype" % "jackson-datatype-jdk8" % "2.13.5", | ||
|
||
"org.apache.iceberg" % "iceberg-api" % "1.6.1", | ||
"org.apache.iceberg" % "iceberg-core" % "1.6.1", | ||
Comment on lines
+580
to
+581
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. Can't use 1.7.1 at the moment because Delta only builds with JDK 8 at the moment as far as I can tell, with the exception of Spark master which is using JDk 17 (spark 4.0 requires JDK 11+) |
||
"org.scalatest" %% "scalatest" % scalaTestVersion % "test", | ||
"junit" % "junit" % "4.13.2" % "test", | ||
"com.novocode" % "junit-interface" % "0.11" % "test", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright (2023) The Delta Lake Project Authors. | ||
* | ||
* Licensed 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 io.delta.kernel.internal.util; | ||
|
||
import io.delta.kernel.types.ArrayType; | ||
import io.delta.kernel.types.BinaryType; | ||
import io.delta.kernel.types.BooleanType; | ||
import io.delta.kernel.types.DataType; | ||
import io.delta.kernel.types.DateType; | ||
import io.delta.kernel.types.DecimalType; | ||
import io.delta.kernel.types.DoubleType; | ||
import io.delta.kernel.types.FieldMetadata; | ||
import io.delta.kernel.types.FloatType; | ||
import io.delta.kernel.types.IntegerType; | ||
import io.delta.kernel.types.LongType; | ||
import io.delta.kernel.types.MapType; | ||
import io.delta.kernel.types.StringType; | ||
import io.delta.kernel.types.StructField; | ||
import io.delta.kernel.types.StructType; | ||
import io.delta.kernel.types.TimestampNTZType; | ||
import io.delta.kernel.types.TimestampType; | ||
import java.util.List; | ||
import org.apache.iceberg.Schema; | ||
import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
import org.apache.iceberg.types.Type; | ||
import org.apache.iceberg.types.TypeUtil; | ||
import org.apache.iceberg.types.Types; | ||
|
||
/** | ||
* Schema visitor to convert Iceberg types to Delta kernel types Most code and behavior is taken | ||
* from {@link org.apache.iceberg.spark.TypeToSparkType} with modifications for copying field IDs to | ||
* FieldMetadata | ||
*/ | ||
class IcebergTypeToStructType extends TypeUtil.SchemaVisitor<DataType> { | ||
@Override | ||
public DataType schema(Schema schema, DataType structType) { | ||
return structType; | ||
} | ||
|
||
public DataType list(Types.ListType list, DataType elementResult) { | ||
return new ArrayType(elementResult, list.isElementOptional()); | ||
} | ||
|
||
@Override | ||
public DataType map(Types.MapType map, DataType keyResult, DataType valueResult) { | ||
return new MapType(keyResult, valueResult, map.isValueOptional()); | ||
} | ||
|
||
@Override | ||
public DataType struct(Types.StructType struct, List<DataType> fieldResults) { | ||
List<Types.NestedField> fields = struct.fields(); | ||
|
||
List<StructField> structFields = Lists.newArrayListWithExpectedSize(fieldResults.size()); | ||
for (int i = 0; i < fields.size(); i++) { | ||
Types.NestedField field = fields.get(i); | ||
DataType type = fieldResults.get(i); | ||
StructField structField = | ||
new StructField( | ||
field.name(), | ||
type, | ||
field.isOptional(), | ||
FieldMetadata.builder() | ||
.putLong(ColumnMapping.COLUMN_MAPPING_ID_KEY, field.fieldId()) | ||
.build()); | ||
// ToDo: No concept of field comments in Delta StructType? | ||
structFields.add(structField); | ||
} | ||
|
||
return new StructType(structFields); | ||
} | ||
|
||
@Override | ||
public DataType field(Types.NestedField field, DataType fieldResult) { | ||
return fieldResult; | ||
} | ||
|
||
@Override | ||
public DataType primitive(Type.PrimitiveType primitive) { | ||
switch (primitive.typeId()) { | ||
case BOOLEAN: | ||
return BooleanType.BOOLEAN; | ||
case INTEGER: | ||
return IntegerType.INTEGER; | ||
case LONG: | ||
return LongType.LONG; | ||
case FLOAT: | ||
return FloatType.FLOAT; | ||
case DOUBLE: | ||
return DoubleType.DOUBLE; | ||
case DATE: | ||
return DateType.DATE; | ||
case TIME: | ||
throw new UnsupportedOperationException("Spark does not support time fields"); | ||
case TIMESTAMP: | ||
Types.TimestampType ts = (Types.TimestampType) primitive; | ||
if (ts.shouldAdjustToUTC()) { | ||
return TimestampType.TIMESTAMP; | ||
} else { | ||
return TimestampNTZType.TIMESTAMP_NTZ; | ||
} | ||
case STRING: | ||
return StringType.STRING; | ||
case UUID: | ||
// use String | ||
return StringType.STRING; | ||
case FIXED: | ||
return BinaryType.BINARY; | ||
case BINARY: | ||
return BinaryType.BINARY; | ||
case DECIMAL: | ||
Types.DecimalType decimal = (Types.DecimalType) primitive; | ||
return new DecimalType(decimal.precision(), decimal.scale()); | ||
default: | ||
throw new UnsupportedOperationException( | ||
"Cannot convert unknown type to Spark: " + primitive); | ||
} | ||
} | ||
} |
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.
Need to make sure this attribution is done correctly