-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Source support primary keys #2488
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ | |
import java.sql.SQLException; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
@@ -135,7 +136,9 @@ public AirbyteCatalog discover(JsonNode config) throws Exception { | |
Optional.ofNullable(config.get("schema")).map(JsonNode::asText)) | ||
.stream() | ||
.map(t -> CatalogHelpers.createAirbyteStream(t.getName(), t.getFields()) | ||
.withSupportedSyncModes(Lists.newArrayList(SyncMode.FULL_REFRESH, SyncMode.INCREMENTAL))) | ||
.withSupportedSyncModes(Lists.newArrayList(SyncMode.FULL_REFRESH, SyncMode.INCREMENTAL)) | ||
.withSourceDefinedPrimaryKey( | ||
t.getPrimaryKeys().stream().filter(Objects::nonNull).map(Collections::singletonList).collect(Collectors.toList()))) | ||
.collect(Collectors.toList())); | ||
} | ||
} | ||
|
@@ -290,7 +293,7 @@ private List<TableInfo> getTables(final JdbcDatabase database, | |
.distinct() | ||
.collect(Collectors.toList()); | ||
|
||
return new TableInfo(JdbcUtils.getFullyQualifiedTableName(t.getSchemaName(), t.getName()), fields); | ||
return new TableInfo(JdbcUtils.getFullyQualifiedTableName(t.getSchemaName(), t.getName()), fields, t.getPrimaryKeys()); | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
|
@@ -354,6 +357,16 @@ private List<TableInfoInternal> discoverInternal(final JdbcDatabase database, | |
return new ColumnInfo(f.get(INTERNAL_COLUMN_NAME).asText(), jdbcType); | ||
}) | ||
.collect(Collectors.toList()))) | ||
.peek(t -> { | ||
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 this would be better done in a for loop after the sequence of stream operations for readability |
||
try { | ||
final List<String> primaryKeys = database.bufferedResultSetQuery( | ||
conn -> conn.getMetaData().getPrimaryKeys(databaseOptional.orElse(null), t.getSchemaName(), t.getName()), | ||
resultSet -> resultSet.getString(JDBC_COLUMN_COLUMN_NAME)); | ||
t.addPrimaryKeys(primaryKeys); | ||
} catch (SQLException e) { | ||
LOGGER.warn(String.format("Could not find primary keys for %s.%s: %s", t.getSchemaName(), t.getName(), e)); | ||
} | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
|
@@ -440,10 +453,12 @@ protected static class TableInfo { | |
|
||
private final String name; | ||
private final List<Field> fields; | ||
private final List<String> primaryKeys; | ||
|
||
public TableInfo(String name, List<Field> fields) { | ||
public TableInfo(String name, List<Field> fields, List<String> primaryKeys) { | ||
this.name = name; | ||
this.fields = fields; | ||
this.primaryKeys = primaryKeys; | ||
} | ||
|
||
public String getName() { | ||
|
@@ -454,18 +469,24 @@ public List<Field> getFields() { | |
return fields; | ||
} | ||
|
||
public List<String> getPrimaryKeys() { | ||
return primaryKeys; | ||
} | ||
|
||
} | ||
|
||
protected static class TableInfoInternal { | ||
|
||
private final String schemaName; | ||
private final String name; | ||
private final List<ColumnInfo> fields; | ||
private final List<String> primaryKeys; | ||
|
||
public TableInfoInternal(String schemaName, String tableName, List<ColumnInfo> fields) { | ||
this.schemaName = schemaName; | ||
this.name = tableName; | ||
this.fields = fields; | ||
this.primaryKeys = new ArrayList<>(); | ||
} | ||
|
||
public String getSchemaName() { | ||
|
@@ -480,6 +501,14 @@ public List<ColumnInfo> getFields() { | |
return fields; | ||
} | ||
|
||
public void addPrimaryKeys(List<String> primaryKeys) { | ||
this.primaryKeys.addAll(primaryKeys); | ||
} | ||
|
||
public List<String> getPrimaryKeys() { | ||
return primaryKeys; | ||
} | ||
|
||
} | ||
|
||
protected static class ColumnInfo { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
styling nit.