-
Notifications
You must be signed in to change notification settings - Fork 4
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
Fix to Handle Big Decimal Data Type #1
base: main
Are you sure you want to change the base?
Conversation
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.
Please restrict pull requests to addressing a single issue
src/main/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/engines/StructToXmlBytes.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/engines/StructToXmlBytes.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/engines/StructToXmlBytes.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/engines/StructToXmlBytes.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/engines/StructToXmlBytes.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/engines/StructToXmlBytes.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/engines/StructToXmlBytes.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/engines/StructToXmlBytes.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/engines/StructToXmlBytes.java
Outdated
Show resolved
Hide resolved
src/main/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/engines/StructToXmlBytes.java
Outdated
Show resolved
Hide resolved
Hi @dalelane , sorry it seems it got auto corrected by the IDE while formatting. |
@dalelane , i have revert back the changes. Pushed only the required changes. |
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.
We talked last week about how the converter currently doesn't support logical types - and how your work with the database connector has raised this as a new requirement.
A quick look at the Javadoc suggests that the four logical types in org.apache.kafka.connect.data are Date
, Decimal
, Time
, Timestamp
Are you planning to add other logical types in follow-on pull requests? Or do you think there is a reason we should add support for only one logical type, but not any others?
Decimal.LOGICAL_NAME.equals(fieldSchema.name()) ? | ||
Decimal.fromLogical(fieldSchema, new BigDecimal(String.valueOf(source.get(field.name())))) : | ||
source.getBytes(field.name()); | ||
if (bytes != null) { |
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.
Under what situations could bytes
be null?
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.
Since that condition was already existing just kept as it is. I have not tested out the scenarios where it could be null.
src/main/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/engines/StructToXmlBytes.java
Outdated
Show resolved
Hide resolved
Yes , I will be creating a new PR for addressing each logical types. |
src/test/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/testutils/StructGenerators.java
Outdated
Show resolved
Hide resolved
src/test/java/com/ibm/eventstreams/kafkaconnect/plugins/xml/testutils/StructGenerators.java
Outdated
Show resolved
Hide resolved
bytes = source.getBytes(field.name()); | ||
if (bytes.length == 1 && "xs:byte".equals(fieldSchema.doc())) { | ||
strRepresentation = Byte.toString(bytes[0]); | ||
} else if (Decimal.LOGICAL_NAME.equals(fieldSchema.name())) { |
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.
Unless I'm reading this incorrectly, I don't think this if
check could ever pass
Reading through this code, I can see we're inside the else
branch of the if (Decimal.LOGICAL_NAME.equals(fieldSchema.name()))
check from line 241
That means this if check could never be satisfied, and that the line below is dead and unreachable code
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.
Sorry , missed to remove this block
String strRepresentation; | ||
|
||
if (Decimal.LOGICAL_NAME.equals(fieldSchema.name())){ | ||
bytes = Decimal.fromLogical(fieldSchema, new BigDecimal(String.valueOf(source.get(field.name())))); |
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.
These two lines are overly complex and I think a little bit circular.
The aim of this code is to start with a BigDecimal
object (source.get(field.name())
) and turn it into a String to add to the XML element.
The code here is turning the BigDecimal into a String in 5 steps:
Step 1 - wrap the BigDecimal in String.valueOf
String.valueOf(theBigDecimal)
Step 2 - put the result in a constructor to turn it back into a BigDecimal
new BigDecimal(String.valueOf(theBigDecimal))
At this point, I think the code has done a round-trip and ended up with the value it started with. And it continues.
Step 3 - use a Connect helper method to turn a BigDecimal into a byte array
byte[] bytes = Decimal.fromLogical(schema,
new BigDecimal(String.valueOf(theBigDecimal)));
Step 4 - use the inverse Connect helper method to turn a byte array back into a BigDecimal
Decimal.toLogical(schema, bytes)
At this point, I think the code has now done another round-trip and ended up with the BigDecimal value it started with again.
Step 5 - use toString() to get a string version
Decimal.toLogical(schema, bytes).toString()
In summary... I think this code would produce an identical result if it was simplified to skip steps 1-4 and just do step 5:
strRepresentation = source.get(field.name()).toString();
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.
Thank you for the suggestion , updated accordingly
src/test/resources/052.xsd
Outdated
<xs:sequence> | ||
<xs:element type="xs:integer" name="ProductID"/> | ||
<xs:element type="xs:string" name="ProductName"/> | ||
<xs:element type="xs:decimal" name="Price"/> |
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.
I think this is losing information.
We started with a Connect schema that specified a scale of 3 (three fraction digits). ref
But the schema here doesn't mention that.
We should try to ensure that the Connect schema is fully reflected in the XML schema.
<xs:element type="xs:decimal" name="Price"/> | |
<xs:element name="Price"> | |
<xs:simpleType> | |
<xs:restriction base="xs:decimal"> | |
<xs:fractionDigits value="3"/> | |
</xs:restriction> | |
</xs:simpleType> | |
</xs:element> |
ref: https://learning.oreilly.com/library/view/xml-schema/0596002521/re24.html
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.
updated
Contributes to: event-integration/eventstreams-planning#12841 Signed-off-by: Priyanka.K.U [email protected] Signed-off-by: Joel Hanson <[email protected]>
b7f7273
to
c102f5a
Compare
@dalelane could you please do a quick review for the latest changes. |
This PR needs improvement by addressing all the combinations. |
No description provided.