Skip to content

feat: add math functions SIGN/CBRT/TRUNC#813

Open
YinZiBenRun wants to merge 1 commit into
apache:masterfrom
YinZiBenRun:feature/add-math-functions-sign-cbrt-trunc
Open

feat: add math functions SIGN/CBRT/TRUNC#813
YinZiBenRun wants to merge 1 commit into
apache:masterfrom
YinZiBenRun:feature/add-math-functions-sign-cbrt-trunc

Conversation

@YinZiBenRun

Copy link
Copy Markdown

What & Why

The math folder udf/table/math/ currently has only E, Log2, and Round — noticeably thin. Adding SIGN (the sign of a
number), CBRT (cube root), and TRUNC (truncate to N decimals) is a quick win that meaningfully fills out the SQL
toolbox.

Resolves #790

Changes

New UDF classes

  • Sign.javasign(x) returns -1 for negative, 0 for zero, 1 for positive. Supports Double/Long/Integer input.
  • Cbrt.javacbrt(x) returns the cube root of x. Supports Double input.
  • Trunc.javatrunc(x, d) truncates x to d decimal places (not round). Supports Double/Long/Integer input with
    optional scale parameter.

Registration

  • Added Sign, Cbrt, Trunc to BuildInSqlFunctionTable.java
  • Added cbrt(Double) and trunc(Double, Integer) static methods to GeaFlowBuiltinFunctions.java (sign already
    existed)

Tests

  • Added MathUdfTest.java — unit tests for Sign, Cbrt, Trunc UDF classes
  • Added testCbrt() and testTrunc() to InternalFunctionsTest.java
  • All tests pass: 20/20

TRUNC vs ROUND — key distinction

trunc uses RoundingMode.DOWN while round uses RoundingMode.HALF_UP:

-- trunc: truncate (no rounding)
SELECT trunc(3.1465, 2)  →  3.14
SELECT trunc(-3.1465, 2) →  -3.14

-- round: round half up
SELECT round(3.1465, 2)  →  3.15
SELECT round(-3.1465, 2) →  -3.15

Edge cases covered

┌──────────┬─────────────────────────────┬────────┬─────────┐
│ Function │          Negative           │  Zero  │  Null   │
├──────────┼─────────────────────────────┼────────┼─────────┤
│ sign     │ ✅ -1                       │ ✅ 0   │ ✅ null │
├──────────┼─────────────────────────────┼────────┼─────────┤
│ cbrt     │ ✅ -2.0 (cbrt(-8))          │ ✅ 0.0 │ ✅ null │
├──────────┼─────────────────────────────┼────────┼─────────┤
│ trunc    │ ✅ -3.14 (trunc(-3.1465,2)) │ ✅ 0.0 │ ✅ null │
└──────────┴─────────────────────────────┴────────┴─────────┘

- sign(x): returns -1 for negative, 0 for zero, 1 for positive
- cbrt(x): returns the cube root of a number
- trunc(x, d): truncates x to d decimal places (not round)

Add UDF classes (Sign.java, Cbrt.java, Trunc.java) and register
them in BuildInSqlFunctionTable. Add cbrt/trunc static methods
to GeaFlowBuiltinFunctions. Add unit tests covering negative,
zero, and null edge cases.

Resolves apache#790

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@YinZiBenRun

Copy link
Copy Markdown
Author

#790

1 similar comment
@YinZiBenRun

Copy link
Copy Markdown
Author

#790

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add the math functions SIGN / CBRT / TRUNC

1 participant