Skip to content

Reduce repetition in the parameter type inference tests #16057

@alamb

Description

@alamb

Is your feature request related to a problem or challenge?

As can be seen in #15743, there is a lot of repeition in the PREPARE and parameter inference tests

This makes it harder to determine what is covered and what is not

Describe the solution you'd like

Now that we have insta once nice way to handle such tests is with a single struct

So instead of

#[test]
fn test_infer_types_from_predicate() {
    let sql = "SELECT id, age FROM person WHERE age = $1";
    let plan = logical_plan(sql).unwrap();
    assert_snapshot!(
        plan,
        @r#"
    Projection: person.id, person.age
      Filter: person.age = $1
        TableScan: person
    "#
    );

    let actual_types = plan.get_parameter_types().unwrap();
    let expected_types = HashMap::from([("$1".to_string(), Some(DataType::Int32))]);
    assert_eq!(actual_types, expected_types);

    // replace params with values
    let param_values = vec![ScalarValue::Int32(Some(10))];
    let plan_with_params = plan.with_param_values(param_values).unwrap();

    assert_snapshot!(
        plan_with_params,
        @r"
    Projection: person.id, person.age
      Filter: person.age = Int32(10)
        TableScan: person
    "
    );
}

The test would look something like

#[test]
fn test_infer_types_from_predicate() {
    let test = ParameterTest {
      sql: "SELECT id, age FROM person WHERE age = $1",
      expected_types: vec![("$1", Some(DataType::Int32))],
      param_values: vec![ScalarValue::Int32(Some(10))],
    };
 
    // call the ParameterTest::run method which:
    // creates a logical plan, asserts the expected types match, 
    // calls `with_parameters` to get a new LogicalPlan and then
    // returns a string with the two expected plans to compare
    assert_snapshot!(
        test.run(),
        @r#"
 ** Initial Plan:
    Projection: person.id, person.age
      Filter: person.age = $1
        TableScan: person
 ** Final Plan:
    Projection: person.id, person.age
      Filter: person.age = Int32(10)
        TableScan: person
    "
    );
}

Describe alternatives you've considered

No response

Additional context

FYI @qstommyshu @kczimm

Metadata

Metadata

Assignees

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