use std::any::TypeId;
use std::sync::Arc;
use crate::tree::ast::expression::{ExpressionKind, StringLiteral};
use crate::tree::typed_ast::expression::TypedExpression;
use crate::func::def::{FunctionDef, ParameterBinding, ParameterBindingProvider, Parameters};
use crate::types::array::Array;
use crate::types::matcher::ExactMatcher;
use crate::types::{Type, BOOLEAN, INT, STRING};
#[derive(Default, Clone)]
pub struct RegexpCount;
impl FunctionDef for RegexpCount {
fn name(&self) -> &'static str {
"regexp_count"
}
fn parameters(&self) -> Parameters {
Parameters::new()
.with("string", ExactMatcher::of(STRING))
.with("pattern", ExactMatcher::of(STRING))
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(INT)
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
#[derive(Default, Clone)]
pub struct RegexpExtractAll2;
impl FunctionDef for RegexpExtractAll2 {
fn name(&self) -> &'static str {
"regexp_extract_all"
}
fn parameters(&self) -> Parameters {
Parameters::new()
.with("string", ExactMatcher::of(STRING))
.with("pattern", ExactMatcher::of(STRING))
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(Array::new(STRING).into())
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
#[derive(Default, Clone)]
pub struct RegexpExtractAll3;
impl FunctionDef for RegexpExtractAll3 {
fn name(&self) -> &'static str {
"regexp_extract_all"
}
fn parameters(&self) -> Parameters {
Parameters::new()
.with("string", ExactMatcher::of(STRING))
.with("pattern", ExactMatcher::of(STRING))
.with("group", ExactMatcher::of(INT))
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(Array::new(STRING).into())
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
#[derive(Default, Clone)]
pub struct RegexpExtract2;
impl FunctionDef for RegexpExtract2 {
fn name(&self) -> &'static str {
"regexp_extract"
}
fn parameters(&self) -> Parameters {
Parameters::new()
.with("string", ExactMatcher::of(STRING))
.with("pattern", ExactMatcher::of(STRING))
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(STRING)
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
#[derive(Default, Clone)]
pub struct RegexpExtract3;
impl FunctionDef for RegexpExtract3 {
fn name(&self) -> &'static str {
"regexp_extract"
}
fn parameters(&self) -> Parameters {
Parameters::new()
.with("string", ExactMatcher::of(STRING))
.with("pattern", ExactMatcher::of(STRING))
.with("group", ExactMatcher::of(INT))
}
fn return_type(&self, bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(bindings.get_by_index(0)?.typ().clone())
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
#[derive(Default, Clone)]
pub struct RegexpLike;
impl FunctionDef for RegexpLike {
fn name(&self) -> &'static str {
"regexp_like"
}
fn parameters(&self) -> Parameters {
Parameters::new()
.with("string", ExactMatcher::of(STRING))
.with("pattern", ExactMatcher::of(STRING))
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(BOOLEAN)
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
#[derive(Default, Clone)]
pub struct RegexpPosition2;
impl FunctionDef for RegexpPosition2 {
fn name(&self) -> &'static str {
"regexp_position"
}
fn parameters(&self) -> Parameters {
Parameters::new()
.with("string", ExactMatcher::of(STRING))
.with("pattern", ExactMatcher::of(STRING))
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(INT)
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
#[derive(Default, Clone)]
pub struct RegexpPosition3;
impl FunctionDef for RegexpPosition3 {
fn name(&self) -> &'static str {
"regexp_position"
}
fn parameters(&self) -> Parameters {
Parameters::new()
.with("string", ExactMatcher::of(STRING))
.with("pattern", ExactMatcher::of(STRING))
.with("start", ExactMatcher::of(INT))
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(INT)
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
#[derive(Default, Clone)]
pub struct RegexpPosition4;
impl FunctionDef for RegexpPosition4 {
fn name(&self) -> &'static str {
"regexp_position"
}
fn parameters(&self) -> Parameters {
Parameters::new()
.with("string", ExactMatcher::of(STRING))
.with("pattern", ExactMatcher::of(STRING))
.with("start", ExactMatcher::of(INT))
.with("occurrence", ExactMatcher::of(INT))
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(INT)
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
#[derive(Default, Clone)]
pub struct RegexpReplace2;
impl FunctionDef for RegexpReplace2 {
fn name(&self) -> &'static str {
"regexp_replace"
}
fn parameters(&self) -> Parameters {
Parameters::new()
.with("string", ExactMatcher::of(STRING))
.with("pattern", ExactMatcher::of(STRING))
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(STRING)
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
#[derive(Default, Clone)]
pub struct RegexpReplace3;
impl FunctionDef for RegexpReplace3 {
fn name(&self) -> &'static str {
"regexp_replace"
}
fn parameters(&self) -> Parameters {
Parameters::new()
.with("string", ExactMatcher::of(STRING))
.with("pattern", ExactMatcher::of(STRING))
.with("replacement", ExactMatcher::of(STRING))
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(STRING)
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
#[derive(Default, Clone)]
pub struct RegexpSplit;
impl FunctionDef for RegexpSplit {
fn name(&self) -> &'static str {
"regexp_split"
}
fn parameters(&self) -> Parameters {
Parameters::new()
.with("string", ExactMatcher::of(STRING))
.with("pattern", ExactMatcher::of(STRING))
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(Array::new(STRING).into())
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
fn validate_pattern(
name: &str,
mut binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
if let ExpressionKind::StringLiteral(pattern) = &binding.get_by_name("pattern")?.ast.kind {
let value =
super::regex_pattern::compile(name, &pattern.value, matches!(name, "~*" | "!~*"))?;
let mut expression = (**binding.get_by_name("pattern")?).clone();
let mut ast = (*expression.ast).clone();
ast.kind = StringLiteral { value }.into();
expression.ast = Arc::new(ast);
binding.replace_by_name("pattern", Arc::new(expression))?;
}
Ok(binding)
}
#[derive(Default, Clone)]
pub struct RegexMatch<const CASE_INSENSITIVE: bool, const NEGATED: bool>;
impl<const CASE_INSENSITIVE: bool, const NEGATED: bool> FunctionDef
for RegexMatch<CASE_INSENSITIVE, NEGATED>
{
fn name(&self) -> &'static str {
match (CASE_INSENSITIVE, NEGATED) {
(false, false) => "~",
(false, true) => "!~",
(true, false) => "~*",
(true, true) => "!~*",
}
}
fn parameters(&self) -> Parameters {
RegexpLike.parameters()
}
fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
Ok(BOOLEAN)
}
fn refine_binding(
&self,
binding: ParameterBinding<Arc<TypedExpression>>,
) -> anyhow::Result<ParameterBinding<Arc<TypedExpression>>> {
validate_pattern(self.name(), binding)
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}