risingwave_expr/aggregate/
scalar_wrapper.rsuse risingwave_common::array::{Array, ArrayBuilderImpl, DataChunk, ListArray};
use risingwave_common::types::ListValue;
use super::*;
use crate::expr::{BoxedExpression, Expression};
#[derive(Debug)]
pub struct ScalarWrapper {
arg_type: DataType,
scalar: BoxedExpression,
}
impl ScalarWrapper {
pub fn new(arg_type: DataType, scalar: BoxedExpression) -> Self {
Self { arg_type, scalar }
}
}
#[async_trait::async_trait]
impl AggregateFunction for ScalarWrapper {
fn return_type(&self) -> DataType {
self.scalar.return_type()
}
fn create_state(&self) -> Result<AggregateState> {
Ok(AggregateState::Any(Box::new(State(
self.arg_type.create_array_builder(0),
))))
}
async fn update(&self, state: &mut AggregateState, input: &StreamChunk) -> Result<()> {
let state = &mut state.downcast_mut::<State>().0;
let column = input.column_at(0);
for i in input.visibility().iter_ones() {
state.append(column.datum_at(i));
}
Ok(())
}
async fn update_range(
&self,
state: &mut AggregateState,
input: &StreamChunk,
range: Range<usize>,
) -> Result<()> {
let state = &mut state.downcast_mut::<State>().0;
let column = input.column_at(0);
for i in input.visibility().iter_ones() {
if i < range.start {
continue;
} else if i >= range.end {
break;
}
state.append(column.datum_at(i));
}
Ok(())
}
async fn get_result(&self, state: &AggregateState) -> Result<Datum> {
let state = &state.downcast_ref::<State>().0;
let list = ListValue::new(state.clone().finish());
let chunk = DataChunk::new(vec![ListArray::from_iter([list]).into_ref()], 1);
let output = self.scalar.eval(&chunk).await?;
Ok(output.to_datum())
}
fn encode_state(&self, _state: &AggregateState) -> Result<Datum> {
panic!("should not store state in state table")
}
fn decode_state(&self, _datum: Datum) -> Result<AggregateState> {
panic!("should not store state in state table")
}
}
#[derive(Debug)]
struct State(ArrayBuilderImpl);
impl EstimateSize for State {
fn estimated_heap_size(&self) -> usize {
self.0.estimated_heap_size()
}
}
impl AggStateDyn for State {}