pub use crate::row_stream::{SQLRowSource, SQLRowStream};
use uqa_core::Value;
use uqa_sql::SQLError;
pub use uqa_sql::ast::FunctionVolatility as SQLFunctionVolatility;
#[derive(Clone, Default)]
pub struct SQLFunctionContext(Option<std::sync::Arc<dyn std::any::Any + Send + Sync>>);
impl SQLFunctionContext {
#[must_use]
pub const fn empty() -> Self {
Self(None)
}
#[must_use]
pub fn new<T: Send + Sync + 'static>(data: T) -> Self {
Self(Some(std::sync::Arc::new(data)))
}
#[must_use]
pub fn get<T: 'static>(&self) -> Option<&T> {
self.0.as_ref()?.downcast_ref()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SQLFunctionOptions {
pub volatility: SQLFunctionVolatility,
pub may_mutate_engine: bool,
}
impl SQLFunctionOptions {
#[must_use]
pub const fn new(volatility: SQLFunctionVolatility, may_mutate_engine: bool) -> Self {
Self {
volatility,
may_mutate_engine,
}
}
#[must_use]
pub const fn read_only(volatility: SQLFunctionVolatility) -> Self {
Self::new(volatility, false)
}
}
impl Default for SQLFunctionOptions {
fn default() -> Self {
Self::new(SQLFunctionVolatility::Volatile, true)
}
}
pub struct RegisteredSQLFunction<F: ?Sized> {
pub function: std::sync::Arc<F>,
pub options: SQLFunctionOptions,
}
impl<F: ?Sized> RegisteredSQLFunction<F> {
pub fn new(function: std::sync::Arc<F>, options: SQLFunctionOptions) -> Self {
Self { function, options }
}
}
impl<F: ?Sized> Clone for RegisteredSQLFunction<F> {
fn clone(&self) -> Self {
Self {
function: self.function.clone(),
options: self.options,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct SQLTableFunctionResult {
pub columns: Vec<String>,
pub rows: Vec<Vec<Value>>,
}
impl SQLTableFunctionResult {
pub fn new(
columns: impl IntoIterator<Item = impl Into<String>>,
rows: Vec<Vec<Value>>,
) -> Self {
Self {
columns: columns.into_iter().map(Into::into).collect(),
rows,
}
}
}
pub struct SQLTableFunctionStream {
pub columns: Vec<String>,
pub rows: SQLRowStream<'static, Vec<Value>>,
}
impl SQLTableFunctionStream {
pub fn new<I>(columns: impl IntoIterator<Item = impl Into<String>>, rows: I) -> Self
where
I: Iterator<Item = Result<Vec<Value>, SQLError>> + Send + 'static,
{
Self {
columns: columns.into_iter().map(Into::into).collect(),
rows: SQLRowStream::new(rows),
}
}
pub fn from_source(
columns: impl IntoIterator<Item = impl Into<String>>,
rows: impl SQLRowSource<Vec<Value>> + 'static,
) -> Self {
Self {
columns: columns.into_iter().map(Into::into).collect(),
rows: SQLRowStream::from_source(rows),
}
}
}
impl From<SQLTableFunctionResult> for SQLTableFunctionStream {
fn from(result: SQLTableFunctionResult) -> Self {
Self {
columns: result.columns,
rows: SQLRowStream::new(result.rows.into_iter().map(Ok)),
}
}
}
pub trait SQLScalarFunction: Send + Sync {
fn call(&self, _args: &[Value]) -> Result<Value, SQLError> {
Err(SQLError::Unsupported(
"scalar function requires an invocation context".into(),
))
}
fn call_with_context(
&self,
_context: &SQLFunctionContext,
args: &[Value],
) -> Result<Value, SQLError> {
self.call(args)
}
}
impl<F> SQLScalarFunction for F
where
F: Fn(&[Value]) -> Result<Value, SQLError> + Send + Sync,
{
fn call(&self, args: &[Value]) -> Result<Value, SQLError> {
self(args)
}
}
pub trait SQLTableFunction: Send + Sync {
fn output_columns(&self) -> Option<&[String]> {
None
}
fn call(&self, _args: &[Value]) -> Result<SQLTableFunctionResult, SQLError> {
Err(SQLError::Unsupported(
"table function implements only the streaming call interface".into(),
))
}
fn call_stream(&self, args: &[Value]) -> Result<SQLTableFunctionStream, SQLError> {
self.call(args).map(Into::into)
}
fn call_with_context(
&self,
_context: &SQLFunctionContext,
args: &[Value],
) -> Result<SQLTableFunctionResult, SQLError> {
self.call(args)
}
fn call_stream_with_context(
&self,
_context: &SQLFunctionContext,
args: &[Value],
) -> Result<SQLTableFunctionStream, SQLError> {
self.call_stream(args)
}
}
impl<F> SQLTableFunction for F
where
F: Fn(&[Value]) -> Result<SQLTableFunctionResult, SQLError> + Send + Sync,
{
fn call(&self, args: &[Value]) -> Result<SQLTableFunctionResult, SQLError> {
self(args)
}
}
pub trait SQLAggregateFunction: Send + Sync {
fn create_state(&self) -> Box<dyn SQLAggregateState>;
}
pub trait SQLAggregateFactory: Send + Sync {
fn try_create_state(&self) -> Result<Box<dyn SQLAggregateState>, SQLError> {
Err(SQLError::Unsupported(
"aggregate factory requires an invocation context".into(),
))
}
fn try_create_state_with_context(
&self,
_context: &SQLFunctionContext,
) -> Result<Box<dyn SQLAggregateState>, SQLError> {
self.try_create_state()
}
}
impl<F: SQLAggregateFunction + ?Sized> SQLAggregateFactory for F {
fn try_create_state(&self) -> Result<Box<dyn SQLAggregateState>, SQLError> {
Ok(self.create_state())
}
}
impl<F, S> SQLAggregateFunction for F
where
F: Fn() -> S + Send + Sync,
S: SQLAggregateState + 'static,
{
fn create_state(&self) -> Box<dyn SQLAggregateState> {
Box::new(self())
}
}
pub trait SQLAggregateState: Send {
fn observe(&mut self, args: &[Value]) -> Result<(), SQLError>;
fn finish(&self) -> Result<Value, SQLError>;
fn close(&mut self) -> Result<(), SQLError> {
Ok(())
}
}
pub trait AggregateFunctionRegistry:
uqa_sql::FunctionTypeResolver + uqa_sql::plan::AggregateClassifier
{
fn registered_aggregate_function(
&self,
name: &str,
) -> Option<std::sync::Arc<dyn SQLAggregateFactory>>;
}