|
| 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 | +//! Placeholder expression. |
| 19 | +
|
| 20 | +use std::{ |
| 21 | + any::Any, |
| 22 | + fmt::{self, Formatter}, |
| 23 | + sync::Arc, |
| 24 | +}; |
| 25 | + |
| 26 | +use arrow::{ |
| 27 | + array::RecordBatch, |
| 28 | + datatypes::{DataType, Field, FieldRef, Schema}, |
| 29 | +}; |
| 30 | +use datafusion_common::{ |
| 31 | + DataFusionError, Result, exec_datafusion_err, tree_node::TreeNode, |
| 32 | +}; |
| 33 | +use datafusion_expr::ColumnarValue; |
| 34 | +use datafusion_physical_expr_common::physical_expr::PhysicalExpr; |
| 35 | +use std::hash::Hash; |
| 36 | + |
| 37 | +/// Physical expression representing a placeholder parameter (e.g., $1, $2, or named parameters) in |
| 38 | +/// the physical plan. |
| 39 | +/// |
| 40 | +/// This expression serves as a placeholder that will be resolved to a literal value during |
| 41 | +/// execution. It should not be evaluated directly. |
| 42 | +#[derive(Debug, PartialEq, Eq, Hash)] |
| 43 | +pub struct PlaceholderExpr { |
| 44 | + /// Placeholder id, e.g. $1 or $a. |
| 45 | + pub id: String, |
| 46 | + /// Derived from expression where placeholder is met. |
| 47 | + pub field: Option<FieldRef>, |
| 48 | +} |
| 49 | + |
| 50 | +impl PlaceholderExpr { |
| 51 | + /// Create a new placeholder expression. |
| 52 | + pub fn new(id: String, data_type: DataType) -> Self { |
| 53 | + let field = Arc::new(Field::new("", data_type, true)); |
| 54 | + Self::new_with_field(id, field) |
| 55 | + } |
| 56 | + |
| 57 | + /// Create a new placeholders expression from a field. |
| 58 | + pub fn new_with_field(id: String, field: FieldRef) -> Self { |
| 59 | + Self { |
| 60 | + id, |
| 61 | + field: Some(field), |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// Create a new placeholder expression without a specified data type. |
| 66 | + pub fn new_without_data_type(id: String) -> Self { |
| 67 | + Self { id, field: None } |
| 68 | + } |
| 69 | + |
| 70 | + fn execution_error(&self) -> DataFusionError { |
| 71 | + exec_datafusion_err!( |
| 72 | + "Placeholder '{}' was not provided a value for execution.", |
| 73 | + self.id |
| 74 | + ) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// Create a placeholder expression. |
| 79 | +pub fn placeholder<I: Into<String>>(id: I, data_type: DataType) -> Arc<dyn PhysicalExpr> { |
| 80 | + Arc::new(PlaceholderExpr::new(id.into(), data_type)) |
| 81 | +} |
| 82 | + |
| 83 | +/// Returns `true` if expression has placeholders. |
| 84 | +pub fn has_placeholders(expr: &Arc<dyn PhysicalExpr>) -> bool { |
| 85 | + expr.exists(|e| Ok(e.as_any().is::<PlaceholderExpr>())) |
| 86 | + .expect("do not return errors") |
| 87 | +} |
| 88 | + |
| 89 | +impl fmt::Display for PlaceholderExpr { |
| 90 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 91 | + write!(f, "{}", self.id) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl PhysicalExpr for PlaceholderExpr { |
| 96 | + fn as_any(&self) -> &dyn Any { |
| 97 | + self |
| 98 | + } |
| 99 | + |
| 100 | + fn return_field(&self, _input_schema: &Schema) -> Result<FieldRef> { |
| 101 | + self.field |
| 102 | + .as_ref() |
| 103 | + .map(Arc::clone) |
| 104 | + .ok_or_else(|| self.execution_error()) |
| 105 | + } |
| 106 | + |
| 107 | + fn evaluate(&self, _batch: &RecordBatch) -> Result<ColumnarValue> { |
| 108 | + Err(self.execution_error()) |
| 109 | + } |
| 110 | + |
| 111 | + fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> { |
| 112 | + vec![] |
| 113 | + } |
| 114 | + |
| 115 | + fn with_new_children( |
| 116 | + self: Arc<Self>, |
| 117 | + children: Vec<Arc<dyn PhysicalExpr>>, |
| 118 | + ) -> Result<Arc<dyn PhysicalExpr>> { |
| 119 | + assert!(children.is_empty()); |
| 120 | + Ok(self) |
| 121 | + } |
| 122 | + |
| 123 | + fn fmt_sql(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 124 | + fmt::Display::fmt(self, f) |
| 125 | + } |
| 126 | +} |
0 commit comments