7575
7676public class Top extends AggregateFunction
7777 implements
78- TwoOptionalArguments ,
79- ToAggregator ,
80- SurrogateExpression ,
81- PostOptimizationVerificationAware {
78+ TwoOptionalArguments ,
79+ ToAggregator ,
80+ SurrogateExpression ,
81+ PostOptimizationVerificationAware {
8282 public static final NamedWriteableRegistry .Entry ENTRY = new NamedWriteableRegistry .Entry (Expression .class , "Top" , Top ::new );
8383
8484 private static final String ORDER_ASC = "ASC" ;
@@ -117,11 +117,19 @@ public Top(
117117 description = "The extra field that, if present, will be the output of the TOP call instead of `field`."
118118 ) Expression outputField
119119 ) {
120- this (source , field , Literal .TRUE , limit , order == null ? Literal .keyword (source , ORDER_ASC ) : order , outputField );
120+ this (source , field , Literal .TRUE , NO_WINDOW , limit , order == null ? Literal .keyword (source , ORDER_ASC ) : order , outputField );
121121 }
122122
123- public Top (Source source , Expression field , Expression filter , Expression limit , Expression order , @ Nullable Expression outputField ) {
124- super (source , field , filter , outputField != null ? asList (limit , order , outputField ) : asList (limit , order ));
123+ public Top (
124+ Source source ,
125+ Expression field ,
126+ Expression filter ,
127+ Expression window ,
128+ Expression limit ,
129+ Expression order ,
130+ @ Nullable Expression outputField
131+ ) {
132+ super (source , field , filter , window , outputField != null ? asList (limit , order , outputField ) : asList (limit , order ));
125133 }
126134
127135 private Top (StreamInput in ) throws IOException {
@@ -130,7 +138,7 @@ private Top(StreamInput in) throws IOException {
130138
131139 @ Override
132140 public Top withFilter (Expression filter ) {
133- return new Top (source (), field (), filter , limitField (), orderField (), outputField ());
141+ return new Top (source (), field (), filter , window (), limitField (), orderField (), outputField ());
134142 }
135143
136144 @ Override
@@ -191,15 +199,15 @@ protected TypeResolution resolveType() {
191199 .and (isString (orderField (), sourceText (), THIRD ));
192200 if (outputField () != null ) {
193201 typeResolution = typeResolution .and (
194- isType (
195- outputField (),
196- dt -> dt == DataType .DATETIME || (dt .isNumeric () && dt != DataType .UNSIGNED_LONG ),
197- sourceText (),
198- FOURTH ,
199- "date" ,
200- "numeric except unsigned_long or counter types"
201- )
202+ isType (
203+ outputField (),
204+ dt -> dt == DataType .DATETIME || (dt .isNumeric () && dt != DataType .UNSIGNED_LONG ),
205+ sourceText (),
206+ FOURTH ,
207+ "date" ,
208+ "numeric except unsigned_long or counter types"
202209 )
210+ )
203211 .and (
204212 isType (
205213 field (),
@@ -305,7 +313,7 @@ public DataType dataType() {
305313
306314 @ Override
307315 protected NodeInfo <Top > info () {
308- return NodeInfo .create (this , Top ::new , field (), filter (), limitField (), orderField (), outputField ());
316+ return NodeInfo .create (this , Top ::new , field (), filter (), window (), limitField (), orderField (), outputField ());
309317 }
310318
311319 @ Override
@@ -316,7 +324,8 @@ public Top replaceChildren(List<Expression> newChildren) {
316324 newChildren .get (1 ),
317325 newChildren .get (2 ),
318326 newChildren .get (3 ),
319- newChildren .size () > 4 ? newChildren .get (4 ) : null
327+ newChildren .get (4 ),
328+ newChildren .size () > 5 ? newChildren .get (5 ) : null
320329 );
321330 }
322331
@@ -388,9 +397,9 @@ public Expression surrogate() {
388397 }
389398 if (orderField () instanceof Literal && limitField () instanceof Literal && limitValue () == 1 ) {
390399 if (orderValue ()) {
391- return new Min (s , field (), filter ());
400+ return new Min (s , field (), filter (), window () );
392401 } else {
393- return new Max (s , field (), filter ());
402+ return new Max (s , field (), filter (), window () );
394403 }
395404 }
396405 return null ;
0 commit comments