use crate::tree::{
ast::{expression::Expression, identifier::Identifier},
typed_ast::expression::TypedExpression,
};
mod access;
mod collections;
mod functions;
mod lambda;
mod literal;
mod operators;
mod special;
pub mod command;
pub mod identifier;
pub mod pipeline;
pub mod query;
pub use command::{
agg_command, append_command, distinct_command, drop_command, explode_command, from_command,
join_table_alias, join_table_reference, limit_command, lookup_table_alias,
lookup_table_reference, match_command, nest_command, parse_command, pattern_quantified,
pattern_with_alias, pattern_with_alias_and_quantifier, quantifier_any_number,
quantifier_at_least_one, quantifier_exactly, quantifier_zero_or_one, rows_command,
select_command, set_command, sort_command, suppress_command, table_ref, union_command,
unnest_command, where_command, window_command, within_command, AggCommandBuilder,
AppendCommandBuilder, DistinctCommandBuilder, DropCommandBuilder, ExplodeCommandBuilder,
FromCommandBuilder, JoinCommandBuilder, LimitCommandBuilder, LookupCommandBuilder,
MatchCommandBuilder, NestCommandBuilder, NoClause, ParseCommandBuilder,
ParseCommandBuilderWithPattern, RowsCommandBuilder, SelectCommandBuilder, SetCommandBuilder,
SortCommandBuilder, SuppressCommandBuilder, UnionCommandBuilder, UnnestCommandBuilder,
ValidParseCommandBuilder, WhereCommandBuilder, WindowCommandBuilder, WithClause,
WithinCommandBuilder,
};
pub use identifier::{ident, IdentifierBuilder};
pub use pipeline::{pipeline, pipeline_from_expression, PipelineBuilder};
pub use query::{query, IntoPipelineRc, QueryBuilder, QueryBuilderWithMain};
pub use access::{
field,
index,
FieldLookupBuilder,
IndexAccessBuilder,
};
pub use collections::{
array,
pair,
struct_literal,
tuple,
ArrayLiteralBuilder,
PairLiteralBuilder,
StructLiteralBuilder,
TupleLiteralBuilder,
};
pub use functions::{
call,
FunctionCallBuilder,
};
pub use lambda::{lambda, lambda1, lambda2, LambdaBuilder, LambdaBuilderWithBody};
pub use literal::{
BoolLiteralBuilder, DecimalLiteralBuilder, DoubleLiteralBuilder, IntLiteralBuilder,
IntervalLiteralBuilder, NullLiteralBuilder, StringLiteralBuilder,
};
pub use operators::{
add,
and,
divide,
eq,
gt,
gte,
in_,
is_not_null,
is_null,
lt,
lte,
modulo,
multiply,
ne,
negate,
not,
not_in,
or,
subtract,
BinaryOperatorBuilder,
UnaryPostfixOperatorBuilder,
UnaryPrefixOperatorBuilder,
};
pub use special::{
at,
at_day,
at_hour,
at_minute,
at_month,
at_second,
at_week,
at_year,
cast,
error,
field_ref,
CastBuilder,
ErrorBuilder,
FieldReferenceBuilder,
TsTruncBuilder,
};
pub trait ExpressionBuilder: std::fmt::Debug {
fn build(&self) -> Expression;
}
pub trait IntoExpressionBuilder {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder>;
}
impl IntoExpressionBuilder for i64 {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
Box::new(IntLiteralBuilder::new(self))
}
}
impl IntoExpressionBuilder for i32 {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
Box::new(IntLiteralBuilder::new(self as i64))
}
}
impl IntoExpressionBuilder for f64 {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
Box::new(DoubleLiteralBuilder::new(self))
}
}
impl IntoExpressionBuilder for f32 {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
Box::new(DoubleLiteralBuilder::new(self as f64))
}
}
impl IntoExpressionBuilder for &str {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
Box::new(StringLiteralBuilder::new(self))
}
}
impl IntoExpressionBuilder for String {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
Box::new(StringLiteralBuilder::new(self))
}
}
impl IntoExpressionBuilder for bool {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
Box::new(BoolLiteralBuilder::new(self))
}
}
use std::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
use std::sync::Arc;
impl<T> IntoExpressionBuilder for Range<T>
where
T: ExpressionBuilder + IntoExpressionBuilder,
{
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
use crate::tree::ast::ops::BinaryOp;
Box::new(BinaryOperatorBuilder::new(
BinaryOp::Range,
self.start,
self.end,
))
}
}
impl<T> IntoExpressionBuilder for RangeFrom<T>
where
T: ExpressionBuilder + IntoExpressionBuilder,
{
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
use crate::tree::ast::ops::BinaryOp;
Box::new(BinaryOperatorBuilder::new(
BinaryOp::Range,
self.start,
null(),
))
}
}
impl<T> IntoExpressionBuilder for RangeInclusive<T>
where
T: ExpressionBuilder + IntoExpressionBuilder,
{
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
use crate::tree::ast::ops::BinaryOp;
let (start, end) = self.into_inner();
Box::new(BinaryOperatorBuilder::new(
BinaryOp::RangeInclusive,
start,
end,
))
}
}
impl<T> IntoExpressionBuilder for RangeToInclusive<T>
where
T: ExpressionBuilder + IntoExpressionBuilder,
{
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
use crate::tree::ast::ops::BinaryOp;
Box::new(BinaryOperatorBuilder::new(
BinaryOp::RangeInclusive,
null(),
self.end,
))
}
}
impl<T> IntoExpressionBuilder for RangeTo<T>
where
T: ExpressionBuilder + IntoExpressionBuilder,
{
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
use crate::tree::ast::ops::BinaryOp;
Box::new(BinaryOperatorBuilder::new(
BinaryOp::Range,
null(),
self.end,
))
}
}
impl IntoExpressionBuilder for RangeFull {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
use crate::tree::ast::expression::UnboundRangeLiteral;
use crate::tree::ast::node::Span;
Box::new(ExpressionWrapper(Expression {
span: Span::NONE,
kind: UnboundRangeLiteral.into(),
}))
}
}
impl IntoExpressionBuilder for Box<dyn ExpressionBuilder> {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
self
}
}
impl IntoExpressionBuilder for Expression {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
Box::new(ExpressionWrapper(self))
}
}
impl IntoExpressionBuilder for Arc<Expression> {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
Box::new(ExpressionWrapper((*self).clone()))
}
}
impl IntoExpressionBuilder for TypedExpression {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
Box::new(ExpressionWrapper((*self.ast).clone()))
}
}
impl IntoExpressionBuilder for Identifier {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
match self {
Identifier::Simple(simple) => Box::new(field_ref(simple.as_str())),
Identifier::Compound(compound) => {
let mut result: Box<dyn ExpressionBuilder> =
Box::new(field_ref(compound.first().as_str()));
for part in compound.rest_parts() {
result = Box::new(field(result, part.as_str()));
}
result
}
}
}
}
impl IntoExpressionBuilder for &Identifier {
fn into_expression_builder(self) -> Box<dyn ExpressionBuilder> {
self.clone().into_expression_builder()
}
}
#[derive(Debug)]
struct ExpressionWrapper(Expression);
impl ExpressionBuilder for ExpressionWrapper {
fn build(&self) -> Expression {
self.0.clone()
}
}
pub fn int(value: i64) -> IntLiteralBuilder {
IntLiteralBuilder::new(value)
}
pub fn double(value: f64) -> DoubleLiteralBuilder {
DoubleLiteralBuilder::new(value)
}
pub fn string(value: impl Into<String>) -> StringLiteralBuilder {
StringLiteralBuilder::new(value)
}
pub fn boolean(value: bool) -> BoolLiteralBuilder {
BoolLiteralBuilder::new(value)
}
pub fn null() -> NullLiteralBuilder {
NullLiteralBuilder::new()
}
pub fn decimal(value: &str) -> Result<DecimalLiteralBuilder, String> {
DecimalLiteralBuilder::from_string(value)
}
pub fn decimal_from_parts(
unscaled_value: i128,
precision: u32,
scale: u32,
) -> DecimalLiteralBuilder {
DecimalLiteralBuilder::new(unscaled_value, precision, scale)
}
use crate::tree::ast::expression::IntervalUnit;
pub fn interval(value: i64, unit: IntervalUnit) -> IntervalLiteralBuilder {
IntervalLiteralBuilder::new(value, unit)
}
pub fn milliseconds(value: i64) -> IntervalLiteralBuilder {
IntervalLiteralBuilder::new(value, IntervalUnit::Millisecond)
}
pub fn seconds(value: i64) -> IntervalLiteralBuilder {
IntervalLiteralBuilder::new(value, IntervalUnit::Second)
}
pub fn minutes(value: i64) -> IntervalLiteralBuilder {
IntervalLiteralBuilder::new(value, IntervalUnit::Minute)
}
pub fn hours(value: i64) -> IntervalLiteralBuilder {
IntervalLiteralBuilder::new(value, IntervalUnit::Hour)
}
pub fn days(value: i64) -> IntervalLiteralBuilder {
IntervalLiteralBuilder::new(value, IntervalUnit::Day)
}
pub fn weeks(value: i64) -> IntervalLiteralBuilder {
IntervalLiteralBuilder::new(value, IntervalUnit::Week)
}
pub fn months(value: i64) -> IntervalLiteralBuilder {
IntervalLiteralBuilder::new(value, IntervalUnit::Month)
}
pub fn quarters(value: i64) -> IntervalLiteralBuilder {
IntervalLiteralBuilder::new(value, IntervalUnit::Quarter)
}
pub fn years(value: i64) -> IntervalLiteralBuilder {
IntervalLiteralBuilder::new(value, IntervalUnit::Year)
}
#[cfg(test)]
mod tests;