chovy-3012 opened a new pull request, #13228:
URL: https://github.com/apache/seatunnel/pull/13228
### Purpose of this pull request
Add the `IF(condition, true_value, false_value)` SQL function to the Zeta
SQL transform. Fixes #13185.
`IF` is registered in the same layer as `MULTI_IF`: the `ZetaSQLFunction.IF`
constant, dispatch in `computeForValue`, and return-type resolution in
`ZetaSQLType.getExpressionType`. A null condition is treated as false, a
non-boolean condition is an error, and only the selected branch is evaluated.
The result type is the common type of the two branches (`getIfCommonType`: a
`NULL` branch is transparent, a string branch widens the result to string,
numeric branches widen to the wider type), so `IF(age < 18, 'Minor', age)`
returns `'40'` for `age = 40`.
### Does this PR introduce _any_ user-facing change?
Yes. Previously `IF` was not a recognized function and a SQL transform using
it failed to plan; it is now supported and documented in
`docs/en/transforms/sql-functions.md` and `docs/zh/transforms/sql-functions.md`.
Usage effects in all scenarios:
| Scenario | Example | Result |
|----------|---------|--------|
| Comparison condition, true | `IF(age < 18, 'Minor', 'Adult')`, `age = 16`
| `'Minor'` |
| Comparison condition, false | `IF(age < 18, 'Minor', 'Adult')`, `age = 40`
| `'Adult'` |
| Boolean column as condition | `IF(flag, 'Yes', 'No')`, `flag = true` |
`'Yes'` |
| Null condition (treated as false) | `IF(flag, 'Yes', 'No')`, `flag = null`
| `'No'` |
| Mixed branch types (string wins) | `IF(age < 18, 'Minor', age)`, `age =
40` | `'40'` |
| Numeric branches widen | `IF(age < 18, 1, 2.5)`, `age = 40` | `2.5`
(DOUBLE) |
| Null branch value | `IF(flag, 'Yes', NULL)`, `flag = false` | `null` |
| Both branches NULL | `IF(flag, NULL, NULL)` | `null` |
| Nested IF | `IF(age < 18, IF(age < 12, 'Child', 'Teenager'), 'Adult')`,
`age = 15` | `'Teenager'` |
| Non-boolean condition | `IF(name, 'a', 'b')`, `name = 'Alice'` | Fails:
`IF function condition must be boolean, but was String` |
| Wrong argument count | `IF(age < 18, 'Minor')` | Fails: `IF function
requires exactly 3 arguments` |
| Branches with no common type | `IF(flag, metrics, 1)`, `metrics` is a MAP
column | Fails: the branches have no common type |
Purely additive new function on the unreleased dev branch; no existing
function behavior changes.
### How was this patch tested?
Unit tests added:
- `SQLSystemFunctionsTest#testIfFunction` — 10 scenarios: comparison
condition both ways, mixed string/number branch (`'40'`), numeric widening
(`2.5`), boolean column, null condition treated as false, null branch,
non-boolean condition rejected, nested `IF` ×2.
- `ZetaSQLFunctionTest#testIfFunction` — 3 scenarios with hand-built
expression trees.
- `ZetaSQLTypeTest` — 7 IF type-resolution assertions (identical types,
string widening, NULL transparency, all-NULL, 2-arg / 4-arg rejected,
incompatible types rejected).
E2E added: `func_if.conf` (3 IF output columns, incl. a mixed-type branch
and a nested IF, verified by Assert sink) executed by `TestSQLIT`.
Copy-paste-able commands and results:
```bash
./mvnw test -pl seatunnel-transforms-v2
# 1959 tests, 0 failures, 18 skipped (pre-existing skips, unrelated to this
change)
./mvnw -DskipUT -DskipIT=false verify -pl
seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2 -am
# compiles the new func_if.conf + TestSQLIT case; execution needs Docker and
runs in CI
```
In addition, the job definitions from `func_if.conf` were executed against a
running Zeta engine instance with the built transforms jar: all row assertions
passed, including negative checks that a non-boolean condition fails with the
expected error message text.
### Check list
* [x] If any new Jar binary package adding in your PR, please add License
Notice according
[New License
Guide](https://github.com/apache/seatunnel/blob/dev/docs/en/developer/new-license.md)
— n/a, no new dependency introduced.
* [x] If necessary, please update the documentation to describe the new
feature. https://github.com/apache/seatunnel/tree/dev/docs
— done, `docs/en/transforms/sql-functions.md` and
`docs/zh/transforms/sql-functions.md`.
* [x] If necessary, please update `incompatible-changes.md` to describe the
incompatibility caused by this PR.
— n/a, purely additive, no existing behavior changed.
* [x] If you are contributing the connector code, please check that the
following files are updated:
1. Update
[plugin-mapping.properties](https://github.com/apache/seatunnel/blob/dev/plugin-mapping.properties)
and add new connector information in it
2. Update the pom file of
[seatunnel-dist](https://github.com/apache/seatunnel/blob/dev/seatunnel-dist/pom.xml)
3. Add ci label in
[label-scope-conf](https://github.com/apache/seatunnel/blob/dev/.github/workflows/labeler/label-scope-conf.yml)
4. Add e2e testcase in
[seatunnel-e2e](https://github.com/apache/seatunnel/tree/dev/seatunnel-e2e/seatunnel-connector-v2-e2e/)
5. Update connector
[plugin_config](https://github.com/apache/seatunnel/blob/dev/config/plugin_config)
— n/a, transform code, not a connector; the E2E case is added in
`seatunnel-e2e/seatunnel-transforms-v2-e2e`.
--
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]