Skip to content

Query with 100 OR conditions overflows stack #1434

@mcassels

Description

@mcassels

Describe the bug
SELECT * FROM table WHERE <condition0> OR <condition1> OR ... succeeds with 50 conditions but overflows stack with 100 conditions.

To Reproduce
Adding this test to arrow-datafusion/datafusion/tests/sql.rs demonstrates the issue:

#[tokio::test]
async fn query_with_many_conditions() -> Result<()> {
    let schema = Arc::new(Schema::new(vec![Field::new("c1", DataType::Utf8, true)]));
    let data = RecordBatch::try_new(
        schema.clone(),
        vec![Arc::new(StringArray::from(vec!["foo", "bar"]))],
    )
    .unwrap();

    let table = MemTable::try_new(schema, vec![vec![data]])?;

    let mut ctx = ExecutionContext::new();
    ctx.register_table("test", Arc::new(table)).unwrap();

    let num_conditions = 50;
    println!("attempted a query with {:?} conditions...", num_conditions);
    let where_clause = (0..num_conditions)
        .map(|i| format!("c1 = 'value{:?}'", i))
        .collect::<Vec<String>>()
        .join(" OR ");
    let sql = format!("SELECT * from test where {};", where_clause);
    execute_to_batches(&mut ctx, &sql).await;
    println!("query succeeded with {:?} conditions", num_conditions);

    let num_conditions = 100;
    println!("attempted a query with {:?} conditions...", num_conditions);
    let where_clause = (0..num_conditions)
        .map(|i| format!("c1 = 'value{:?}'", i))
        .collect::<Vec<String>>()
        .join(" OR ");
    let sql = format!("SELECT * from test where {};", where_clause);
    execute_to_batches(&mut ctx, &sql).await;
    println!("query succeeded with {:?} conditions", num_conditions);
    Ok(())
}

Output:

running 1 test
attempted a query with 50 conditions...
query succeeded with 50 conditions
attempted a query with 100 conditions...

thread 'query_with_many_conditions' has overflowed its stack
fatal runtime error: stack overflow
error: test failed, to rerun pass '--test sql'

Expected behavior
Expected query to succeed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions