Skip to content

Commit 5995b0e

Browse files
authored
ESQL: Reuse references() (elastic#134452)
Makes calls to `Expression#references()` reuse the reference set returned by it's child when there is only one child. This should save on many `AttributeSet`s, especially in degenerate plans that have a zillion `EVAL`s. Things like `Alias` will always be able to reuse the child references. So will unary functions. And probably other things. This does require that folks treat the `references()` as immutable. I thikn they technically are mutable, but no one I *saw* mutates them because that's... going to break things anyway.
1 parent 329e2c1 commit 5995b0e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/Expression.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.xpack.esql.core.util.StringUtils;
1616

1717
import java.util.List;
18+
import java.util.Set;
1819
import java.util.function.Supplier;
1920

2021
/**
@@ -101,7 +102,9 @@ public Object fold(FoldContext ctx) {
101102

102103
public abstract Nullability nullable();
103104

104-
// the references/inputs/leaves of the expression tree
105+
/**
106+
* {@link Set} of {@link Attribute}s referenced by this {@link Expression}.
107+
*/
105108
public AttributeSet references() {
106109
if (lazyReferences == null) {
107110
lazyReferences = Expressions.references(children());

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/Expressions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ public static List<Object> fold(FoldContext ctx, List<? extends Expression> exps
101101
}
102102

103103
public static AttributeSet references(List<? extends Expression> exps) {
104+
if (exps.size() == 1) {
105+
/*
106+
* If we're getting the references from a single expression it's safe
107+
* to just use its references. This is quite common. We use a ton of
108+
* Aliases, for example. And every unary function can share its references
109+
* with its input.
110+
*/
111+
return exps.getFirst().references();
112+
}
104113
return AttributeSet.of(exps, Expression::references);
105114
}
106115

0 commit comments

Comments
 (0)