Skip to content

Commit

Permalink
Minor: move Column related tests and rename column.rs (#11573)
Browse files Browse the repository at this point in the history
* Minor: move `Column` related tests

* Rename column.rs to unknown_column.rs
  • Loading branch information
jonahgao authored Jul 22, 2024
1 parent 81d06f2 commit 4417a94
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
46 changes: 46 additions & 0 deletions datafusion/physical-expr-common/src/expressions/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,49 @@ impl Column {
pub fn col(name: &str, schema: &Schema) -> Result<Arc<dyn PhysicalExpr>> {
Ok(Arc::new(Column::new_with_schema(name, schema)?))
}

#[cfg(test)]
mod test {
use super::Column;
use crate::physical_expr::PhysicalExpr;

use arrow::array::StringArray;
use arrow::datatypes::{DataType, Field, Schema};
use arrow::record_batch::RecordBatch;
use datafusion_common::Result;

use std::sync::Arc;

#[test]
fn out_of_bounds_data_type() {
let schema = Schema::new(vec![Field::new("foo", DataType::Utf8, true)]);
let col = Column::new("id", 9);
let error = col.data_type(&schema).expect_err("error").strip_backtrace();
assert!("Internal error: PhysicalExpr Column references column 'id' at index 9 (zero-based) \
but input schema only has 1 columns: [\"foo\"].\nThis was likely caused by a bug in \
DataFusion's code and we would welcome that you file an bug report in our issue tracker".starts_with(&error))
}

#[test]
fn out_of_bounds_nullable() {
let schema = Schema::new(vec![Field::new("foo", DataType::Utf8, true)]);
let col = Column::new("id", 9);
let error = col.nullable(&schema).expect_err("error").strip_backtrace();
assert!("Internal error: PhysicalExpr Column references column 'id' at index 9 (zero-based) \
but input schema only has 1 columns: [\"foo\"].\nThis was likely caused by a bug in \
DataFusion's code and we would welcome that you file an bug report in our issue tracker".starts_with(&error))
}

#[test]
fn out_of_bounds_evaluate() -> Result<()> {
let schema = Schema::new(vec![Field::new("foo", DataType::Utf8, true)]);
let data: StringArray = vec!["data"].into();
let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(data)])?;
let col = Column::new("id", 9);
let error = col.evaluate(&batch).expect_err("error").strip_backtrace();
assert!("Internal error: PhysicalExpr Column references column 'id' at index 9 (zero-based) \
but input schema only has 1 columns: [\"foo\"].\nThis was likely caused by a bug in \
DataFusion's code and we would welcome that you file an bug report in our issue tracker".starts_with(&error));
Ok(())
}
}
4 changes: 2 additions & 2 deletions datafusion/physical-expr/src/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#[macro_use]
mod binary;
mod case;
mod column;
mod in_list;
mod is_not_null;
mod is_null;
Expand All @@ -29,6 +28,7 @@ mod negative;
mod no_op;
mod not;
mod try_cast;
mod unknown_column;

/// Module with some convenient methods used in expression building
pub mod helpers {
Expand All @@ -48,7 +48,6 @@ pub use crate::PhysicalSortExpr;

pub use binary::{binary, BinaryExpr};
pub use case::{case, CaseExpr};
pub use column::UnKnownColumn;
pub use datafusion_expr::utils::format_state_name;
pub use datafusion_physical_expr_common::expressions::column::{col, Column};
pub use datafusion_physical_expr_common::expressions::literal::{lit, Literal};
Expand All @@ -61,3 +60,4 @@ pub use negative::{negative, NegativeExpr};
pub use no_op::NoOp;
pub use not::{not, NotExpr};
pub use try_cast::{try_cast, TryCastExpr};
pub use unknown_column::UnKnownColumn;
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

//! Column expression
//! UnKnownColumn expression
use std::any::Any;
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -100,49 +100,3 @@ impl PartialEq<dyn Any> for UnKnownColumn {
false
}
}

#[cfg(test)]
mod test {
use crate::expressions::Column;
use crate::PhysicalExpr;

use arrow::array::StringArray;
use arrow::datatypes::{DataType, Field, Schema};
use arrow::record_batch::RecordBatch;
use datafusion_common::Result;

use std::sync::Arc;

#[test]
fn out_of_bounds_data_type() {
let schema = Schema::new(vec![Field::new("foo", DataType::Utf8, true)]);
let col = Column::new("id", 9);
let error = col.data_type(&schema).expect_err("error").strip_backtrace();
assert!("Internal error: PhysicalExpr Column references column 'id' at index 9 (zero-based) \
but input schema only has 1 columns: [\"foo\"].\nThis was likely caused by a bug in \
DataFusion's code and we would welcome that you file an bug report in our issue tracker".starts_with(&error))
}

#[test]
fn out_of_bounds_nullable() {
let schema = Schema::new(vec![Field::new("foo", DataType::Utf8, true)]);
let col = Column::new("id", 9);
let error = col.nullable(&schema).expect_err("error").strip_backtrace();
assert!("Internal error: PhysicalExpr Column references column 'id' at index 9 (zero-based) \
but input schema only has 1 columns: [\"foo\"].\nThis was likely caused by a bug in \
DataFusion's code and we would welcome that you file an bug report in our issue tracker".starts_with(&error))
}

#[test]
fn out_of_bounds_evaluate() -> Result<()> {
let schema = Schema::new(vec![Field::new("foo", DataType::Utf8, true)]);
let data: StringArray = vec!["data"].into();
let batch = RecordBatch::try_new(Arc::new(schema), vec![Arc::new(data)])?;
let col = Column::new("id", 9);
let error = col.evaluate(&batch).expect_err("error").strip_backtrace();
assert!("Internal error: PhysicalExpr Column references column 'id' at index 9 (zero-based) \
but input schema only has 1 columns: [\"foo\"].\nThis was likely caused by a bug in \
DataFusion's code and we would welcome that you file an bug report in our issue tracker".starts_with(&error));
Ok(())
}
}

0 comments on commit 4417a94

Please sign in to comment.