001package gudusoft.gsqlparser.nodes;
002
003import gudusoft.gsqlparser.TSourceToken;
004import gudusoft.gsqlparser.nodes.hana.TTimeTravel;
005import gudusoft.gsqlparser.nodes.mssql.TForXMLClause;
006import gudusoft.gsqlparser.nodes.mssql.TOptionClause;
007import gudusoft.gsqlparser.nodes.teradata.TExpandOnClause;
008import gudusoft.gsqlparser.nodes.hive.*;
009import gudusoft.gsqlparser.nodes.teradata.THashByClause;
010import gudusoft.gsqlparser.nodes.vertica.TTimeSeries;
011import gudusoft.gsqlparser.stmt.TSelectSqlStatement;
012
013import java.util.ArrayList;
014
015/**
016 * Internal used by parser, reprsents raw parse tree node of select statement.
017*/
018public class TSelectSqlNode extends TParseTreeNode{
019
020    private TForXMLClause forXMLClause;
021
022    public void setForXMLClause(TForXMLClause forXMLClause) {
023        this.forXMLClause = forXMLClause;
024    }
025
026    public TForXMLClause getForXMLClause() {
027        return forXMLClause;
028    }
029
030    public void setRelationExpr(TRelationExpr relationExpr) {
031        this.relationExpr = relationExpr;
032    }
033
034    public TRelationExpr getRelationExpr() {
035        return relationExpr;
036    }
037
038    private TRelationExpr relationExpr = null;
039
040    private TClusterBy clusterBy;
041
042    public void setClusterBy(TClusterBy clusterBy) {
043        this.clusterBy = clusterBy;
044    }
045
046    public TClusterBy getClusterBy() {
047        return clusterBy;
048    }
049
050    private boolean isConsume;
051
052    public void setConsume(boolean consume) {
053        isConsume = consume;
054    }
055
056    public boolean isConsume() {
057        return isConsume;
058    }
059
060    public void setHiveFromQuery(boolean hiveFromQuery) {
061        isHiveFromQuery = hiveFromQuery;
062    }
063
064    private boolean isHiveFromQuery = false;
065
066    public boolean isHiveFromQuery() {
067        return isHiveFromQuery;
068    }
069
070    public void setHiveBodyList(ArrayList<TParseTreeNode> hiveBodyList) {
071        this.hiveBodyList = hiveBodyList;
072    }
073
074    /**
075     * @deprecated since 2.6.3.5, insert and select statement after from query
076     * is represented by {@link TSelectSqlNode#getSelectSqlNodes()} or {@link TInsertSqlNode#getInsertSqlNodes()}
077     *
078     * @return
079     */
080    public ArrayList<TParseTreeNode> getHiveBodyList() {
081        return hiveBodyList;
082    }
083
084    private ArrayList<TParseTreeNode> hiveBodyList;
085
086
087
088//    private TExpression skipOffset;
089//    private TExpression firstMax;
090//    private TExpression limitMax;
091//
092//    public void setSkipOffset(TExpression skipOffset) {
093//        this.skipOffset = skipOffset;
094//    }
095//
096//    public void setFirstMax(TExpression firstMax) {
097//        this.firstMax = firstMax;
098//    }
099//
100//    public void setLimitMax(TExpression limitMax) {
101//        this.limitMax = limitMax;
102//    }
103//
104//    /**
105//     * Informix skip offset
106//     * @return skip offset
107//     */
108//    public TExpression getSkipOffset() {
109//
110//        return skipOffset;
111//    }
112//
113//    /**
114//     * Informix first Max
115//     * @return first Max
116//     */
117//    public TExpression getFirstMax() {
118//        return firstMax;
119//    }
120//
121//    /**
122//     * Informix limit max
123//     * @return limit max
124//     */
125//    public TExpression getLimitMax() {
126//        return limitMax;
127//    }
128
129    private THashByClause hashByClause;
130
131    public void setHashByClause(THashByClause hashByClause) {
132        this.hashByClause = hashByClause;
133    }
134
135    public THashByClause getHashByClause() {
136        return hashByClause;
137    }
138
139    private THavingClause havingClause;
140
141    public void setHavingClause(THavingClause havingClause) {
142        this.havingClause = havingClause;
143        if (this.groupByClause != null){
144            this.groupByClause.setHavingClause(havingClause.getCondition());
145        }else{
146            this.groupByClause = new TGroupBy();
147            this.groupByClause.setHavingClause(havingClause.getCondition());
148        }
149    }
150
151    public THavingClause getHavingClause() {
152        return havingClause;
153    }
154
155    private TPTNodeList<TParseTreeNode> queryClauseList;
156
157    public void setQueryClauseList(TPTNodeList<TParseTreeNode> queryClauseList) {
158        this.queryClauseList = queryClauseList;
159        for(int i=0;i<this.queryClauseList.size();i++){
160            TParseTreeNode parseTreeNode = this.queryClauseList.getElement(i);
161            if (parseTreeNode instanceof TFromTableList){
162                //from clause
163                this.setFromTableList((TFromTableList)parseTreeNode);
164            }else if (parseTreeNode instanceof THashByClause){
165                this.setHashByClause((THashByClause)parseTreeNode);
166            }else if (parseTreeNode instanceof TWhereClause){
167                this.setWhereCondition((TWhereClause)parseTreeNode);
168            }else if (parseTreeNode instanceof TGroupBy){
169                this.setGroupByClause((TGroupBy)parseTreeNode);
170            }else if (parseTreeNode instanceof THavingClause){
171                this.setHavingClause((THavingClause)parseTreeNode);
172            }else if (parseTreeNode instanceof TQualifyClause){
173                this.setQualifyClause((TQualifyClause)parseTreeNode);
174            }else if (parseTreeNode instanceof TSampleClause){
175                this.setSampleClause((TSampleClause)parseTreeNode);
176            }else if (parseTreeNode instanceof TExpandOnClause){
177                this.setExpandOnClause((TExpandOnClause)parseTreeNode);
178            }else if (parseTreeNode instanceof TTeradataWithClause){
179                this.setWithClause((TTeradataWithClause)parseTreeNode);
180            }else if (parseTreeNode instanceof TOrderBy){
181                this.setOrderbyClause((TOrderBy)parseTreeNode);
182            }else if (parseTreeNode instanceof TIntoClause){
183                this.setIntoClause((TIntoClause)parseTreeNode);
184            }else if (parseTreeNode instanceof TClusterBy){
185                this.setClusterBy((TClusterBy)parseTreeNode);
186            }else if (parseTreeNode instanceof TDistributeBy){
187                this.setDistributeBy((TDistributeBy)parseTreeNode);
188            }else if (parseTreeNode instanceof TSortBy){
189                this.setSortBy((TSortBy)parseTreeNode);
190            }
191        }
192    }
193
194    public void setSelectModifiers(ArrayList<TSelectModifier> selectModifiers) {
195        this.selectModifiers = selectModifiers;
196    }
197
198    public ArrayList<TSelectModifier> getSelectModifiers() {
199        return selectModifiers;
200    }
201
202    private ArrayList<TSelectModifier> selectModifiers;
203
204    private TWithTableLock withTableLock;
205
206    public void setWithTableLock(TWithTableLock withTableLock) {
207        this.withTableLock = withTableLock;
208    }
209
210    public TWithTableLock getWithTableLock() {
211
212        return withTableLock;
213    }
214
215    private TTimeSeries timeSeries;
216
217    public void setTimeSeries(TTimeSeries timeSeries) {
218        this.timeSeries = timeSeries;
219    }
220
221    public TTimeSeries getTimeSeries() {
222
223        return timeSeries;
224    }
225
226    private TTimeTravel timeTravel; // hana
227    private THintClause hintClause; //hana
228
229    public TTimeTravel getTimeTravel() {
230        return timeTravel;
231    }
232
233    public void setClauses(TPTNodeList<TParseTreeNode> clauses){
234        for(int i=0;i<clauses.size();i++){
235            TParseTreeNode node = clauses.getElement(i);
236            if (node instanceof TForUpdate){
237                this.forupdateClause = (TForUpdate)node;
238            }else if (node instanceof TOrderBy){
239                this.orderbyClause = (TOrderBy)node;
240            }else if (node instanceof TLimitClause){
241                this.limitClause  = (TLimitClause)node;
242            }else if (node instanceof TTimeTravel){
243                this.timeTravel = (TTimeTravel)node;
244            }
245        }
246    }
247
248
249    public void setHintClause(THintClause hintClause) {
250        this.hintClause = hintClause;
251    }
252
253    public THintClause getHintClause() {
254
255        return hintClause;
256    }
257
258    public int getParenthesisCount() {
259        return parenthesisCount;
260    }
261
262    public void incParenthesisCount() {
263       this.parenthesisCount++;
264    }
265
266    private int parenthesisCount = 0;
267    private int parenthissisCountBeforeOrder = 0;
268
269
270    public void setParenthesisCount(int parenthesisCount) {
271        this.parenthesisCount = parenthesisCount;
272    }
273
274    public void setParenthissisCountBeforeOrder() {
275
276        this.parenthissisCountBeforeOrder = parenthesisCount;
277        parenthesisCount = 0;
278    }
279
280    public int getParenthissisCountBeforeOrder() {
281
282        return parenthissisCountBeforeOrder;
283    }
284
285    private TOffsetClause offsetClause;
286
287    public void setOffsetClause(TOffsetClause offsetClause) {
288        this.offsetClause = offsetClause;
289    }
290
291    public TOffsetClause getOffsetClause() {
292
293        return offsetClause;
294    }
295
296    public void setOptionClause(TOptionClause optionClause) {
297        this.optionClause = optionClause;
298    }
299
300    public TOptionClause getOptionClause() {
301
302        return optionClause;
303    }
304
305    /**
306     * sql server option clause
307     * @see gudusoft.gsqlparser.nodes.mssql.TOptionClause
308     */
309    private TOptionClause optionClause;
310
311    private TLateralView lateralView;
312
313    public void setLateralView(TLateralView lateralView) {
314        this.lateralView = lateralView;
315    }
316
317    public TLateralView getLateralView() {
318
319        return lateralView;
320    }
321
322    private TSortBy sortBy;
323
324    public void setSortBy(TSortBy sortBy) {
325        this.sortBy = sortBy;
326    }
327
328    public TSortBy getSortBy() {
329
330        return sortBy;
331    }
332
333    private TDistributeBy distributeBy;
334
335    public void setDistributeBy(TDistributeBy distributeBy) {
336        this.distributeBy = distributeBy;
337    }
338
339    public TDistributeBy getDistributeBy() {
340
341        return distributeBy;
342    }
343
344//    private THiveClusterBy hiveClusterBy;
345//
346//    public void setHiveClusterBy(THiveClusterBy hiveClusterBy) {
347//        this.hiveClusterBy = hiveClusterBy;
348//    }
349//
350//    public THiveClusterBy getHiveClusterBy() {
351//
352//        return hiveClusterBy;
353//    }
354
355    private THiveTransformClause transformClause;
356
357    public THiveTransformClause getTransformClause() {
358        return transformClause;
359    }
360
361    public void setTransformClause(THiveTransformClause transformClause) {
362        this.transformClause = transformClause;
363    }
364
365    public void setHiveHintClause(THiveHintClause hiveHintClause) {
366        this.hiveHintClause = hiveHintClause;
367    }
368
369    public THiveHintClause getHiveHintClause() {
370        return hiveHintClause;
371    }
372
373    private THiveHintClause hiveHintClause;
374
375    private TIntoTableClause intoTableClause;
376
377    public void setIntoTableClause(TIntoTableClause intoTableClause) {
378        this.intoTableClause = intoTableClause;
379    }
380
381    public TIntoTableClause getIntoTableClause() {
382
383        return intoTableClause;
384    }
385
386    public void setSelectToken(TSourceToken selectToken) {
387        this.selectToken = selectToken;
388    }
389
390    public TSourceToken getSelectToken() {
391
392        return selectToken;
393    }
394
395    private TSourceToken selectToken = null;
396
397    private TResultColumnList resultColumnList;
398
399    public TSelectDistinct getSelectDistinct() {
400        return selectDistinct;
401    }
402
403    public void setSelectDistinct(TSelectDistinct selectDistinct) {
404
405        this.selectDistinct = selectDistinct;
406    }
407
408    private TSelectDistinct selectDistinct = null;
409
410    public void setTopClause(TTopClause topClause) {
411        this.topClause = topClause;
412    }
413
414    public TTopClause getTopClause() {
415
416        return topClause;
417    }
418
419    private TTopClause topClause = null;
420
421    public void setComputeClause(TComputeClause computeClause) {
422
423        this.computeClause = computeClause;
424    }
425
426    public TComputeClause getComputeClause() {
427        return computeClause;
428    }
429
430    private TComputeClause computeClause = null;
431    
432    public TResultColumnList getResultColumnList() {
433        return resultColumnList;
434    }
435
436    public TFromTableList getFromTableList() {
437        return fromTableList;
438    }
439
440    public TWhereClause getWhereCondition() {
441        return whereCondition;
442    }
443
444    private TFromTableList fromTableList;
445
446    public void setResultColumnList(TResultColumnList resultColumnList) {
447        this.resultColumnList = resultColumnList;
448    }
449
450    public void setFromTableList(TFromTableList fromTableList) {
451        this.fromTableList = fromTableList;
452    }
453
454    public void setWhereCondition(TWhereClause whereCondition) {
455        this.whereCondition = whereCondition;
456    }
457
458    private TWhereClause whereCondition;
459
460    private TSelectSqlNode leftNode,rightNode;
461
462    public TCTEList cteList;
463
464    public TCTEList getCteList() {
465        return cteList;
466    }
467
468    // Oracle 12c WITH FUNCTION/PROCEDURE support - inline PL/SQL definitions
469    private TStatementListSqlNode withPlsqlItems;
470
471    public TStatementListSqlNode getWithPlsqlItems() {
472        return withPlsqlItems;
473    }
474
475    public void setWithPlsqlItems(TStatementListSqlNode withPlsqlItems) {
476        this.withPlsqlItems = withPlsqlItems;
477    }
478
479    public int getSetOperator() {
480        return setOperator;
481    }
482
483    private int setOperator = 0;
484    private boolean setOperatorByName = false;
485
486    public void setSetOperator(int setOperator) {
487        this.setOperator = setOperator;
488    }
489
490    /**
491     * Returns true if the set operator uses BY NAME matching (Snowflake).
492     * When BY NAME is used, columns are matched by their names rather than positions.
493     */
494    public boolean isSetOperatorByName() {
495        return setOperatorByName;
496    }
497
498    /**
499     * Sets whether the set operator uses BY NAME matching.
500     */
501    public void setSetOperatorByName(boolean byName) {
502        this.setOperatorByName = byName;
503    }
504
505    public void setSetOperator(TSourceToken st1, TSourceToken st2)
506    {
507        if (st1.toString().equalsIgnoreCase("union")){
508            if (st2 == null){
509                this.setOperator = TSelectSqlStatement.setOperator_union;
510            }else if (st2.toString().equalsIgnoreCase("all")){
511                this.setOperator = TSelectSqlStatement.setOperator_unionall;
512            }else if (st2.toString().equalsIgnoreCase("distinct")){
513                this.setOperator = TSelectSqlStatement.SET_OPERATOR_UNIONDISTINCT;
514            }
515        }
516        else if (st1.toString().equalsIgnoreCase("intersect")){
517            if (st2 == null){
518                this.setOperator = TSelectSqlStatement.setOperator_intersect;
519            }else if (st2.toString().equalsIgnoreCase("all")){
520                this.setOperator = TSelectSqlStatement.setOperator_intersectall;
521            }else if (st2.toString().equalsIgnoreCase("distinct")){
522                this.setOperator = TSelectSqlStatement.SET_OPERATOR_INTERSECTDISTINCT;
523            }
524        }
525        else if (st1.toString().equalsIgnoreCase("minus")){
526            if (st2 == null){
527                this.setOperator = TSelectSqlStatement.setOperator_minus;
528            }else if (st2.toString().equalsIgnoreCase("all")){
529                this.setOperator = TSelectSqlStatement.setOperator_minusall;
530            }else if (st2.toString().equalsIgnoreCase("distinct")){
531                this.setOperator = TSelectSqlStatement.SET_OPERATOR_MINUSDISTINCT;
532            }
533        }
534        else if (st1.toString().equalsIgnoreCase("except")){
535            if (st2 == null){
536                this.setOperator = TSelectSqlStatement.setOperator_except;
537            }else if (st2.toString().equalsIgnoreCase("all")){
538                this.setOperator = TSelectSqlStatement.setOperator_exceptall;
539            }else if (st2.toString().equalsIgnoreCase("distinct")){
540                this.setOperator = TSelectSqlStatement.SET_OPERATOR_EXCEPTDISTINCT;
541            }
542        }
543    }
544
545    public TSelectSqlNode getLeftNode() {
546        return leftNode;
547    }
548
549    public TSelectSqlNode getRightNode() {
550        return rightNode;
551    }
552
553    public void init(Object arg1, Object arg2)
554    {
555        leftNode = (TSelectSqlNode)arg1;
556        rightNode = (TSelectSqlNode)arg2;
557    }
558
559    public boolean isCombinedQuery(){
560    // combine multiple queries using the set operators UNION, UNION ALL, INTERSECT, and MINUS.
561        return (setOperator > 0);
562    }
563
564    public TOrderBy getOrderbyClause() {
565        return orderbyClause;
566    }
567
568    private TOrderBy orderbyClause;
569
570    public void setOrderbyClause(TOrderBy orderbyClause) {
571        this.orderbyClause = orderbyClause;
572        if  (orderbyClause == null) return;
573
574        if (orderbyClause.getOffsetClause() != null ){ // 在 sqlserver yacc rule中, offset clause 先被挂在了order by clause 下
575            this.setOffsetClause(orderbyClause.getOffsetClause());
576        }
577        if (orderbyClause.getFetchFirstClause() != null){
578            this.setFetchFirstClause(orderbyClause.getFetchFirstClause());
579        }
580
581    }
582
583    public TForUpdate getForupdateClause() {
584        return forupdateClause;
585    }
586
587    private TForUpdate forupdateClause;
588
589    public void setForupdateClause(TParseTreeNode forupdateClause) {
590        if (forupdateClause instanceof TForUpdate){
591            this.forupdateClause = (TForUpdate) forupdateClause;
592        }else if (forupdateClause instanceof TForXMLClause){
593            this.forXMLClause = (TForXMLClause)forupdateClause;
594        }
595    }
596
597    public THierarchical getHierarchicalClause() {
598        return hierarchicalClause;
599    }
600
601    private THierarchical hierarchicalClause;
602
603    public void setHierarchicalClause(THierarchical hierarchicalClause) {
604        this.hierarchicalClause = hierarchicalClause;
605    }
606
607    public TGroupBy getGroupByClause() {
608        return groupByClause;
609    }
610
611    private TGroupBy groupByClause;
612
613    public void setGroupByClause(TGroupBy groupByClause) {
614        this.groupByClause = groupByClause;
615        if (groupByClause != null){
616            if ((groupByClause.getQualifyClause() != null)&&(this.getQualifyClause() == null)){
617                this.setQualifyClause(groupByClause.getQualifyClause());
618            }
619            if ((this.getHavingClause() != null)&&(this.groupByClause.getHavingClause() == null)){
620                this.groupByClause.setHavingClause(this.getHavingClause().getCondition());
621            }
622        }
623    }
624
625    private TIntoClause intoClause = null;
626
627    public void setIntoClause(TIntoClause intoClause) {
628        if (intoClause == null) return;
629        this.intoClause = intoClause;
630    }
631
632    public TIntoClause getIntoClause() {
633
634        return intoClause;
635    }
636
637    private TQualifyClause qualifyClause = null;
638
639    public void setQualifyClause(TQualifyClause qualifyClause) {
640        if (qualifyClause == null) return;
641        this.qualifyClause = qualifyClause;
642    }
643
644    public TQualifyClause getQualifyClause() {
645
646        return qualifyClause;
647    }
648
649    private TSampleClause sampleClause = null;
650
651    public void setSampleClause(TSampleClause sampleClause) {
652        this.sampleClause = sampleClause;
653    }
654
655    public TSampleClause getSampleClause() {
656
657        return sampleClause;
658    }
659
660    private TTeradataWithClause withClause = null;
661
662    public TTeradataWithClause getWithClause() {
663        return withClause;
664    }
665
666    public void setWithClause(TTeradataWithClause withClause) {
667
668        this.withClause = withClause;
669    }
670
671    private TLimitClause limitClause = null;
672
673    public void setLimitClause(TLimitClause limitClause) {
674        this.limitClause = limitClause;
675    }
676
677    public TLimitClause getLimitClause() {
678
679        return limitClause;
680    }
681
682    private TFetchFirstClause fetchFirstClause = null;
683
684    public void setFetchFirstClause(TFetchFirstClause fetchFirstClause) {
685        this.fetchFirstClause = fetchFirstClause;
686    }
687
688    public TFetchFirstClause getFetchFirstClause() {
689
690        return fetchFirstClause;
691    }
692
693    private TOptimizeForClause optimizeForClause = null;
694
695    public void setOptimizeForClause(TOptimizeForClause optimizeForClause) {
696        this.optimizeForClause = optimizeForClause;
697    }
698
699    public TOptimizeForClause getOptimizeForClause() {
700
701        return optimizeForClause;
702    }
703
704    private TIsolationClause isolationClause = null;
705
706    public void setIsolationClause(TIsolationClause isolationClause) {
707        this.isolationClause = isolationClause;
708    }
709
710    public TIsolationClause getIsolationClause() {
711        return isolationClause;
712    }
713
714    public void setValueClause(TValueClause valueClause) {
715        this.valueClause = valueClause;
716        this.setStartToken(valueClause);
717        this.setEndToken(valueClause);
718    }
719
720    public TValueClause getValueClause() {
721
722        return valueClause;
723    }
724
725    private TValueClause valueClause = null;
726
727    private TPTNodeList <TLockingClause> lockingClauses;
728
729    public TPTNodeList<TLockingClause> getLockingClauses() {
730        return lockingClauses;
731    }
732
733    public void setLockingClauses(TPTNodeList<TLockingClause> lockingClauses) {
734        this.lockingClauses = lockingClauses;
735    }
736
737//    private TSelectLimit selectLimit;
738//
739//    public void setSelectLimit(TSelectLimit selectLimit) {
740//        this.selectLimit = selectLimit;
741//    }
742//
743//    public TSelectLimit getSelectLimit() {
744//
745//        return selectLimit;
746//    }
747
748    private  TWindowClause  windowClause;
749
750    public void setWindowClause(TWindowClause windowClause) {
751        this.windowClause = windowClause;
752    }
753
754    public TWindowClause getWindowClause() {
755
756        return windowClause;
757    }
758
759    private TExpandOnClause expandOnClause;
760
761    public TExpandOnClause getExpandOnClause() {
762        return expandOnClause;
763    }
764
765    public void setExpandOnClause(TExpandOnClause expandOnClause) {
766
767        this.expandOnClause = expandOnClause;
768    }
769
770    public static void setFromList(ArrayList<TSelectSqlNode> selectSqlNode, TFromTableList fromTables){
771        for(int i=0;i<selectSqlNode.size();i++){
772            TSelectSqlNode select = selectSqlNode.get(i);
773            select.setFromTableList(fromTables);
774        }
775    }
776
777    private ArrayList<TSelectSqlNode> selectSqlNodes;
778
779    public ArrayList<TSelectSqlNode> getSelectSqlNodes() {
780        return selectSqlNodes;
781    }
782
783    public void setSelectSqlNodes(ArrayList<TSelectSqlNode> selectSqlNodes){
784        this.selectSqlNodes = selectSqlNodes;
785    }
786
787    public static TSelectSqlNode getFirstSelectSqlNode(ArrayList<TSelectSqlNode> selectSqlNodes){
788        return  selectSqlNodes.get(0);
789    }
790
791    // StarRocks pipe operator support
792    private TPTNodeList<TParseTreeNode> pipeOperators;
793    private boolean isPipeQuery = false;
794
795    public TPTNodeList<TParseTreeNode> getPipeOperators() {
796        return pipeOperators;
797    }
798
799    public void setPipeOperators(TPTNodeList<TParseTreeNode> pipeOperators) {
800        this.pipeOperators = pipeOperators;
801    }
802
803    public boolean isPipeQuery() {
804        return isPipeQuery;
805    }
806
807    public void setIsPipeQuery(boolean isPipeQuery) {
808        this.isPipeQuery = isPipeQuery;
809    }
810
811}
812