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

Fix to Handle Big Decimal Data Type #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

priyankaku
Copy link

No description provided.

Copy link
Collaborator

@dalelane dalelane left a 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

@priyankaku
Copy link
Author

Please restrict pull requests to addressing a single issue

Hi @dalelane , sorry it seems it got auto corrected by the IDE while formatting.
I will revert it back , PR was intended only to fix BigDecimal issue.

@priyankaku
Copy link
Author

@dalelane , i have revert back the changes. Pushed only the required changes.

Copy link
Collaborator

@dalelane dalelane left a 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) {
Copy link
Collaborator

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?

Copy link
Author

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.

@priyankaku
Copy link
Author

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?

Yes , I will be creating a new PR for addressing each logical types.

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())) {
Copy link
Collaborator

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

Copy link
Author

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()))));
Copy link
Collaborator

@dalelane dalelane Jun 19, 2024

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();

Copy link
Author

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

<xs:sequence>
<xs:element type="xs:integer" name="ProductID"/>
<xs:element type="xs:string" name="ProductName"/>
<xs:element type="xs:decimal" name="Price"/>
Copy link
Collaborator

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.

Suggested change
<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

Copy link
Author

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]>
@Joel-hanson
Copy link
Contributor

@dalelane could you please do a quick review for the latest changes.

@Joel-hanson
Copy link
Contributor

This PR needs improvement by addressing all the combinations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants