use std::sync::Arc;
use arrow::{
datatypes::{DataType, Schema},
record_batch::RecordBatch,
};
use crate::error::Result;
use crate::physical_plan::{ColumnarValue, PhysicalExpr};
#[derive(Debug)]
pub struct Column {
name: String,
}
impl Column {
pub fn new(name: &str) -> Self {
Self {
name: name.to_owned(),
}
}
pub fn name(&self) -> &str {
&self.name
}
}
impl std::fmt::Display for Column {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
impl PhysicalExpr for Column {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn data_type(&self, input_schema: &Schema) -> Result<DataType> {
Ok(input_schema
.field_with_name(&self.name)?
.data_type()
.clone())
}
fn nullable(&self, input_schema: &Schema) -> Result<bool> {
Ok(input_schema.field_with_name(&self.name)?.is_nullable())
}
fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
Ok(ColumnarValue::Array(
batch.column(batch.schema().index_of(&self.name)?).clone(),
))
}
}
pub fn col(name: &str) -> Arc<dyn PhysicalExpr> {
Arc::new(Column::new(name))
}