mz_expr/
lib.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Core expression language.
11
12#![warn(missing_debug_implementations)]
13
14use std::collections::BTreeSet;
15use std::ops::Deref;
16
17use mz_repr::GlobalId;
18use proptest_derive::Arbitrary;
19use serde::{Deserialize, Serialize};
20
21mod id;
22mod interpret;
23mod linear;
24mod relation;
25mod scalar;
26
27pub mod explain;
28pub mod row;
29pub mod virtual_syntax;
30pub mod visit;
31
32pub use id::{Id, LocalId, ProtoId, ProtoLocalId, SourceInstanceId};
33pub use interpret::{ColumnSpec, ColumnSpecs, Interpreter, ResultSpec, Trace, TraceSummary};
34pub use linear::plan::{MfpPlan, SafeMfpPlan};
35pub use linear::util::{join_permutations, permutation_for_arrangement};
36pub use linear::{
37    MapFilterProject, ProtoMapFilterProject, ProtoMfpPlan, ProtoSafeMfpPlan, memoize_expr,
38};
39pub use relation::func::order_aggregate_datums as order_aggregate_datums_exported_for_benchmarking;
40pub use relation::func::{
41    AggregateFunc, AnalyzedRegex, AnalyzedRegexOpts, CaptureGroupDesc, LagLeadType,
42    NaiveOneByOneAggr, OneByOneAggr, TableFunc,
43};
44pub use relation::join_input_mapper::JoinInputMapper;
45pub use relation::{
46    AccessStrategy, AggregateExpr, CollectionPlan, ColumnOrder, JoinImplementation,
47    JoinInputCharacteristics, LetRecLimit, MirRelationExpr, ProtoAggregateExpr, ProtoAggregateFunc,
48    ProtoColumnOrder, ProtoRowSetFinishing, ProtoTableFunc, RECURSION_LIMIT, RowSetFinishing,
49    WindowFrame, WindowFrameBound, WindowFrameUnits, canonicalize, compare_columns,
50    non_nullable_columns,
51};
52pub use scalar::func::{self, BinaryFunc, UnaryFunc, UnmaterializableFunc, VariadicFunc};
53pub use scalar::{
54    EvalError, FilterCharacteristics, MirScalarExpr, ProtoDomainLimit, ProtoEvalError,
55    ProtoMirScalarExpr, like_pattern,
56};
57
58/// A [`MirRelationExpr`] that claims to have been optimized, e.g., by an
59/// `transform::Optimizer`.
60#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Arbitrary)]
61pub struct OptimizedMirRelationExpr(pub MirRelationExpr);
62
63impl OptimizedMirRelationExpr {
64    /// Declare that the input `expr` is optimized, without actually running it
65    /// through an optimizer. This can be useful to mark as optimized literal
66    /// `MirRelationExpr`s that are obviously optimal, without invoking the whole
67    /// machinery of the optimizer.
68    pub fn declare_optimized(expr: MirRelationExpr) -> OptimizedMirRelationExpr {
69        OptimizedMirRelationExpr(expr)
70    }
71
72    /// Get mutable access to the inner [MirRelationExpr]
73    ///
74    /// Callers of this method need to ensure that the underlying expression stays optimized after
75    /// any mutations are applied
76    pub fn as_inner(&self) -> &MirRelationExpr {
77        &self.0
78    }
79
80    /// Get mutable access to the inner [MirRelationExpr]
81    ///
82    /// Callers of this method need to ensure that the underlying expression stays optimized after
83    /// any mutations are applied
84    pub fn as_inner_mut(&mut self) -> &mut MirRelationExpr {
85        &mut self.0
86    }
87
88    pub fn into_inner(self) -> MirRelationExpr {
89        self.0
90    }
91}
92
93impl Deref for OptimizedMirRelationExpr {
94    type Target = MirRelationExpr;
95
96    fn deref(&self) -> &Self::Target {
97        &self.0
98    }
99}
100
101impl CollectionPlan for OptimizedMirRelationExpr {
102    fn depends_on_into(&self, out: &mut BTreeSet<GlobalId>) {
103        self.0.depends_on_into(out)
104    }
105}