Skip to main content

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 serde::{Deserialize, Serialize};
19
20mod id;
21mod interpret;
22mod linear;
23mod relation;
24mod scalar;
25
26pub mod explain;
27pub mod row;
28pub mod virtual_syntax;
29pub mod visit;
30
31pub use id::{Id, LocalId, SourceInstanceId};
32pub use interpret::{ColumnSpec, ColumnSpecs, Interpreter, ResultSpec, Trace, TraceSummary};
33pub use linear::plan::{MfpPlan, SafeMfpPlan};
34pub use linear::util::{join_permutations, permutation_for_arrangement};
35pub use linear::{MapFilterProject, memoize_expr};
36pub use relation::func::REPEAT_ROW_NAME;
37pub use relation::func::order_aggregate_datums as order_aggregate_datums_exported_for_benchmarking;
38pub use relation::func::{
39    AggregateFunc, AnalyzedRegex, AnalyzedRegexOpts, CaptureGroupDesc, LagLeadType,
40    NaiveOneByOneAggr, OneByOneAggr, TableFunc,
41};
42pub use relation::join_input_mapper::JoinInputMapper;
43pub use relation::{
44    AccessStrategy, AggregateExpr, CollectionPlan, ColumnOrder, JoinImplementation,
45    JoinInputCharacteristics, LetRecLimit, MirRelationExpr, RECURSION_LIMIT, RowComparator,
46    RowSetFinishing, RowSetFinishingIncremental, WindowFrame, WindowFrameBound, WindowFrameUnits,
47    canonicalize, compare_columns, non_nullable_columns,
48};
49pub use scalar::func::{self, BinaryFunc, UnaryFunc, UnmaterializableFunc, VariadicFunc};
50pub use scalar::{
51    EvalError, FilterCharacteristics, MirScalarExpr, ProtoDomainLimit, ProtoEvalError, like_pattern,
52};
53
54/// A [`MirRelationExpr`] that claims to have been optimized, e.g., by an
55/// `transform::Optimizer`.
56#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
57pub struct OptimizedMirRelationExpr(pub MirRelationExpr);
58
59impl OptimizedMirRelationExpr {
60    /// Declare that the input `expr` is optimized, without actually running it
61    /// through an optimizer. This can be useful to mark as optimized literal
62    /// `MirRelationExpr`s that are obviously optimal, without invoking the whole
63    /// machinery of the optimizer.
64    pub fn declare_optimized(expr: MirRelationExpr) -> OptimizedMirRelationExpr {
65        OptimizedMirRelationExpr(expr)
66    }
67
68    /// Get mutable access to the inner [MirRelationExpr]
69    ///
70    /// Callers of this method need to ensure that the underlying expression stays optimized after
71    /// any mutations are applied
72    pub fn as_inner(&self) -> &MirRelationExpr {
73        &self.0
74    }
75
76    /// Get mutable access to the inner [MirRelationExpr]
77    ///
78    /// Callers of this method need to ensure that the underlying expression stays optimized after
79    /// any mutations are applied
80    pub fn as_inner_mut(&mut self) -> &mut MirRelationExpr {
81        &mut self.0
82    }
83
84    pub fn into_inner(self) -> MirRelationExpr {
85        self.0
86    }
87}
88
89impl Deref for OptimizedMirRelationExpr {
90    type Target = MirRelationExpr;
91
92    fn deref(&self) -> &Self::Target {
93        &self.0
94    }
95}
96
97impl CollectionPlan for OptimizedMirRelationExpr {
98    fn depends_on_into(&self, out: &mut BTreeSet<GlobalId>) {
99        self.0.depends_on_into(out)
100    }
101}