MCPcopy Create free account
hub / github.com/apache/datafusion / create_placeholder_expr

Method create_placeholder_expr

datafusion/sql/src/expr/value.rs:106–149  ·  view source on GitHub ↗

Create a placeholder expression Both named (`$foo`) and positional (`$1`, `$2`, ...) placeholder styles are supported.

(
        param: String,
        param_data_types: &[Option<FieldRef>],
    )

Source from the content-addressed store, hash-verified

104 /// Create a placeholder expression
105 /// Both named (`$foo`) and positional (`$1`, `$2`, ...) placeholder styles are supported.
106 fn create_placeholder_expr(
107 param: String,
108 param_data_types: &[Option<FieldRef>],
109 ) -> Result<Expr> {
110 // Try to parse the placeholder as a number. If the placeholder does not have a valid
111 // positional value, assume we have a named placeholder.
112 let index = param[1..].parse::<usize>();
113 let idx = match index {
114 Ok(0) => {
115 return plan_err!(
116 "Invalid placeholder, zero is not a valid index: {param}"
117 );
118 }
119 Ok(index) => index - 1,
120 Err(_) => {
121 return if param_data_types.is_empty() {
122 Ok(Expr::Placeholder(Placeholder::new_with_field(param, None)))
123 } else {
124 // FIXME: This branch is shared by params from PREPARE and CREATE FUNCTION, but
125 // only CREATE FUNCTION currently supports named params. For now, we rewrite
126 // these to positional params.
127 let named_param_pos = param_data_types.iter().position(|v| {
128 v.as_ref().is_some_and(|field| field.name() == &param[1..])
129 });
130 match named_param_pos {
131 Some(pos) => Ok(Expr::Placeholder(Placeholder::new_with_field(
132 format!("${}", pos + 1),
133 param_data_types.get(pos).and_then(|v| v.clone()),
134 ))),
135 None => plan_err!("Unknown placeholder: {param}"),
136 }
137 };
138 }
139 };
140 // Check if the placeholder is in the parameter list
141 // FIXME: In the CREATE FUNCTION branch, param_type = None should raise an error
142 let param_type = param_data_types.get(idx).and_then(|v| v.clone());
143 // Data type of the parameter
144 debug!("type of param {param} param_data_types[idx]: {param_type:?}");
145
146 Ok(Expr::Placeholder(Placeholder::new_with_field(
147 param, param_type,
148 )))
149 }
150
151 pub(super) fn sql_array_literal(
152 &self,

Callers

nothing calls this directly

Calls 7

PlaceholderClass · 0.85
is_emptyMethod · 0.45
iterMethod · 0.45
as_refMethod · 0.45
nameMethod · 0.45
getMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected