Skip to main content

mz_expr/scalar/reduce/
unary.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//! Post-order rewrites for `CallUnary` nodes.
11
12use mz_repr::{ReprColumnType, RowArena};
13
14use crate::MirScalarExpr;
15use crate::scalar::func::{self, UnaryFunc, VariadicFunc};
16
17pub(super) fn reduce_call_unary(
18    e: &mut MirScalarExpr,
19    column_types: &[ReprColumnType],
20    temp_storage: &RowArena,
21) {
22    let MirScalarExpr::CallUnary { func, expr } = e else {
23        unreachable!()
24    };
25
26    if expr.is_literal() && *func != UnaryFunc::Panic(func::Panic) {
27        *e = MirScalarExpr::literal(e.eval(&[], temp_storage), e.typ(column_types).scalar_type);
28        return;
29    }
30
31    // `RecordGet(i)(RecordCreate(args))` → `args[i]`.
32    if let UnaryFunc::RecordGet(func::RecordGet(i)) = *func {
33        if let MirScalarExpr::CallVariadic {
34            func: VariadicFunc::RecordCreate(..),
35            exprs,
36        } = &mut **expr
37        {
38            *e = exprs.swap_remove(i);
39        }
40    }
41}