shehabgamin commented on code in PR #16125: URL: https://github.com/apache/datafusion/pull/16125#discussion_r2160232657
########## datafusion/spark/src/function/math/factorial.rs: ########## @@ -0,0 +1,195 @@ +// 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. + +use std::any::Any; +use std::sync::Arc; + +use arrow::array::{Array, Int64Array}; +use arrow::datatypes::DataType; +use arrow::datatypes::DataType::Int64; +use datafusion_common::{ + cast::as_int64_array, exec_err, DataFusionError, Result, ScalarValue, +}; +use datafusion_expr::Signature; +use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Volatility}; + +/// <https://spark.apache.org/docs/latest/api/sql/index.html#factorial> +#[derive(Debug)] +pub struct SparkFactorial { + signature: Signature, + aliases: Vec<String>, +} + +impl Default for SparkFactorial { + fn default() -> Self { + Self::new() + } +} + +impl SparkFactorial { + pub fn new() -> Self { + Self { + signature: Signature::uniform(1, vec![Int64], Volatility::Immutable), Review Comment: Input should be `Int32`, according to these docs: https://docs.databricks.com/aws/en/sql/language-manual/functions/factorial I'm not exactly sure how the `.slt` tests passed, since the input is specifically `Int32` in those tests. There may be some coercion behind the scenes perhaps. To opt out of any potential implicit coercion, you can use `Signature::user_defined` and do something like this: ``` fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> { if arg_types.len() != 1 { return exec_err!( "`factorial` function requires 1 argument, got {}", arg_types.len() ); } match arg_types[0] { DataType::Int32 => Ok(vec![DataType::Int32]), _ => exec_err!( "`factorial` function does not support type {}", arg_types[0] ), } } ``` ########## datafusion/sqllogictest/test_files/spark/math/factorial.slt: ########## @@ -23,6 +23,15 @@ ## Original Query: SELECT factorial(5); ## PySpark 3.5.5 Result: {'factorial(5)': 120, 'typeof(factorial(5))': 'bigint', 'typeof(5)': 'int'} -#query -#SELECT factorial(5::int); +query I +SELECT factorial(5::INT); +---- +120 +query I +SELECT factorial(a) from VALUES (0::INT), (20::INT), (21::INT), (NULL) AS t(a); Review Comment: The last place to check for tests would be: https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.factorial.html But for `factorial`, the test in the link is already covered by the test here. And the url above comes from the testing guide: https://github.com/apache/datafusion/blob/85eebcd25dfbe8e2d2d75d85b8683de8be4851e8/datafusion/sqllogictest/test_files/spark/README.md#finding-test-cases ########## datafusion/sqllogictest/test_files/spark/math/factorial.slt: ########## @@ -23,6 +23,15 @@ ## Original Query: SELECT factorial(5); ## PySpark 3.5.5 Result: {'factorial(5)': 120, 'typeof(factorial(5))': 'bigint', 'typeof(5)': 'int'} -#query -#SELECT factorial(5::int); +query I +SELECT factorial(5::INT); Review Comment: Perfect! For anyone wondering, this test comes from here: https://spark.apache.org/docs/latest/api/sql/#factorial And the url above comes from the testing guide: https://github.com/apache/datafusion/blob/85eebcd25dfbe8e2d2d75d85b8683de8be4851e8/datafusion/sqllogictest/test_files/spark/README.md#finding-test-cases ########## datafusion/sqllogictest/test_files/spark/math/factorial.slt: ########## @@ -23,6 +23,15 @@ ## Original Query: SELECT factorial(5); ## PySpark 3.5.5 Result: {'factorial(5)': 120, 'typeof(factorial(5))': 'bigint', 'typeof(5)': 'int'} -#query -#SELECT factorial(5::int); +query I +SELECT factorial(5::INT); +---- +120 +query I +SELECT factorial(a) from VALUES (0::INT), (20::INT), (21::INT), (NULL) AS t(a); Review Comment: ```suggestion SELECT factorial(a) from VALUES (-1::INT), (0::INT), (1::INT), (2::INT), (3::INT), (4::INT), (5::INT), (6::INT), (7::INT), (8::INT), (9::INT), (10::INT), (11::INT), (12::INT), (13::INT), (14::INT), (15::INT), (16::INT), (17::INT), (18::INT), (19::INT), (20::INT), (21::INT), (NULL) AS t(a); ``` For anyone wondering, this test comes from here: https://docs.databricks.com/aws/en/sql/language-manual/functions/factorial And the url above comes from the testing guide: https://github.com/apache/datafusion/blob/85eebcd25dfbe8e2d2d75d85b8683de8be4851e8/datafusion/sqllogictest/test_files/spark/README.md#finding-test-cases -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org