|
| 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 | +//! Defines physical expressions that can evaluated at runtime during query execution |
| 19 | +
|
| 20 | +use crate::error::{DataFusionError, Result}; |
| 21 | +use crate::physical_plan::{ |
| 22 | + window_functions::BuiltInWindowFunctionExpr, PhysicalExpr, WindowAccumulator, |
| 23 | +}; |
| 24 | +use crate::scalar::ScalarValue; |
| 25 | +use arrow::datatypes::{DataType, Field}; |
| 26 | +use std::any::Any; |
| 27 | +use std::convert::TryFrom; |
| 28 | +use std::sync::Arc; |
| 29 | + |
| 30 | +/// first_value expression |
| 31 | +#[derive(Debug)] |
| 32 | +pub struct FirstValue { |
| 33 | + name: String, |
| 34 | + data_type: DataType, |
| 35 | + expr: Arc<dyn PhysicalExpr>, |
| 36 | +} |
| 37 | + |
| 38 | +impl FirstValue { |
| 39 | + /// Create a new FIRST_VALUE window aggregate function |
| 40 | + pub fn new(expr: Arc<dyn PhysicalExpr>, name: String, data_type: DataType) -> Self { |
| 41 | + Self { |
| 42 | + name, |
| 43 | + data_type, |
| 44 | + expr, |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl BuiltInWindowFunctionExpr for FirstValue { |
| 50 | + /// Return a reference to Any that can be used for downcasting |
| 51 | + fn as_any(&self) -> &dyn Any { |
| 52 | + self |
| 53 | + } |
| 54 | + |
| 55 | + fn field(&self) -> Result<Field> { |
| 56 | + let nullable = true; |
| 57 | + Ok(Field::new(&self.name, self.data_type.clone(), nullable)) |
| 58 | + } |
| 59 | + |
| 60 | + fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> { |
| 61 | + vec![self.expr.clone()] |
| 62 | + } |
| 63 | + |
| 64 | + fn name(&self) -> &str { |
| 65 | + &self.name |
| 66 | + } |
| 67 | + |
| 68 | + fn create_accumulator(&self) -> Result<Box<dyn WindowAccumulator>> { |
| 69 | + Ok(Box::new(NthValueAccumulator::try_new( |
| 70 | + 1, |
| 71 | + self.data_type.clone(), |
| 72 | + )?)) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// sql values start with 1, so we can use 0 to indicate the special last value behavior |
| 77 | +const SPECIAL_SIZE_VALUE_FOR_LAST: u32 = 0; |
| 78 | + |
| 79 | +/// last_value expression |
| 80 | +#[derive(Debug)] |
| 81 | +pub struct LastValue { |
| 82 | + name: String, |
| 83 | + data_type: DataType, |
| 84 | + expr: Arc<dyn PhysicalExpr>, |
| 85 | +} |
| 86 | + |
| 87 | +impl LastValue { |
| 88 | + /// Create a new FIRST_VALUE window aggregate function |
| 89 | + pub fn new(expr: Arc<dyn PhysicalExpr>, name: String, data_type: DataType) -> Self { |
| 90 | + Self { |
| 91 | + name, |
| 92 | + data_type, |
| 93 | + expr, |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl BuiltInWindowFunctionExpr for LastValue { |
| 99 | + /// Return a reference to Any that can be used for downcasting |
| 100 | + fn as_any(&self) -> &dyn Any { |
| 101 | + self |
| 102 | + } |
| 103 | + |
| 104 | + fn field(&self) -> Result<Field> { |
| 105 | + let nullable = true; |
| 106 | + Ok(Field::new(&self.name, self.data_type.clone(), nullable)) |
| 107 | + } |
| 108 | + |
| 109 | + fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> { |
| 110 | + vec![self.expr.clone()] |
| 111 | + } |
| 112 | + |
| 113 | + fn name(&self) -> &str { |
| 114 | + &self.name |
| 115 | + } |
| 116 | + |
| 117 | + fn create_accumulator(&self) -> Result<Box<dyn WindowAccumulator>> { |
| 118 | + Ok(Box::new(NthValueAccumulator::try_new( |
| 119 | + SPECIAL_SIZE_VALUE_FOR_LAST, |
| 120 | + self.data_type.clone(), |
| 121 | + )?)) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/// nth_value expression |
| 126 | +#[derive(Debug)] |
| 127 | +pub struct NthValue { |
| 128 | + name: String, |
| 129 | + n: u32, |
| 130 | + data_type: DataType, |
| 131 | + expr: Arc<dyn PhysicalExpr>, |
| 132 | +} |
| 133 | + |
| 134 | +impl NthValue { |
| 135 | + /// Create a new NTH_VALUE window aggregate function |
| 136 | + pub fn try_new( |
| 137 | + expr: Arc<dyn PhysicalExpr>, |
| 138 | + name: String, |
| 139 | + n: u32, |
| 140 | + data_type: DataType, |
| 141 | + ) -> Result<Self> { |
| 142 | + if n == SPECIAL_SIZE_VALUE_FOR_LAST { |
| 143 | + Err(DataFusionError::Execution( |
| 144 | + "nth_value expect n to be > 0".to_owned(), |
| 145 | + )) |
| 146 | + } else { |
| 147 | + Ok(Self { |
| 148 | + name, |
| 149 | + n, |
| 150 | + data_type, |
| 151 | + expr, |
| 152 | + }) |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +impl BuiltInWindowFunctionExpr for NthValue { |
| 158 | + /// Return a reference to Any that can be used for downcasting |
| 159 | + fn as_any(&self) -> &dyn Any { |
| 160 | + self |
| 161 | + } |
| 162 | + |
| 163 | + fn field(&self) -> Result<Field> { |
| 164 | + let nullable = true; |
| 165 | + Ok(Field::new(&self.name, self.data_type.clone(), nullable)) |
| 166 | + } |
| 167 | + |
| 168 | + fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> { |
| 169 | + vec![self.expr.clone()] |
| 170 | + } |
| 171 | + |
| 172 | + fn name(&self) -> &str { |
| 173 | + &self.name |
| 174 | + } |
| 175 | + |
| 176 | + fn create_accumulator(&self) -> Result<Box<dyn WindowAccumulator>> { |
| 177 | + Ok(Box::new(NthValueAccumulator::try_new( |
| 178 | + self.n, |
| 179 | + self.data_type.clone(), |
| 180 | + )?)) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +#[derive(Debug)] |
| 185 | +struct NthValueAccumulator { |
| 186 | + // n the target nth_value, however we'll reuse it for last_value acc, so when n == 0 it specifically |
| 187 | + // means last; also note that it is totally valid for n to be larger than the number of rows input |
| 188 | + // in which case all the values shall be null |
| 189 | + n: u32, |
| 190 | + offset: u32, |
| 191 | + value: ScalarValue, |
| 192 | +} |
| 193 | + |
| 194 | +impl NthValueAccumulator { |
| 195 | + /// new count accumulator |
| 196 | + pub fn try_new(n: u32, data_type: DataType) -> Result<Self> { |
| 197 | + Ok(Self { |
| 198 | + n, |
| 199 | + offset: 0, |
| 200 | + // null value of that data_type by default |
| 201 | + value: ScalarValue::try_from(&data_type)?, |
| 202 | + }) |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +impl WindowAccumulator for NthValueAccumulator { |
| 207 | + fn scan(&mut self, values: &[ScalarValue]) -> Result<Option<ScalarValue>> { |
| 208 | + if self.n == SPECIAL_SIZE_VALUE_FOR_LAST { |
| 209 | + // for last_value function |
| 210 | + self.value = values[0].clone(); |
| 211 | + } else if self.offset < self.n { |
| 212 | + self.offset += 1; |
| 213 | + if self.offset == self.n { |
| 214 | + self.value = values[0].clone(); |
| 215 | + } |
| 216 | + } |
| 217 | + Ok(None) |
| 218 | + } |
| 219 | + |
| 220 | + fn evaluate(&self) -> Result<Option<ScalarValue>> { |
| 221 | + Ok(Some(self.value.clone())) |
| 222 | + } |
| 223 | +} |
0 commit comments