eveniota opened a new pull request, #68524:
URL: https://github.com/apache/doris/pull/68524
### What problem does this PR solve?
Issue Number: #48203
Problem Summary: Adds the Trino-compatible scalar function
`human_readable_seconds(int/bigint)` to Apache Doris.
#### Function Specification & Behavior:
Converts a duration in seconds into a human-readable duration format using
units in order:
days (`d`), hours (`h`), minutes (`m`), and seconds (`s`). Intermediate
zero-valued units are
omitted.
- Special zero case: `human_readable_seconds(0)` -> `"0s"`
- Single/multi-unit cases:
- `human_readable_seconds(60)` -> `"1m"`
- `human_readable_seconds(3661)` -> `"1h 1m 1s"`
- `human_readable_seconds(86401)` -> `"1d 1s"`
- Maximum boundary (`Long.MAX_VALUE = 9223372036854775807`):
`9223372036854775807 % 86400 = 55807` -> `"106751991167300d 15h 30m 7s"`
- Nullability & Negative values:
- `NULL` input returns `NULL`.
- Negative values (including `INT_MIN` / `BIGINT_MIN`) return `NULL`.
#### Technical Details:
1. **BE Vectorized Kernel (`FunctionHumanReadableSeconds`)**:
- Implements high-performance batch evaluation for `ColumnInt64` and
`ColumnInt32`.
- Uses local stack-buffer formatting (`char buf[48]`) with
`std::to_chars` and pre-reserved column memory to eliminate dynamic heap
allocations per row.
- Division and modulo operations by compile-time constants (`86400`,
`3600`, `60`) leverage compiler reciprocal multiplication.
- Explicitly overrides `use_default_implementation_for_nulls() =
false` to safely handle computed nulls (negative inputs) and unnested
`ColumnNullable` input null maps simultaneously.
2. **FE Nereids Planner & Optimizer**:
- Added `HumanReadableSeconds` scalar function with `AlwaysNullable`
(to account for computed nulls on non-null columns) and
`ExplicitlyCastableSignature` for `BigIntType` and `IntegerType`.
- Registered in `BuiltinScalarFunctions.java` and
`ScalarFunctionVisitor.java`.
3. **FE Constant Folding**:
- Added compile-time constant folding in
`DateTimeExtractAndTransform.java` via `@ExecFunction` for integer literal
types (`BigIntLiteral`, `IntegerLiteral`, `SmallIntLiteral`,
`TinyIntLiteral`).
4. **Testing**:
- **BE-UT**: Verified all combinations (vector, const, nullable,
nullable-const) via
`check_function_all_arg_comb<DataTypeString, true>` in
`function_time_test.cpp`.
- **FE-UT**: Verified compile-time folding rules in
`FoldConstantTest.java`.
- **Regression Tests**: Added `test_human_readable_seconds.groovy`
covering constant folding and batch table scans.
### Release note
Support `human_readable_seconds` SQL function to convert seconds into
human-readable duration strings (e.g., '1d 1h 1m 1s').
### Check List (For Author)
- Test <!-- At least one of them must be included. -->
- [x] Regression test
- [x] Unit Test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
- [ ] Previous test can cover this change.
- [ ] No code files have been changed.
- [ ] Other reason <!-- Add your reason? -->
- Behavior changed:
- [x] No.
- [ ] Yes. <!-- Explain the behavior change -->
- Does this need documentation?
- [ ] No.
- [x] Yes. https://github.com/apache/doris-website/pull/4174
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label <!-- Add branch pick label that this PR should
merge into -->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]