Skip to content
Merged
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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已添加端到端 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)。

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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.runtime.query.udf;

import org.apache.geaflow.dsl.runtime.query.QueryTester;
import org.testng.annotations.Test;

/**
* End-to-end SQL tests for the SIGN, CBRT, and TRUNC math UDFs.
*
* <p>Unlike the unit tests in {@code MathUdfTest} which invoke the Java
* {@code eval()} methods directly, these tests exercise the full SQL
* pipeline: function registration in {@code BuildInSqlFunctionTable},
* overload resolution, type inference, and runtime invocation through
* the GeaFlow query engine.</p>
*/
public class MathSignCbrtTruncTest {

@Test
public void testSignCbrtTrunc() throws Exception {
QueryTester
.build()
.withQueryPath("/query/math_sign_cbrt_trunc_001.sql")
.execute()
.checkSinkResult();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1|3.14|5|7
2|-2.71|-8|-3
3|0.0|0|0
4|27.0|100|64
5|-8.0|-27|-8
6|3.1465|999|999
7|-3.1465|-999|-999
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1|1.0|1.0|1.0|1.4643443505031195|3.14|3.0|5|7
2|-1.0|-1.0|-1.0|-1.3941936390611858|-2.71|-2.0|-8|-3
3|0.0|0.0|0.0|0.0|0.0|0.0|0|0
4|1.0|1.0|1.0|3.0|27.0|27.0|100|64
5|-1.0|-1.0|-1.0|-2.0|-8.0|-8.0|-27|-8
6|1.0|1.0|1.0|1.4653540827785496|3.14|3.0|999|999
7|-1.0|-1.0|-1.0|-1.4653540827785496|-3.14|-3.0|-999|-999
Loading
Loading