Skip to content
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 #8245

Closed
simonvandel opened this issue Nov 17, 2023 · 4 comments · Fixed by #8384
Closed

Support named query parameters #8245

simonvandel opened this issue Nov 17, 2023 · 4 comments · Fixed by #8384
Labels
enhancement New feature or request

Comments

@simonvandel
Copy link
Contributor

simonvandel commented Nov 17, 2023

Is your feature request related to a problem or challenge?

https://docs.rs/datafusion/latest/datafusion/dataframe/struct.DataFrame.html#method.with_param_values allows filling query parameters with values. However, each parameter is positional, so that it corresponds to $1,$2 and so forth.

In certain situations, it might be clearer to use named parameters, e.g $foo.

Describe the solution you'd like

Would it be possible to support named query parameters?

On a cursory look, it seems like the underlying LogicalPlan already supports mapping parameter ids to values, so this might "just" be to change the with_param_values API to allow specifying the named parameter ids.

Describe alternatives you've considered

Keep using positional query parameters.

Additional context

No response

@simonvandel simonvandel added the enhancement New feature or request label Nov 17, 2023
@alamb
Copy link
Contributor

alamb commented Nov 17, 2023

I agree @simonvandel this is both a good idea, and would be a straightforward change

@Asura7969
Copy link
Contributor

I looked at the relevant code, and the placeholders are used in the following three sections:

  1. PREPARE Statement
  2. Insert Statement
  3. Select Statement (query)

I don't think 1 and 2 need to support named queries, just support 3
This is is preliminary thinking. If you have some idea please advice.
@alamb @simonvandel

@alamb
Copy link
Contributor

alamb commented Nov 30, 2023

I don't think 1 and 2 need to support named queries, just support 3

I don't fully understand what you are suggesting here.

I thought the proposal was to change https://docs.rs/datafusion/latest/datafusion/dataframe/struct.DataFrame.html#method.with_param_values to take a different API, perhaps like

pub fn with_param_values(self, param_values: Impl IntoIterator<Item = (&str, ScalarValue)>) -> Result<Self>

So you could call it like

let results = ctx
  .sql("SELECT a FROM example WHERE b = $foo")
  .await?
   // replace $foo with value 2
  .with_param_values(vec![
     "$foo", ScalarValue::from(2i64)
   ])?
  .collect()
  .await?;

@Asura7969
Copy link
Contributor

So you could call it like

let results = ctx
  .sql("SELECT a FROM example WHERE b = $foo")
  .await?
   // replace $foo with value 2
  .with_param_values(vec![
     "$foo", ScalarValue::from(2i64)
   ])?
  .collect()
  .await?;

Maybe I can't describe it clearly, but my thoughts are the same as yours,thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants