-
Notifications
You must be signed in to change notification settings - Fork 422
Numeric type widening converters #1877
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
Open
hemanthsavasere
wants to merge
4
commits into
apache:main
Choose a base branch
from
hemanthsavasere:numeric-type-widening-converters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b9cf3d9
Enhance numeric type widening support in PojoToRowConverter and add c…
hemanthsavasere 3e3c9ce
Enhance numeric type widening support in PojoToRowConverter and add u…
hemanthsavasere 83f9c44
Enhance documentation for POJO to Row conversion and numeric type wid…
hemanthsavasere 025f642
Deleting unecessary tests file
hemanthsavasere 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -115,15 +115,23 @@ private PojoType.Property requireProperty(String fieldName) { | |
| private static FieldToRow createFieldConverter(PojoType.Property prop, DataType fieldType) { | ||
| switch (fieldType.getTypeRoot()) { | ||
| case BOOLEAN: | ||
| case BINARY: | ||
| case BYTES: | ||
| return prop::read; // No casting needed | ||
|
|
||
| case TINYINT: | ||
| return (obj) -> convertToTinyInt(prop, prop.read(obj)); | ||
| case SMALLINT: | ||
| return (obj) -> convertToSmallInt(prop, prop.read(obj)); | ||
| case INTEGER: | ||
| return (obj) -> convertToInteger(prop, prop.read(obj)); | ||
| case BIGINT: | ||
| return (obj) -> convertToBigInt(prop, prop.read(obj)); | ||
| case FLOAT: | ||
| return (obj) -> convertToFloat(prop, prop.read(obj)); | ||
| case DOUBLE: | ||
| case BINARY: | ||
| case BYTES: | ||
| return prop::read; | ||
| return (obj) -> convertToDouble(prop, prop.read(obj)); | ||
|
|
||
| case CHAR: | ||
| case STRING: | ||
| return (obj) -> convertTextValue(fieldType, prop, prop.read(obj)); | ||
|
|
@@ -234,6 +242,135 @@ private static FieldToRow createFieldConverter(PojoType.Property prop, DataType | |
| prop.name)); | ||
| } | ||
|
|
||
| /** Converts a numeric POJO property to TINYINT (Byte). */ | ||
| private static @Nullable Byte convertToTinyInt(PojoType.Property prop, @Nullable Object v) { | ||
| if (v == null) { | ||
| return null; | ||
| } | ||
| if (v instanceof Byte) { | ||
| return (Byte) v; | ||
| } | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Field %s cannot be converted to TINYINT. Type: %s", | ||
| prop.name, v.getClass().getName())); | ||
| } | ||
|
|
||
| /** Converts a numeric POJO property to SMALLINT (Short) with widening support. */ | ||
| private static @Nullable Short convertToSmallInt(PojoType.Property prop, @Nullable Object v) { | ||
| if (v == null) { | ||
| return null; | ||
| } | ||
| if (v instanceof Short) { | ||
| return (Short) v; | ||
| } | ||
| if (v instanceof Byte) { | ||
| return ((Byte) v).shortValue(); // byte -> short widening | ||
| } | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Field %s cannot be converted to SMALLINT. Type: %s", | ||
| prop.name, v.getClass().getName())); | ||
| } | ||
|
|
||
| /** Converts a numeric POJO property to INTEGER (Integer) with widening support. */ | ||
| private static @Nullable Integer convertToInteger(PojoType.Property prop, @Nullable Object v) { | ||
| if (v == null) { | ||
| return null; | ||
| } | ||
| if (v instanceof Integer) { | ||
| return (Integer) v; | ||
| } | ||
| if (v instanceof Short) { | ||
| return ((Short) v).intValue(); // short -> int widening | ||
| } | ||
| if (v instanceof Byte) { | ||
| return ((Byte) v).intValue(); // byte -> int widening | ||
| } | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Field %s cannot be converted to INTEGER. Type: %s", | ||
| prop.name, v.getClass().getName())); | ||
| } | ||
|
|
||
| /** Converts a numeric POJO property to BIGINT (Long) with widening support. */ | ||
| private static @Nullable Long convertToBigInt(PojoType.Property prop, @Nullable Object v) { | ||
| if (v == null) { | ||
| return null; | ||
| } | ||
| if (v instanceof Long) { | ||
| return (Long) v; | ||
| } | ||
| if (v instanceof Integer) { | ||
| return ((Integer) v).longValue(); // int -> long widening | ||
| } | ||
| if (v instanceof Short) { | ||
| return ((Short) v).longValue(); // short -> long widening | ||
| } | ||
| if (v instanceof Byte) { | ||
| return ((Byte) v).longValue(); // byte -> long widening | ||
| } | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Field %s cannot be converted to BIGINT. Type: %s", | ||
| prop.name, v.getClass().getName())); | ||
| } | ||
|
|
||
| /** Converts a numeric POJO property to FLOAT (Float) with widening support. */ | ||
| private static @Nullable Float convertToFloat(PojoType.Property prop, @Nullable Object v) { | ||
| if (v == null) { | ||
| return null; | ||
| } | ||
| if (v instanceof Float) { | ||
| return (Float) v; | ||
| } | ||
| if (v instanceof Long) { | ||
| return ((Long) v).floatValue(); // long -> float widening | ||
| } | ||
| if (v instanceof Integer) { | ||
| return ((Integer) v).floatValue(); // int -> float widening | ||
| } | ||
| if (v instanceof Short) { | ||
| return ((Short) v).floatValue(); // short -> float widening | ||
| } | ||
| if (v instanceof Byte) { | ||
| return ((Byte) v).floatValue(); // byte -> float widening | ||
| } | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Field %s cannot be converted to FLOAT. Type: %s", | ||
| prop.name, v.getClass().getName())); | ||
| } | ||
|
|
||
| /** Converts a numeric POJO property to DOUBLE (Double) with widening support. */ | ||
| private static @Nullable Double convertToDouble(PojoType.Property prop, @Nullable Object v) { | ||
| if (v == null) { | ||
| return null; | ||
| } | ||
| if (v instanceof Double) { | ||
| return (Double) v; | ||
| } | ||
| if (v instanceof Float) { | ||
| return ((Float) v).doubleValue(); // float -> double widening | ||
| } | ||
| if (v instanceof Long) { | ||
| return ((Long) v).doubleValue(); // long -> double widening | ||
| } | ||
| if (v instanceof Integer) { | ||
| return ((Integer) v).doubleValue(); // int -> double widening | ||
| } | ||
| if (v instanceof Short) { | ||
| return ((Short) v).doubleValue(); // short -> double widening | ||
| } | ||
| if (v instanceof Byte) { | ||
| return ((Byte) v).doubleValue(); // byte -> double widening | ||
| } | ||
|
Comment on lines
+353
to
+367
Contributor
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 wonder this can be replaced with something like if (v instanceof Number) {
return ((Number) v).doubleValue();
}similar for other places |
||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Field %s cannot be converted to DOUBLE. Type: %s", | ||
| prop.name, v.getClass().getName())); | ||
| } | ||
|
|
||
| private interface FieldToRow { | ||
| Object readAndConvert(Object pojo) throws Exception; | ||
| } | ||
|
|
||
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.
can you elaborate why
is considered as non-numeric?