|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! This module contains predicate expressions. |
| 19 | +//! Predicate expressions are used to filter data, and evaluates to a boolean value. For example, |
| 20 | +//! `a > 10` is a predicate expression, and it evaluates to `true` if `a` is greater than `10`, |
| 21 | +
|
| 22 | +use crate::expr::{BoundReference, PredicateOperator, UnboundReference}; |
| 23 | +use crate::spec::Literal; |
| 24 | +use std::collections::HashSet; |
| 25 | + |
| 26 | +/// Logical expression, such as `AND`, `OR`, `NOT`. |
| 27 | +pub struct LogicalExpression<T, const N: usize> { |
| 28 | + inputs: [Box<T>; N], |
| 29 | +} |
| 30 | + |
| 31 | +/// Unary predicate, for example, `a IS NULL`. |
| 32 | +pub struct UnaryExpression<T> { |
| 33 | + /// Operator of this predicate, must be single operand operator. |
| 34 | + op: PredicateOperator, |
| 35 | + /// Term of this predicate, for example, `a` in `a IS NULL`. |
| 36 | + term: T, |
| 37 | +} |
| 38 | + |
| 39 | +/// Binary predicate, for example, `a > 10`. |
| 40 | +pub struct BinaryExpression<T> { |
| 41 | + /// Operator of this predicate, must be binary operator, such as `=`, `>`, `<`, etc. |
| 42 | + op: PredicateOperator, |
| 43 | + /// Term of this predicate, for example, `a` in `a > 10`. |
| 44 | + term: T, |
| 45 | + /// Literal of this predicate, for example, `10` in `a > 10`. |
| 46 | + literal: Literal, |
| 47 | +} |
| 48 | + |
| 49 | +/// Set predicates, for example, `a in (1, 2, 3)`. |
| 50 | +pub struct SetExpression<T> { |
| 51 | + /// Operator of this predicate, must be set operator, such as `IN`, `NOT IN`, etc. |
| 52 | + op: PredicateOperator, |
| 53 | + /// Term of this predicate, for example, `a` in `a in (1, 2, 3)`. |
| 54 | + term: T, |
| 55 | + /// Literals of this predicate, for example, `(1, 2, 3)` in `a in (1, 2, 3)`. |
| 56 | + literals: HashSet<Literal>, |
| 57 | +} |
| 58 | + |
| 59 | +/// Unbound predicate expression before binding to a schema. |
| 60 | +pub enum UnboundPredicate { |
| 61 | + /// And predicate, for example, `a > 10 AND b < 20`. |
| 62 | + And(LogicalExpression<UnboundPredicate, 2>), |
| 63 | + /// Or predicate, for example, `a > 10 OR b < 20`. |
| 64 | + Or(LogicalExpression<UnboundPredicate, 2>), |
| 65 | + /// Not predicate, for example, `NOT (a > 10)`. |
| 66 | + Not(LogicalExpression<UnboundPredicate, 1>), |
| 67 | + /// Unary expression, for example, `a IS NULL`. |
| 68 | + Unary(UnaryExpression<UnboundReference>), |
| 69 | + /// Binary expression, for example, `a > 10`. |
| 70 | + Binary(BinaryExpression<UnboundReference>), |
| 71 | + /// Set predicates, for example, `a in (1, 2, 3)`. |
| 72 | + Set(SetExpression<UnboundReference>), |
| 73 | +} |
| 74 | + |
| 75 | +/// Bound predicate expression after binding to a schema. |
| 76 | +pub enum BoundPredicate { |
| 77 | + /// An expression always evaluates to true. |
| 78 | + AlwaysTrue, |
| 79 | + /// An expression always evaluates to false. |
| 80 | + AlwaysFalse, |
| 81 | + /// An expression combined by `AND`, for example, `a > 10 AND b < 20`. |
| 82 | + And(LogicalExpression<BoundPredicate, 2>), |
| 83 | + /// An expression combined by `OR`, for example, `a > 10 OR b < 20`. |
| 84 | + Or(LogicalExpression<BoundPredicate, 2>), |
| 85 | + /// An expression combined by `NOT`, for example, `NOT (a > 10)`. |
| 86 | + Not(LogicalExpression<BoundPredicate, 1>), |
| 87 | + /// Unary expression, for example, `a IS NULL`. |
| 88 | + Unary(UnaryExpression<BoundReference>), |
| 89 | + /// Binary expression, for example, `a > 10`. |
| 90 | + Binary(BinaryExpression<BoundReference>), |
| 91 | + /// Set predicates, for example, `a in (1, 2, 3)`. |
| 92 | + Set(SetExpression<BoundReference>), |
| 93 | +} |
0 commit comments