001package gudusoft.gsqlparser.nodes;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.nodes.hive.THiveFromQuerySqlNode;
005import gudusoft.gsqlparser.nodes.hive.THiveKeyValueProperty;
006import gudusoft.gsqlparser.nodes.hive.THivePartitionedTableFunction;
007import gudusoft.gsqlparser.nodes.snowflake.TAtBeforeClause;
008import gudusoft.gsqlparser.nodes.snowflake.TStageReference;
009import gudusoft.gsqlparser.nodes.teradata.THashByClause;
010import gudusoft.gsqlparser.nodes.teradata.TTDUnpivot;
011
012import java.util.ArrayList;
013
014/**
015* Represents table source in from clause of select/delete statement, and update table clause of update statement.    
016*
017*/
018public class TFromTable extends TNodeWithAliasClause{
019
020//    private TPartitionClause partitionClause;
021//
022//    public void setPartitionClause(TPartitionClause partitionClause) {
023//
024//        this.partitionClause = partitionClause;
025//    }
026//
027//    public TPartitionClause getPartitionClause() {
028//        return partitionClause;
029//    }
030
031    private TStageReference stageReference;
032
033    public TStageReference getStageReference() {
034        return stageReference;
035    }
036
037    private TAtBeforeClause timeTravelClause;
038
039    public void setTimeTravelClause(TAtBeforeClause timeTravelClause) {
040        this.timeTravelClause = timeTravelClause;
041    }
042
043    public TAtBeforeClause getTimeTravelClause() {
044        return timeTravelClause;
045    }
046    private TCaseJoinClause caseJoin;
047
048    public TCaseJoinClause getCaseJoin() {
049        return caseJoin;
050    }
051
052    private TExpression joinCondition;
053
054    public void setJoinCondition(TExpression joinCondition) {
055        this.joinCondition = joinCondition;
056    }
057
058    /**
059     * used in couchbase only
060     * @return
061     */
062    public TExpression getJoinCondition() {
063
064        return joinCondition;
065    }
066
067    private int parenthesisCount = 0;
068    private int parenthesisAfterAliasCount = 0;
069
070    public TPxGranule getPxGranule() {
071        return pxGranule;
072    }
073
074    public void incParenthesisCount(){
075        parenthesisCount++;
076    }
077
078    public void incParenthesisAfterAliasCount(){
079        parenthesisAfterAliasCount++;
080    }
081
082    public int getParenthesisCount() {
083        return parenthesisCount;
084    }
085
086    public int getParenthesisAfterAliasCount() {
087        return parenthesisAfterAliasCount;
088    }
089
090    public void setTableProperties(TPTNodeList<THiveKeyValueProperty> tableProperties) {
091        this.tableProperties = tableProperties;
092    }
093
094    /**
095     *
096     * @return hive table property list
097     */
098    public TPTNodeList<THiveKeyValueProperty> getTableProperties() {
099
100        return tableProperties;
101    }
102
103    private TPTNodeList<THiveKeyValueProperty> tableProperties;
104
105    private THiveFromQuerySqlNode fromQuerySqlNode;
106
107    private TPartitionExtensionClause partitionExtensionClause;
108
109    public TPartitionExtensionClause getPartitionExtensionClause() {
110        return partitionExtensionClause;
111    }
112
113    public void setPartitionExtensionClause(TPartitionExtensionClause partitionExtensionClause) {
114
115        this.partitionExtensionClause = partitionExtensionClause;
116    }
117
118    private TColumnDefinitionList columnDefinitions;
119
120    public TColumnDefinitionList getColumnDefinitions() {
121        return columnDefinitions;
122    }
123
124    private TValueClause valueClause;
125
126    public TValueClause getValueClause() {
127        return valueClause;
128    }
129
130    private TJsonTable jsonTable;
131
132    public TJsonTable getJsonTable() {
133        return jsonTable;
134    }
135
136    private TXmlTable xmlTable;
137
138    public TXmlTable getXmlTable() {
139        return xmlTable;
140    }
141
142    public TObjectName getTableObjectName() {
143        return tableObjectName;
144    }
145
146    public TExpression getTableExpr() {
147        return tableExpr;
148    }
149
150    public void setTableObjectName(TObjectName tableObjectName) {
151        //tableObjectName.parseTablename();
152        this.tableObjectName = tableObjectName;
153        this.tableObjectName.setObjectType(TObjectName.ttobjTable);
154        this.tableObjectName.setDbObjectType(EDbObjectType.table);
155    }
156
157    private TObjectName tableObjectName;
158
159    public TOpenQuery getOpenQuery() {
160        return openQuery;
161    }
162
163    private TPTNodeList<TTableHint> tableHintList;
164
165    public void setTableHintList(TPTNodeList<TTableHint> tableHintList) {
166        this.tableHintList = tableHintList;
167    }
168
169    public TPTNodeList<TTableHint> getTableHintList() {
170
171        return tableHintList;
172    }
173
174    public TSelectSqlNode getSubquerynode() {
175        return subquerynode;
176    }
177
178    private TSelectSqlNode subquerynode;
179    private TExpression tableExpr;
180
181    private TMultiTargetList rowList;
182
183    public TMultiTargetList getRowList() {
184        return rowList;
185    }
186
187    private TSourceToken tableonly;
188
189    private boolean tableKeyword = false;
190    private boolean onlyKeyword = false;
191
192    /**
193     * Redshift array-unnesting positional index alias.
194     * <p>
195     * In Redshift you can iterate over an array column in the FROM clause and
196     * bind a name to the element as well as to its ordinal position, e.g.
197     * <pre>FROM schema.tbl t, t.array_col AS element AT idx</pre>
198     * Here {@code element} becomes the table/element alias (the regular
199     * {@link #getAliasClause() aliasClause}) and {@code idx} is captured here as
200     * the array index alias.
201     */
202    private TObjectName arrayIndexAlias;
203
204    public TObjectName getArrayIndexAlias() {
205        return arrayIndexAlias;
206    }
207
208    public void setArrayIndexAlias(TObjectName arrayIndexAlias) {
209        this.arrayIndexAlias = arrayIndexAlias;
210    }
211
212    /**
213     * Redshift SUPER object unpivoting from-clause item, e.g.
214     * <pre>FROM c.c_orders AS o, UNPIVOT o AS val AT attr</pre>
215     * When {@code true} this from item unpivots the SUPER expression: the
216     * regular {@link #getAliasClause() aliasClause} is the value alias and
217     * {@link #getArrayIndexAlias() arrayIndexAlias} (from {@code AT}) is the
218     * attribute-name alias.
219     */
220    private boolean superUnpivot = false;
221
222    public boolean isSuperUnpivot() {
223        return superUnpivot;
224    }
225
226    public void setSuperUnpivot(boolean superUnpivot) {
227        this.superUnpivot = superUnpivot;
228    }
229
230    public boolean isTableKeyword() {
231        return tableKeyword;
232    }
233
234    public boolean isOnlyKeyword() {
235        return onlyKeyword;
236    }
237
238    public TJoinExpr getJoinExpr() {
239        return joinExpr;
240    }
241
242    private TJoinExpr joinExpr;
243
244    public ETableSource getFromtableType() {
245        return fromtableType;
246
247    }
248
249    private ETableSource fromtableType;
250
251
252    public TOpenDatasource getOpenDatasource() {
253        return openDatasource;
254    }
255
256    private TOpenDatasource openDatasource = null;
257
258    public TOpenRowSet getOpenRowSet() {
259        return openRowSet;
260    }
261
262    public TOpenXML getOpenXML() {
263        return openXML;
264    }
265
266    private TOpenXML openXML = null;
267
268    private TOpenRowSet openRowSet = null;
269
270    private TOpenQuery openQuery = null;
271    
272    public TContainsTable getContainsTable() {
273        return containsTable;
274    }
275
276    private TContainsTable containsTable = null;
277
278    public void setTableonly(TSourceToken tableonly) {
279        this.tableonly = tableonly;
280        if (tableonly.toString().equalsIgnoreCase("only")){
281            onlyKeyword = true;
282        }else if(tableonly.toString().equalsIgnoreCase("table")){
283            tableKeyword = true;
284            if (tableExpr != null){
285                fromtableType = ETableSource.tableExpr;
286            }
287        }
288    }
289
290    public void setTableSample(TTableSample tableSample) {
291        this.tableSample = tableSample;
292    }
293
294    public void setPxGranule(TPxGranule pxGranule) {
295        this.pxGranule = pxGranule;
296    }
297
298    public void setFlashback(TFlashback flashback) {
299        this.flashback = flashback;
300    }
301
302    private TTableSample tableSample;
303    private TPxGranule pxGranule;
304
305    public TFlashback getFlashback() {
306        return flashback;
307    }
308
309    private TFlashback flashback;
310
311    public TTableSample getTableSample() {
312        return tableSample;
313    }
314
315    private TDataChangeTable datachangeTable = null;
316    private TRelationExpr relationExpr;
317
318    /**
319     * Postgresql relation expr
320     * @return
321     */
322    public TRelationExpr getRelationExpr() {
323        return relationExpr;
324    }
325
326    /**
327     * DB2 data change
328     * @return
329     */
330    public TDataChangeTable getDatachangeTable() {
331        return datachangeTable;
332    }
333
334
335    public TMergeSqlNode getMergeSqlNode() {
336        return mergeSqlNode;
337    }
338
339    private TMergeSqlNode mergeSqlNode;
340    private TPivotedTable pivotedTable;
341
342    public TPivotedTable getPivotedTable() {
343        return pivotedTable;
344    }
345
346    public void setPivotClause(TPivotClause pivotClause) {
347        this.pivotClause = pivotClause;
348    }
349
350    public TPivotClause getPivotClause() {
351
352        return pivotClause;
353    }
354
355    private TPivotClause pivotClause = null;
356
357    /**
358     * a table-valued function, in SQL Server, Postgresql
359     * @return
360     */
361    public TFunctionCall getFuncCall() {
362        return funcCall;
363    }
364
365    private TFunctionCall funcCall = null;
366
367    private TInformixOuterClause outerClause;
368
369    private TFromTableList fromTableList;
370
371    public TFromTableList getFromTableList() {
372        return fromTableList;
373    }
374
375    public TInformixOuterClause getOuterClause() {
376        return outerClause;
377    }
378
379    private THivePartitionedTableFunction partitionedTableFunction;
380
381
382    public THivePartitionedTableFunction getPartitionedTableFunction() {
383        return partitionedTableFunction;
384    }
385
386    private ArrayList<TLateralView> lateralViewList;
387
388    public void setLateralViewList(ArrayList<TLateralView> lateralViewList) {
389        this.lateralViewList = lateralViewList;
390    }
391
392    /**
393     *
394     * @return hive lateral view list
395     */
396    public ArrayList<TLateralView> getLateralViewList() {
397
398        return lateralViewList;
399    }
400
401    private TExpressionList joinExprList;
402
403    public void setJoinExprList(TExpressionList joinExprList) {
404        this.joinExprList = joinExprList;
405    }
406
407    /**
408     *
409     * @return hive uniqueJoinExpr list
410     */
411    public TExpressionList getJoinExprList() {
412
413        return joinExprList;
414    }
415
416    public THiveFromQuerySqlNode getFromQuerySqlNode() {
417        return fromQuerySqlNode;
418    }
419
420    public void setColumnDefinitions(TColumnDefinitionList columnDefinitions) {
421        this.columnDefinitions = columnDefinitions;
422        if (this.columnDefinitions != null){
423            fromtableType = ETableSource.externalTable;
424        }
425    }
426
427    private TDummy tableFunctionReturn;
428
429    public void setTableFunctionReturn(TDummy tableFunctionReturn) {
430        if (tableFunctionReturn == null) return;
431        this.tableFunctionReturn = tableFunctionReturn;
432        if (this.tableFunctionReturn.list1 != null){
433            this.columnDefinitions = (TColumnDefinitionList)this.tableFunctionReturn.list1;
434        }
435    }
436
437    /** Input correlation name inside a Teradata table operator's ON clause. */
438    private TAliasClause tableOperatorInputAlias;
439    /** Partition expressions over the ON input, separate from its output alias. */
440    private TPartitionClause tableOperatorPartitionBy;
441    /** Ordering of rows supplied to a Teradata table operator. */
442    private TOrderBy tableOperatorOrderBy;
443
444    public TAliasClause getTableOperatorInputAlias() { return tableOperatorInputAlias; }
445    public void setTableOperatorInputAlias(TAliasClause value) { tableOperatorInputAlias = value; }
446    public TPartitionClause getTableOperatorPartitionBy() { return tableOperatorPartitionBy; }
447    public void setTableOperatorPartitionBy(TPartitionClause value) { tableOperatorPartitionBy = value; }
448    public TOrderBy getTableOperatorOrderBy() { return tableOperatorOrderBy; }
449    public void setTableOperatorOrderBy(TOrderBy value) { tableOperatorOrderBy = value; }
450
451    /**
452     * Teradata HASH BY clause for table functions.
453     * <p>
454     * Example: HASH BY col1, col2
455     */
456    private THashByClause hashByClause;
457
458    /**
459     * Gets the HASH BY clause for Teradata table functions.
460     *
461     * @return the HASH BY clause, or null if not specified
462     */
463    public THashByClause getHashByClause() {
464        return hashByClause;
465    }
466
467    /**
468     * Sets the HASH BY clause for Teradata table functions.
469     *
470     * @param hashByClause the HASH BY clause
471     */
472    public void setHashByClause(THashByClause hashByClause) {
473        this.hashByClause = hashByClause;
474    }
475
476    /**
477     * Teradata LOCAL ORDER BY clause for table functions.
478     * <p>
479     * Example: LOCAL ORDER BY col1, col2
480     */
481    private TOrderBy localOrderBy;
482
483    /**
484     * Gets the LOCAL ORDER BY clause for Teradata table functions.
485     *
486     * @return the LOCAL ORDER BY clause, or null if not specified
487     */
488    public TOrderBy getLocalOrderBy() {
489        return localOrderBy;
490    }
491
492    /**
493     * Sets the LOCAL ORDER BY clause for Teradata table functions.
494     *
495     * @param localOrderBy the LOCAL ORDER BY clause
496     */
497    public void setLocalOrderBy(TOrderBy localOrderBy) {
498        this.localOrderBy = localOrderBy;
499    }
500
501    // Constants for type codes
502    private static final int TYPE_OBJECT_NAME = 1;
503    private static final int TYPE_SELECT_SQL_NODE = 2;
504    private static final int TYPE_EXPRESSION = 3;
505    private static final int TYPE_JOIN_EXPR = 4;
506    private static final int TYPE_DUMMY = 5;
507    private static final int TYPE_CONTAINS_TABLE = 6;
508    private static final int TYPE_OPEN_ROW_SET = 7;
509    private static final int TYPE_OPEN_XML = 8;
510    private static final int TYPE_OPEN_DATASOURCE = 9;
511    private static final int TYPE_OPEN_QUERY = 10;
512    private static final int TYPE_DATA_CHANGE_TABLE = 11;
513    private static final int TYPE_RELATION_EXPR = 12;
514    private static final int TYPE_JSON_TABLE = 13;
515    private static final int TYPE_FUNCTION_CALL = 14;
516    private static final int TYPE_XML_TABLE = 15;
517    private static final int TYPE_INFORMIX_OUTER_CLAUSE = 16;
518    private static final int TYPE_FROM_TABLE_LIST = 17;
519    private static final int TYPE_HIVE_PARTITIONED_TABLE_FUNCTION = 18;
520    private static final int TYPE_HIVE_FROM_QUERY_SQL_NODE = 19;
521    private static final int TYPE_PIVOTED_TABLE = 20;
522    private static final int TYPE_MERGE_SQL_NODE = 21;
523    private static final int TYPE_TD_UNPIVOT = 22;
524    private static final int TYPE_UNNEST_CLAUSE = 23;
525    private static final int TYPE_VALUE_CLAUSE = 24;
526    private static final int TYPE_CASE_JOIN_CLAUSE = 25;
527    private static final int TYPE_STAGE_REFERENCE = 26;
528
529    public void init(Object arg1) {
530        if (arg1 == null) return;
531
532        int typeCode = getTypeCode(arg1);
533
534        switch (typeCode) {
535            case TYPE_OBJECT_NAME:
536                setTableObjectName((TObjectName) arg1);
537                fromtableType = ETableSource.objectname;
538                break;
539            case TYPE_SELECT_SQL_NODE:
540                subquerynode = (TSelectSqlNode) arg1;
541                fromtableType = ETableSource.subquery;
542                setStartToken(subquerynode.getStartToken());
543                setEndToken(subquerynode.getEndToken());
544                break;
545            case TYPE_EXPRESSION:
546                initExpression((TExpression) arg1);
547                break;
548            case TYPE_JOIN_EXPR:
549                joinExpr = (TJoinExpr) arg1;
550                fromtableType = ETableSource.join;
551                break;
552            case TYPE_DUMMY:
553                initTableDefInDummyNode((TDummy) arg1);
554                break;
555            case TYPE_CONTAINS_TABLE:
556                containsTable = (TContainsTable) arg1;
557                fromtableType = ETableSource.containsTable;
558                break;
559            case TYPE_OPEN_ROW_SET:
560                openRowSet = (TOpenRowSet) arg1;
561                fromtableType = ETableSource.openrowset;
562                break;
563            case TYPE_OPEN_XML:
564                openXML = (TOpenXML) arg1;
565                fromtableType = ETableSource.openxml;
566                break;
567            case TYPE_OPEN_DATASOURCE:
568                openDatasource = (TOpenDatasource) arg1;
569                fromtableType = ETableSource.opendatasource;
570                break;
571            case TYPE_OPEN_QUERY:
572                openQuery = (TOpenQuery) arg1;
573                fromtableType = ETableSource.openquery;
574                break;
575            case TYPE_DATA_CHANGE_TABLE:
576                datachangeTable = (TDataChangeTable) arg1;
577                fromtableType = ETableSource.datachangeTable;
578                break;
579            case TYPE_RELATION_EXPR:
580                initRelationExpr((TRelationExpr) arg1);
581                break;
582            case TYPE_JSON_TABLE:
583                jsonTable = (TJsonTable) arg1;
584                fromtableType = ETableSource.jsonTable;
585                break;
586            case TYPE_FUNCTION_CALL:
587                // This will handle TFunctionCall and all its subclasses
588                funcCall = (TFunctionCall) arg1;
589                fromtableType = ETableSource.function;
590                break;
591            case TYPE_XML_TABLE:
592                xmlTable = (TXmlTable) arg1;
593                fromtableType = ETableSource.xmltable;
594                break;
595            case TYPE_INFORMIX_OUTER_CLAUSE:
596                outerClause = (TInformixOuterClause) arg1;
597                fromtableType = ETableSource.informixOuter;
598                break;
599            case TYPE_FROM_TABLE_LIST:
600                fromTableList = (TFromTableList) arg1;
601                fromtableType = ETableSource.table_ref_list;
602                break;
603            case TYPE_HIVE_PARTITIONED_TABLE_FUNCTION:
604                partitionedTableFunction = (THivePartitionedTableFunction) arg1;
605                break;
606            case TYPE_HIVE_FROM_QUERY_SQL_NODE:
607                fromQuerySqlNode = (THiveFromQuerySqlNode) arg1;
608                fromtableType = ETableSource.hiveFromQuery;
609                break;
610            case TYPE_PIVOTED_TABLE:
611                pivotedTable = (TPivotedTable) arg1;
612                fromtableType = ETableSource.pivoted_table;
613                break;
614            case TYPE_MERGE_SQL_NODE:
615                mergeSqlNode = (TMergeSqlNode) arg1;
616                fromtableType = ETableSource.output_merge;
617                break;
618            case TYPE_TD_UNPIVOT:
619                tdUnpivot = (TTDUnpivot) arg1;
620                fromtableType = ETableSource.td_unpivot;
621                break;
622            case TYPE_UNNEST_CLAUSE:
623                unnestClause = (TUnnestClause) arg1;
624                fromtableType = ETableSource.unnest;
625                break;
626            case TYPE_VALUE_CLAUSE:
627                valueClause = (TValueClause) arg1;
628                fromtableType = ETableSource.rowList;
629                break;
630            case TYPE_CASE_JOIN_CLAUSE:
631                caseJoin = (TCaseJoinClause) arg1;
632                fromtableType = ETableSource.caseJoin;
633                break;
634            case TYPE_STAGE_REFERENCE:
635                stageReference = (TStageReference) arg1;
636                fromtableType = ETableSource.stageReference;
637                break;
638            default:
639                // Handle unexpected types or throw an exception
640                throw new IllegalArgumentException("Unexpected argument type: " + arg1.getClass().getSimpleName());
641        }
642    }
643
644    private int getTypeCode(Object arg1) {
645        if (arg1 instanceof TObjectName) return TYPE_OBJECT_NAME;
646        if (arg1 instanceof TSelectSqlNode) return TYPE_SELECT_SQL_NODE;
647        if (arg1 instanceof TExpression) return TYPE_EXPRESSION;
648        if (arg1 instanceof TJoinExpr) return TYPE_JOIN_EXPR;
649        if (arg1 instanceof TDummy) return TYPE_DUMMY;
650        if (arg1 instanceof TContainsTable) return TYPE_CONTAINS_TABLE;
651        if (arg1 instanceof TOpenRowSet) return TYPE_OPEN_ROW_SET;
652        if (arg1 instanceof TOpenXML) return TYPE_OPEN_XML;
653        if (arg1 instanceof TOpenDatasource) return TYPE_OPEN_DATASOURCE;
654        if (arg1 instanceof TOpenQuery) return TYPE_OPEN_QUERY;
655        if (arg1 instanceof TDataChangeTable) return TYPE_DATA_CHANGE_TABLE;
656        if (arg1 instanceof TRelationExpr) return TYPE_RELATION_EXPR;
657        if (arg1 instanceof TJsonTable) return TYPE_JSON_TABLE;
658        if (arg1 instanceof TFunctionCall) return TYPE_FUNCTION_CALL;
659        if (arg1 instanceof TXmlTable) return TYPE_XML_TABLE;
660        if (arg1 instanceof TInformixOuterClause) return TYPE_INFORMIX_OUTER_CLAUSE;
661        if (arg1 instanceof TFromTableList) return TYPE_FROM_TABLE_LIST;
662        if (arg1 instanceof THivePartitionedTableFunction) return TYPE_HIVE_PARTITIONED_TABLE_FUNCTION;
663        if (arg1 instanceof THiveFromQuerySqlNode) return TYPE_HIVE_FROM_QUERY_SQL_NODE;
664        if (arg1 instanceof TPivotedTable) return TYPE_PIVOTED_TABLE;
665        if (arg1 instanceof TMergeSqlNode) return TYPE_MERGE_SQL_NODE;
666        if (arg1 instanceof TTDUnpivot) return TYPE_TD_UNPIVOT;
667        if (arg1 instanceof TUnnestClause) return TYPE_UNNEST_CLAUSE;
668        if (arg1 instanceof TValueClause) return TYPE_VALUE_CLAUSE;
669        if (arg1 instanceof TCaseJoinClause) return TYPE_CASE_JOIN_CLAUSE;
670        if (arg1 instanceof TStageReference) return TYPE_STAGE_REFERENCE;
671        return 0; // Unknown type
672    }
673    private void initExpression(TExpression expr) {
674        tableExpr = expr;
675        fromtableType = ETableSource.tableExpr;
676        if (tableExpr.getExpressionType() == EExpressionType.simple_object_name_t) {
677            setTableObjectName(tableExpr.getObjectOperand());
678            fromtableType = ETableSource.objectname;
679        }
680    }
681
682    private void initRelationExpr(TRelationExpr expr) {
683        relationExpr = expr;
684        setTableObjectName(relationExpr.getRelationName());
685        fromtableType = ETableSource.objectname;
686        // FROM ONLY t / TABLE ONLY t: the table without its inheritance children
687        if (relationExpr.getOnly() != null) {
688            onlyKeyword = true;
689        }
690    }
691    private TUnnestClause unnestClause;
692
693    public TUnnestClause getUnnestClause() {
694        return unnestClause;
695    }
696
697    private TTDUnpivot tdUnpivot;
698
699    public TTDUnpivot getTdUnpivot() {
700        return tdUnpivot;
701    }
702
703    private TPTNodeList <TTableHint> nameListToTableHint(Object arg1,boolean isSybaseIndexHint){
704        TPTNodeList<TTableHint> hints = new TPTNodeList<TTableHint>();
705        if (arg1 instanceof TObjectNameList){
706            TObjectNameList columns = (TObjectNameList)arg1;
707            for (int i=0;i<columns.size();i++){
708                TObjectName column = columns.getObjectName(i);
709                if ((isSybaseIndexHint)||(isSQLServerHint(column.toString()))){
710                    TTableHint tableHint = new TTableHint();
711                    tableHint.setHint(column);
712                    if (i == 0)
713                      tableHint.setStartToken(column.getStartToken());
714                    if (i == columns.size() - 1)
715                      tableHint.setEndToken(column.getStartToken());
716                    hints.addElement(tableHint);
717                }else {
718                    hints.removeAllElements();
719                    break;
720                }
721            }
722        }else if (arg1 instanceof TExpressionList){
723            TExpressionList expressionList = (TExpressionList)arg1;
724            for(int i=0;i<expressionList.size();i++){
725                TExpression expression = expressionList.getExpression(i);
726                if ((isSybaseIndexHint)||(isSQLServerHint(expression.toString()))){
727                    TTableHint tableHint = new TTableHint();
728                    tableHint.setHint(expression.getObjectOperand());
729                    if (i == 0)
730                      tableHint.setStartToken(expression.getStartToken());
731                    if (i == expressionList.size() - 1)
732                      tableHint.setEndToken(expression.getStartToken());
733                    hints.addElement(tableHint);
734                }else {
735                    hints.removeAllElements();
736                    break;
737                }
738            }
739        }
740
741        if (hints.size() > 0)
742            return hints;
743        else
744            return null;
745    }
746
747    private boolean isSQLServerHint(String pstr){
748        boolean retval = false;
749        String[] hints = new String[] {
750        "fastfirstrow","holdlock","nolock","nowait","paglock",
751       "readcommitted","readcommittedlock","readpast","readuncommitted","repeatableread",
752       "rowlock","serializable","tablock","tablockx","updlock",
753       "xlock","keepidentity","keepdefaults","ignore_constraints","ignore_triggers",
754       "index"
755        };
756
757        for(int i=0;i<21;i++)
758        {
759            if (hints[i].compareToIgnoreCase(pstr) == 0){
760                retval = true;
761                break;
762            }
763        }
764
765        return retval;
766    }
767    
768    private void initTableDefInDummyNode(TDummy pdummy){
769        fromtableType = ETableSource.objectname; //default, may change to ftt_function later
770
771       if (pdummy.int1 == 0){
772           setTableObjectName((TObjectName)(pdummy.node1));
773       }else if (pdummy.int1 == 1){
774           if (pdummy.list1 == null){
775               //it's a function
776               funcCall =  new TFunctionCall();//(TFunctionCall)createNode(ENodeType.T_FunctionCall.getId(),((TObjectName)pdummy.node1));
777               funcCall.init(((TObjectName) pdummy.node1),EFunctionType.udf_t);
778               //((TObjectName)pdummy.node1).parseFunctionName();
779               // ((TObjectName)pdummy.node1).setObjectType(TObjectName.ttobjFunctionName);
780               funcCall.setStartToken(pdummy.getStartToken());
781               funcCall.setEndToken(pdummy.getEndToken());
782               funcCall.setArgs(null);
783               fromtableType = ETableSource.function;
784           }else{
785               tableHintList = nameListToTableHint(((TExpressionList)pdummy.list1),false);
786              if ( tableHintList != null){
787              //if (isSQLServerHint(((TExpressionList)pdummy.list1).getExpression(0).toString())){
788                  //this is sql server hint, not a function, so set table name
789                  setTableObjectName((TObjectName)(pdummy.node1));
790              }else{
791                  //it's a function
792                  funcCall =  new TFunctionCall();//(TFunctionCall)createNode(ENodeType.T_FunctionCall.getId(),((TObjectName)pdummy.node1));
793                  funcCall.init(((TObjectName)pdummy.node1),EFunctionType.udf_t);
794                  //((TObjectName)pdummy.node1).parseFunctionName();
795                 // ((TObjectName)pdummy.node1).setObjectType(TObjectName.ttobjFunctionName);
796                  funcCall.setArgs((TExpressionList)pdummy.list1);
797                  fromtableType = ETableSource.function;
798                  funcCall.setStartToken(pdummy.getStartToken());
799                  funcCall.setEndToken(pdummy.getEndToken());
800              }
801           }
802       }else if (pdummy.int1 == 2){
803           setTableObjectName((TObjectName)(pdummy.node1));
804           tableHintList = nameListToTableHint(pdummy.list1,false); //sql server only, maybe deleted in future
805       }else if (pdummy.int1 == 3){
806           setTableObjectName((TObjectName)(pdummy.node1));
807           tableHintList = (TPTNodeList)(pdummy.node3); //sybase index table hint
808       }
809
810       if (pdummy.node2 != null){
811           this.setAliasClause((TAliasClause)(pdummy.node2));
812           if (this.getAliasClause().isSybaseIndexHint()){
813             tableHintList = this.getAliasClause().getTableHints();
814             this.getAliasClause().setEndToken(this.getAliasClause().getAliasName().getEndToken());
815           }else
816               if (this.getAliasClause().getColumns() != null){
817                   tableHintList = nameListToTableHint(this.getAliasClause().getColumns(),this.getAliasClause().isSybaseIndexHint());
818                   if (tableHintList != null){
819                       //this is not column list of table alias, this is table hint like NOLOCK in this sql:
820                       // SELECT COUNT(DISTINCT(SD_KEY)) FROM SHORTWINDOW PAST (NOLOCK)
821                       this.getAliasClause().setEndToken(this.getAliasClause().getAliasName().getEndToken());
822                   }
823               }
824
825       }
826
827    }
828
829    public void init(Object arg1,Object arg2)
830    {
831        init(arg1);
832        if (arg2 != null){
833            this.setAliasClause((TAliasClause)arg2);
834            // this is table alias
835            this.getAliasClause().setDBObjectToAlias(EDbObjectType.table_alias);
836        }
837    }
838
839    public void setTableProperties(TDummyList tableProperties){
840        for(int i=0;i<tableProperties.size();i++){
841            TDummy dummy = tableProperties.getDummyItem(i);
842            TParseTreeNode node = dummy.node1;
843            if (node instanceof TAliasClause){
844                this.setAliasClause( (TAliasClause)node);
845            }
846        }
847    }
848
849    public void setParenthesisCount(int parenthesisCount) {
850        this.parenthesisCount = parenthesisCount;
851    }
852
853    public void setParenthesisAfterAliasCount(int parenthesisAfterAliasCount) {
854        this.parenthesisAfterAliasCount = parenthesisAfterAliasCount;
855    }
856
857    public void setXmlTable(TXmlTable xmlTable) {
858        this.xmlTable = xmlTable;
859    }
860
861    public void setTableExpr(TExpression tableExpr) {
862        this.tableExpr = tableExpr;
863    }
864
865    public void setRowList(TMultiTargetList rowList) {
866        this.rowList = rowList;
867    }
868
869    public void setTableKeyword(boolean tableKeyword) {
870        this.tableKeyword = tableKeyword;
871    }
872
873    public void setOnlyKeyword(boolean onlyKeyword) {
874        this.onlyKeyword = onlyKeyword;
875    }
876
877    public void setJoinExpr(TJoinExpr joinExpr) {
878        this.joinExpr = joinExpr;
879    }
880
881    public void setFromtableType(ETableSource fromtableType) {
882        this.fromtableType = fromtableType;
883    }
884
885    public void setOpenDatasource(TOpenDatasource openDatasource) {
886        this.openDatasource = openDatasource;
887    }
888
889    public void setOpenXML(TOpenXML openXML) {
890        this.openXML = openXML;
891    }
892
893    public void setOpenRowSet(TOpenRowSet openRowSet) {
894        this.openRowSet = openRowSet;
895    }
896
897    public void setOpenQuery(TOpenQuery openQuery) {
898        this.openQuery = openQuery;
899    }
900
901    public void setContainsTable(TContainsTable containsTable) {
902        this.containsTable = containsTable;
903    }
904
905    public void setDatachangeTable(TDataChangeTable datachangeTable) {
906        this.datachangeTable = datachangeTable;
907    }
908
909    public void setRelationExpr(TRelationExpr relationExpr) {
910        this.relationExpr = relationExpr;
911    }
912
913    public void setPivotedTable(TPivotedTable pivotedTable) {
914        this.pivotedTable = pivotedTable;
915    }
916
917    public void setFuncCall(TFunctionCall funcCall) {
918        this.funcCall = funcCall;
919    }
920
921    public void setOuterClause(TInformixOuterClause outerClause) {
922        this.outerClause = outerClause;
923    }
924
925    public void setFromTableList(TFromTableList fromTableList) {
926        this.fromTableList = fromTableList;
927    }
928
929    public void setPartitionedTableFunction(THivePartitionedTableFunction partitionedTableFunction) {
930        this.partitionedTableFunction = partitionedTableFunction;
931    }
932
933    public void accept(TParseTreeVisitor v){
934        v.preVisit(this);
935        v.postVisit(this);
936    }
937
938    public void acceptChildren(TParseTreeVisitor v) {
939        v.preVisit(this);
940        switch (fromtableType){
941            case objectname:
942                tableObjectName.acceptChildren(v);
943                break;
944            case subquery:
945                // subquerynode
946                break;
947            case tableExpr:
948                tableExpr.acceptChildren(v);
949                break;
950            case join:
951                joinExpr.acceptChildren(v);
952                break;
953            case function:
954                funcCall.acceptChildren(v);
955                break;
956            case containsTable:
957                containsTable.acceptChildren(v);
958                break;
959            case openrowset:
960                openRowSet.acceptChildren(v);
961                break;
962            case openxml:
963                openXML.acceptChildren(v);
964                break;
965            case opendatasource:
966                openDatasource.acceptChildren(v);
967                break;
968            case openquery:
969                openQuery.acceptChildren(v);
970                break;
971            case datachangeTable:
972                datachangeTable.acceptChildren(v);
973                break;
974            case xmltable:
975                xmlTable.acceptChildren(v);
976                break;
977            case informixOuter:
978                outerClause.acceptChildren(v);
979                break;
980            case table_ref_list:
981                fromTableList.acceptChildren(v);
982                break;
983            case hiveFromQuery:
984                fromQuerySqlNode.acceptChildren(v);
985                break;
986            case pivoted_table:
987                pivotedTable.acceptChildren(v);
988                break;
989            case output_merge:
990                mergeSqlNode.acceptChildren(v);
991                break;
992            case td_unpivot:
993                tdUnpivot.acceptChildren(v);
994                break;
995            case unnest:
996                unnestClause.acceptChildren(v);
997                break;
998            case jsonTable:
999                jsonTable.acceptChildren(v);
1000                break;
1001            case rowList:
1002                valueClause.acceptChildren(v);
1003                break;
1004        }
1005
1006        v.postVisit(this);
1007    }
1008}