-
Notifications
You must be signed in to change notification settings - Fork 41
Untie decimal conversion from arrow metadata #853
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| import com.databricks.jdbc.exception.DatabricksValidationException; | ||
| import com.databricks.jdbc.log.JdbcLogger; | ||
| import com.databricks.jdbc.log.JdbcLoggerFactory; | ||
| import com.databricks.sdk.service.sql.ColumnInfo; | ||
| import com.databricks.sdk.service.sql.ColumnInfoTypeName; | ||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
|
|
@@ -61,7 +62,8 @@ public static Object convert( | |
| ValueVector columnVector, | ||
| int vectorIndex, | ||
| ColumnInfoTypeName requiredType, | ||
| String arrowMetadata) | ||
| String arrowMetadata, | ||
| ColumnInfo columnInfo) | ||
| throws DatabricksSQLException { | ||
| // check isNull before getting the object from the vector | ||
| if (columnVector.isNull(vectorIndex)) { | ||
|
|
@@ -102,7 +104,7 @@ public static Object convert( | |
| case DOUBLE: | ||
| return convertToNumber(object, Double::parseDouble, Number::doubleValue); | ||
| case DECIMAL: | ||
| return convertToDecimal(object, arrowMetadata); | ||
| return convertToDecimal(object, columnInfo); | ||
| case BINARY: | ||
| return convertToByteArray(object); | ||
| case BOOLEAN: | ||
|
|
@@ -262,30 +264,22 @@ private static byte[] convertToByteArray(Object object) { | |
| return (byte[]) object; | ||
| } | ||
|
|
||
| static BigDecimal convertToDecimal(Object object, String arrowMetadata) | ||
| static BigDecimal convertToDecimal(Object object, ColumnInfo columnInfo) | ||
| throws DatabricksValidationException { | ||
| BigDecimal bigDecimal = null; | ||
| int bigDecimalScale = columnInfo.getTypePrecision().intValue(); | ||
| if (object instanceof Text) { | ||
|
vikrantpuppala marked this conversation as resolved.
|
||
| return new BigDecimal(object.toString()); | ||
| bigDecimal = new BigDecimal(object.toString()); | ||
| } else if (object instanceof Number) { | ||
| bigDecimal = new BigDecimal(object.toString()); | ||
|
Collaborator
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. for number to bigDecimal, we need to convert through string?
Collaborator
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. We can do something like this in case of Number instance: if (number instanceof BigDecimal) {
Collaborator
Author
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. what's the reason to do this?
Collaborator
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. because we can directly use the parsed value from different Number types, instead of converting to string and then parsing |
||
| } | ||
| int scale; | ||
| try { | ||
| scale = | ||
| Integer.parseInt( | ||
| arrowMetadata | ||
| .substring(arrowMetadata.indexOf(',') + 1, arrowMetadata.indexOf(')')) | ||
| .trim()); | ||
| } catch (Exception e) { | ||
| LOGGER.error( | ||
| e, "Failed to parse scale from arrow metadata: {}. Defaulting to scale 0", arrowMetadata); | ||
| scale = 0; | ||
| if (bigDecimal == null) { | ||
| String errorMessage = | ||
| String.format("Unsupported object type for decimal conversion: %s", object.getClass()); | ||
| LOGGER.error(errorMessage); | ||
| throw new DatabricksValidationException(errorMessage); | ||
| } | ||
| if (object instanceof Number) { | ||
| return new BigDecimal(object.toString()).setScale(scale, RoundingMode.HALF_UP); | ||
| } | ||
| String errorMessage = | ||
| String.format("Unsupported object type for decimal conversion: %s", object.getClass()); | ||
| LOGGER.error(errorMessage); | ||
| throw new DatabricksValidationException(errorMessage); | ||
| return bigDecimal.setScale(bigDecimalScale, RoundingMode.HALF_UP); | ||
| } | ||
|
|
||
| private static <T extends Number> T convertToNumber( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.