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

[HUDI-5829] Optimize conversion from json to row format when sanitizing field names #11941

Merged
merged 9 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,25 +304,12 @@ public Pair<Boolean, Object> convert(Object value, String name, Schema schema) {

// Case 2: Input is a number or String number.
LogicalTypes.Decimal decimalType = (LogicalTypes.Decimal) schema.getLogicalType();
Pair<Boolean, BigDecimal> parseResult = parseObjectToBigDecimal(value);
Pair<Boolean, BigDecimal> parseResult = parseObjectToBigDecimal(value, schema);
if (Boolean.FALSE.equals(parseResult.getLeft())) {
return Pair.of(false, null);
}
BigDecimal bigDecimal = parseResult.getRight();

// As we don't do rounding, the validation will enforce the scale part and the integer part are all within the
// limit. As a result, if scale is 2 precision is 5, we only allow 3 digits for the integer.
// Allowed: 123.45, 123, 0.12
// Disallowed: 1234 (4 digit integer while the scale has already reserved 2 digit out of the 5 digit precision)
// 123456, 0.12345
if (bigDecimal.scale() > decimalType.getScale()
|| (bigDecimal.precision() - bigDecimal.scale()) > (decimalType.getPrecision() - decimalType.getScale())) {
// Correspond to case
// org.apache.avro.AvroTypeException: Cannot encode decimal with scale 5 as scale 2 without rounding.
// org.apache.avro.AvroTypeException: Cannot encode decimal with scale 3 as scale 2 without rounding
return Pair.of(false, null);
}

Comment on lines -313 to -325
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have moved this code to DecimalFieldProcessor class, so this can be reused by both JsonToAvro and JsonToRow processors.

switch (schema.getType()) {
case BYTES:
// Convert to primitive Arvo type that logical type Decimal uses.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,37 @@ protected static boolean isValidDecimalTypeConfig(Schema schema) {
* @return Pair object, with left as boolean indicating if the parsing was successful and right as the
* BigDecimal value.
*/
protected static Pair<Boolean, BigDecimal> parseObjectToBigDecimal(Object obj) {
protected static Pair<Boolean, BigDecimal> parseObjectToBigDecimal(Object obj, Schema schema) {
BigDecimal bigDecimal = null;
if (obj instanceof Number) {
return Pair.of(true, BigDecimal.valueOf(((Number) obj).doubleValue()));
bigDecimal = BigDecimal.valueOf(((Number) obj).doubleValue());
}

// Case 2: Object is a number in String format.
if (obj instanceof String) {
BigDecimal bigDecimal = null;
try {
bigDecimal = new BigDecimal(((String) obj));
} catch (java.lang.NumberFormatException ignored) {
/* ignore */
}
return Pair.of(bigDecimal != null, bigDecimal);
}
return Pair.of(false, null);

if (bigDecimal == null) {
return Pair.of(false, null);
}
// As we don't do rounding, the validation will enforce the scale part and the integer part are all within the
// limit. As a result, if scale is 2 precision is 5, we only allow 3 digits for the integer.
// Allowed: 123.45, 123, 0.12
// Disallowed: 1234 (4 digit integer while the scale has already reserved 2 digit out of the 5 digit precision)
// 123456, 0.12345
LogicalTypes.Decimal decimalType = (LogicalTypes.Decimal) schema.getLogicalType();
if (bigDecimal.scale() > decimalType.getScale()
|| (bigDecimal.precision() - bigDecimal.scale()) > (decimalType.getPrecision() - decimalType.getScale())) {
// Correspond to case
// org.apache.avro.AvroTypeException: Cannot encode decimal with scale 5 as scale 2 without rounding.
// org.apache.avro.AvroTypeException: Cannot encode decimal with scale 3 as scale 2 without rounding
return Pair.of(false, null);
}
return Pair.of(true, bigDecimal);
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hudi.utilities.exception;

import org.apache.hudi.exception.HoodieJsonConversionException;

public class HoodieJsonToRowConversionException extends HoodieJsonConversionException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


public HoodieJsonToRowConversionException(String msg) {
super(msg);
}

public HoodieJsonToRowConversionException(String msg, Throwable t) {
super(msg, t);
}
}
Loading
Loading