the-other-tim-brown commented on code in PR #801: URL: https://github.com/apache/incubator-xtable/pull/801#discussion_r2841417340
########## xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelConversionTarget.java: ########## @@ -0,0 +1,434 @@ +/* + * 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.kernel; + +import java.util.*; Review Comment: nit: remove the `*` import ########## xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelConversionTarget.java: ########## @@ -0,0 +1,434 @@ +/* + * 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.kernel; + +import java.util.*; + +import lombok.Getter; +import lombok.Setter; + +import scala.collection.Seq; + +import com.google.common.annotations.VisibleForTesting; + +import io.delta.kernel.Table; +import io.delta.kernel.engine.Engine; +import io.delta.kernel.internal.SnapshotImpl; +import io.delta.kernel.internal.actions.Metadata; +import io.delta.kernel.internal.actions.RowBackedAction; +import io.delta.kernel.types.StructField; +import io.delta.kernel.types.StructType; + +import org.apache.xtable.conversion.TargetTable; +import org.apache.xtable.model.InternalTable; +import org.apache.xtable.model.metadata.TableSyncMetadata; +import org.apache.xtable.model.schema.InternalPartitionField; +import org.apache.xtable.model.schema.InternalSchema; +import org.apache.xtable.model.storage.InternalFilesDiff; +import org.apache.xtable.model.storage.PartitionFileGroup; +import org.apache.xtable.model.storage.TableFormat; +import org.apache.xtable.spi.sync.ConversionTarget; + +public class DeltaKernelConversionTarget implements ConversionTarget { + private DeltaKernelSchemaExtractor schemaExtractor; + private DeltaKernelPartitionExtractor partitionExtractor; + private DeltaKernelDataFileUpdatesExtractor dataKernelFileUpdatesExtractor; + + private String basePath; + private long logRetentionInHours; + private DeltaKernelConversionTarget.TransactionState transactionState; + private Engine engine; + + public DeltaKernelConversionTarget(TargetTable targetTable, Engine engine) { + this( + targetTable.getBasePath(), + targetTable.getMetadataRetention().toHours(), + engine, + DeltaKernelSchemaExtractor.getInstance(), + DeltaKernelPartitionExtractor.getInstance(), + DeltaKernelDataFileUpdatesExtractor.builder() + .engine(engine) + .basePath(targetTable.getBasePath()) + .includeColumnStats(false) + .build()); + } + + @VisibleForTesting + DeltaKernelConversionTarget( + String tableDataPath, + long logRetentionInHours, + Engine engine, + DeltaKernelSchemaExtractor schemaExtractor, + DeltaKernelPartitionExtractor partitionExtractor, + DeltaKernelDataFileUpdatesExtractor dataKernelFileUpdatesExtractor) { + this.basePath = tableDataPath; + this.schemaExtractor = schemaExtractor; + this.partitionExtractor = partitionExtractor; + this.dataKernelFileUpdatesExtractor = dataKernelFileUpdatesExtractor; + this.engine = engine; + this.logRetentionInHours = logRetentionInHours; + } + + @Override + public void init(TargetTable targetTable, org.apache.hadoop.conf.Configuration configuration) { + Engine engine = io.delta.kernel.defaults.engine.DefaultEngine.create(configuration); Review Comment: Can these classes be imported to avoid the fully qualified names here? ########## xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelConversionTarget.java: ########## @@ -0,0 +1,434 @@ +/* + * 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.kernel; + +import java.util.*; + +import lombok.Getter; +import lombok.Setter; + +import scala.collection.Seq; + +import com.google.common.annotations.VisibleForTesting; + +import io.delta.kernel.Table; +import io.delta.kernel.engine.Engine; +import io.delta.kernel.internal.SnapshotImpl; +import io.delta.kernel.internal.actions.Metadata; +import io.delta.kernel.internal.actions.RowBackedAction; +import io.delta.kernel.types.StructField; +import io.delta.kernel.types.StructType; + +import org.apache.xtable.conversion.TargetTable; +import org.apache.xtable.model.InternalTable; +import org.apache.xtable.model.metadata.TableSyncMetadata; +import org.apache.xtable.model.schema.InternalPartitionField; +import org.apache.xtable.model.schema.InternalSchema; +import org.apache.xtable.model.storage.InternalFilesDiff; +import org.apache.xtable.model.storage.PartitionFileGroup; +import org.apache.xtable.model.storage.TableFormat; +import org.apache.xtable.spi.sync.ConversionTarget; + +public class DeltaKernelConversionTarget implements ConversionTarget { + private DeltaKernelSchemaExtractor schemaExtractor; + private DeltaKernelPartitionExtractor partitionExtractor; + private DeltaKernelDataFileUpdatesExtractor dataKernelFileUpdatesExtractor; + + private String basePath; + private long logRetentionInHours; + private DeltaKernelConversionTarget.TransactionState transactionState; + private Engine engine; + + public DeltaKernelConversionTarget(TargetTable targetTable, Engine engine) { + this( + targetTable.getBasePath(), + targetTable.getMetadataRetention().toHours(), + engine, + DeltaKernelSchemaExtractor.getInstance(), + DeltaKernelPartitionExtractor.getInstance(), + DeltaKernelDataFileUpdatesExtractor.builder() + .engine(engine) + .basePath(targetTable.getBasePath()) + .includeColumnStats(false) + .build()); + } + + @VisibleForTesting + DeltaKernelConversionTarget( + String tableDataPath, + long logRetentionInHours, + Engine engine, + DeltaKernelSchemaExtractor schemaExtractor, + DeltaKernelPartitionExtractor partitionExtractor, + DeltaKernelDataFileUpdatesExtractor dataKernelFileUpdatesExtractor) { + this.basePath = tableDataPath; + this.schemaExtractor = schemaExtractor; + this.partitionExtractor = partitionExtractor; + this.dataKernelFileUpdatesExtractor = dataKernelFileUpdatesExtractor; + this.engine = engine; + this.logRetentionInHours = logRetentionInHours; + } + + @Override + public void init(TargetTable targetTable, org.apache.hadoop.conf.Configuration configuration) { + Engine engine = io.delta.kernel.defaults.engine.DefaultEngine.create(configuration); + + this.basePath = targetTable.getBasePath(); + this.logRetentionInHours = targetTable.getMetadataRetention().toHours(); + this.engine = engine; + this.schemaExtractor = DeltaKernelSchemaExtractor.getInstance(); + this.partitionExtractor = DeltaKernelPartitionExtractor.getInstance(); + this.dataKernelFileUpdatesExtractor = + DeltaKernelDataFileUpdatesExtractor.builder() + .engine(engine) + .basePath(targetTable.getBasePath()) + .includeColumnStats(false) Review Comment: Let's include an inline comment that this is set to false since the object is used to read the existing set of files in the table for snapshot syncs. It took me a while to understand why this was `false` ########## xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelConversionTarget.java: ########## @@ -0,0 +1,434 @@ +/* + * 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.kernel; + +import java.util.*; + +import lombok.Getter; +import lombok.Setter; + +import scala.collection.Seq; + +import com.google.common.annotations.VisibleForTesting; + +import io.delta.kernel.Table; +import io.delta.kernel.engine.Engine; +import io.delta.kernel.internal.SnapshotImpl; +import io.delta.kernel.internal.actions.Metadata; +import io.delta.kernel.internal.actions.RowBackedAction; +import io.delta.kernel.types.StructField; +import io.delta.kernel.types.StructType; + +import org.apache.xtable.conversion.TargetTable; +import org.apache.xtable.model.InternalTable; +import org.apache.xtable.model.metadata.TableSyncMetadata; +import org.apache.xtable.model.schema.InternalPartitionField; +import org.apache.xtable.model.schema.InternalSchema; +import org.apache.xtable.model.storage.InternalFilesDiff; +import org.apache.xtable.model.storage.PartitionFileGroup; +import org.apache.xtable.model.storage.TableFormat; +import org.apache.xtable.spi.sync.ConversionTarget; + +public class DeltaKernelConversionTarget implements ConversionTarget { + private DeltaKernelSchemaExtractor schemaExtractor; + private DeltaKernelPartitionExtractor partitionExtractor; + private DeltaKernelDataFileUpdatesExtractor dataKernelFileUpdatesExtractor; + + private String basePath; + private long logRetentionInHours; + private DeltaKernelConversionTarget.TransactionState transactionState; + private Engine engine; + + public DeltaKernelConversionTarget(TargetTable targetTable, Engine engine) { + this( + targetTable.getBasePath(), + targetTable.getMetadataRetention().toHours(), + engine, + DeltaKernelSchemaExtractor.getInstance(), + DeltaKernelPartitionExtractor.getInstance(), + DeltaKernelDataFileUpdatesExtractor.builder() + .engine(engine) + .basePath(targetTable.getBasePath()) + .includeColumnStats(false) + .build()); + } + + @VisibleForTesting + DeltaKernelConversionTarget( + String tableDataPath, + long logRetentionInHours, + Engine engine, + DeltaKernelSchemaExtractor schemaExtractor, + DeltaKernelPartitionExtractor partitionExtractor, + DeltaKernelDataFileUpdatesExtractor dataKernelFileUpdatesExtractor) { + this.basePath = tableDataPath; + this.schemaExtractor = schemaExtractor; + this.partitionExtractor = partitionExtractor; + this.dataKernelFileUpdatesExtractor = dataKernelFileUpdatesExtractor; + this.engine = engine; + this.logRetentionInHours = logRetentionInHours; + } + + @Override + public void init(TargetTable targetTable, org.apache.hadoop.conf.Configuration configuration) { + Engine engine = io.delta.kernel.defaults.engine.DefaultEngine.create(configuration); + + this.basePath = targetTable.getBasePath(); + this.logRetentionInHours = targetTable.getMetadataRetention().toHours(); + this.engine = engine; + this.schemaExtractor = DeltaKernelSchemaExtractor.getInstance(); + this.partitionExtractor = DeltaKernelPartitionExtractor.getInstance(); + this.dataKernelFileUpdatesExtractor = + DeltaKernelDataFileUpdatesExtractor.builder() + .engine(engine) + .basePath(targetTable.getBasePath()) + .includeColumnStats(false) + .build(); + } + + @Override + public void beginSync(InternalTable table) { + this.transactionState = + new DeltaKernelConversionTarget.TransactionState(engine, logRetentionInHours); + } + + @Override + public void syncSchema(InternalSchema schema) { + transactionState.setLatestSchema(schema); + } + + @Override + public void syncPartitionSpec(List<InternalPartitionField> partitionSpec) { + if (partitionSpec != null) { + Map<String, StructField> spec = + partitionExtractor.convertToDeltaPartitionFormat(partitionSpec); + for (Map.Entry<String, StructField> e : spec.entrySet()) { + transactionState.getPartitionColumns().add(e.getKey()); + if (e.getValue() != null + && transactionState.getLatestSchema().fields().stream() + .noneMatch(field -> field.getName().equals(e.getValue().getName()))) { + // add generated columns to schema. + transactionState.addColumn(e.getValue()); + } + } + } + } + + @Override + public void syncMetadata(TableSyncMetadata metadata) { + transactionState.setMetadata(metadata); + } + + @Override + public void syncFilesForSnapshot(List<PartitionFileGroup> partitionedDataFiles) { + Table table = Table.forPath(engine, basePath); + transactionState.setActions( + dataKernelFileUpdatesExtractor.applySnapshot( + table, partitionedDataFiles, transactionState.getLatestSchemaInternal())); + } + + @Override + public void syncFilesForDiff(InternalFilesDiff internalFilesDiff) { + Table table = Table.forPath(engine, basePath); + transactionState.setActions( + dataKernelFileUpdatesExtractor.applyDiff( + internalFilesDiff, + transactionState.getLatestSchemaInternal(), + table.getPath(engine), + table.getLatestSnapshot(engine).getSchema())); + } + + @Override + public void completeSync() { + transactionState.commitTransaction(); + transactionState = null; + } + + @Override + public Optional<TableSyncMetadata> getTableMetadata() { + Table table = Table.forPath(engine, basePath); + io.delta.kernel.Snapshot snapshot = table.getLatestSnapshot(engine); Review Comment: Let's import this class as well? ########## xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelConversionTarget.java: ########## @@ -0,0 +1,434 @@ +/* + * 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.kernel; + +import java.util.*; + +import lombok.Getter; +import lombok.Setter; + +import scala.collection.Seq; + +import com.google.common.annotations.VisibleForTesting; + +import io.delta.kernel.Table; +import io.delta.kernel.engine.Engine; +import io.delta.kernel.internal.SnapshotImpl; +import io.delta.kernel.internal.actions.Metadata; +import io.delta.kernel.internal.actions.RowBackedAction; +import io.delta.kernel.types.StructField; +import io.delta.kernel.types.StructType; + +import org.apache.xtable.conversion.TargetTable; +import org.apache.xtable.model.InternalTable; +import org.apache.xtable.model.metadata.TableSyncMetadata; +import org.apache.xtable.model.schema.InternalPartitionField; +import org.apache.xtable.model.schema.InternalSchema; +import org.apache.xtable.model.storage.InternalFilesDiff; +import org.apache.xtable.model.storage.PartitionFileGroup; +import org.apache.xtable.model.storage.TableFormat; +import org.apache.xtable.spi.sync.ConversionTarget; + +public class DeltaKernelConversionTarget implements ConversionTarget { + private DeltaKernelSchemaExtractor schemaExtractor; + private DeltaKernelPartitionExtractor partitionExtractor; + private DeltaKernelDataFileUpdatesExtractor dataKernelFileUpdatesExtractor; + + private String basePath; + private long logRetentionInHours; + private DeltaKernelConversionTarget.TransactionState transactionState; + private Engine engine; + + public DeltaKernelConversionTarget(TargetTable targetTable, Engine engine) { + this( + targetTable.getBasePath(), + targetTable.getMetadataRetention().toHours(), + engine, + DeltaKernelSchemaExtractor.getInstance(), + DeltaKernelPartitionExtractor.getInstance(), + DeltaKernelDataFileUpdatesExtractor.builder() + .engine(engine) + .basePath(targetTable.getBasePath()) + .includeColumnStats(false) + .build()); + } + + @VisibleForTesting + DeltaKernelConversionTarget( + String tableDataPath, + long logRetentionInHours, + Engine engine, + DeltaKernelSchemaExtractor schemaExtractor, + DeltaKernelPartitionExtractor partitionExtractor, + DeltaKernelDataFileUpdatesExtractor dataKernelFileUpdatesExtractor) { + this.basePath = tableDataPath; + this.schemaExtractor = schemaExtractor; + this.partitionExtractor = partitionExtractor; + this.dataKernelFileUpdatesExtractor = dataKernelFileUpdatesExtractor; + this.engine = engine; + this.logRetentionInHours = logRetentionInHours; + } + + @Override + public void init(TargetTable targetTable, org.apache.hadoop.conf.Configuration configuration) { + Engine engine = io.delta.kernel.defaults.engine.DefaultEngine.create(configuration); + + this.basePath = targetTable.getBasePath(); + this.logRetentionInHours = targetTable.getMetadataRetention().toHours(); + this.engine = engine; + this.schemaExtractor = DeltaKernelSchemaExtractor.getInstance(); + this.partitionExtractor = DeltaKernelPartitionExtractor.getInstance(); + this.dataKernelFileUpdatesExtractor = + DeltaKernelDataFileUpdatesExtractor.builder() + .engine(engine) + .basePath(targetTable.getBasePath()) + .includeColumnStats(false) + .build(); + } + + @Override + public void beginSync(InternalTable table) { + this.transactionState = + new DeltaKernelConversionTarget.TransactionState(engine, logRetentionInHours); + } + + @Override + public void syncSchema(InternalSchema schema) { + transactionState.setLatestSchema(schema); + } + + @Override + public void syncPartitionSpec(List<InternalPartitionField> partitionSpec) { + if (partitionSpec != null) { + Map<String, StructField> spec = + partitionExtractor.convertToDeltaPartitionFormat(partitionSpec); + for (Map.Entry<String, StructField> e : spec.entrySet()) { + transactionState.getPartitionColumns().add(e.getKey()); + if (e.getValue() != null + && transactionState.getLatestSchema().fields().stream() + .noneMatch(field -> field.getName().equals(e.getValue().getName()))) { + // add generated columns to schema. + transactionState.addColumn(e.getValue()); + } + } + } + } + + @Override + public void syncMetadata(TableSyncMetadata metadata) { + transactionState.setMetadata(metadata); + } + + @Override + public void syncFilesForSnapshot(List<PartitionFileGroup> partitionedDataFiles) { + Table table = Table.forPath(engine, basePath); + transactionState.setActions( + dataKernelFileUpdatesExtractor.applySnapshot( + table, partitionedDataFiles, transactionState.getLatestSchemaInternal())); + } + + @Override + public void syncFilesForDiff(InternalFilesDiff internalFilesDiff) { + Table table = Table.forPath(engine, basePath); + transactionState.setActions( + dataKernelFileUpdatesExtractor.applyDiff( + internalFilesDiff, + transactionState.getLatestSchemaInternal(), + table.getPath(engine), + table.getLatestSnapshot(engine).getSchema())); + } + + @Override + public void completeSync() { + transactionState.commitTransaction(); + transactionState = null; + } + + @Override + public Optional<TableSyncMetadata> getTableMetadata() { + Table table = Table.forPath(engine, basePath); + io.delta.kernel.Snapshot snapshot = table.getLatestSnapshot(engine); + + // Cast to SnapshotImpl to access internal getMetadata() method + Metadata metadata = ((SnapshotImpl) snapshot).getMetadata(); + + // Get configuration from metadata + Map<String, String> configuration = metadata.getConfiguration(); + String metadataJson = configuration.get(TableSyncMetadata.XTABLE_METADATA); + + return TableSyncMetadata.fromJson(metadataJson); + } + + @Override + public String getTableFormat() { + return TableFormat.DELTA; + } + + @Override + public Optional<String> getTargetCommitIdentifier(String sourceIdentifier) { + Table table = Table.forPath(engine, basePath); + io.delta.kernel.Snapshot currentSnapshot = table.getLatestSnapshot(engine); + + // Cast to TableImpl to access getChanges API + io.delta.kernel.internal.TableImpl tableImpl = (io.delta.kernel.internal.TableImpl) table; Review Comment: Import here as well? ########## xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelConversionTarget.java: ########## @@ -0,0 +1,434 @@ +/* + * 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.kernel; + +import java.util.*; + +import lombok.Getter; +import lombok.Setter; + +import scala.collection.Seq; + +import com.google.common.annotations.VisibleForTesting; + +import io.delta.kernel.Table; +import io.delta.kernel.engine.Engine; +import io.delta.kernel.internal.SnapshotImpl; +import io.delta.kernel.internal.actions.Metadata; +import io.delta.kernel.internal.actions.RowBackedAction; +import io.delta.kernel.types.StructField; +import io.delta.kernel.types.StructType; + +import org.apache.xtable.conversion.TargetTable; +import org.apache.xtable.model.InternalTable; +import org.apache.xtable.model.metadata.TableSyncMetadata; +import org.apache.xtable.model.schema.InternalPartitionField; +import org.apache.xtable.model.schema.InternalSchema; +import org.apache.xtable.model.storage.InternalFilesDiff; +import org.apache.xtable.model.storage.PartitionFileGroup; +import org.apache.xtable.model.storage.TableFormat; +import org.apache.xtable.spi.sync.ConversionTarget; + +public class DeltaKernelConversionTarget implements ConversionTarget { + private DeltaKernelSchemaExtractor schemaExtractor; + private DeltaKernelPartitionExtractor partitionExtractor; + private DeltaKernelDataFileUpdatesExtractor dataKernelFileUpdatesExtractor; + + private String basePath; + private long logRetentionInHours; + private DeltaKernelConversionTarget.TransactionState transactionState; + private Engine engine; + + public DeltaKernelConversionTarget(TargetTable targetTable, Engine engine) { + this( + targetTable.getBasePath(), + targetTable.getMetadataRetention().toHours(), + engine, + DeltaKernelSchemaExtractor.getInstance(), + DeltaKernelPartitionExtractor.getInstance(), + DeltaKernelDataFileUpdatesExtractor.builder() + .engine(engine) + .basePath(targetTable.getBasePath()) + .includeColumnStats(false) + .build()); + } + + @VisibleForTesting + DeltaKernelConversionTarget( + String tableDataPath, + long logRetentionInHours, + Engine engine, + DeltaKernelSchemaExtractor schemaExtractor, + DeltaKernelPartitionExtractor partitionExtractor, + DeltaKernelDataFileUpdatesExtractor dataKernelFileUpdatesExtractor) { + this.basePath = tableDataPath; + this.schemaExtractor = schemaExtractor; + this.partitionExtractor = partitionExtractor; + this.dataKernelFileUpdatesExtractor = dataKernelFileUpdatesExtractor; + this.engine = engine; + this.logRetentionInHours = logRetentionInHours; + } + + @Override + public void init(TargetTable targetTable, org.apache.hadoop.conf.Configuration configuration) { + Engine engine = io.delta.kernel.defaults.engine.DefaultEngine.create(configuration); + + this.basePath = targetTable.getBasePath(); + this.logRetentionInHours = targetTable.getMetadataRetention().toHours(); + this.engine = engine; + this.schemaExtractor = DeltaKernelSchemaExtractor.getInstance(); + this.partitionExtractor = DeltaKernelPartitionExtractor.getInstance(); + this.dataKernelFileUpdatesExtractor = + DeltaKernelDataFileUpdatesExtractor.builder() + .engine(engine) + .basePath(targetTable.getBasePath()) + .includeColumnStats(false) + .build(); + } + + @Override + public void beginSync(InternalTable table) { + this.transactionState = + new DeltaKernelConversionTarget.TransactionState(engine, logRetentionInHours); + } + + @Override + public void syncSchema(InternalSchema schema) { + transactionState.setLatestSchema(schema); + } + + @Override + public void syncPartitionSpec(List<InternalPartitionField> partitionSpec) { + if (partitionSpec != null) { + Map<String, StructField> spec = + partitionExtractor.convertToDeltaPartitionFormat(partitionSpec); + for (Map.Entry<String, StructField> e : spec.entrySet()) { Review Comment: You can also use `spec.forEach((field, fieldSchema) ->` to make the code a bit more readable. Or let's avoid using `e` and have more easily readable variable names ########## xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelConversionTarget.java: ########## @@ -0,0 +1,434 @@ +/* + * 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.kernel; + +import java.util.*; + +import lombok.Getter; +import lombok.Setter; + +import scala.collection.Seq; + +import com.google.common.annotations.VisibleForTesting; + +import io.delta.kernel.Table; +import io.delta.kernel.engine.Engine; +import io.delta.kernel.internal.SnapshotImpl; +import io.delta.kernel.internal.actions.Metadata; +import io.delta.kernel.internal.actions.RowBackedAction; +import io.delta.kernel.types.StructField; +import io.delta.kernel.types.StructType; + +import org.apache.xtable.conversion.TargetTable; +import org.apache.xtable.model.InternalTable; +import org.apache.xtable.model.metadata.TableSyncMetadata; +import org.apache.xtable.model.schema.InternalPartitionField; +import org.apache.xtable.model.schema.InternalSchema; +import org.apache.xtable.model.storage.InternalFilesDiff; +import org.apache.xtable.model.storage.PartitionFileGroup; +import org.apache.xtable.model.storage.TableFormat; +import org.apache.xtable.spi.sync.ConversionTarget; + +public class DeltaKernelConversionTarget implements ConversionTarget { + private DeltaKernelSchemaExtractor schemaExtractor; + private DeltaKernelPartitionExtractor partitionExtractor; + private DeltaKernelDataFileUpdatesExtractor dataKernelFileUpdatesExtractor; + + private String basePath; + private long logRetentionInHours; + private DeltaKernelConversionTarget.TransactionState transactionState; + private Engine engine; + + public DeltaKernelConversionTarget(TargetTable targetTable, Engine engine) { + this( + targetTable.getBasePath(), + targetTable.getMetadataRetention().toHours(), + engine, + DeltaKernelSchemaExtractor.getInstance(), + DeltaKernelPartitionExtractor.getInstance(), + DeltaKernelDataFileUpdatesExtractor.builder() + .engine(engine) + .basePath(targetTable.getBasePath()) + .includeColumnStats(false) + .build()); + } + + @VisibleForTesting + DeltaKernelConversionTarget( + String tableDataPath, + long logRetentionInHours, + Engine engine, + DeltaKernelSchemaExtractor schemaExtractor, + DeltaKernelPartitionExtractor partitionExtractor, + DeltaKernelDataFileUpdatesExtractor dataKernelFileUpdatesExtractor) { + this.basePath = tableDataPath; + this.schemaExtractor = schemaExtractor; + this.partitionExtractor = partitionExtractor; + this.dataKernelFileUpdatesExtractor = dataKernelFileUpdatesExtractor; + this.engine = engine; + this.logRetentionInHours = logRetentionInHours; + } + + @Override + public void init(TargetTable targetTable, org.apache.hadoop.conf.Configuration configuration) { + Engine engine = io.delta.kernel.defaults.engine.DefaultEngine.create(configuration); + + this.basePath = targetTable.getBasePath(); + this.logRetentionInHours = targetTable.getMetadataRetention().toHours(); + this.engine = engine; + this.schemaExtractor = DeltaKernelSchemaExtractor.getInstance(); + this.partitionExtractor = DeltaKernelPartitionExtractor.getInstance(); + this.dataKernelFileUpdatesExtractor = + DeltaKernelDataFileUpdatesExtractor.builder() + .engine(engine) + .basePath(targetTable.getBasePath()) + .includeColumnStats(false) + .build(); + } + + @Override + public void beginSync(InternalTable table) { + this.transactionState = + new DeltaKernelConversionTarget.TransactionState(engine, logRetentionInHours); + } + + @Override + public void syncSchema(InternalSchema schema) { + transactionState.setLatestSchema(schema); + } + + @Override + public void syncPartitionSpec(List<InternalPartitionField> partitionSpec) { + if (partitionSpec != null) { + Map<String, StructField> spec = + partitionExtractor.convertToDeltaPartitionFormat(partitionSpec); + for (Map.Entry<String, StructField> e : spec.entrySet()) { + transactionState.getPartitionColumns().add(e.getKey()); + if (e.getValue() != null + && transactionState.getLatestSchema().fields().stream() + .noneMatch(field -> field.getName().equals(e.getValue().getName()))) { + // add generated columns to schema. + transactionState.addColumn(e.getValue()); + } + } + } + } + + @Override + public void syncMetadata(TableSyncMetadata metadata) { + transactionState.setMetadata(metadata); + } + + @Override + public void syncFilesForSnapshot(List<PartitionFileGroup> partitionedDataFiles) { + Table table = Table.forPath(engine, basePath); + transactionState.setActions( + dataKernelFileUpdatesExtractor.applySnapshot( + table, partitionedDataFiles, transactionState.getLatestSchemaInternal())); + } + + @Override + public void syncFilesForDiff(InternalFilesDiff internalFilesDiff) { + Table table = Table.forPath(engine, basePath); + transactionState.setActions( + dataKernelFileUpdatesExtractor.applyDiff( + internalFilesDiff, + transactionState.getLatestSchemaInternal(), + table.getPath(engine), + table.getLatestSnapshot(engine).getSchema())); + } + + @Override + public void completeSync() { + transactionState.commitTransaction(); + transactionState = null; + } + + @Override + public Optional<TableSyncMetadata> getTableMetadata() { + Table table = Table.forPath(engine, basePath); + io.delta.kernel.Snapshot snapshot = table.getLatestSnapshot(engine); + + // Cast to SnapshotImpl to access internal getMetadata() method + Metadata metadata = ((SnapshotImpl) snapshot).getMetadata(); + + // Get configuration from metadata + Map<String, String> configuration = metadata.getConfiguration(); + String metadataJson = configuration.get(TableSyncMetadata.XTABLE_METADATA); + + return TableSyncMetadata.fromJson(metadataJson); + } + + @Override + public String getTableFormat() { + return TableFormat.DELTA; + } + + @Override + public Optional<String> getTargetCommitIdentifier(String sourceIdentifier) { + Table table = Table.forPath(engine, basePath); + io.delta.kernel.Snapshot currentSnapshot = table.getLatestSnapshot(engine); + + // Cast to TableImpl to access getChanges API + io.delta.kernel.internal.TableImpl tableImpl = (io.delta.kernel.internal.TableImpl) table; + + // Request COMMITINFO actions to read commit metadata + java.util.Set<io.delta.kernel.internal.DeltaLogActionUtils.DeltaAction> actionSet = Review Comment: Import both set and hashset -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
