-
Notifications
You must be signed in to change notification settings - Fork 182
Delta Kernel Draft PR #729
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
base: main
Are you sure you want to change the base?
Changes from 8 commits
dfea6a4
b75bc7c
16134b3
3929e95
c6379b5
6deb5f7
05d9984
c7ba4b9
0ff36a5
18ab9d6
e906091
e00241c
3fdfd31
e0102e3
381722a
809bfe8
40172f2
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 |
---|---|---|
|
@@ -53,7 +53,7 @@ | |
<module>xtable-utilities</module> | ||
<module>xtable-aws</module> | ||
<module>xtable-hive-metastore</module> | ||
<module>xtable-service</module> | ||
<!-- <module>xtable-service</module>--> | ||
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. This should be added back, any reason why you had to comment this out? 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 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. @vaibhavk1992 I think if you rebase with latest main branch you shouldn't see those failures. |
||
</modules> | ||
|
||
<properties> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,6 +110,19 @@ | |
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.delta</groupId> | ||
<artifactId>delta-kernel-api</artifactId> | ||
<version>4.0.0</version> | ||
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 you add a properly in the root pom called <delta.kernel.version>4.0.0</delta.kernel.version>, instead of using the hardcoded value? Also curious how you ended up choosing delta kernel version, is there some specific version that needs to align with delta lake version we have in the repo? |
||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.delta</groupId> | ||
<artifactId>delta-kernel-defaults</artifactId> | ||
<version>4.0.0</version> | ||
</dependency> | ||
|
||
|
||
<!-- Hadoop dependencies --> | ||
<dependency> | ||
<groupId>org.apache.hadoop</groupId> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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.xtable.delta; | ||
|
||
import org.apache.hadoop.conf.Configuration; | ||
|
||
import io.delta.kernel.defaults.engine.DefaultEngine; | ||
import io.delta.kernel.engine.Engine; | ||
|
||
import org.apache.xtable.conversion.ConversionSourceProvider; | ||
import org.apache.xtable.conversion.SourceTable; | ||
import org.apache.xtable.kernel.DeltaKernelConversionSource; | ||
|
||
public class DeltaKernelConversionSourceProvider extends ConversionSourceProvider<Long> { | ||
@Override | ||
public DeltaKernelConversionSource getConversionSourceInstance(SourceTable sourceTable) { | ||
Configuration hadoopConf = new Configuration(); | ||
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. Is there any reason why you are creating a new hadoopConf, can you instead use the hadoopConf from the parent class similar to what |
||
Engine engine = DefaultEngine.create(hadoopConf); | ||
// DeltaTable deltaTable = DeltaT/able.forPath(sourceTable.getBasePath()); | ||
return DeltaKernelConversionSource.builder() | ||
.tableName(sourceTable.getName()) | ||
.basePath(sourceTable.getBasePath()) | ||
.engine(engine) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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.xtable.delta; | ||
|
||
import java.util.*; | ||
|
||
import io.delta.kernel.types.DataType; | ||
import io.delta.kernel.types.IntegerType; | ||
import io.delta.kernel.types.StringType; | ||
import io.delta.kernel.types.StructType; | ||
|
||
import org.apache.xtable.collectors.CustomCollectors; | ||
import org.apache.xtable.model.schema.InternalField; | ||
import org.apache.xtable.model.schema.InternalSchema; | ||
import org.apache.xtable.model.schema.InternalType; | ||
import org.apache.xtable.schema.SchemaUtils; | ||
|
||
public class DeltaKernelSchemaExtractor { | ||
|
||
private static final String DELTA_COLUMN_MAPPING_ID = "delta.columnMapping.id"; | ||
private static final DeltaKernelSchemaExtractor INSTANCE = new DeltaKernelSchemaExtractor(); | ||
private static final Map<InternalSchema.MetadataKey, Object> | ||
DEFAULT_TIMESTAMP_PRECISION_METADATA = | ||
Collections.singletonMap( | ||
InternalSchema.MetadataKey.TIMESTAMP_PRECISION, InternalSchema.MetadataValue.MICROS); | ||
|
||
public static DeltaKernelSchemaExtractor getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
public InternalSchema toInternalSchema_v2(StructType structType) { | ||
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 can just call this |
||
return toInternalSchema_v2(structType, null, false, null); | ||
} | ||
|
||
String trimmedTypeName = ""; | ||
|
||
private InternalSchema toInternalSchema_v2( | ||
DataType dataType, String parentPath, boolean nullable, String comment) { | ||
|
||
Map<InternalSchema.MetadataKey, Object> metadata = null; | ||
List<InternalField> fields = null; | ||
InternalType type = null; | ||
if (dataType instanceof IntegerType) { | ||
type = InternalType.INT; | ||
trimmedTypeName = "integer"; | ||
} | ||
if (dataType instanceof StringType) { | ||
type = InternalType.STRING; | ||
trimmedTypeName = "string"; | ||
} | ||
if (dataType instanceof StructType) { | ||
// Handle StructType | ||
StructType structType = (StructType) dataType; | ||
// your logic here | ||
|
||
fields = | ||
structType.fields().stream() | ||
.filter( | ||
field -> | ||
!field | ||
.getMetadata() | ||
.contains(DeltaPartitionExtractor.DELTA_GENERATION_EXPRESSION)) | ||
.map( | ||
field -> { | ||
Integer fieldId = | ||
field.getMetadata().contains(DELTA_COLUMN_MAPPING_ID) | ||
? Long.valueOf(field.getMetadata().getLong(DELTA_COLUMN_MAPPING_ID)) | ||
.intValue() | ||
: null; | ||
String fieldComment = | ||
field.getMetadata().contains("comment") | ||
? field.getMetadata().getString("comment") | ||
: null; | ||
InternalSchema schema = | ||
toInternalSchema_v2( | ||
field.getDataType(), | ||
SchemaUtils.getFullyQualifiedPath(parentPath, field.getName()), | ||
field.isNullable(), | ||
fieldComment); | ||
return InternalField.builder() | ||
.name(field.getName()) | ||
.fieldId(fieldId) | ||
.parentPath(parentPath) | ||
.schema(schema) | ||
.defaultValue( | ||
field.isNullable() ? InternalField.Constants.NULL_DEFAULT_VALUE : null) | ||
.build(); | ||
}) | ||
.collect(CustomCollectors.toList(structType.fields().size())); | ||
type = InternalType.RECORD; | ||
trimmedTypeName = "struct"; | ||
} | ||
|
||
return InternalSchema.builder() | ||
.name(trimmedTypeName) | ||
.dataType(type) | ||
.comment(comment) | ||
.isNullable(nullable) | ||
.metadata(metadata) | ||
.fields(fields) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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.xtable.delta; | ||
|
||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import lombok.Builder; | ||
|
||
import io.delta.kernel.*; | ||
import io.delta.kernel.engine.Engine; | ||
|
||
import org.apache.xtable.model.InternalTable; | ||
import org.apache.xtable.model.schema.InternalField; | ||
import org.apache.xtable.model.schema.InternalPartitionField; | ||
import org.apache.xtable.model.schema.InternalSchema; | ||
import org.apache.xtable.model.schema.InternalType; | ||
import org.apache.xtable.model.storage.DataLayoutStrategy; | ||
import org.apache.xtable.model.storage.TableFormat; | ||
|
||
/** | ||
* Extracts {@link InternalTable} canonical representation of a table at a point in time for Delta. | ||
*/ | ||
@Builder | ||
public class DeltaKernelTableExtractor { | ||
@Builder.Default | ||
private static final DeltaKernelSchemaExtractor schemaExtractor = | ||
DeltaKernelSchemaExtractor.getInstance(); | ||
|
||
private final String basePath; | ||
|
||
public InternalTable table( | ||
Table deltaKernelTable, Snapshot snapshot, Engine engine, String tableName, String basePath) { | ||
try { | ||
// Get schema from Delta Kernel's snapshot | ||
io.delta.kernel.types.StructType schema = snapshot.getSchema(); | ||
|
||
System.out.println("Kernelschema: " + 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. [nit] well need to remove these in final version of the pr. |
||
|
||
InternalSchema internalSchema = schemaExtractor.toInternalSchema_v2(schema); | ||
// io.delta.kernel.types.StructType schema = snapshot.getSchema(); | ||
//// InternalSchema internalSchema = schemaExtractor.toInternalSchema_v2(schema); | ||
// InternalSchema internalSchema = | ||
// schemaExtractor.toInternalSchema(snapshot.getSchema()); | ||
|
||
// Get partition columns | ||
System.out.println("Partition columns: " + internalSchema); | ||
List<String> partitionColumnNames = snapshot.getPartitionColumnNames(); | ||
List<InternalPartitionField> partitionFields = new ArrayList<>(); | ||
for (String columnName : partitionColumnNames) { | ||
InternalField sourceField = | ||
InternalField.builder() | ||
.name(columnName) | ||
.schema( | ||
InternalSchema.builder() | ||
.name(columnName) | ||
.dataType(InternalType.STRING) // Assuming string type for partition columns | ||
.build()) | ||
.build(); | ||
|
||
// Create the partition field with the source field | ||
partitionFields.add(InternalPartitionField.builder().sourceField(sourceField).build()); | ||
} | ||
|
||
DataLayoutStrategy dataLayoutStrategy = | ||
partitionFields.isEmpty() | ||
? DataLayoutStrategy.FLAT | ||
: DataLayoutStrategy.HIVE_STYLE_PARTITION; | ||
|
||
// Get the timestamp | ||
long timestamp = snapshot.getTimestamp(engine) * 1000; // Convert to milliseconds | ||
System.out.println("InternalTable basepath" + basePath); | ||
return InternalTable.builder() | ||
.tableFormat(TableFormat.DELTA) | ||
.basePath(basePath) | ||
.name(tableName) | ||
.layoutStrategy(dataLayoutStrategy) | ||
.partitioningFields(partitionFields) | ||
.readSchema(internalSchema) | ||
.latestCommitTime(Instant.ofEpochMilli(timestamp)) | ||
.latestMetadataPath(basePath + "/_delta_log") | ||
.build(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to extract table information using Delta Kernel", e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,7 @@ | |
|
||
package org.apache.xtable.delta; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.*; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
@@ -41,22 +37,10 @@ | |
import org.apache.xtable.model.schema.InternalType; | ||
import org.apache.xtable.schema.SchemaUtils; | ||
|
||
/** | ||
* Converts between Delta and InternalTable schemas. Some items to be aware of: | ||
* | ||
* <ul> | ||
* <li>Delta schemas are represented as Spark StructTypes which do not have enums so the enum | ||
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 can live this file as is right? |
||
* types are lost when converting from XTable to Delta Lake representations | ||
* <li>Delta does not have a fixed length byte array option so {@link InternalType#FIXED} is | ||
* simply translated to a {@link org.apache.spark.sql.types.BinaryType} | ||
* <li>Similarly, {@link InternalType#TIMESTAMP_NTZ} is translated to a long in Delta Lake | ||
* </ul> | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class DeltaSchemaExtractor { | ||
private static final String DELTA_COLUMN_MAPPING_ID = "delta.columnMapping.id"; | ||
private static final DeltaSchemaExtractor INSTANCE = new DeltaSchemaExtractor(); | ||
// Timestamps in Delta are microsecond precision by default | ||
private static final Map<InternalSchema.MetadataKey, Object> | ||
DEFAULT_TIMESTAMP_PRECISION_METADATA = | ||
Collections.singletonMap( | ||
|
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.
why comment this?