martin-g commented on code in PR #18979: URL: https://github.com/apache/datafusion/pull/18979#discussion_r2573013027
########## datafusion/sqllogictest/test_files/scalar.slt: ########## @@ -317,6 +317,50 @@ select ceil(100.1234, 1) query error DataFusion error: This feature is not implemented: CEIL with datetime is not supported select ceil(100.1234 to year) +# ceil with decimal argument +query RRRR +select + ceil(arrow_cast(1.23,'Decimal128(10,2)')), + ceil(arrow_cast(-1.23,'Decimal128(10,2)')), + ceil(arrow_cast(123.00,'Decimal128(10,2)')), + ceil(arrow_cast(-123.00,'Decimal128(10,2)')); +---- +2 -1 123 -123 + +# ceil with decimal32 argument (ensure decimal output) +query TTTTTTTT +select + arrow_typeof(ceil(arrow_cast(9.01,'Decimal32(7,2)'))), + arrow_cast(ceil(arrow_cast(9.01,'Decimal32(7,2)')), 'Utf8'), Review Comment: I think the cast to Utf8 is used here to print the Decimal type and the part after the decimal point (e.g. `.00`). This way one could see the precision of the result ########## datafusion/functions/src/math/decimal.rs: ########## @@ -0,0 +1,127 @@ +// 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::ops::Rem; +use std::sync::Arc; + +use arrow::array::{ArrayRef, AsArray, PrimitiveArray}; +use arrow::datatypes::{ArrowNativeTypeOp, DecimalType}; +use arrow::error::ArrowError; +use arrow_buffer::ArrowNativeType; +use datafusion_common::{DataFusionError, Result}; + +pub(super) fn apply_decimal_op<T, F>( + array: &ArrayRef, + scale: i8, + fn_name: &str, + op: F, +) -> Result<ArrayRef> +where + T: DecimalType, + T::Native: ArrowNativeType + ArrowNativeTypeOp, + F: Fn(T::Native, T::Native) -> std::result::Result<T::Native, ArrowError>, +{ + if scale <= 0 { + return Ok(Arc::clone(array)); + } + + let factor = decimal_scale_factor::<T>(scale, fn_name)?; + let decimal = array.as_primitive::<T>(); + let data_type = array.data_type().clone(); + + let result: PrimitiveArray<T> = decimal + .try_unary(|value| op(value, factor))? + .with_data_type(data_type); + + Ok(Arc::new(result)) +} + +fn decimal_scale_factor<T>(scale: i8, fn_name: &str) -> Result<T::Native> +where + T: DecimalType, + T::Native: ArrowNativeType + ArrowNativeTypeOp, +{ + let base = <T::Native as ArrowNativeType>::from_usize(10).ok_or_else(|| { + DataFusionError::Execution(format!( + "Decimal scale {scale} is too large for {fn_name}" Review Comment: Wrong error message. ########## datafusion/sqllogictest/test_files/scalar.slt: ########## @@ -317,6 +317,50 @@ select ceil(100.1234, 1) query error DataFusion error: This feature is not implemented: CEIL with datetime is not supported select ceil(100.1234 to year) +# ceil with decimal argument +query RRRR +select + ceil(arrow_cast(1.23,'Decimal128(10,2)')), + ceil(arrow_cast(-1.23,'Decimal128(10,2)')), + ceil(arrow_cast(123.00,'Decimal128(10,2)')), + ceil(arrow_cast(-123.00,'Decimal128(10,2)')); +---- +2 -1 123 -123 + +# ceil with decimal32 argument (ensure decimal output) +query TTTTTTTT +select + arrow_typeof(ceil(arrow_cast(9.01,'Decimal32(7,2)'))), + arrow_cast(ceil(arrow_cast(9.01,'Decimal32(7,2)')), 'Utf8'), + arrow_typeof(ceil(arrow_cast(-9.01,'Decimal32(7,2)'))), + arrow_cast(ceil(arrow_cast(-9.01,'Decimal32(7,2)')), 'Utf8'), + arrow_typeof(ceil(arrow_cast(10.00,'Decimal32(7,2)'))), + arrow_cast(ceil(arrow_cast(10.00,'Decimal32(7,2)')), 'Utf8'), + arrow_typeof(ceil(arrow_cast(-0.99,'Decimal32(7,2)'))), + arrow_cast(ceil(arrow_cast(-0.99,'Decimal32(7,2)')), 'Utf8'); +---- +Decimal32(7, 2) 10.00 Decimal32(7, 2) -9.00 Decimal32(7, 2) 10.00 Decimal32(7, 2) 0.00 + +# ceil with decimal64 zero scale +query TTTT +select + arrow_typeof(ceil(arrow_cast(123456789,'Decimal64(18,0)'))), + arrow_cast(ceil(arrow_cast(123456789,'Decimal64(18,0)')), 'Utf8'), + arrow_typeof(ceil(arrow_cast(-987654321,'Decimal64(18,0)'))), + arrow_cast(ceil(arrow_cast(-987654321,'Decimal64(18,0)')), 'Utf8'); +---- +Decimal64(18, 0) 123456789 Decimal64(18, 0) -987654321 + +# ceil with decimal256 argument +query TTTT +select + arrow_typeof(ceil(arrow_cast('9999999999999999999999999999999999.01','Decimal256(38,2)'))), + arrow_cast(ceil(arrow_cast('9999999999999999999999999999999999.01','Decimal256(38,2)')), 'Utf8'), + arrow_typeof(ceil(arrow_cast('-9999999999999999999999999999999999.01','Decimal256(38,2)'))), + arrow_cast(ceil(arrow_cast('-9999999999999999999999999999999999.01','Decimal256(38,2)')), 'Utf8'); +---- +Decimal256(38, 2) 10000000000000000000000000000000000.00 Decimal256(38, 2) -9999999999999999999999999999999999.00 Review Comment: It looks OK to me ########## datafusion/sqllogictest/test_files/scalar.slt: ########## @@ -317,6 +317,16 @@ select ceil(100.1234, 1) query error DataFusion error: This feature is not implemented: CEIL with datetime is not supported select ceil(100.1234 to year) +# ceil with decimal argument +query RRRR +select + ceil(arrow_cast(1.23,'Decimal128(10,2)')), Review Comment: Did you add a test case for this ? Maybe I missed it -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
