use arrow::datatypes::Field;
use super::{ArrowDataType, Expr, QueryError, QueryErrorCode, Result};
use crate::expr::{CaseWhen, LiteralValue};
pub(crate) fn merged_count_field(field: Field, global_merge: bool) -> Field {
if global_merge { field.with_nullable(true) } else { field }
}
pub(crate) fn zero_filled_count_expr(count: Expr, count_type: &ArrowDataType) -> Result<Expr> {
let zero = LiteralValue::integer_zero(count_type).ok_or_else(|| {
QueryError::new(
QueryErrorCode::UnsupportedType,
"query_engine.aggregate.count_state_type",
"a count state must have an integer type",
)
.with_fact("count_type", format!("{count_type:?}"))
})?;
Ok(Expr::case(vec![CaseWhen::new(Expr::is_null(count.clone()), Expr::literal(zero))], count))
}