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

Fix InListExpr to return the correct number of rows #8601

Merged
merged 3 commits into from
Dec 22, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion datafusion/physical-expr/src/expressions/in_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,10 @@ impl PhysicalExpr for InListExpr {
}

fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
let num_rows = batch.num_rows();
let value = self.expr.evaluate(batch)?;
let r = match &self.static_filter {
Some(f) => f.contains(value.into_array(1)?.as_ref(), self.negated)?,
Some(f) => f.contains(value.into_array(num_rows)?.as_ref(), self.negated)?,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix -- the rest of the PR is test

None => {
let value = value.into_array(batch.num_rows())?;
Copy link
Contributor

@Dandandan Dandandan Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use the let value = value.into_array(batch.num_rows())?; for both arms here (before matching).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea -- done in 7cde104

let found = self.list.iter().map(|expr| expr.evaluate(batch)).try_fold(
Expand Down Expand Up @@ -1267,4 +1268,52 @@ mod tests {

Ok(())
}

#[test]
fn in_list_no_cols() -> Result<()> {
// test logic when the in_list expression doesn't have any columns
let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]);
let a = Int32Array::from(vec![Some(1), Some(2), None]);
let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;

let list = vec![lit(ScalarValue::from(1i32)), lit(ScalarValue::from(6i32))];

// 1 IN (1, 6)
let expr = lit(ScalarValue::Int32(Some(1)));
in_list!(
batch,
list.clone(),
&false,
// should have three outputs, as the input batch has three rows
vec![Some(true), Some(true), Some(true)],
expr,
&schema
);

// 2 IN (1, 6)
let expr = lit(ScalarValue::Int32(Some(2)));
in_list!(
batch,
list.clone(),
&false,
// should have three outputs, as the input batch has three rows
vec![Some(false), Some(false), Some(false)],
expr,
&schema
);

// NULL IN (1, 6)
let expr = lit(ScalarValue::Int32(None));
in_list!(
batch,
list.clone(),
&false,
// should have three outputs, as the input batch has three rows
vec![None, None, None],
expr,
&schema
);

Ok(())
}
}