You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@geaflow.apache.org by "YinZiBenRun (via GitHub)" <gi...@apache.org> on 2026/07/19 04:54:47 UTC
[PR] feat: add math functions SIGN/CBRT/TRUNC [geaflow]
YinZiBenRun opened a new pull request, #813:
URL: https://github.com/apache/geaflow/pull/813
## 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.java** — `sign(x)` returns -1 for negative, 0 for zero, 1 for positive. Supports Double/Long/Integer input.
- **Cbrt.java** — `cbrt(x)` returns the cube root of x. Supports Double input.
- **Trunc.java** — `trunc(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`:
```sql
-- 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 │
└──────────┴─────────────────────────────┴────────┴─────────┘
--
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: issues-unsubscribe@geaflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org
Re: [PR] feat: add math functions SIGN/CBRT/TRUNC [geaflow]
Posted by "Leomrlin (via GitHub)" <gi...@apache.org>.
Leomrlin commented on code in PR #813:
URL: https://github.com/apache/geaflow/pull/813#discussion_r3613355607
##########
geaflow/geaflow-dsl/geaflow-dsl-plan/src/test/java/org/apache/geaflow/dsl/udf/math/MathUdfTest.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.geaflow.dsl.udf.math;
+
+import org.apache.geaflow.dsl.udf.table.math.Cbrt;
+import org.apache.geaflow.dsl.udf.table.math.Sign;
+import org.apache.geaflow.dsl.udf.table.math.Trunc;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class MathUdfTest {
+
+ @Test
+ public void testSign() {
Review Comment:
These tests verify the Java methods directly, but they do not exercise SQL registration, overload resolution, type inference, or runtime invocation.
Could we add an end-to-end SQL test for SIGN/CBRT/TRUNC there?
--
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: issues-unsubscribe@geaflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org
Re: [PR] feat: add math functions SIGN/CBRT/TRUNC [geaflow]
Posted by "YinZiBenRun (via GitHub)" <gi...@apache.org>.
YinZiBenRun commented on PR #813:
URL: https://github.com/apache/geaflow/pull/813#issuecomment-5014438526
#790
--
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: issues-unsubscribe@geaflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org
Re: [PR] feat: add math functions SIGN/CBRT/TRUNC [geaflow]
Posted by "YinZiBenRun (via GitHub)" <gi...@apache.org>.
YinZiBenRun commented on PR #813:
URL: https://github.com/apache/geaflow/pull/813#issuecomment-5014437795
#790
--
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: issues-unsubscribe@geaflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org
Re: [PR] feat: add math functions SIGN/CBRT/TRUNC [geaflow]
Posted by "YinZiBenRun (via GitHub)" <gi...@apache.org>.
YinZiBenRun commented on code in PR #813:
URL: https://github.com/apache/geaflow/pull/813#discussion_r3631852223
##########
geaflow/geaflow-dsl/geaflow-dsl-plan/src/test/java/org/apache/geaflow/dsl/udf/math/MathUdfTest.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.geaflow.dsl.udf.math;
+
+import org.apache.geaflow.dsl.udf.table.math.Cbrt;
+import org.apache.geaflow.dsl.udf.table.math.Sign;
+import org.apache.geaflow.dsl.udf.table.math.Trunc;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class MathUdfTest {
+
+ @Test
+ public void testSign() {
Review Comment:
已添加端到端 SQL 测试,通过完整查询管线验证函数的注册、重载解析、类型推断和运行时调用。测试在 geaflow-dsl-runtime 模块:
- MathSignCbrtTruncTest.java - 通过 QueryTester 驱动测试
- query/math_sign_cbrt_trunc_001.sql - 覆盖 sign(Double/Long/Integer)、cbrt(Double)、trunc(Double 带/不带 scale、Long、Integer)
- data/ + expect/ - 输入数据与期望输出
测试揭示了 Calcite 真实的重载解析行为:sign(bigint)/sign(int) 会被提升为 double,匹配 sign(Double) 重载(返回 1.0/-1.0);而
trunc(bigint)/trunc(int) 则精确匹配 Long/Integer 重载。期望输出里记录了这个差异。
已在本地验证通过(Tests run: 1, Failures: 0, Errors: 0)。
--
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: issues-unsubscribe@geaflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org
Re: [PR] feat: add math functions SIGN/CBRT/TRUNC [geaflow]
Posted by "Leomrlin (via GitHub)" <gi...@apache.org>.
Leomrlin merged PR #813:
URL: https://github.com/apache/geaflow/pull/813
--
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: issues-unsubscribe@geaflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@geaflow.apache.org
For additional commands, e-mail: issues-help@geaflow.apache.org