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

Incorrect result from cast(interval day to second as varchar) #9384

Closed
mbasmanova opened this issue Apr 5, 2024 · 2 comments
Closed

Incorrect result from cast(interval day to second as varchar) #9384

mbasmanova opened this issue Apr 5, 2024 · 2 comments
Labels
bug Something isn't working correctness triage Newly created issue that needs attention.

Comments

@mbasmanova
Copy link
Contributor

Bug description

Velox seems to handle 'interval day to second' as an integer when casting to 'varchar'. That produces incorrect results.

For example, casting 1 day interval to varchar should return '1 00:00:00.000' but Velox returns '1'.

presto> select typeof(date('2024-01-10') - date('2024-01-9'));
         _col0
------------------------
 interval day to second

presto> select cast((date('2024-01-10') - date('2024-01-9')) as varchar);
     _col0
----------------
 1 00:00:00.000

presto> select cast((now() - date('2024-01-10')) as varchar);
      _col0
-----------------
 86 03:35:33.990

Velox repro:

TEST_F(CastExprTest, xxx) {
  // cast(interval day to second) as varchar.

  auto data = makeRowVector({
      makeFlatVector<int64_t>({1, 2, 3}, INTERVAL_DAY_TIME()),
  });

  auto result = evaluate("cast(c0 as VARCHAR)", data);
  LOG(ERROR) << result->toString();
  LOG(ERROR) << result->toString(0, 10);
}

[FLAT VARCHAR: 3 elements, no nulls]
0: 1
1: 2
2: 3

CC: @amitkdutta @kagamiori @kgpai @svm1 @aditi-pandit

System information

n/a

Relevant logs

No response

@mbasmanova mbasmanova added bug Something isn't working triage Newly created issue that needs attention. correctness labels Apr 5, 2024
@mbasmanova
Copy link
Contributor Author

Presto's implementation:

presto-main/src/main/java/com/facebook/presto/type/IntervalDayTimeOperators.java

    @ScalarOperator(CAST)
    @LiteralParameters("x")
    @SqlType("varchar(x)")
    public static Slice castToSlice(@SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long value)
    {
        return utf8Slice(formatMillis(value));
    }

presto-client/src/main/java/com/facebook/presto/client/IntervalDayTime.java

    public static String formatMillis(long millis)
    {
        if (millis == Long.MIN_VALUE) {
            return LONG_MIN_VALUE;
        }
        String sign = "";
        if (millis < 0) {
            sign = "-";
            millis = -millis;
        }

        long day = millis / MILLIS_IN_DAY;
        millis %= MILLIS_IN_DAY;
        long hour = millis / MILLIS_IN_HOUR;
        millis %= MILLIS_IN_HOUR;
        long minute = millis / MILLIS_IN_MINUTE;
        millis %= MILLIS_IN_MINUTE;
        long second = millis / MILLIS_IN_SECOND;
        millis %= MILLIS_IN_SECOND;

        return format("%s%d %02d:%02d:%02d.%03d", sign, day, hour, minute, second, millis);
    }

@mbasmanova
Copy link
Contributor Author

Velox has IntervalDayTimeType::valueToString that can be used to implement cast to varchar.

mbasmanova added a commit to mbasmanova/velox-1 that referenced this issue Apr 5, 2024
Summary:
CAST expression used to handle INTERVAL DAY TO SECOND values as BIGINT. This allowed invalid casts (e.g. from varchar, to double, etc.) and produced incorrect results when casting to varchar.

Casting 1 second interval used to return '1000' instead of '0 00:00:01.000'.

Fixes facebookincubator#9384

Differential Revision: D55796600
mbasmanova added a commit to mbasmanova/velox-1 that referenced this issue Apr 5, 2024
Summary:

CAST expression used to handle INTERVAL DAY TO SECOND values as BIGINT. This allowed invalid casts (e.g. from varchar, to double, etc.) and produced incorrect results when casting to varchar.

Casting 1 second interval used to return '1000' instead of '0 00:00:01.000'.

Fixes facebookincubator#9384

Differential Revision: D55796600
mbasmanova added a commit to mbasmanova/velox-1 that referenced this issue Apr 5, 2024
Summary:

CAST expression used to handle INTERVAL DAY TO SECOND values as BIGINT. This allowed invalid casts (e.g. from varchar, to double, etc.) and produced incorrect results when casting to varchar.

Casting 1 second interval used to return '1000' instead of '0 00:00:01.000'.

Fixes facebookincubator#9384

Differential Revision: D55796600
mbasmanova added a commit to mbasmanova/velox-1 that referenced this issue Apr 5, 2024
Summary:

CAST expression used to handle INTERVAL DAY TO SECOND values as BIGINT. This allowed invalid casts (e.g. from varchar, to double, etc.) and produced incorrect results when casting to varchar.

Casting 1 second interval used to return '1000' instead of '0 00:00:01.000'.

Fixes facebookincubator#9384

Differential Revision: D55796600
mbasmanova added a commit to mbasmanova/velox-1 that referenced this issue Apr 5, 2024
Summary:

CAST expression used to handle INTERVAL DAY TO SECOND values as BIGINT. This allowed invalid casts (e.g. from varchar, to double, etc.) and produced incorrect results when casting to varchar.

Casting 1 second interval used to return '1000' instead of '0 00:00:01.000'.

Fixes facebookincubator#9384

Reviewed By: xiaoxmeng

Differential Revision: D55796600
mbasmanova added a commit to mbasmanova/velox-1 that referenced this issue Apr 5, 2024
Summary:

CAST expression used to handle INTERVAL DAY TO SECOND values as BIGINT. This allowed invalid casts (e.g. from varchar, to double, etc.) and produced incorrect results when casting to varchar.

Casting 1 second interval used to return '1000' instead of '0 00:00:01.000'.

Fixes facebookincubator#9384

Reviewed By: xiaoxmeng

Differential Revision: D55796600
Joe-Abraham pushed a commit to Joe-Abraham/velox that referenced this issue Jun 7, 2024
Summary:
Pull Request resolved: facebookincubator#9386

CAST expression used to handle INTERVAL DAY TO SECOND values as BIGINT. This allowed invalid casts (e.g. from varchar, to double, etc.) and produced incorrect results when casting to varchar.

Casting 1 second interval used to return '1000' instead of '0 00:00:01.000'.

Fixes facebookincubator#9384

Reviewed By: xiaoxmeng

Differential Revision: D55796600

fbshipit-source-id: 3d51a8eb18b434315bf9285f58b8f2cdbedca63d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctness triage Newly created issue that needs attention.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant