Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/139609.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 139609
summary: Fix ABSENT/PRESENT on agg with false filter
area: ES|QL
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,15 @@ is_absent:integer
1
// end::absent-as-integer-result[]
;

fixAbsentOnStatsWithFalseFilter
required_capability: fix_present_and_absent_on_stats_with_false_filter

ROW a = 3
| STATS x = ABSENT(a) WHERE FALSE
| KEEP x
;

x:boolean
true
;
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,15 @@ is_present:integer
0
// end::present-as-integer-result[]
;

fixPresentOnStatsWithFalseFilter
required_capability: fix_present_and_absent_on_stats_with_false_filter

ROW a = 3
| STATS x = PRESENT(a) WHERE FALSE
| KEEP x
;

x:boolean
false
;
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,13 @@ public enum Cap {
*/
FIX_INLINE_STATS_INCORRECT_PRUNNING(INLINE_STATS.enabled),

/**
* {@link org.elasticsearch.xpack.esql.optimizer.rules.logical.ReplaceStatsFilteredAggWithEval} replaced a stats
* with false filter with null with {@link org.elasticsearch.xpack.esql.expression.function.aggregate.Present} or
* {@link org.elasticsearch.xpack.esql.expression.function.aggregate.Absent}
*/
FIX_PRESENT_AND_ABSENT_ON_STATS_WITH_FALSE_FILTER,

// Last capability should still have a comma for fewer merge conflicts when adding new ones :)
// This comment prevents the semicolon from being on the previous capability when Spotless formats the file.
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.util.Holder;
import org.elasticsearch.xpack.esql.expression.function.aggregate.Absent;
import org.elasticsearch.xpack.esql.expression.function.aggregate.AggregateFunction;
import org.elasticsearch.xpack.esql.expression.function.aggregate.Count;
import org.elasticsearch.xpack.esql.expression.function.aggregate.CountDistinct;
import org.elasticsearch.xpack.esql.expression.function.aggregate.Present;
import org.elasticsearch.xpack.esql.plan.logical.Aggregate;
import org.elasticsearch.xpack.esql.plan.logical.Eval;
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
Expand Down Expand Up @@ -73,7 +75,7 @@ protected LogicalPlan rule(LogicalPlan plan) {
&& aggFunction.filter() instanceof Literal literal
&& Boolean.FALSE.equals(literal.value())) {

Object value = aggFunction instanceof Count || aggFunction instanceof CountDistinct ? 0L : null;
Object value = mapNullToValue(aggFunction);
Alias newAlias = alias.replaceChild(Literal.of(aggFunction, value));
newEvals.add(newAlias);
newProjections.add(newAlias.toAttribute());
Expand Down Expand Up @@ -117,6 +119,16 @@ protected LogicalPlan rule(LogicalPlan plan) {
return plan;
}

private static Object mapNullToValue(AggregateFunction aggFunction) {
return switch (aggFunction) {
case Count ignored -> 0L;
case CountDistinct ignored -> 0L;
case Absent ignored -> true;
case Present ignored -> false;
default -> null;
};
}

private static LogicalPlan updateAggregate(
Aggregate agg,
List<NamedExpression> newAggs,
Expand Down