Chenrui created CALCITE-6789:
--------------------------------

             Summary: A NumberFormatException was thrown when performing an 
aggregate query on a csv file.
                 Key: CALCITE-6789
                 URL: https://issues.apache.org/jira/browse/CALCITE-6789
             Project: Calcite
          Issue Type: Bug
          Components: csv-adapter, linq4j
    Affects Versions: 1.38.0
            Reporter: Chenrui


I have a Csv file as shown below:
{code:java}
id, name, sex, age
1244275574052069377,CVS--User,,
1244451590208847873,CVS--Li Si,,12
1245192850573905921,CVS--Zhang San,2,12{code}
As you can see, both the `sex` and `age` of the first row of data are empty.

When I use an aggregate SQL statement to query
{code:java}
select name, sum(age)  from csv.test group by name{code}
A NumberFormatException is thrown.

By tracing the exception stack, it is found that the situation of empty strings 
is not judged in 
`{+}{color:#172b4d}org.apache.calcite.linq4j.tree.Primitive#charToDecimalCast{color}{+}`.

The problem is solved by modifying it using the following method:
{code:java}
public static @Nullable Object charToDecimalCast(
      @Nullable String value, int precision, int scale, RoundingMode 
roundingMode) {
    if (value == null || value.isEmpty()) {
      return null;
    }
    BigDecimal result = new BigDecimal(value.trim());
    return checkOverflow(result, precision, scale, roundingMode);
  }{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to