Skip to content

Commit bdc56ca

Browse files
authored
Add bounds check to Column physical expression (#3238)
1 parent c11f303 commit bdc56ca

1 file changed

Lines changed: 64 additions & 1 deletion

File tree

  • datafusion/physical-expr/src/expressions

‎datafusion/physical-expr/src/expressions/column.rs‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use arrow::{
2525
};
2626

2727
use crate::PhysicalExpr;
28-
use datafusion_common::Result;
28+
use datafusion_common::{DataFusionError, Result};
2929
use datafusion_expr::ColumnarValue;
3030

3131
/// Represents the column at a given index in a RecordBatch
@@ -74,21 +74,84 @@ impl PhysicalExpr for Column {
7474

7575
/// Get the data type of this expression, given the schema of the input
7676
fn data_type(&self, input_schema: &Schema) -> Result<DataType> {
77+
self.bounds_check(input_schema)?;
7778
Ok(input_schema.field(self.index).data_type().clone())
7879
}
7980

8081
/// Decide whehter this expression is nullable, given the schema of the input
8182
fn nullable(&self, input_schema: &Schema) -> Result<bool> {
83+
self.bounds_check(input_schema)?;
8284
Ok(input_schema.field(self.index).is_nullable())
8385
}
8486

8587
/// Evaluate the expression
8688
fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
89+
self.bounds_check(batch.schema().as_ref())?;
8790
Ok(ColumnarValue::Array(batch.column(self.index).clone()))
8891
}
8992
}
9093

94+
impl Column {
95+
fn bounds_check(&self, input_schema: &Schema) -> Result<()> {
96+
if self.index < input_schema.fields.len() {
97+
Ok(())
98+
} else {
99+
Err(DataFusionError::Internal(format!(
100+
"PhysicalExpr Column references column '{}' at index {} (zero-based) but input schema only has {} columns: {:?}",
101+
self.name,
102+
self.index, input_schema.fields.len(), input_schema.fields().iter().map(|f| f.name().clone()).collect::<Vec<String>>())))
103+
}
104+
}
105+
}
106+
91107
/// Create a column expression
92108
pub fn col(name: &str, schema: &Schema) -> Result<Arc<dyn PhysicalExpr>> {
93109
Ok(Arc::new(Column::new_with_schema(name, schema)?))
94110
}
111+
112+
#[cfg(test)]
113+
mod test {
114+
use crate::expressions::Column;
115+
use crate::PhysicalExpr;
116+
use arrow::array::StringArray;
117+
use arrow::datatypes::{DataType, Field, Schema};
118+
use arrow::record_batch::RecordBatch;
119+
use datafusion_common::Result;
120+
use std::sync::Arc;
121+
122+
#[test]
123+
fn out_of_bounds_data_type() {
124+
let schema = Schema::new(vec![Field::new("foo", DataType::Utf8, true)]);
125+
let col = Column::new("id", 9);
126+
let error = col.data_type(&schema).expect_err("error");
127+
assert_eq!("Internal error: PhysicalExpr Column references column 'id' at index 9 (zero-based) \
128+
but input schema only has 1 columns: [\"foo\"]. This was likely caused by a bug in \
129+
DataFusion's code and we would welcome that you file an bug report in our issue tracker",
130+
&format!("{}", error))
131+
}
132+
133+
#[test]
134+
fn out_of_bounds_nullable() {
135+
let schema = Schema::new(vec![Field::new("foo", DataType::Utf8, true)]);
136+
let col = Column::new("id", 9);
137+
let error = col.nullable(&schema).expect_err("error");
138+
assert_eq!("Internal error: PhysicalExpr Column references column 'id' at index 9 (zero-based) \
139+
but input schema only has 1 columns: [\"foo\"]. This was likely caused by a bug in \
140+
DataFusion's code and we would welcome that you file an bug report in our issue tracker",
141+
&format!("{}", error))
142+
}
143+
144+
#[test]
145+
fn out_of_bounds_evaluate() -> Result<()> {
146+
let schema = Schema::new(vec![Field::new("foo", DataType::Utf8, true)]);
147+
let data: StringArray = vec!["data"].into();
148+
let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(data)])?;
149+
let col = Column::new("id", 9);
150+
let error = col.evaluate(&batch).expect_err("error");
151+
assert_eq!("Internal error: PhysicalExpr Column references column 'id' at index 9 (zero-based) \
152+
but input schema only has 1 columns: [\"foo\"]. This was likely caused by a bug in \
153+
DataFusion's code and we would welcome that you file an bug report in our issue tracker",
154+
&format!("{}", error));
155+
Ok(())
156+
}
157+
}

0 commit comments

Comments
 (0)