From 9aa5e1b2b86bf2e8b333aa797076927ef35526b1 Mon Sep 17 00:00:00 2001 From: brijrajk <22271048+brijrajk@users.noreply.github.com> Date: Thu, 28 May 2026 16:26:38 +0530 Subject: [PATCH] [GLUTEN-12157][VL] Register sin, tan, tanh, radians in Velox sparksql function registry --- .../MathFunctionsValidateSuite.scala | 34 ++++++++++++++++++- .../ScalarFunctionsValidateSuite.scala | 13 +++++-- .../functions/RegistrationAllFunctions.cc | 9 +++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/backends-velox/src/test/scala/org/apache/gluten/functions/MathFunctionsValidateSuite.scala b/backends-velox/src/test/scala/org/apache/gluten/functions/MathFunctionsValidateSuite.scala index 2b2922629c7..c69069cf014 100644 --- a/backends-velox/src/test/scala/org/apache/gluten/functions/MathFunctionsValidateSuite.scala +++ b/backends-velox/src/test/scala/org/apache/gluten/functions/MathFunctionsValidateSuite.scala @@ -66,7 +66,15 @@ class MathFunctionsValidateSuiteAnsiOn extends FunctionsValidateSuite { } } -abstract class MathFunctionsValidateSuite extends FunctionsValidateSuite { +class MathFunctionsValidateSuite extends FunctionsValidateSuite { + + // Disable ANSI mode: Spark 4 enables it by default, which wraps math functions + // in ANSI check nodes and prevents ProjectExecTransformer from being the top-level + // plan node. ANSI-specific behaviour is tested in MathFunctionsValidateSuiteAnsiOn. + override protected def sparkConf: SparkConf = { + super.sparkConf + .set(SQLConf.ANSI_ENABLED.key, "false") + } disableFallbackCheck import testImplicits._ @@ -295,6 +303,12 @@ abstract class MathFunctionsValidateSuite extends FunctionsValidateSuite { } } + test("radians") { + runQueryAndCompare("SELECT radians(l_orderkey) from lineitem limit 1") { + checkGlutenPlan[ProjectExecTransformer] + } + } + test("rint") { withTempPath { path => @@ -332,6 +346,24 @@ abstract class MathFunctionsValidateSuite extends FunctionsValidateSuite { } } + test("sin") { + runQueryAndCompare("SELECT sin(l_orderkey) from lineitem limit 1") { + checkGlutenPlan[ProjectExecTransformer] + } + } + + test("tan") { + runQueryAndCompare("SELECT tan(l_orderkey) from lineitem limit 1") { + checkGlutenPlan[ProjectExecTransformer] + } + } + + test("tanh") { + runQueryAndCompare("SELECT tanh(l_orderkey) from lineitem limit 1") { + checkGlutenPlan[ProjectExecTransformer] + } + } + test("try_add") { runQueryAndCompare( "select try_add(cast(l_orderkey as int), 1), try_add(cast(l_orderkey as int), 2147483647)" + diff --git a/backends-velox/src/test/scala/org/apache/gluten/functions/ScalarFunctionsValidateSuite.scala b/backends-velox/src/test/scala/org/apache/gluten/functions/ScalarFunctionsValidateSuite.scala index cd49c23029b..54a92beb3f2 100644 --- a/backends-velox/src/test/scala/org/apache/gluten/functions/ScalarFunctionsValidateSuite.scala +++ b/backends-velox/src/test/scala/org/apache/gluten/functions/ScalarFunctionsValidateSuite.scala @@ -19,14 +19,23 @@ package org.apache.gluten.functions import org.apache.gluten.config.GlutenConfig import org.apache.gluten.execution.{BatchScanExecTransformer, FilterExecTransformer, ProjectExecTransformer} -import org.apache.spark.SparkException +import org.apache.spark.{SparkConf, SparkException} import org.apache.spark.sql.Row import org.apache.spark.sql.catalyst.optimizer.NullPropagation import org.apache.spark.sql.execution.ProjectExec import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.types._ -abstract class ScalarFunctionsValidateSuite extends FunctionsValidateSuite { +class ScalarFunctionsValidateSuite extends FunctionsValidateSuite { + + // Disable ANSI mode: Spark 4 enables it by default, which wraps scalar functions + // in ANSI check nodes and prevents ProjectExecTransformer from being the top-level + // plan node. + override protected def sparkConf: SparkConf = { + super.sparkConf + .set(SQLConf.ANSI_ENABLED.key, "false") + } + disableFallbackCheck import testImplicits._ diff --git a/cpp/velox/operators/functions/RegistrationAllFunctions.cc b/cpp/velox/operators/functions/RegistrationAllFunctions.cc index dd1be7805c7..2d6c62e67a9 100644 --- a/cpp/velox/operators/functions/RegistrationAllFunctions.cc +++ b/cpp/velox/operators/functions/RegistrationAllFunctions.cc @@ -24,6 +24,7 @@ #include "velox/functions/iceberg/Register.h" #include "velox/functions/lib/CheckedArithmetic.h" #include "velox/functions/lib/RegistrationHelpers.h" +#include "velox/functions/prestosql/Arithmetic.h" #include "velox/functions/prestosql/aggregates/RegisterAggregateFunctions.h" #include "velox/functions/prestosql/registration/RegistrationFunctions.h" #include "velox/functions/prestosql/window/WindowFunctionsRegistration.h" @@ -76,6 +77,14 @@ void registerFunctionOverwrite() { kRowConstructorWithAllNull, std::make_unique(kRowConstructorWithAllNull)); + // Register math functions that are present in the prestosql implementation + // but not yet in the sparksql registry. These are semantically identical + // to Spark's behavior for the same names. + velox::registerFunction({"radians"}); + velox::registerFunction({"sin"}); + velox::registerFunction({"tan"}); + velox::registerFunction({"tanh"}); + velox::functions::registerPrestoVectorFunctions(); }