use std::fmt::Formatter;
use prost::Message;
use vortex_array::ArrayRef;
use vortex_array::ExecutionCtx;
use vortex_array::IntoArray;
use vortex_array::arrays::ConstantArray;
use vortex_array::arrays::ScalarFn;
use vortex_array::arrays::ScalarFnArray;
use vortex_array::dtype::DType;
use vortex_array::proto::scalar as pb;
use vortex_array::scalar::Scalar;
use vortex_array::scalar_fn::Arity;
use vortex_array::scalar_fn::ChildName;
use vortex_array::scalar_fn::ExecutionArgs;
use vortex_array::scalar_fn::ReduceNode;
use vortex_array::scalar_fn::ReduceNodeValidity;
use vortex_array::scalar_fn::ScalarFnId;
use vortex_array::scalar_fn::ScalarFnVTable;
use vortex_array::scalar_fn::ScalarFnVTableExt;
use vortex_array::scalar_fn::display::ExprDisplay;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_session::VortexSession;
use vortex_session::registry::CachedId;
use super::PreparedSetData;
#[derive(Clone, Debug)]
pub struct PreparedSet;
impl PreparedSet {
pub fn try_new_array(list: Scalar, len: usize) -> VortexResult<ArrayRef> {
Ok(Self::new_array(PreparedSetData::try_new(list)?, len))
}
pub(crate) fn new_array(set: PreparedSetData, len: usize) -> ArrayRef {
ScalarFnArray::try_new_with_len(Self.bind(set), [], len)
.vortex_expect("A prepared set has no children, and its dtype is the list dtype")
.into_array()
}
pub fn set_of(array: &ArrayRef) -> Option<&PreparedSetData> {
array
.as_opt::<ScalarFn>()?
.data()
.scalar_fn()
.as_opt::<Self>()
}
}
impl ScalarFnVTable for PreparedSet {
type Options = PreparedSetData;
fn id(&self) -> ScalarFnId {
static ID: CachedId = CachedId::new("vortex.list.prepared_set");
*ID
}
fn serialize(&self, set: &Self::Options) -> VortexResult<Option<Vec<u8>>> {
Ok(Some(set.list().to_proto()?.encode_to_vec()))
}
fn deserialize(&self, metadata: &[u8], session: &VortexSession) -> VortexResult<Self::Options> {
let list = Scalar::from_proto(&pb::Scalar::decode(metadata)?, session)?;
PreparedSetData::try_new(list)
}
fn arity(&self, _options: &Self::Options) -> Arity {
Arity::Exact(0)
}
fn child_name(&self, _options: &Self::Options, _child_idx: usize) -> ChildName {
unreachable!()
}
fn fmt_sql(
&self,
set: &Self::Options,
_expr: &dyn ExprDisplay,
f: &mut Formatter<'_>,
) -> std::fmt::Result {
write!(f, "{}", set.list())
}
fn return_dtype(&self, set: &Self::Options, _arg_dtypes: &[DType]) -> VortexResult<DType> {
Ok(set.list().dtype().clone())
}
fn execute(
&self,
set: &Self::Options,
args: &dyn ExecutionArgs,
_ctx: &mut ExecutionCtx,
) -> VortexResult<ArrayRef> {
Ok(ConstantArray::new(set.list().clone(), args.row_count()).into_array())
}
fn validity<T: ReduceNode>(
&self,
_set: &Self::Options,
node: &T,
) -> VortexResult<ReduceNodeValidity<T>> {
Ok(ReduceNodeValidity::Reduced(node.new_constant(true.into())))
}
fn is_strict(&self, _options: &Self::Options) -> bool {
true
}
fn is_infallible(&self, _options: &Self::Options) -> bool {
true
}
}