-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support named query parameters #8384
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
7afeb8b
Minor: Improve the document format of JoinHashMap
Asura7969 6332bec
Merge remote-tracking branch 'origin/main'
Asura7969 cc5e0c7
Merge remote-tracking branch 'origin/main'
Asura7969 a114310
Merge remote-tracking branch 'origin/main'
Asura7969 928c811
Merge remote-tracking branch 'origin/main'
Asura7969 839093e
Merge remote-tracking branch 'origin/main'
Asura7969 a836cde
Merge remote-tracking branch 'origin/main'
Asura7969 5648dc7
Merge branch 'apache:main' into main
Asura7969 a670409
Merge branch 'apache:main' into main
Asura7969 22894a3
Merge branch 'apache:main' into main
Asura7969 73a59d2
Merge branch 'apache:main' into main
Asura7969 46409c2
Merge branch 'apache:main' into main
Asura7969 8a86a4c
Merge branch 'apache:main' into main
Asura7969 cf5c584
Merge branch 'apache:main' into main
Asura7969 62ae9b9
Merge branch 'apache:main' into main
Asura7969 da02fa2
Merge branch 'apache:main' into main
Asura7969 d98eb2e
Merge branch 'apache:main' into main
Asura7969 79e7216
Merge branch 'apache:main' into main
Asura7969 ba51abd
Merge branch 'apache:main' into main
Asura7969 2468f52
Merge branch 'apache:main' into main
Asura7969 180c303
Merge branch 'apache:main' into main
Asura7969 68980ba
Merge branch 'apache:main' into main
Asura7969 9411940
Merge branch 'apache:main' into main
Asura7969 ba28346
Merge branch 'apache:main' into main
Asura7969 df0942f
Merge branch 'apache:main' into main
Asura7969 d6381c2
support named query parameters
Asura7969 69a2080
cargo fmt
Asura7969 49e617e
add `ParamValues` conversion
Asura7969 5b8c3b7
improve doc
Asura7969 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use crate::error::{_internal_err, _plan_err}; | ||
use crate::{DataFusionError, Result, ScalarValue}; | ||
use arrow_schema::DataType; | ||
use std::collections::HashMap; | ||
|
||
/// The parameter value corresponding to the placeholder | ||
#[derive(Debug, Clone)] | ||
pub enum ParamValues { | ||
/// for positional query parameters, like select * from test where a > $1 and b = $2 | ||
LIST(Vec<ScalarValue>), | ||
/// for named query parameters, like select * from test where a > $foo and b = $goo | ||
MAP(HashMap<String, ScalarValue>), | ||
} | ||
|
||
impl ParamValues { | ||
/// Verify parameter list length and type | ||
pub fn verify(&self, expect: &Vec<DataType>) -> Result<()> { | ||
match self { | ||
ParamValues::LIST(list) => { | ||
// Verify if the number of params matches the number of values | ||
if expect.len() != list.len() { | ||
return _plan_err!( | ||
"Expected {} parameters, got {}", | ||
expect.len(), | ||
list.len() | ||
); | ||
} | ||
|
||
// Verify if the types of the params matches the types of the values | ||
let iter = expect.iter().zip(list.iter()); | ||
for (i, (param_type, value)) in iter.enumerate() { | ||
if *param_type != value.data_type() { | ||
return _plan_err!( | ||
"Expected parameter of type {:?}, got {:?} at index {}", | ||
param_type, | ||
value.data_type(), | ||
i | ||
); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
ParamValues::MAP(_) => { | ||
// If it is a named query, variables can be reused, | ||
// but the lengths are not necessarily equal | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
pub fn get_placeholders_with_values( | ||
&self, | ||
id: &String, | ||
data_type: &Option<DataType>, | ||
) -> Result<ScalarValue> { | ||
match self { | ||
ParamValues::LIST(list) => { | ||
if id.is_empty() || id == "$0" { | ||
return _plan_err!("Empty placeholder id"); | ||
} | ||
// convert id (in format $1, $2, ..) to idx (0, 1, ..) | ||
let idx = id[1..].parse::<usize>().map_err(|e| { | ||
DataFusionError::Internal(format!( | ||
"Failed to parse placeholder id: {e}" | ||
)) | ||
})? - 1; | ||
// value at the idx-th position in param_values should be the value for the placeholder | ||
let value = list.get(idx).ok_or_else(|| { | ||
DataFusionError::Internal(format!( | ||
"No value found for placeholder with id {id}" | ||
)) | ||
})?; | ||
// check if the data type of the value matches the data type of the placeholder | ||
if Some(value.data_type()) != *data_type { | ||
return _internal_err!( | ||
"Placeholder value type mismatch: expected {:?}, got {:?}", | ||
data_type, | ||
value.data_type() | ||
); | ||
} | ||
Ok(value.clone()) | ||
} | ||
ParamValues::MAP(map) => { | ||
// convert name (in format $a, $b, ..) to mapped values (a, b, ..) | ||
let name = &id[1..]; | ||
// value at the name position in param_values should be the value for the placeholder | ||
let value = map.get(name).ok_or_else(|| { | ||
DataFusionError::Internal(format!( | ||
"No value found for placeholder with name {id}" | ||
)) | ||
})?; | ||
// check if the data type of the value matches the data type of the placeholder | ||
if Some(value.data_type()) != *data_type { | ||
return _internal_err!( | ||
"Placeholder value type mismatch: expected {:?}, got {:?}", | ||
data_type, | ||
value.data_type() | ||
); | ||
} | ||
Ok(value.clone()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl From<Vec<ScalarValue>> for ParamValues { | ||
fn from(value: Vec<ScalarValue>) -> Self { | ||
Self::LIST(value) | ||
} | ||
} | ||
|
||
impl<K> From<Vec<(K, ScalarValue)>> for ParamValues | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👌 very nice |
||
where | ||
K: Into<String>, | ||
{ | ||
fn from(value: Vec<(K, ScalarValue)>) -> Self { | ||
let value: HashMap<String, ScalarValue> = | ||
value.into_iter().map(|(k, v)| (k.into(), v)).collect(); | ||
Self::MAP(value) | ||
} | ||
} | ||
|
||
impl<K> From<HashMap<K, ScalarValue>> for ParamValues | ||
where | ||
K: Into<String>, | ||
{ | ||
fn from(value: HashMap<K, ScalarValue>) -> Self { | ||
let value: HashMap<String, ScalarValue> = | ||
value.into_iter().map(|(k, v)| (k.into(), v)).collect(); | ||
Self::MAP(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is more appropriate to encapsulate it into a struct