-
Notifications
You must be signed in to change notification settings - Fork 174
feat: add math functions SIGN/CBRT/TRUNC #813
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
Open
YinZiBenRun
wants to merge
1
commit into
apache:master
Choose a base branch
from
YinZiBenRun:feature/add-math-functions-sign-cbrt-trunc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...eaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/udf/table/math/Cbrt.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * 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.table.math; | ||
|
|
||
| import org.apache.geaflow.dsl.common.function.Description; | ||
| import org.apache.geaflow.dsl.common.function.UDF; | ||
|
|
||
| @Description(name = "cbrt", description = "Returns the cube root of the given value.") | ||
| public class Cbrt extends UDF { | ||
|
|
||
| public Double eval(Double a) { | ||
| if (a == null) { | ||
| return null; | ||
| } | ||
| return Math.cbrt(a); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
...eaflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/udf/table/math/Sign.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * 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.table.math; | ||
|
|
||
| import org.apache.geaflow.dsl.common.function.Description; | ||
| import org.apache.geaflow.dsl.common.function.UDF; | ||
|
|
||
| @Description(name = "sign", description = "Returns the sign of the given value.") | ||
| public class Sign extends UDF { | ||
|
|
||
| public Double eval(Double a) { | ||
| if (a == null) { | ||
| return null; | ||
| } | ||
| return Math.signum(a); | ||
| } | ||
|
|
||
| public Long eval(Long n) { | ||
| if (n == null) { | ||
| return null; | ||
| } | ||
| if (n > 0) { | ||
| return 1L; | ||
| } else if (n < 0) { | ||
| return -1L; | ||
| } else { | ||
| return 0L; | ||
| } | ||
| } | ||
|
|
||
| public Integer eval(Integer n) { | ||
| if (n == null) { | ||
| return null; | ||
| } | ||
| if (n > 0) { | ||
| return 1; | ||
| } else if (n < 0) { | ||
| return -1; | ||
| } else { | ||
| return 0; | ||
| } | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
...aflow-dsl/geaflow-dsl-plan/src/main/java/org/apache/geaflow/dsl/udf/table/math/Trunc.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * 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.table.math; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
| import org.apache.geaflow.dsl.common.function.Description; | ||
| import org.apache.geaflow.dsl.common.function.UDF; | ||
|
|
||
| @Description(name = "trunc", description = "Truncates x to d decimal places") | ||
| public class Trunc extends UDF { | ||
|
|
||
| private Double eval(Double n, int i) { | ||
| double d = n; | ||
| if (Double.isNaN(d) || Double.isInfinite(d)) { | ||
| return d; | ||
| } else { | ||
| return BigDecimal.valueOf(d).setScale(i, RoundingMode.DOWN).doubleValue(); | ||
| } | ||
| } | ||
|
|
||
| public Double eval(Double n) { | ||
| if (n == null) { | ||
| return null; | ||
| } | ||
| return eval(n, 0); | ||
| } | ||
|
|
||
| public Long eval(Long n) { | ||
| return n; | ||
| } | ||
|
|
||
| public Integer eval(Integer n) { | ||
| return n; | ||
| } | ||
|
|
||
| public Double eval(Double n, Long i) { | ||
| if ((n == null) || (i == null)) { | ||
| return null; | ||
| } | ||
| return eval(n, i.intValue()); | ||
| } | ||
|
|
||
| public Double eval(Double n, Integer i) { | ||
| if ((n == null) || (i == null)) { | ||
| return null; | ||
| } | ||
| return eval(n, i.intValue()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...aflow-dsl/geaflow-dsl-plan/src/test/java/org/apache/geaflow/dsl/udf/math/MathUdfTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() { | ||
| Sign sign = new Sign(); | ||
| // Double | ||
| Assert.assertEquals(sign.eval(3.14), 1.0); | ||
| Assert.assertEquals(sign.eval(-3.14), -1.0); | ||
| Assert.assertEquals(sign.eval(0.0), 0.0); | ||
| Assert.assertNull(sign.eval((Double) null)); | ||
| // Long | ||
| Assert.assertEquals(sign.eval(5L), Long.valueOf(1L)); | ||
| Assert.assertEquals(sign.eval(-5L), Long.valueOf(-1L)); | ||
| Assert.assertEquals(sign.eval(0L), Long.valueOf(0L)); | ||
| Assert.assertNull(sign.eval((Long) null)); | ||
| // Integer | ||
| Assert.assertEquals(sign.eval(5), Integer.valueOf(1)); | ||
| Assert.assertEquals(sign.eval(-5), Integer.valueOf(-1)); | ||
| Assert.assertEquals(sign.eval(0), Integer.valueOf(0)); | ||
| Assert.assertNull(sign.eval((Integer) null)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCbrt() { | ||
| Cbrt cbrt = new Cbrt(); | ||
| Assert.assertEquals(cbrt.eval(27.0), 3.0); | ||
| Assert.assertEquals(cbrt.eval(-8.0), -2.0); | ||
| Assert.assertEquals(cbrt.eval(0.0), 0.0); | ||
| Assert.assertEquals(cbrt.eval(1.0), 1.0); | ||
| Assert.assertNull(cbrt.eval(null)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTrunc() { | ||
| Trunc trunc = new Trunc(); | ||
| // Trunc vs Round: trunc(3.1465, 2) = 3.14, round(3.1465, 2) = 3.15 | ||
| Assert.assertEquals(trunc.eval(3.1465, 2L), 3.14); | ||
| Assert.assertEquals(trunc.eval(3.1415, 2L), 3.14); | ||
| // Negative: DOWN mode truncates toward zero | ||
| Assert.assertEquals(trunc.eval(-3.1465, 2L), -3.14); | ||
| // Default: truncate to 0 decimal places | ||
| Assert.assertEquals(trunc.eval(3.1415), 3.0); | ||
| Assert.assertEquals(trunc.eval(-3.9), -3.0); | ||
| // Integer scale | ||
| Assert.assertEquals(trunc.eval(3.1465, 2), 3.14); | ||
| // Null handling | ||
| Assert.assertNull(trunc.eval(null, 2L)); | ||
| Assert.assertNull(trunc.eval(3.14, (Long) null)); | ||
| Assert.assertNull(trunc.eval(null, 2)); | ||
| Assert.assertNull(trunc.eval(3.14, (Integer) null)); | ||
| Assert.assertNull(trunc.eval((Double) null)); | ||
| // Long/Integer pass-through | ||
| Assert.assertEquals(trunc.eval(5L), Long.valueOf(5L)); | ||
| Assert.assertEquals(trunc.eval(5), Integer.valueOf(5)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?