|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use databend_common_column::bitmap::Bitmap; |
| 16 | + |
| 17 | +use super::ConstantFolder; |
| 18 | +use crate::Column; |
| 19 | +use crate::ColumnBuilder; |
| 20 | +use crate::ColumnIndex; |
| 21 | +use crate::EvalContext; |
| 22 | +use crate::Expr; |
| 23 | +use crate::Value; |
| 24 | +use crate::function::ScalarFunction; |
| 25 | +use crate::property::Domain; |
| 26 | +use crate::types::boolean::BooleanDomain; |
| 27 | +use crate::types::nullable::NullableDomain; |
| 28 | +use crate::types::string::StringDomain; |
| 29 | +use crate::types::*; |
| 30 | + |
| 31 | +impl<'a, Index: ColumnIndex> ConstantFolder<'a, Index> { |
| 32 | + pub(super) fn is_monotonic(&self, function_name: &str, args: &[Expr<Index>]) -> bool { |
| 33 | + self.fn_registry |
| 34 | + .properties |
| 35 | + .get(function_name) |
| 36 | + .is_some_and(|property| { |
| 37 | + if let [arg] = args { |
| 38 | + property.monotonicity |
| 39 | + || property.monotonicity_by_type.contains(&arg.data_type()) |
| 40 | + } else { |
| 41 | + false |
| 42 | + } |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + pub(super) fn calculate_monotonicity_domain( |
| 47 | + &self, |
| 48 | + return_type: &DataType, |
| 49 | + input_domain: &Domain, |
| 50 | + generics: &[DataType], |
| 51 | + eval: &dyn ScalarFunction, |
| 52 | + ) -> Option<Domain> { |
| 53 | + let input = input_domain.boundary_column()?; |
| 54 | + let mut ctx = EvalContext { |
| 55 | + generics, |
| 56 | + num_rows: 2, |
| 57 | + validity: None, |
| 58 | + errors: None, |
| 59 | + func_ctx: self.func_ctx, |
| 60 | + suppress_error: false, |
| 61 | + strict_eval: true, |
| 62 | + }; |
| 63 | + let Value::Column(col) = eval.eval(&[Value::Column(input)], &mut ctx) else { |
| 64 | + return None; |
| 65 | + }; |
| 66 | + |
| 67 | + // if error happens, domain maybe incorrect |
| 68 | + // min, max: String("2024-09-02 00:00") String("2024-09-02 00:0�") |
| 69 | + // to_date(s) > to_date('2024-01-1') |
| 70 | + let domain = if ctx.has_error(0) || ctx.has_error(1) { |
| 71 | + // Preserve the successful boundary and widen only the failed side. |
| 72 | + // For example, a malformed minimum string must not discard a valid |
| 73 | + // maximum timestamp that can still prove an upper bound. |
| 74 | + // This assumes all currently registered monotonic functions are |
| 75 | + // non-decreasing. Supporting a decreasing function here requires |
| 76 | + // recording its direction in `FunctionProperty` and reversing the |
| 77 | + // fallback boundary. |
| 78 | + let full_domain = Domain::full(return_type); |
| 79 | + let Some(fallback) = full_domain.boundary_column() else { |
| 80 | + return Some(full_domain); |
| 81 | + }; |
| 82 | + let mut builder = ColumnBuilder::with_capacity(return_type, 2); |
| 83 | + for (index, (value, fallback)) in col.iter().zip(fallback.iter()).enumerate() { |
| 84 | + if ctx.has_error(index) { |
| 85 | + builder.push(fallback); |
| 86 | + } else { |
| 87 | + builder.push(value); |
| 88 | + } |
| 89 | + } |
| 90 | + builder.build().domain() |
| 91 | + } else { |
| 92 | + col.domain() |
| 93 | + }; |
| 94 | + |
| 95 | + if !return_type.is_nullable_or_null() { |
| 96 | + return Some(domain); |
| 97 | + } |
| 98 | + |
| 99 | + Some(match domain { |
| 100 | + Domain::Nullable(mut domain) => { |
| 101 | + domain.has_null = true; |
| 102 | + Domain::Nullable(domain) |
| 103 | + } |
| 104 | + domain => Domain::Nullable(NullableDomain { |
| 105 | + has_null: true, |
| 106 | + value: Some(Box::new(domain)), |
| 107 | + }), |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl Domain { |
| 113 | + /// Materialize the finite, non-NULL endpoints used for monotonicity probing. |
| 114 | + fn boundary_column(&self) -> Option<Column> { |
| 115 | + Some(match self { |
| 116 | + Domain::Number(domain) => crate::with_number_type!(|NUM| match domain { |
| 117 | + NumberDomain::NUM(SimpleDomain { min, max }) => |
| 118 | + Column::Number(NumberColumn::NUM(Buffer::from(vec![*min, *max])),), |
| 119 | + }), |
| 120 | + Domain::Decimal(domain) => crate::with_decimal_type!(|DECIMAL| match domain { |
| 121 | + DecimalDomain::DECIMAL(SimpleDomain { min, max }, size) => Column::Decimal( |
| 122 | + DecimalColumn::DECIMAL(Buffer::from(vec![*min, *max]), *size), |
| 123 | + ), |
| 124 | + }), |
| 125 | + Domain::Boolean(BooleanDomain { |
| 126 | + has_false, |
| 127 | + has_true, |
| 128 | + }) => match (*has_false, *has_true) { |
| 129 | + (true, true) => Column::Boolean(Bitmap::from([false, true])), |
| 130 | + (true, false) => Column::Boolean(Bitmap::from([false, false])), |
| 131 | + (false, true) => Column::Boolean(Bitmap::from([true, true])), |
| 132 | + (false, false) => return None, |
| 133 | + }, |
| 134 | + Domain::String(StringDomain { |
| 135 | + min, |
| 136 | + max: Some(max), |
| 137 | + }) => Column::String(StringColumn::from_slice([min.as_str(), max.as_str()])), |
| 138 | + Domain::Timestamp(SimpleDomain { min, max }) => { |
| 139 | + Column::Timestamp(Buffer::from(vec![*min, *max])) |
| 140 | + } |
| 141 | + Domain::TimestampTz(SimpleDomain { min, max }) => { |
| 142 | + Column::TimestampTz(Buffer::from(vec![*min, *max])) |
| 143 | + } |
| 144 | + Domain::Date(SimpleDomain { min, max }) => Column::Date(Buffer::from(vec![*min, *max])), |
| 145 | + Domain::Interval(SimpleDomain { min, max }) => { |
| 146 | + Column::Interval(Buffer::from(vec![*min, *max])) |
| 147 | + } |
| 148 | + Domain::Nullable(NullableDomain { |
| 149 | + value: Some(domain), |
| 150 | + .. |
| 151 | + }) => NullableColumn::new_column(domain.boundary_column()?, Bitmap::new_trued(2)), |
| 152 | + _ => return None, |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
0 commit comments