Skip to content
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

Add getSchema() to SourceClient interface #294

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions api/src/main/java/io/onetable/spi/extractor/SourceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import io.onetable.model.*;
import io.onetable.model.CommitsBacklog;
import io.onetable.model.schema.OneSchema;
import io.onetable.model.schema.SchemaCatalog;

/**
Expand All @@ -48,6 +49,14 @@ public interface SourceClient<COMMIT> extends Closeable {
*/
SchemaCatalog getSchemaCatalog(OneTable table, COMMIT commit);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we can eliminate the SchemaCatalog class with this change?
It was one of the objectives of #77


/**
* Extracts the {@link OneSchema} as of the latest state.
Copy link
Contributor

Choose a reason for hiding this comment

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

This method requires a OneTable object as an argument, which corresponds to the table format at a certain point in time. The method should return the schema that matches the OneTable object, not the most recent one.

Copy link
Contributor

@ashvina ashvina Dec 17, 2023

Choose a reason for hiding this comment

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

@the-other-tim-brown, could you please confirm this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's correct

Copy link
Author

Choose a reason for hiding this comment

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

Okay, for the point-in-time schema, we would need the commit/snapshot param. I will make the changes

Copy link
Contributor

Choose a reason for hiding this comment

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

@coded9 the changes required for this are quite straightforward. You can copy the implementation for the getSchemaCatalog that already handles this but simply return the schema and not the schema catalog object.

Copy link
Contributor

Choose a reason for hiding this comment

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

I actually wonder if this getSchema method is even required when the schema itself is already present in the OneTable object.

*
* @param table the current state of the table
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment might require an update, depending on Tim's response.

* @return
*/
OneSchema getSchema(OneTable table);

/**
* Extracts the {@link OneSnapshot} as of latest state.
*
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/io/onetable/delta/DeltaSourceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public SchemaCatalog getSchemaCatalog(OneTable table, Long version) {
return SchemaCatalog.builder().schemas(schemas).build();
}

@Override
public OneSchema getSchema(OneTable table) {
return table.getReadSchema();
}

@Override
public OneSnapshot getCurrentSnapshot() {
DeltaLog deltaLog = DeltaLog.forTable(sparkSession, basePath);
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/io/onetable/hudi/HudiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import io.onetable.exception.OneIOException;
import io.onetable.model.*;
import io.onetable.model.CommitsBacklog;
import io.onetable.model.schema.OneSchema;
import io.onetable.model.schema.SchemaCatalog;
import io.onetable.spi.extractor.SourceClient;

Expand Down Expand Up @@ -82,6 +83,11 @@ public SchemaCatalog getSchemaCatalog(OneTable table, HoodieInstant commit) {
return HudiSchemaCatalogExtractor.catalogWithTableSchema(table);
}

@Override
public OneSchema getSchema(OneTable table) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add unit tests for Hudi and Delta sources also.

return table.getReadSchema();
}

@Override
public OneSnapshot getCurrentSnapshot() {
HoodieActiveTimeline activeTimeline = metaClient.getActiveTimeline();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ public SchemaCatalog getSchemaCatalog(OneTable table, Snapshot snapshot) {
return SchemaCatalog.builder().schemas(catalog).build();
}

@Override
public OneSchema getSchema(OneTable table) {
Table iceTable = getSourceTable();
IcebergSchemaExtractor schemaExtractor = IcebergSchemaExtractor.getInstance();
return schemaExtractor.fromIceberg(iceTable.schema());
}

@Override
public OneSnapshot getCurrentSnapshot() {
Table iceTable = getSourceTable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,17 @@ public void testGetCurrentCommitState(@TempDir Path workingDir) throws IOExcepti
// validatePendingCommits(catalogSales, snapshot1, snapshot2, snapshot3b, snapshot4);
}

@Test
public void getSchema(@TempDir Path workingDir) throws IOException {
Table catalogSales = createTestTableWithData(workingDir.toString());
PerTableConfig sourceTableConfig = getPerTableConfig(catalogSales);
IcebergSourceClient client = clientProvider.getSourceClientInstance(sourceTableConfig);
IcebergSourceClient spyClient = spy(client);
OneSchema schema = spyClient.getSchema(null);
Assertions.assertNotNull(schema);
validateSchema(schema, catalogSales.schema());
}

private void validatePendingCommits(Table table, Snapshot lastSync, Snapshot... snapshots) {
InstantsForIncrementalSync instant =
InstantsForIncrementalSync.builder()
Expand Down