From a8df45f6e9cd015f131f60bd3c2da2d8b6f6be7f Mon Sep 17 00:00:00 2001 From: Jiayu Liu Date: Sun, 6 Feb 2022 13:29:20 +0800 Subject: [PATCH] move signature, type signature, and volatility to split module --- datafusion-expr/src/lib.rs | 2 + datafusion-expr/src/signature.rs | 116 ++++++++++++++++++++++ datafusion/src/physical_plan/functions.rs | 98 +----------------- 3 files changed, 119 insertions(+), 97 deletions(-) create mode 100644 datafusion-expr/src/signature.rs diff --git a/datafusion-expr/src/lib.rs b/datafusion-expr/src/lib.rs index 13fa93ed6a2e..d2b10b404a8b 100644 --- a/datafusion-expr/src/lib.rs +++ b/datafusion-expr/src/lib.rs @@ -17,10 +17,12 @@ mod aggregate_function; mod operator; +mod signature; mod window_frame; mod window_function; pub use aggregate_function::AggregateFunction; pub use operator::Operator; +pub use signature::{Signature, TypeSignature, Volatility}; pub use window_frame::{WindowFrame, WindowFrameBound, WindowFrameUnits}; pub use window_function::{BuiltInWindowFunction, WindowFunction}; diff --git a/datafusion-expr/src/signature.rs b/datafusion-expr/src/signature.rs new file mode 100644 index 000000000000..5c27f422c105 --- /dev/null +++ b/datafusion-expr/src/signature.rs @@ -0,0 +1,116 @@ +// 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 arrow::datatypes::DataType; + +///A function's volatility, which defines the functions eligibility for certain optimizations +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] +pub enum Volatility { + /// Immutable - An immutable function will always return the same output when given the same input. An example of this is [BuiltinScalarFunction::Cos]. + Immutable, + /// Stable - A stable function may return different values given the same input accross different queries but must return the same value for a given input within a query. An example of this is [BuiltinScalarFunction::Now]. + Stable, + /// Volatile - A volatile function may change the return value from evaluation to evaluation. Mutiple invocations of a volatile function may return different results when used in the same query. An example of this is [BuiltinScalarFunction::Random]. + Volatile, +} + +/// A function's type signature, which defines the function's supported argument types. +#[derive(Debug, Clone, PartialEq, Hash)] +pub enum TypeSignature { + /// arbitrary number of arguments of an common type out of a list of valid types + // A function such as `concat` is `Variadic(vec![DataType::Utf8, DataType::LargeUtf8])` + Variadic(Vec), + /// arbitrary number of arguments of an arbitrary but equal type + // A function such as `array` is `VariadicEqual` + // The first argument decides the type used for coercion + VariadicEqual, + /// fixed number of arguments of an arbitrary but equal type out of a list of valid types + // A function of one argument of f64 is `Uniform(1, vec![DataType::Float64])` + // A function of one argument of f64 or f32 is `Uniform(1, vec![DataType::Float32, DataType::Float64])` + Uniform(usize, Vec), + /// exact number of arguments of an exact type + Exact(Vec), + /// fixed number of arguments of arbitrary types + Any(usize), + /// One of a list of signatures + OneOf(Vec), +} + +///The Signature of a function defines its supported input types as well as its volatility. +#[derive(Debug, Clone, PartialEq, Hash)] +pub struct Signature { + /// type_signature - The types that the function accepts. See [TypeSignature] for more information. + pub type_signature: TypeSignature, + /// volatility - The volatility of the function. See [Volatility] for more information. + pub volatility: Volatility, +} + +impl Signature { + /// new - Creates a new Signature from any type signature and the volatility. + pub fn new(type_signature: TypeSignature, volatility: Volatility) -> Self { + Signature { + type_signature, + volatility, + } + } + /// variadic - Creates a variadic signature that represents an arbitrary number of arguments all from a type in common_types. + pub fn variadic(common_types: Vec, volatility: Volatility) -> Self { + Self { + type_signature: TypeSignature::Variadic(common_types), + volatility, + } + } + /// variadic_equal - Creates a variadic signature that represents an arbitrary number of arguments of the same type. + pub fn variadic_equal(volatility: Volatility) -> Self { + Self { + type_signature: TypeSignature::VariadicEqual, + volatility, + } + } + /// uniform - Creates a function with a fixed number of arguments of the same type, which must be from valid_types. + pub fn uniform( + arg_count: usize, + valid_types: Vec, + volatility: Volatility, + ) -> Self { + Self { + type_signature: TypeSignature::Uniform(arg_count, valid_types), + volatility, + } + } + /// exact - Creates a signture which must match the types in exact_types in order. + pub fn exact(exact_types: Vec, volatility: Volatility) -> Self { + Signature { + type_signature: TypeSignature::Exact(exact_types), + volatility, + } + } + /// any - Creates a signature which can a be made of any type but of a specified number + pub fn any(arg_count: usize, volatility: Volatility) -> Self { + Signature { + type_signature: TypeSignature::Any(arg_count), + volatility, + } + } + /// one_of Creates a signature which can match any of the [TypeSignature]s which are passed in. + pub fn one_of(type_signatures: Vec, volatility: Volatility) -> Self { + Signature { + type_signature: TypeSignature::OneOf(type_signatures), + volatility, + } + } +} diff --git a/datafusion/src/physical_plan/functions.rs b/datafusion/src/physical_plan/functions.rs index 7d7cda75e867..af157c02fc38 100644 --- a/datafusion/src/physical_plan/functions.rs +++ b/datafusion/src/physical_plan/functions.rs @@ -56,103 +56,7 @@ use fmt::{Debug, Formatter}; use std::convert::From; use std::{any::Any, fmt, str::FromStr, sync::Arc}; -/// A function's type signature, which defines the function's supported argument types. -#[derive(Debug, Clone, PartialEq, Hash)] -pub enum TypeSignature { - /// arbitrary number of arguments of an common type out of a list of valid types - // A function such as `concat` is `Variadic(vec![DataType::Utf8, DataType::LargeUtf8])` - Variadic(Vec), - /// arbitrary number of arguments of an arbitrary but equal type - // A function such as `array` is `VariadicEqual` - // The first argument decides the type used for coercion - VariadicEqual, - /// fixed number of arguments of an arbitrary but equal type out of a list of valid types - // A function of one argument of f64 is `Uniform(1, vec![DataType::Float64])` - // A function of one argument of f64 or f32 is `Uniform(1, vec![DataType::Float32, DataType::Float64])` - Uniform(usize, Vec), - /// exact number of arguments of an exact type - Exact(Vec), - /// fixed number of arguments of arbitrary types - Any(usize), - /// One of a list of signatures - OneOf(Vec), -} - -///The Signature of a function defines its supported input types as well as its volatility. -#[derive(Debug, Clone, PartialEq, Hash)] -pub struct Signature { - /// type_signature - The types that the function accepts. See [TypeSignature] for more information. - pub type_signature: TypeSignature, - /// volatility - The volatility of the function. See [Volatility] for more information. - pub volatility: Volatility, -} - -impl Signature { - /// new - Creates a new Signature from any type signature and the volatility. - pub fn new(type_signature: TypeSignature, volatility: Volatility) -> Self { - Signature { - type_signature, - volatility, - } - } - /// variadic - Creates a variadic signature that represents an arbitrary number of arguments all from a type in common_types. - pub fn variadic(common_types: Vec, volatility: Volatility) -> Self { - Self { - type_signature: TypeSignature::Variadic(common_types), - volatility, - } - } - /// variadic_equal - Creates a variadic signature that represents an arbitrary number of arguments of the same type. - pub fn variadic_equal(volatility: Volatility) -> Self { - Self { - type_signature: TypeSignature::VariadicEqual, - volatility, - } - } - /// uniform - Creates a function with a fixed number of arguments of the same type, which must be from valid_types. - pub fn uniform( - arg_count: usize, - valid_types: Vec, - volatility: Volatility, - ) -> Self { - Self { - type_signature: TypeSignature::Uniform(arg_count, valid_types), - volatility, - } - } - /// exact - Creates a signture which must match the types in exact_types in order. - pub fn exact(exact_types: Vec, volatility: Volatility) -> Self { - Signature { - type_signature: TypeSignature::Exact(exact_types), - volatility, - } - } - /// any - Creates a signature which can a be made of any type but of a specified number - pub fn any(arg_count: usize, volatility: Volatility) -> Self { - Signature { - type_signature: TypeSignature::Any(arg_count), - volatility, - } - } - /// one_of Creates a signature which can match any of the [TypeSignature]s which are passed in. - pub fn one_of(type_signatures: Vec, volatility: Volatility) -> Self { - Signature { - type_signature: TypeSignature::OneOf(type_signatures), - volatility, - } - } -} - -///A function's volatility, which defines the functions eligibility for certain optimizations -#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] -pub enum Volatility { - /// Immutable - An immutable function will always return the same output when given the same input. An example of this is [BuiltinScalarFunction::Cos]. - Immutable, - /// Stable - A stable function may return different values given the same input accross different queries but must return the same value for a given input within a query. An example of this is [BuiltinScalarFunction::Now]. - Stable, - /// Volatile - A volatile function may change the return value from evaluation to evaluation. Mutiple invocations of a volatile function may return different results when used in the same query. An example of this is [BuiltinScalarFunction::Random]. - Volatile, -} +pub use datafusion_expr::{Signature, TypeSignature, Volatility}; /// Scalar function ///