Skip to content

Apache DataFusion SQL Query Engine

feat: support spark sec (#18728)

Vrishabh91eba1dfc350
diff --git a/datafusion/spark/src/function/math/mod.rs b/datafusion/spark/src/function/math/mod.rs
index 4560a8e72e..fe8c2b1da0 100644
--- a/datafusion/spark/src/function/math/mod.rs
+++ b/datafusion/spark/src/function/math/mod.rs
@@ -35,6 +35,7 @@ make_udf_function!(modulus::SparkPmod, pmod);
make_udf_function!(rint::SparkRint, rint);
make_udf_function!(width_bucket::SparkWidthBucket, width_bucket);
make_udf_function!(trigonometry::SparkCsc, csc);
+make_udf_function!(trigonometry::SparkSec, sec);
pub mod expr_fn {
use datafusion_functions::export_functions;
@@ -51,6 +52,7 @@ pub mod expr_fn {
export_functions!((rint, "Returns the double value that is closest in value to the argument and is equal to a mathematical integer.", arg1));
export_functions!((width_bucket, "Returns the bucket number into which the value of this expression would fall after being evaluated.", arg1 arg2 arg3 arg4));
export_functions!((csc, "Returns the cosecant of expr.", arg1));
+ export_functions!((sec, "Returns the secant of expr.", arg1));
}
pub fn functions() -> Vec<Arc<ScalarUDF>> {
@@ -63,5 +65,6 @@ pub fn functions() -> Vec<Arc<ScalarUDF>> {
rint(),
width_bucket(),
csc(),
+ sec(),
]
}
diff --git a/datafusion/spark/src/function/math/trigonometry.rs b/datafusion/spark/src/function/math/trigonometry.rs
index a6bcf8044f..85b10f5b99 100644
--- a/datafusion/spark/src/function/math/trigonometry.rs
+++ b/datafusion/spark/src/function/math/trigonometry.rs
@@ -95,3 +95,73 @@ fn spark_csc(arg: &ColumnarValue) -> Result<ColumnarValue> {
)),
}
}
+
+static SEC_FUNCTION_NAME: &str = "sec";
+
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#sec>
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkSec {
+ signature: Signature,
+}
+
+impl Default for SparkSec {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl SparkSec {
+ pub fn new() -> Self {
+ Self {
+ signature: Signature::exact(vec![DataType::Float64], Volatility::Immutable),
+ }
+ }
+}
+
+impl ScalarUDFImpl for SparkSec {
+ fn as_any(&self) -> &dyn Any {
+ self
+ }
+
+ fn name(&self) -> &str {
+ SEC_FUNCTION_NAME
+ }
+
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+ Ok(DataType::Float64)
+ }
+
+ fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
+ let [arg] = take_function_args(self.name(), &args.args)?;
+ spark_sec(arg)
+ }
+}
+
+fn spark_sec(arg: &ColumnarValue) -> Result<ColumnarValue> {
+ match arg {
+ ColumnarValue::Scalar(ScalarValue::Float64(value)) => Ok(ColumnarValue::Scalar(
+ ScalarValue::Float64(value.map(|x| 1.0 / x.cos())),
+ )),
+ ColumnarValue::Array(array) => match array.data_type() {
+ DataType::Float64 => Ok(ColumnarValue::Array(Arc::new(
+ array
+ .as_primitive::<Float64Type>()
+ .unary::<_, Float64Type>(|x| 1.0 / x.cos()),
+ ) as ArrayRef)),
+ other => Err(unsupported_data_type_exec_err(
+ SEC_FUNCTION_NAME,
+ format!("{}", DataType::Float64).as_str(),
+ other,
+ )),
+ },
+ other => Err(unsupported_data_type_exec_err(
+ SEC_FUNCTION_NAME,
+ format!("{}", DataType::Float64).as_str(),
+ &other.data_type(),
+ )),
+ }
+}
diff --git a/datafusion/sqllogictest/test_files/spark/math/sec.slt b/datafusion/sqllogictest/test_files/spark/math/sec.slt
index 6c49a34549..c95d583ce9 100644
--- a/datafusion/sqllogictest/test_files/spark/math/sec.slt
+++ b/datafusion/sqllogictest/test_files/spark/math/sec.slt
@@ -23,5 +23,23 @@
## Original Query: SELECT sec(0);
## PySpark 3.5.5 Result: {'SEC(0)': 1.0, 'typeof(SEC(0))': 'double', 'typeof(0)': 'int'}
-#query
-#SELECT sec(0::int);
+query R
+SELECT sec(0::int);
+----
+1
+
+query R
+SELECT sec(a) FROM (VALUES (0::INT), (1::INT), (-1::INT), (null)) AS t(a);
+----
+1
+1.850815717680926
+1.850815717680926
+NULL
+
+query R
+SELECT sec(a) FROM (VALUES (pi()), (3 * pi()/2), (pi()/2) , (arrow_cast('NAN','Float32'))) AS t(a);
+----
+-1
+-5443746451065123
+16331239353195370
+NaN