alamb commented on code in PR #10000:
URL:
https://github.com/apache/arrow-datafusion/pull/10000#discussion_r1556368128
##########
datafusion/proto/proto/datafusion.proto:
##########
@@ -555,12 +555,12 @@ enum ScalarFunction {
Log = 11;
// 12 was Log10
// 13 was Log2
- Round = 14;
+ // 14 was Round
Review Comment:
Wow -- this list is looking pretty small now 🙏
##########
datafusion/functions/src/math/round.rs:
##########
@@ -0,0 +1,252 @@
+// 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::{ArrayRef, Float32Array, Float64Array, Int64Array};
+use arrow::datatypes::DataType;
+use arrow::datatypes::DataType::{Float32, Float64};
+
+use crate::utils::make_scalar_function;
+use datafusion_common::{exec_err, DataFusionError, Result, ScalarValue};
+use datafusion_expr::TypeSignature::Exact;
+use datafusion_expr::{ColumnarValue, FuncMonotonicity};
+use datafusion_expr::{ScalarUDFImpl, Signature, Volatility};
+
+#[derive(Debug)]
+pub struct RoundFunc {
+ signature: Signature,
+}
+
+impl Default for RoundFunc {
+ fn default() -> Self {
+ RoundFunc::new()
+ }
+}
+
+impl RoundFunc {
+ pub fn new() -> Self {
+ use DataType::*;
+ Self {
+ signature: Signature::one_of(
+ vec![
+ Exact(vec![Float64, Int64]),
+ Exact(vec![Float32, Int64]),
+ Exact(vec![Float64]),
+ Exact(vec![Float32]),
+ ],
+ Volatility::Immutable,
+ ),
+ }
+ }
+}
+
+impl ScalarUDFImpl for RoundFunc {
+ fn as_any(&self) -> &dyn Any {
+ self
+ }
+
+ fn name(&self) -> &str {
+ "round"
+ }
+
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+
+ fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+ match arg_types[0] {
+ Float32 => Ok(Float32),
+ _ => Ok(Float64),
+ }
+ }
+
+ fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
+ make_scalar_function(round, vec![])(args)
+ }
+
+ fn monotonicity(&self) -> Result<Option<FuncMonotonicity>> {
+ Ok(Some(vec![Some(true)]))
+ }
+}
+
+/// Round SQL function
+pub fn round(args: &[ArrayRef]) -> Result<ArrayRef> {
+ if args.len() != 1 && args.len() != 2 {
+ return exec_err!(
+ "round function requires one or two arguments, got {}",
+ args.len()
+ );
+ }
+
+ let mut decimal_places =
ColumnarValue::Scalar(ScalarValue::Int64(Some(0)));
+
+ if args.len() == 2 {
+ decimal_places = ColumnarValue::Array(args[1].clone());
Review Comment:
yes, I agree.
These are some of the oldest functions in DataFusion so were written at a
very different time. We have much better patterns now
--
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]