Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlTypeName;

import java.util.List;
Expand All @@ -24,20 +29,7 @@ public enum AggregateFunction {
SUM0(Type.SIMPLE, SqlKind.SUM0),
MIN(Type.SIMPLE, SqlKind.MIN),
MAX(Type.SIMPLE, SqlKind.MAX),
COUNT(Type.SIMPLE, SqlKind.COUNT, fields(IF("count", new ArrowType.Int(64, true), SUM))) {
/**
* Single-arg {@code COUNT(DISTINCT x)} collapses onto {@link SqlStdOperatorTable#APPROX_COUNT_DISTINCT}.
* Distinctness is global and cannot be reduced additively across shards;
* {@link #APPROX_COUNT_DISTINCT} (Type.APPROXIMATE) carries the cross-shard merge.
*/
@Override
public SqlAggFunction resolveOperator(SqlAggFunction op, boolean isDistinct, int argCount) {
if (isDistinct && argCount == 1) {
return SqlStdOperatorTable.APPROX_COUNT_DISTINCT;
}
return op;
}
},
COUNT(Type.SIMPLE, SqlKind.COUNT, fields(IF("count", new ArrowType.Int(64, true), SUM))),
AVG(Type.SIMPLE, SqlKind.AVG),

STDDEV_POP(Type.STATISTICAL, SqlKind.STDDEV_POP),
Expand All @@ -51,7 +43,7 @@ public SqlAggFunction resolveOperator(SqlAggFunction op, boolean isDistinct, int
COLLECT(Type.STATE_EXPANDING, SqlKind.COLLECT),
LISTAGG(Type.STATE_EXPANDING, SqlKind.LISTAGG),

APPROX_COUNT_DISTINCT(Type.APPROXIMATE, SqlKind.OTHER, fields(IF("sketch", new ArrowType.Binary(), null))),
APPROX_COUNT_DISTINCT(Type.APPROXIMATE, SqlKind.OTHER, fields(IF("hll_registers", new ArrowType.Binary(), null))),
TAKE(Type.STATE_EXPANDING, SqlKind.OTHER, fields(IF("take_state", IntermediateTypeResolver.passThroughArg0(), null))),
FIRST(Type.STATE_EXPANDING, SqlKind.OTHER, fields(IF("first_state", IntermediateTypeResolver.passThroughArg0(), null))),
LAST(Type.STATE_EXPANDING, SqlKind.OTHER, fields(IF("last_state", IntermediateTypeResolver.passThroughArg0(), null))),
Expand Down Expand Up @@ -145,14 +137,6 @@ public static AggregateFunction fromSqlKind(SqlKind kind) {
return null;
}

/**
* Resolves an aggregate call shape (operator + flags) onto a standard {@link SqlAggFunction}.
* Default returns {@code op} unchanged; override per enum value to encode rewrites.
*/
public SqlAggFunction resolveOperator(SqlAggFunction op, boolean isDistinct, int argCount) {
return op;
}

/** Case-insensitive name lookup; throws if not recognized. */
public static AggregateFunction fromNameOrError(String name) {
try {
Expand Down Expand Up @@ -188,6 +172,39 @@ public static AggregateFunction fromSqlAggFunction(SqlAggFunction op) {
throw new IllegalStateException("No AggregateFunction mapping for SqlAggFunction [" + op.getName() + "]");
}

/** Evaluates opaque engine-native-merge state to a sortable scalar (used by TopK). */
public static final SqlOperator REDUCE_EVAL_OP = new SqlFunction(
"reduce_eval",
SqlKind.OTHER_FUNCTION,
ReturnTypes.BIGINT_NULLABLE,
null,
OperandTypes.ANY_ANY,
SqlFunctionCategory.USER_DEFINED_FUNCTION
);

/**
* Returns true if the given aggregate call uses engine-native merge (single intermediate
* field whose reducer is the function itself — e.g. APPROX_COUNT_DISTINCT with HLL sketches).
*/
/** Returns the UDAF name used by the reduce_eval UDF to evaluate partial state to a scalar. */
public String reduceEvalName() {
return switch (this) {
case APPROX_COUNT_DISTINCT -> "approx_distinct";
default -> throw new IllegalStateException(this + " has no reduce_eval mapping");
};
}

public static boolean isEngineNativeMerge(org.apache.calcite.rel.core.AggregateCall call) {
AggregateFunction func;
try {
func = fromSqlAggFunction(call.getAggregation());
} catch (IllegalStateException e) {
return false;
}
List<IntermediateField> fields = func.intermediateFields();
return fields != null && fields.size() == 1 && fields.get(0).reducer() == func;
}

private static List<IntermediateField> fields(IntermediateField... fs) {
return List.of(fs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.type.SqlTypeName;
import org.opensearch.test.OpenSearchTestCase;

Expand Down Expand Up @@ -91,7 +89,7 @@ public void testApproxCountDistinctHasDecomposition() {
public void testApproxCountDistinctReducerIsSelf() {
List<AggregateFunction.IntermediateField> fields = APPROX_COUNT_DISTINCT.intermediateFields();
assertEquals(1, fields.size());
assertEquals("sketch", fields.get(0).name());
assertEquals("hll_registers", fields.get(0).name());
assertSame(APPROX_COUNT_DISTINCT, fields.get(0).reducer());
assertEquals(SqlTypeName.VARBINARY, resolve(fields.get(0), integer).getSqlTypeName());
}
Expand Down Expand Up @@ -197,28 +195,4 @@ public void testFromSqlKindResolvesExistingEntries() {
public void testFromSqlKindReturnsNullForOther() {
assertNull(AggregateFunction.fromSqlKind(SqlKind.OTHER));
}

public void testResolveOperatorRewritesCountDistinctToApprox() {
SqlAggFunction op = SqlStdOperatorTable.COUNT;
SqlAggFunction canon = AggregateFunction.COUNT.resolveOperator(op, /*isDistinct=*/ true, 1);
assertSame(SqlStdOperatorTable.APPROX_COUNT_DISTINCT, canon);
}

public void testResolveOperatorLeavesNonDistinctCountUnchanged() {
SqlAggFunction op = SqlStdOperatorTable.COUNT;
assertSame(op, AggregateFunction.COUNT.resolveOperator(op, false, 1));
}

public void testResolveOperatorLeavesMultiArgDistinctUnchanged() {
// count(DISTINCT a, b) is not rewritten — distinctness across multiple args isn't
// reducible to single-arg APPROX_COUNT_DISTINCT.
SqlAggFunction op = SqlStdOperatorTable.COUNT;
assertSame(op, AggregateFunction.COUNT.resolveOperator(op, true, 2));
}

public void testResolveOperatorDefaultIsIdentity() {
// Default override (e.g. SUM) returns the input op unchanged regardless of flags.
SqlAggFunction op = SqlStdOperatorTable.SUM;
assertSame(op, AggregateFunction.SUM.resolveOperator(op, true, 1));
}
}
84 changes: 55 additions & 29 deletions sandbox/plugins/analytics-backend-datafusion/rust/src/agg_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

use std::sync::Arc;

use datafusion::physical_expr::PhysicalExpr;
use datafusion::physical_optimizer::combine_partial_final_agg::CombinePartialFinalAggregate;
use datafusion::physical_optimizer::optimizer::{PhysicalOptimizer, PhysicalOptimizerRule};
use datafusion::physical_plan::aggregates::{AggregateExec, AggregateMode};
use datafusion::physical_plan::coalesce_partitions::CoalescePartitionsExec;
use datafusion::physical_plan::repartition::RepartitionExec;
use datafusion::physical_plan::expressions::Column;
use datafusion::physical_plan::projection::ProjectionExec;
use datafusion::physical_plan::ExecutionPlan;
use datafusion_common::Result;

Expand Down Expand Up @@ -48,13 +49,24 @@ pub(crate) fn apply_aggregate_mode(
}
}

/// Returns the output schema of the Partial aggregate without rebuilding the plan tree.
/// Used by `derive_schema_from_partial_plan` where we only need types, not an executable plan.
pub(crate) fn partial_aggregate_schema(plan: &Arc<dyn ExecutionPlan>) -> Option<arrow::datatypes::SchemaRef> {
find_partial_input(Arc::clone(plan)).map(|p| p.schema())
}

/// Walks the plan tree and strips the half that doesn't match `target`.
fn force_aggregate_mode(
plan: Arc<dyn ExecutionPlan>,
target: AggregateMode,
) -> Result<Arc<dyn ExecutionPlan>> {
if let Some(agg) = plan.as_any().downcast_ref::<AggregateExec>() {
if *agg.mode() == target {
// Treat `FinalPartitioned` as `Final`: DataFusion picks `FinalPartitioned` for
// grouped aggregates that consume hash-partitioned input and `Final` for scalar /
// un-partitioned ones. Both are the FINAL half of the Partial/Final pair we strip.
let agg_is_target = *agg.mode() == target
|| (target == AggregateMode::Final && *agg.mode() == AggregateMode::FinalPartitioned);
if agg_is_target {
// Keep this node, recurse into children
let new_children: Vec<Arc<dyn ExecutionPlan>> = agg
.children()
Expand All @@ -81,49 +93,63 @@ fn force_aggregate_mode(
}
_ => Ok(plan),
}
} else if plan.as_any().downcast_ref::<RepartitionExec>().is_some()
|| plan
.as_any()
.downcast_ref::<CoalescePartitionsExec>()
.is_some()
{
// Transparent — recurse through
let new_children: Vec<Arc<dyn ExecutionPlan>> = plan
.children()
.into_iter()
.map(|c| force_aggregate_mode(Arc::clone(c), target))
.collect::<Result<_>>()?;
plan.with_new_children(new_children)
} else if plan.children().len() == 1 {
// Single-input wrapper — recurse transparently.
let old_child = Arc::clone(plan.children()[0]);
let new_child = force_aggregate_mode(old_child.clone(), target)?;

// DataFusion's ProjectionMapping::try_new asserts col.name() == input_schema.field(i).name();
// with_new_children triggers it. Remap columns to the post-strip schema so it passes.
if let Some(proj) = plan.as_any().downcast_ref::<ProjectionExec>() {
if old_child.schema() != new_child.schema() {
let new_schema = &new_child.schema();
let remapped: Vec<(Arc<dyn PhysicalExpr>, String)> = proj.expr().iter()
.map(|pe| (remap_column(pe.expr.clone(), new_schema), pe.alias.clone()))
.collect();
return Ok(Arc::new(ProjectionExec::try_new(remapped, new_child)?));
}
}

plan.with_new_children(vec![new_child])
} else {
// Leaf or unrelated node — return as-is
// Leaf or multi-input node — return as-is
Ok(plan)
}
}

/// Walks down through RepartitionExec/CoalescePartitionsExec to find an
/// Walks down through any single-input wrapper (RelabelExec / RepartitionExec /
/// CoalescePartitionsExec / ProjectionExec / etc.) to find an
/// AggregateExec(Partial) and returns the entire Partial subtree (the
/// AggregateExec node itself, not just its input).
fn find_partial_input(plan: Arc<dyn ExecutionPlan>) -> Option<Arc<dyn ExecutionPlan>> {
if let Some(agg) = plan.as_any().downcast_ref::<AggregateExec>() {
if *agg.mode() == AggregateMode::Partial {
return Some(plan);
}
return None;
// Non-Partial aggregate (Final/FinalPartitioned) — look into its input for Partial
return find_partial_input(Arc::clone(agg.input()));
}
if plan.as_any().downcast_ref::<RepartitionExec>().is_some()
|| plan
.as_any()
.downcast_ref::<CoalescePartitionsExec>()
.is_some()
{
let children = plan.children();
if children.len() == 1 {
return find_partial_input(Arc::clone(children[0]));
}
let children = plan.children();
if children.len() == 1 {
return find_partial_input(Arc::clone(children[0]));
}
None
}


/// Updates Column expression names to match the given schema (by index). Recurses into children.
fn remap_column(expr: Arc<dyn PhysicalExpr>, schema: &arrow::datatypes::SchemaRef) -> Arc<dyn PhysicalExpr> {
if let Some(col) = expr.as_any().downcast_ref::<Column>() {
return Arc::new(Column::new(schema.field(col.index()).name(), col.index()));
}
let children = expr.children();
if children.is_empty() { return expr; }
let new_children: Vec<_> = children.into_iter().map(|c| remap_column(c.clone(), schema)).collect();
let fallback = expr.clone();
expr.with_new_children(new_children).unwrap_or(fallback)
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
28 changes: 28 additions & 0 deletions sandbox/plugins/analytics-backend-datafusion/rust/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,7 @@ fn derive_schema_from_partial_plan(
let state = SessionStateBuilder::new()
.with_config(SessionConfig::new())
.with_default_features()
.with_physical_optimizer_rules(crate::agg_mode::physical_optimizer_rules_without_combine())
.build();
let ctx = SessionContext::new_with_state(state);
crate::udf::register_all(&ctx);
Expand Down Expand Up @@ -1186,8 +1187,35 @@ fn derive_schema_from_partial_plan(
let _ = ctx.register_table(&table_name, Arc::new(table));
}

// Extract the substrait-declared output names from Plan.Root.names — these are the
// user-facing aliases Java wrote (e.g. "RegionID", "u") and must match what the
// FINAL substrait's Read.base_schema declares on the coordinator side.
let declared_names: Vec<String> = plan.relations.iter().find_map(|pr| {
if let Some(substrait::proto::plan_rel::RelType::Root(rr)) = pr.rel_type.as_ref() {
Some(rr.names.clone())
} else {
None
}
}).unwrap_or_default();

let logical_plan = futures::executor::block_on(from_substrait_plan(&session_state, &plan))?;
let physical_plan = futures::executor::block_on(session_state.create_physical_plan(&logical_plan))?;

// Engine-native-merge (HLL): Partial has Binary fields that differ from the top (Int64).
// Use Partial schema + Root.names so coordinator sees the correct Binary wire type.
// All other plans: use top schema directly (matches main behavior).
if let Some(partial_schema) = crate::agg_mode::partial_aggregate_schema(&physical_plan) {
let has_binary = partial_schema.fields().iter().any(|f| matches!(f.data_type(), arrow::datatypes::DataType::Binary));
if has_binary && !declared_names.is_empty() && declared_names.len() == partial_schema.fields().len() {
use arrow::datatypes::{Field, Schema};
let coerced = crate::schema_coerce::coerce_inferred_schema(partial_schema);
let fields: Vec<Field> = coerced.fields().iter().zip(declared_names.iter())
.map(|(f, name)| Field::new(name.as_str(), f.data_type().clone(), f.is_nullable())
.with_metadata(f.metadata().clone()))
.collect();
return Ok(Arc::new(Schema::new_with_metadata(fields, coerced.metadata().clone())));
}
}
Ok(crate::schema_coerce::coerce_inferred_schema(physical_plan.schema()))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ impl LocalSession {
let logical_plan = from_substrait_plan(&self.ctx.state(), &plan).await?;
let dataframe = self.ctx.execute_logical_plan(logical_plan).await?;
let physical_plan = dataframe.create_physical_plan().await?;

let target_schema = crate::schema_coerce::coerce_inferred_schema(physical_plan.schema());
let physical_plan = crate::relabel_exec::wrap_if_relabel_needed(physical_plan, target_schema)?;
datafusion::physical_plan::execute_stream(physical_plan, self.ctx.task_ctx())
Expand Down Expand Up @@ -217,16 +218,19 @@ impl LocalSession {
))
})?;
let logical_plan = from_substrait_plan(&self.ctx.state(), &plan).await?;
log_debug!("DataFusion logical plan (reduce):\n{}", logical_plan.display_indent());
let dataframe = self.ctx.execute_logical_plan(logical_plan).await?;
let physical_plan = dataframe.create_physical_plan().await?;
let target_schema = crate::schema_coerce::coerce_inferred_schema(physical_plan.schema());
let physical_plan = crate::relabel_exec::wrap_if_relabel_needed(physical_plan, target_schema)?;
log_debug!("DataFusion physical plan (reduce):\n{}", displayable(physical_plan.as_ref()).indent(true));
// Strip first so `force_aggregate_mode(Final)` can find the Final/Partial pair
// through the raw plan; then derive `target_schema` and wrap with RelabelExec from
// the stripped output (otherwise the relabel target would carry the pre-strip Final
// output type tag and fail when wrapping the stripped tree).
let stripped = crate::agg_mode::apply_aggregate_mode(
physical_plan,
crate::agg_mode::Mode::Final,
)?;

let target_schema = crate::schema_coerce::coerce_inferred_schema(stripped.schema());
let stripped = crate::relabel_exec::wrap_if_relabel_needed(stripped, target_schema)?;
self.prepared_plan = Some(stripped);
Ok(())
}
Expand Down
Loading
Loading