Skip to main content

vlorql_core/function/
def.rs

1//! Core data types for the function registry.
2//!
3//! Defines [`FunctionKind`], [`Dialect`], and [`FunctionDef`] – the
4//! building blocks used by the builder, registry, and all downstream
5//! consumers.
6
7use std::borrow::Cow;
8
9use crate::schema::DataType;
10
11/// The broad category of a SQL function.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13pub enum FunctionKind {
14    /// Scalar function (e.g. `UPPER`, `LENGTH`).
15    Scalar,
16    /// Aggregate function (e.g. `SUM`, `COUNT`).
17    Aggregate,
18    /// Window function (e.g. `ROW_NUMBER`).
19    Window,
20}
21
22/// Target SQL dialect for function availability.
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
24pub enum Dialect {
25    /// Available in all dialects (generic).
26    Generic,
27    /// PostgreSQL.
28    Postgres,
29    /// MySQL.
30    MySql,
31    /// SQLite.
32    Sqlite,
33}
34
35/// Metadata for a SQL function.
36#[derive(Debug, Clone)]
37pub struct FunctionDef {
38    /// All accepted names (first is the canonical name).
39    pub names: Vec<Cow<'static, str>>,
40    /// The category of the function.
41    pub kind: FunctionKind,
42    /// Minimum number of arguments (inclusive).
43    pub min_args: usize,
44    /// Maximum number of arguments (`None` = unlimited).
45    pub max_args: Option<usize>,
46    /// Expected parameter types for type-checking (`None` = skip check).
47    pub param_types: Option<Vec<Option<DataType>>>,
48    /// Return type for type inference (`None` = unknown).
49    pub return_type: Option<DataType>,
50    /// Whether `DISTINCT` is allowed (e.g. `COUNT(DISTINCT col)`).
51    pub supports_distinct: bool,
52    /// Whether an `ORDER BY` child clause is allowed.
53    pub supports_order_by: bool,
54    /// Whether `*` is accepted as an argument (e.g. `COUNT(*)`).
55    pub allows_star: bool,
56    /// Dialects this function is available in. Empty = all dialects.
57    pub dialects: Vec<Dialect>,
58}
59
60impl FunctionDef {
61    /// Returns the canonical (first) name.
62    pub fn canonical_name(&self) -> &str {
63        self.names.first().map_or("", |n| n.as_ref())
64    }
65
66    /// Returns `true` if `name` matches the canonical name or any alias.
67    pub fn accepts_name(&self, name: &str) -> bool {
68        self.names.iter().any(|n| n.eq_ignore_ascii_case(name))
69    }
70
71    /// Returns `true` if this function is available for the given dialect.
72    pub fn supports_dialect(&self, dialect: Dialect) -> bool {
73        self.dialects.is_empty()
74            || self.dialects.contains(&Dialect::Generic)
75            || self.dialects.contains(&dialect)
76    }
77}