use std::sync::Arc;
use arrow::datatypes::DataType;
use chrono::{DateTime, Utc};
use datafusion_common::config::ConfigOptions;
use datafusion_common::{DFSchema, DFSchemaRef, Result};
use crate::{Expr, ExprSchemable};
#[derive(Debug, Clone)]
pub struct SimplifyContext {
schema: DFSchemaRef,
query_execution_start_time: Option<DateTime<Utc>>,
config_options: Arc<ConfigOptions>,
}
impl Default for SimplifyContext {
fn default() -> Self {
Self {
schema: Arc::new(DFSchema::empty()),
query_execution_start_time: None,
config_options: Arc::new(ConfigOptions::default()),
}
}
}
impl SimplifyContext {
pub fn with_config_options(mut self, config_options: Arc<ConfigOptions>) -> Self {
self.config_options = config_options;
self
}
pub fn with_schema(mut self, schema: DFSchemaRef) -> Self {
self.schema = schema;
self
}
pub fn with_query_execution_start_time(
mut self,
query_execution_start_time: Option<DateTime<Utc>>,
) -> Self {
self.query_execution_start_time = query_execution_start_time;
self
}
pub fn with_current_time(mut self) -> Self {
self.query_execution_start_time = Some(Utc::now());
self
}
pub fn schema(&self) -> &DFSchemaRef {
&self.schema
}
pub fn is_boolean_type(&self, expr: &Expr) -> Result<bool> {
Ok(expr.get_type(&self.schema)? == DataType::Boolean)
}
pub fn nullable(&self, expr: &Expr) -> Result<bool> {
expr.nullable(self.schema.as_ref())
}
pub fn get_data_type(&self, expr: &Expr) -> Result<DataType> {
expr.get_type(&self.schema)
}
pub fn query_execution_start_time(&self) -> Option<DateTime<Utc>> {
self.query_execution_start_time
}
pub fn config_options(&self) -> &Arc<ConfigOptions> {
&self.config_options
}
}
#[derive(Debug)]
pub enum ExprSimplifyResult {
Simplified(Expr),
Original(Vec<Expr>),
}