Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@
import org.apache.geaflow.dsl.udf.table.date.WeekDay;
import org.apache.geaflow.dsl.udf.table.date.WeekOfYear;
import org.apache.geaflow.dsl.udf.table.date.Year;
import org.apache.geaflow.dsl.udf.table.math.Cbrt;
import org.apache.geaflow.dsl.udf.table.math.E;
import org.apache.geaflow.dsl.udf.table.math.Log2;
import org.apache.geaflow.dsl.udf.table.math.Round;
import org.apache.geaflow.dsl.udf.table.math.Sign;
import org.apache.geaflow.dsl.udf.table.math.Trunc;
import org.apache.geaflow.dsl.udf.table.other.Direction;
import org.apache.geaflow.dsl.udf.table.other.EdgeSrcId;
import org.apache.geaflow.dsl.udf.table.other.EdgeTargetId;
Expand Down Expand Up @@ -169,9 +172,12 @@ public class BuildInSqlFunctionTable extends ListSqlOperatorTable {
.add(GeaFlowFunction.of(ArrayUnion.class))

// udf.table.math
.add(GeaFlowFunction.of(Cbrt.class))
.add(GeaFlowFunction.of(E.class))
.add(GeaFlowFunction.of(Log2.class))
.add(GeaFlowFunction.of(Round.class))
.add(GeaFlowFunction.of(Sign.class))
.add(GeaFlowFunction.of(Trunc.class))

// udf.table.string
.add(GeaFlowFunction.of(Ascii2String.class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,26 @@ public static Double round(Double a, Integer n) {
}
}

public static Double cbrt(Double a) {
if (a == null) {
return null;
}
return Math.cbrt(a);
}

public static Double trunc(Double a, Integer n) {
if (a == null || n == null) {
return null;
}

if (Double.isNaN(a) || Double.isInfinite(a)) {
return a;
} else {
return BigDecimal.valueOf(a).setScale(n, RoundingMode.DOWN)
.doubleValue();
}
}

public static Boolean equal(Long a, Long b) {
if (a == null || b == null) {
return null;
Expand Down
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);
}
}
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;
}
}
}
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import static org.apache.geaflow.dsl.schema.function.GeaFlowBuiltinFunctions.atan;
import static org.apache.geaflow.dsl.schema.function.GeaFlowBuiltinFunctions.ceil;
import static org.apache.geaflow.dsl.schema.function.GeaFlowBuiltinFunctions.cos;
import static org.apache.geaflow.dsl.schema.function.GeaFlowBuiltinFunctions.cbrt;
import static org.apache.geaflow.dsl.schema.function.GeaFlowBuiltinFunctions.cot;
import static org.apache.geaflow.dsl.schema.function.GeaFlowBuiltinFunctions.trunc;
import static org.apache.geaflow.dsl.schema.function.GeaFlowBuiltinFunctions.degrees;
import static org.apache.geaflow.dsl.schema.function.GeaFlowBuiltinFunctions.divide;
import static org.apache.geaflow.dsl.schema.function.GeaFlowBuiltinFunctions.equal;
Expand Down Expand Up @@ -645,6 +647,24 @@ public void testOtherFunction() {
Assert.assertNull(round(doubleNull, 2));
}

@Test
public void testCbrt() {
assertEquals(cbrt(8.0), 2.0);
assertEquals(cbrt(27.0), 3.0);
assertEquals(cbrt(-8.0), -2.0);
assertEquals(cbrt(0.0), 0.0);
Assert.assertNull(cbrt(doubleNull));
}

@Test
public void testTrunc() {
assertEquals(trunc(3.567, 2), 3.56);
assertEquals(trunc(3.567, 0), 3.0);
assertEquals(trunc(-3.567, 2), -3.56);
Assert.assertNull(trunc(doubleNull, 2));
Assert.assertNull(trunc(3.567, intNull));
}

@Test
public void testGeaFlowUserDefinedTableFunction() {
GQLJavaTypeFactory typeFactory = GQLJavaTypeFactory.create();
Expand Down
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() {

Copy link
Copy Markdown
Contributor

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?

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));
}
}
Loading