1616import net .sf .jsqlparser .statement .select .OrderByElement ;
1717
1818/**
19- * Analytic function. The name of the function is variable but the parameters following the special
20- * analytic function path. e.g. row_number() over (order by test). Additional there can be an
21- * expression for an analytical aggregate like sum(col) or the "all collumns" wildcard like
22- * count(*).
19+ * Analytic function. The name of the function is variable but the parameters following the special analytic function
20+ * path. e.g. row_number() over (order by test). Additional there can be an expression for an analytical aggregate like
21+ * sum(col) or the "all collumns" wildcard like count(*).
2322 *
2423 * @author tw
2524 */
2625public class AnalyticExpression extends ASTNodeAccessImpl implements Expression {
2726
28- private final OrderByClause orderBy = new OrderByClause ();
29- private final PartitionByClause partitionBy = new PartitionByClause ();
3027 private String name ;
3128 private Expression expression ;
3229 private Expression offset ;
@@ -39,8 +36,9 @@ public class AnalyticExpression extends ASTNodeAccessImpl implements Expression
3936 private boolean ignoreNulls = false ; //IGNORE NULLS inside function parameters
4037 private boolean ignoreNullsOutside = false ; //IGNORE NULLS outside function parameters
4138 private Expression filterExpression = null ;
42- private WindowElement windowElement = null ;
4339 private List <OrderByElement > funcOrderBy = null ;
40+ private String windowName = null ; // refers to an external window definition (paritionBy, orderBy, windowElement)
41+ private WindowDefinition windowDef = new WindowDefinition ();
4442
4543 public AnalyticExpression () {
4644 }
@@ -76,11 +74,11 @@ public void accept(ExpressionVisitor expressionVisitor) {
7674 }
7775
7876 public List <OrderByElement > getOrderByElements () {
79- return orderBy .getOrderByElements ();
77+ return windowDef . orderBy .getOrderByElements ();
8078 }
8179
8280 public void setOrderByElements (List <OrderByElement > orderByElements ) {
83- orderBy .setOrderByElements (orderByElements );
81+ windowDef . orderBy .setOrderByElements (orderByElements );
8482 }
8583
8684 public KeepExpression getKeep () {
@@ -92,19 +90,19 @@ public void setKeep(KeepExpression keep) {
9290 }
9391
9492 public ExpressionList getPartitionExpressionList () {
95- return partitionBy .getPartitionExpressionList ();
93+ return windowDef . partitionBy .getPartitionExpressionList ();
9694 }
9795
9896 public void setPartitionExpressionList (ExpressionList partitionExpressionList ) {
9997 setPartitionExpressionList (partitionExpressionList , false );
10098 }
10199
102100 public void setPartitionExpressionList (ExpressionList partitionExpressionList , boolean brackets ) {
103- partitionBy .setPartitionExpressionList (partitionExpressionList , brackets );
101+ windowDef . partitionBy .setPartitionExpressionList (partitionExpressionList , brackets );
104102 }
105103
106104 public boolean isPartitionByBrackets () {
107- return partitionBy .isBrackets ();
105+ return windowDef . partitionBy .isBrackets ();
108106 }
109107
110108 public String getName () {
@@ -140,11 +138,11 @@ public void setDefaultValue(Expression defaultValue) {
140138 }
141139
142140 public WindowElement getWindowElement () {
143- return windowElement ;
141+ return windowDef . windowElement ;
144142 }
145143
146144 public void setWindowElement (WindowElement windowElement ) {
147- this .windowElement = windowElement ;
145+ windowDef .windowElement = windowElement ;
148146 }
149147
150148 public AnalyticType getType () {
@@ -187,6 +185,22 @@ public void setIgnoreNullsOutside(boolean ignoreNullsOutside) {
187185 this .ignoreNullsOutside = ignoreNullsOutside ;
188186 }
189187
188+ public String getWindowName () {
189+ return windowName ;
190+ }
191+
192+ public void setWindowName (String windowName ) {
193+ this .windowName = windowName ;
194+ }
195+
196+ public WindowDefinition getWindowDefinition () {
197+ return windowDef ;
198+ }
199+
200+ public void setWindowDefinition (WindowDefinition windowDef ) {
201+ this .windowDef = windowDef ;
202+ }
203+
190204 @ Override
191205 @ SuppressWarnings ({"PMD.CyclomaticComplexity" , "PMD.NPathComplexity" , "PMD.MissingBreakInSwitch" })
192206 public String toString () {
@@ -210,11 +224,11 @@ public String toString() {
210224 if (isIgnoreNulls ()) {
211225 b .append (" IGNORE NULLS" );
212226 }
213- if (funcOrderBy != null ) {
227+ if (funcOrderBy != null ) {
214228 b .append (" ORDER BY " );
215- b .append ( funcOrderBy .stream ().map (OrderByElement ::toString ).collect (joining (", " )));
229+ b .append (funcOrderBy .stream ().map (OrderByElement ::toString ).collect (joining (", " )));
216230 }
217-
231+
218232 b .append (") " );
219233 if (keep != null ) {
220234 b .append (keep .toString ()).append (" " );
@@ -232,7 +246,7 @@ public String toString() {
232246 if (isIgnoreNullsOutside ()) {
233247 b .append ("IGNORE NULLS " );
234248 }
235-
249+
236250 switch (type ) {
237251 case FILTER_ONLY :
238252 return b .toString ();
@@ -242,20 +256,14 @@ public String toString() {
242256 default :
243257 b .append ("OVER" );
244258 }
245- b .append (" (" );
246259
247- partitionBy .toStringPartitionBy (b );
248- orderBy .toStringOrderByElements (b );
249-
250- if (windowElement != null ) {
251- if (orderBy .getOrderByElements () != null ) {
252- b .append (' ' );
253- }
254- b .append (windowElement );
260+ if (windowName != null ) {
261+ b .append (" " ).append (windowName );
262+ } else {
263+ b .append (" " );
264+ b .append (windowDef .toString ());
255265 }
256266
257- b .append (")" );
258-
259267 return b .toString ();
260268 }
261269
0 commit comments