use super::{arithmetic_bare, arithmetic_indexed};
use crate::{
Bare, BareValueDomain, Explain, IndexDomain, Indexed, Labeled, Operand, QueryResult,
capabilities::ValueMultiply,
execution::EvaluationCache,
operations::{
Apply, ArgumentSource, ElementKernel, ElementPipeline, Keyed, Operation, OperationContext,
Prepare, Unaligned,
},
optimizer::{Estimate, OperationInputs, OptimizerHints, PlanIdentity, PlanInputs, Stats},
registry::{describe::ArgumentRetention, operation_manifest},
traits::Multiply,
};
use graphrecords_core::GraphRecord;
#[derive(Clone, Explain, Operation, OperationInputs, OptimizerHints, PlanIdentity, PlanInputs)]
#[operation(scope = Element)]
#[explain(label = "Multiply")]
#[plan(optimizer_hints(empty = if_all))]
pub struct MultiplyOperation<A> {
#[argument]
argument: A,
}
impl<A: Prepare> Prepare for MultiplyOperation<A> {
type Prepared<'a>
= A::Prepared<'a>
where
Self: 'a;
fn prepare<'a>(
&'a self,
graphrecord: &'a GraphRecord,
cache: &'a EvaluationCache<'a>,
) -> QueryResult<Self::Prepared<'a>> {
self.argument.prepare(graphrecord, cache)
}
}
impl<I, V, A> ElementKernel<Indexed<I, V>> for MultiplyOperation<A>
where
I: IndexDomain,
V: ValueMultiply,
A: ArgumentSource<Keyed<I>, V>,
{
type Emission = A::Retention;
type OutShape = Indexed<I, V>;
fn pipeline<'a>(
_graphrecord: &'a GraphRecord,
prepared: Self::Prepared<'a>,
) -> QueryResult<ElementPipeline<'a, Indexed<I, V>, Self>> {
Ok(arithmetic_indexed::<_, V, A>(
prepared,
Self::LABEL,
V::multiply,
))
}
fn estimate(&self, input: Estimate, _stats: &Stats) -> Estimate {
input.with_unknown_distinct()
}
}
impl<V, A> ElementKernel<Bare<V>> for MultiplyOperation<A>
where
V: ValueMultiply + BareValueDomain,
A: ArgumentSource<Unaligned, V>,
{
type Emission = A::Retention;
type OutShape = Bare<V>;
fn pipeline<'a>(
_graphrecord: &'a GraphRecord,
prepared: Self::Prepared<'a>,
) -> QueryResult<ElementPipeline<'a, Bare<V>, Self>> {
Ok(arithmetic_bare::<V, A>(prepared, Self::LABEL, V::multiply))
}
fn estimate(&self, input: Estimate, _stats: &Stats) -> Estimate {
input.with_unknown_distinct()
}
}
impl<O, A> Multiply<A> for O
where
MultiplyOperation<A>: Operation,
O: Apply<MultiplyOperation<A>>,
{
type ReturnOperand = O::Output;
fn multiply(&self, argument: A) -> Self::ReturnOperand {
Self::ReturnOperand::new(OperationContext::new(
self.clone(),
MultiplyOperation { argument },
))
}
}
operation_manifest! {
MultiplyOperation<A> {
method: Multiply<A>::multiply;
scope: element;
kernel {
parameters: <I: IndexDomain, V: ValueMultiply>;
argument: A: ArgumentSource<Keyed<I>, V>;
input: Indexed<I, V>;
output: Indexed<I, V>;
emission: ArgumentRetention;
}
kernel {
parameters: <V: ValueMultiply + BareValueDomain>;
argument: A: ArgumentSource<Unaligned, V>;
input: Bare<V>;
output: Bare<V>;
emission: ArgumentRetention;
}
}
}