Skip to content

Support named query parameters #8245

Closed

Description

@simonvandel

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

Activity

  1. alamb commented on Nov 17, 2023

    @alamb
    Contributor

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

  2. Asura7969 commented on Nov 30, 2023

    @Asura7969
    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

  3. alamb commented on Nov 30, 2023

    @alamb
    Contributor

    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?;
    
  4. Asura7969 commented on Dec 1, 2023

    @Asura7969
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions