47
47
import static org .elasticsearch .xpack .esql .type .EsqlDataTypes .CARTESIAN_POINT ;
48
48
import static org .elasticsearch .xpack .esql .type .EsqlDataTypes .GEO_POINT ;
49
49
50
- public class AggregateMapper {
50
+ final class AggregateMapper {
51
51
52
- static final List <String > NUMERIC = List .of ("Int" , "Long" , "Double" );
53
- static final List <String > SPATIAL = List .of ("GeoPoint" , "CartesianPoint" );
52
+ private static final List <String > NUMERIC = List .of ("Int" , "Long" , "Double" );
53
+ private static final List <String > SPATIAL = List .of ("GeoPoint" , "CartesianPoint" );
54
54
55
55
/** List of all mappable ESQL agg functions (excludes surrogates like AVG = SUM/COUNT). */
56
- static final List <? extends Class <? extends Function >> AGG_FUNCTIONS = List .of (
56
+ private static final List <? extends Class <? extends Function >> AGG_FUNCTIONS = List .of (
57
57
Count .class ,
58
58
CountDistinct .class ,
59
59
Max .class ,
@@ -66,23 +66,19 @@ public class AggregateMapper {
66
66
);
67
67
68
68
/** Record of agg Class, type, and grouping (or non-grouping). */
69
- record AggDef (Class <?> aggClazz , String type , String extra , boolean grouping ) {}
69
+ private record AggDef (Class <?> aggClazz , String type , String extra , boolean grouping ) {}
70
70
71
71
/** Map of AggDef types to intermediate named expressions. */
72
- private final Map <AggDef , List <IntermediateStateDesc >> mapper ;
72
+ private static final Map <AggDef , List <IntermediateStateDesc >> mapper = AGG_FUNCTIONS .stream ()
73
+ .flatMap (AggregateMapper ::typeAndNames )
74
+ .flatMap (AggregateMapper ::groupingAndNonGrouping )
75
+ .collect (Collectors .toUnmodifiableMap (aggDef -> aggDef , AggregateMapper ::lookupIntermediateState ));
73
76
74
77
/** Cache of aggregates to intermediate expressions. */
75
- private final HashMap <Expression , List <? extends NamedExpression >> cache = new HashMap <>() ;
78
+ private final HashMap <Expression , List <? extends NamedExpression >> cache ;
76
79
77
80
AggregateMapper () {
78
- this (AGG_FUNCTIONS );
79
- }
80
-
81
- AggregateMapper (List <? extends Class <? extends Function >> aggregateFunctionClasses ) {
82
- mapper = aggregateFunctionClasses .stream ()
83
- .flatMap (AggregateMapper ::typeAndNames )
84
- .flatMap (AggregateMapper ::groupingAndNonGrouping )
85
- .collect (Collectors .toUnmodifiableMap (aggDef -> aggDef , AggregateMapper ::lookupIntermediateState ));
81
+ cache = new HashMap <>();
86
82
}
87
83
88
84
public List <? extends NamedExpression > mapNonGrouping (List <? extends Expression > aggregates ) {
@@ -108,11 +104,10 @@ public List<? extends NamedExpression> mapGrouping(Expression aggregate) {
108
104
}
109
105
110
106
private Stream <? extends NamedExpression > map (Expression aggregate , boolean grouping ) {
111
- aggregate = Alias .unwrap (aggregate );
112
- return cache .computeIfAbsent (aggregate , aggKey -> computeEntryForAgg (aggKey , grouping )).stream ();
107
+ return cache .computeIfAbsent (Alias .unwrap (aggregate ), aggKey -> computeEntryForAgg (aggKey , grouping )).stream ();
113
108
}
114
109
115
- private List <? extends NamedExpression > computeEntryForAgg (Expression aggregate , boolean grouping ) {
110
+ private static List <? extends NamedExpression > computeEntryForAgg (Expression aggregate , boolean grouping ) {
116
111
var aggDef = aggDefOrNull (aggregate , grouping );
117
112
if (aggDef != null ) {
118
113
var is = getNonNull (aggDef );
@@ -128,7 +123,7 @@ private List<? extends NamedExpression> computeEntryForAgg(Expression aggregate,
128
123
}
129
124
130
125
/** Gets the agg from the mapper - wrapper around map::get for more informative failure.*/
131
- private List <IntermediateStateDesc > getNonNull (AggDef aggDef ) {
126
+ private static List <IntermediateStateDesc > getNonNull (AggDef aggDef ) {
132
127
var l = mapper .get (aggDef );
133
128
if (l == null ) {
134
129
throw new EsqlIllegalArgumentException ("Cannot find intermediate state for: " + aggDef );
@@ -268,9 +263,4 @@ private static String dataTypeToString(DataType type, Class<?> aggClass) {
268
263
throw new EsqlIllegalArgumentException ("illegal agg type: " + type .typeName ());
269
264
}
270
265
}
271
-
272
- private static Expression unwrapAlias (Expression expression ) {
273
- if (expression instanceof Alias alias ) return alias .child ();
274
- return expression ;
275
- }
276
266
}
0 commit comments