-
Notifications
You must be signed in to change notification settings - Fork 56
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
Propagate array item types from SQL to Avro schema #931
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1dc7af2
Propagate array item types from SQL to Avro schema
shnapz 439fe89
Add tests
shnapz 01d9cc5
Fix checkstyle errors
shnapz 9f3f3c4
Update e2e/e2e.sh
shnapz 55141f9
Update e2e/e2e.sh
shnapz 8ebcbd6
fix e2e
shnapz e913ef4
Update docs
shnapz 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,8 @@ public static Schema createSchemaByReadingOneRow( | |
try (Statement statement = connection.createStatement()) { | ||
final ResultSet resultSet = statement.executeQuery(queryBuilderArgs.sqlQueryWithLimitOne()); | ||
|
||
resultSet.next(); | ||
|
||
final Schema schema = | ||
createAvroSchema( | ||
resultSet, | ||
|
@@ -107,7 +109,7 @@ public static Schema createAvroSchema( | |
.prop("tableName", tableName) | ||
.prop("connectionUrl", connectionUrl) | ||
.fields(); | ||
return createAvroFields(meta, builder, useLogicalTypes).endRecord(); | ||
return createAvroFields(resultSet, builder, useLogicalTypes).endRecord(); | ||
} | ||
|
||
static String getDatabaseTableName(final ResultSetMetaData meta) throws SQLException { | ||
|
@@ -123,11 +125,13 @@ static String getDatabaseTableName(final ResultSetMetaData meta) throws SQLExcep | |
} | ||
|
||
private static SchemaBuilder.FieldAssembler<Schema> createAvroFields( | ||
final ResultSetMetaData meta, | ||
final SchemaBuilder.FieldAssembler<Schema> builder, | ||
final ResultSet resultSet, | ||
final SchemaBuilder.FieldAssembler<Schema> builder, | ||
final boolean useLogicalTypes) | ||
throws SQLException { | ||
|
||
ResultSetMetaData meta = resultSet.getMetaData(); | ||
|
||
for (int i = 1; i <= meta.getColumnCount(); i++) { | ||
|
||
final String columnName; | ||
|
@@ -140,7 +144,8 @@ private static SchemaBuilder.FieldAssembler<Schema> createAvroFields( | |
final int columnType = meta.getColumnType(i); | ||
final String typeName = JDBCType.valueOf(columnType).getName(); | ||
final String columnClassName = meta.getColumnClassName(i); | ||
final SchemaBuilder.FieldBuilder<Schema> field = | ||
final String columnTypeName = meta.getColumnTypeName(i); | ||
SchemaBuilder.FieldBuilder<Schema> field = | ||
builder | ||
.name(normalizeForAvro(columnName)) | ||
.doc(String.format("From sqlType %d %s (%s)", columnType, typeName, columnClassName)) | ||
|
@@ -149,13 +154,21 @@ private static SchemaBuilder.FieldAssembler<Schema> createAvroFields( | |
.prop("typeName", typeName) | ||
.prop("columnClassName", columnClassName); | ||
|
||
if (columnTypeName != null) { | ||
field = field.prop("columnTypeName", columnTypeName); | ||
} | ||
|
||
final SchemaBuilder.BaseTypeBuilder< | ||
SchemaBuilder.UnionAccumulator<SchemaBuilder.NullDefault<Schema>>> | ||
fieldSchemaBuilder = field.type().unionOf().nullBuilder().endNull().and(); | ||
|
||
Integer arrayItemType = resultSet.isFirst() && columnType == ARRAY | ||
? resultSet.getArray(i).getBaseType() : null; | ||
|
||
final SchemaBuilder.UnionAccumulator<SchemaBuilder.NullDefault<Schema>> schemaFieldAssembler = | ||
setAvroColumnType( | ||
columnType, | ||
arrayItemType, | ||
meta.getPrecision(i), | ||
columnClassName, | ||
useLogicalTypes, | ||
|
@@ -181,6 +194,7 @@ private static SchemaBuilder.FieldAssembler<Schema> createAvroFields( | |
private static SchemaBuilder.UnionAccumulator<SchemaBuilder.NullDefault<Schema>> | ||
setAvroColumnType( | ||
final int columnType, | ||
final Integer arrayItemType, | ||
final int precision, | ||
final String columnClassName, | ||
final boolean useLogicalTypes, | ||
|
@@ -225,10 +239,12 @@ private static SchemaBuilder.FieldAssembler<Schema> createAvroFields( | |
} else { | ||
return field.bytesType(); | ||
} | ||
case ARRAY: | ||
return setAvroColumnType(arrayItemType, null, precision, columnClassName, | ||
useLogicalTypes, field.array().items()); | ||
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. if you run e2e tests this change will result in:
|
||
case BINARY: | ||
case VARBINARY: | ||
case LONGVARBINARY: | ||
case ARRAY: | ||
case BLOB: | ||
return field.bytesType(); | ||
case DOUBLE: | ||
|
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
#!/usr/bin/env bash | ||
|
||
echo $1 | ||
|
||
shnapz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# fail on error | ||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
readonly SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)" | ||
readonly PROJECT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)" | ||
readonly MOUNT_OUTPUT=0 | ||
|
||
# This file contatins psql views with complex types to validate and troubleshoot dbeam | ||
|
||
|
@@ -65,9 +68,14 @@ pack() { | |
} | ||
|
||
run_docker_dbeam() { | ||
OUTPUT_MOUNT_EXP="" | ||
if [ "$MOUNT_OUTPUT" -eq 1 ]; then | ||
OUTPUT_MOUNT_EXP=--mount="type=bind,source=$SCRIPT_PATH,target=$SCRIPT_PATH" | ||
fi | ||
time docker run --interactive --rm \ | ||
--net="$DOCKER_NETWORK" \ | ||
--mount="type=bind,source=$PROJECT_PATH/dbeam-core/target,target=/dbeam" \ | ||
$OUTPUT_MOUNT_EXP \ | ||
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. needed to get test output from container to the local folder. This is how I verified the change
shnapz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--memory=1G \ | ||
--entrypoint=/usr/bin/java \ | ||
"$JAVA_DOCKER_IMAGE" \ | ||
|
Oops, something went wrong.
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.
it is needed to call
resultSet.getArray(i)
. Also it corresponds to the method name