use std::sync::Arc;
use futures::stream;
use surrealdb_types::{SqlFormat, ToSql};
use crate::exec::context::{ContextLevel, ExecutionContext};
use crate::exec::physical_expr::{EvalContext, PhysicalExpr};
use crate::exec::{
AccessMode, CardinalityHint, ExecOperator, FlowResult, OperatorMetrics, ValueBatch,
ValueBatchStream,
};
#[derive(Debug, Clone)]
pub struct ExprPlan {
pub expr: Arc<dyn PhysicalExpr>,
pub(crate) metrics: Arc<OperatorMetrics>,
}
impl ExprPlan {
pub(crate) fn new(expr: Arc<dyn PhysicalExpr>) -> Self {
Self {
expr,
metrics: Arc::new(OperatorMetrics::new()),
}
}
}
impl ExecOperator for ExprPlan {
fn name(&self) -> &'static str {
"Expr"
}
fn attrs(&self) -> Vec<(String, String)> {
vec![("expr".to_string(), self.expr.to_sql())]
}
fn required_context(&self) -> ContextLevel {
self.expr.required_context()
}
fn access_mode(&self) -> AccessMode {
self.expr.access_mode()
}
fn cardinality_hint(&self) -> CardinalityHint {
CardinalityHint::AtMostOne
}
fn metrics(&self) -> Option<&OperatorMetrics> {
Some(&self.metrics)
}
fn expressions(&self) -> Vec<(&str, &Arc<dyn PhysicalExpr>)> {
vec![("expr", &self.expr)]
}
fn execute(&self, ctx: &ExecutionContext) -> FlowResult<ValueBatchStream> {
let expr = Arc::clone(&self.expr);
let ctx = ctx.clone();
Ok(Box::pin(stream::once(async move {
let eval_ctx = EvalContext::from_exec_ctx(&ctx);
let value = expr.evaluate(eval_ctx).await?;
Ok(ValueBatch {
values: vec![value],
})
})))
}
fn is_scalar(&self) -> bool {
true
}
}
impl ToSql for ExprPlan {
fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
self.expr.fmt_sql(f, fmt);
}
}