mod case;
mod normalize;
mod ordering;
mod truth_admission;
use crate::db::query::plan::expr::canonicalize::{
case::canonicalize_normalized_bool_case_in_bool_context, truth_admission::TruthWrapperScope,
};
use crate::db::{
QueryError,
query::{plan::expr::Expr, preparation::PreparationWork},
};
#[cfg(feature = "sql")]
pub(in crate::db) use case::case_expansions_are_bounded;
pub(in crate::db) use normalize::is_normalized_bool_expr;
#[cfg(feature = "sql")]
pub(in crate::db) use normalize::simplify_bool_expr_constants;
#[cfg(feature = "sql")]
pub(in crate::db) use truth_admission::scalar_where_truth_condition_is_admitted;
pub(in crate::db) use truth_admission::truth_condition_binary_compare_op;
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) struct CanonicalExpr {
expr: Expr,
}
impl CanonicalExpr {
const fn new(expr: Expr) -> Self {
Self { expr }
}
pub(in crate::db) fn into_expr(self) -> Expr {
self.expr
}
}
pub(in crate::db) fn normalize_bool_expr_artifact(
expr: Expr,
work: &PreparationWork<'_>,
) -> Result<CanonicalExpr, QueryError> {
let expr = normalize::normalize_bool_expr_impl(expr, work)?;
debug_assert!(is_normalized_bool_expr(&expr));
Ok(CanonicalExpr::new(expr))
}
pub(in crate::db) fn normalize_bool_expr(
expr: Expr,
work: &PreparationWork<'_>,
) -> Result<Expr, QueryError> {
normalize_bool_expr_artifact(expr, work).map(CanonicalExpr::into_expr)
}
pub(in crate::db) fn canonicalize_scalar_where_bool_expr_artifact(
expr: Expr,
work: &PreparationWork<'_>,
) -> Result<CanonicalExpr, QueryError> {
canonicalize_bool_expr_artifact(expr, TruthWrapperScope::ScalarWhere, work)
}
pub(in crate::db) fn canonicalize_scalar_where_bool_expr(
expr: Expr,
work: &PreparationWork<'_>,
) -> Result<Expr, QueryError> {
canonicalize_scalar_where_bool_expr_artifact(expr, work).map(CanonicalExpr::into_expr)
}
pub(in crate::db) fn canonicalize_grouped_having_bool_expr_artifact(
expr: Expr,
work: &PreparationWork<'_>,
) -> Result<CanonicalExpr, QueryError> {
canonicalize_bool_expr_artifact(expr, TruthWrapperScope::GroupedHaving, work)
}
pub(in crate::db) fn canonicalize_grouped_having_bool_expr(
expr: Expr,
work: &PreparationWork<'_>,
) -> Result<Expr, QueryError> {
canonicalize_grouped_having_bool_expr_artifact(expr, work).map(CanonicalExpr::into_expr)
}
fn canonicalize_bool_expr_artifact(
expr: Expr,
scope: TruthWrapperScope,
work: &PreparationWork<'_>,
) -> Result<CanonicalExpr, QueryError> {
let expr = normalize_bool_expr(expr, work)?;
debug_assert!(is_normalized_bool_expr(&expr));
let collapse_null = match scope {
TruthWrapperScope::ScalarWhere => true,
TruthWrapperScope::GroupedHaving => false,
};
let expr =
canonicalize_normalized_bool_case_in_bool_context(expr, collapse_null, Some(scope), work)?;
let expr = normalize_bool_expr(expr, work)?;
debug_assert!(is_normalized_bool_expr(&expr));
Ok(CanonicalExpr::new(expr))
}