use std::any::TypeId;
use crate::func::def::{FunctionDef, ParameterBindingProvider, Parameters, SpecialPosition};
use crate::types::array::Array;
use crate::types::matcher::{AnyMatcher, ExactMatcher, NumericMatcher, OrMatcher};
use crate::types::{Type, DOUBLE, INT, STRING, TIMESTAMP};
#[derive(Default, Clone)]
pub struct MatchFirst;
impl FunctionDef for MatchFirst {
fn name(&self) -> &'static str {
"first"
}
fn parameters(&self) -> Parameters {
Parameters::new().with("expression", AnyMatcher::default())
}
fn return_type(&self, bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(bindings.get_by_index(0)?.typ().clone())
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
fn special_position(&self) -> Option<SpecialPosition> {
Some(SpecialPosition::Match)
}
}
#[derive(Default, Clone)]
pub struct MatchLast;
impl FunctionDef for MatchLast {
fn name(&self) -> &'static str {
"last"
}
fn parameters(&self) -> Parameters {
Parameters::new().with("expression", AnyMatcher::default())
}
fn return_type(&self, bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(bindings.get_by_index(0)?.typ().clone())
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
fn special_position(&self) -> Option<SpecialPosition> {
Some(SpecialPosition::Match)
}
}
#[derive(Default, Clone)]
pub struct MatchCountStar;
impl FunctionDef for MatchCountStar {
fn name(&self) -> &'static str {
"count"
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(INT)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
fn special_position(&self) -> Option<SpecialPosition> {
Some(SpecialPosition::Match)
}
}
#[derive(Default, Clone)]
pub struct MatchCountAny;
impl FunctionDef for MatchCountAny {
fn name(&self) -> &'static str {
"count"
}
fn parameters(&self) -> Parameters {
Parameters::new().with("x", AnyMatcher::default())
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(INT)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
fn special_position(&self) -> Option<SpecialPosition> {
Some(SpecialPosition::Match)
}
}
#[derive(Default, Clone)]
pub struct MatchSum;
impl FunctionDef for MatchSum {
fn name(&self) -> &'static str {
"sum"
}
fn parameters(&self) -> Parameters {
Parameters::new().with("x", NumericMatcher::default())
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(DOUBLE)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
fn special_position(&self) -> Option<SpecialPosition> {
Some(SpecialPosition::Match)
}
}
#[derive(Default, Clone)]
pub struct MatchAvg;
impl FunctionDef for MatchAvg {
fn name(&self) -> &'static str {
"avg"
}
fn parameters(&self) -> Parameters {
Parameters::new().with("x", NumericMatcher::default())
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(DOUBLE)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
fn special_position(&self) -> Option<SpecialPosition> {
Some(SpecialPosition::Match)
}
}
#[derive(Default, Clone)]
pub struct MatchMin;
impl FunctionDef for MatchMin {
fn name(&self) -> &'static str {
"min"
}
fn parameters(&self) -> Parameters {
Parameters::new().with(
"x",
OrMatcher::default()
.with(NumericMatcher::default())
.with(ExactMatcher::of(STRING))
.with(ExactMatcher::of(TIMESTAMP)),
)
}
fn return_type(&self, bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(bindings.get_by_index(0)?.typ().clone())
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
fn special_position(&self) -> Option<SpecialPosition> {
Some(SpecialPosition::Match)
}
}
#[derive(Default, Clone)]
pub struct MatchMax;
impl FunctionDef for MatchMax {
fn name(&self) -> &'static str {
"max"
}
fn parameters(&self) -> Parameters {
Parameters::new().with(
"x",
OrMatcher::default()
.with(NumericMatcher::default())
.with(ExactMatcher::of(STRING))
.with(ExactMatcher::of(TIMESTAMP)),
)
}
fn return_type(&self, bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(bindings.get_by_index(0)?.typ().clone())
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
fn special_position(&self) -> Option<SpecialPosition> {
Some(SpecialPosition::Match)
}
}
#[derive(Default, Clone)]
pub struct MatchArrayAgg;
impl FunctionDef for MatchArrayAgg {
fn name(&self) -> &'static str {
"array_agg"
}
fn parameters(&self) -> Parameters {
Parameters::new().with("x", AnyMatcher::default())
}
fn return_type(&self, bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(Array::new(bindings.get_by_index(0)?.typ().clone()).into())
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
fn special_position(&self) -> Option<SpecialPosition> {
Some(SpecialPosition::Match)
}
}
#[derive(Default, Clone)]
pub struct MatchCountDistinct;
impl FunctionDef for MatchCountDistinct {
fn name(&self) -> &'static str {
"count_distinct"
}
fn parameters(&self) -> Parameters {
Parameters::new().with("x", AnyMatcher::default())
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(INT)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
fn special_position(&self) -> Option<SpecialPosition> {
Some(SpecialPosition::Match)
}
}