001package gudusoft.gsqlparser.nodes;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.nodes.hive.THiveKeyValueProperty;
005import gudusoft.gsqlparser.nodes.hive.THiveTableBuckets;
006import gudusoft.gsqlparser.nodes.hive.THiveTableFileFormat;
007import gudusoft.gsqlparser.nodes.hive.THiveTableSkewed;
008import gudusoft.gsqlparser.nodes.mysql.TMySQLIndexOption;
009import gudusoft.gsqlparser.nodes.postgresql.TPartitionBoundSpecSqlNode;
010import gudusoft.gsqlparser.nodes.mssql.TPeriodForClause;
011
012import java.util.ArrayList;
013
014/**
015 *  Type of alter table option, {@link #getOptionType()} returns the type of various option:
016 *  <br>
017 *  <p>Column</p>
018 *  <ul>
019 *      <li>{@link EAlterTableOptionType#AddColumn}, check {@link #getColumnDefinitionList()} for newly added columns</li>
020 *      <li>{@link EAlterTableOptionType#AlterColumn}, alter column option, {@link #getColumnName()} returns the column name.
021 *      <br>check {@link #getAlterColumnSubType()} for the subtype of alter column option.
022 *      <ul>
023 *          <li>{@link EAlterColumnSubType#SetDefault}, {@link #getDefaultExpr()} returns default expression</li>
024 *          <li>{@link EAlterColumnSubType#DropDefault}</li>
025 *          <li>{@link EAlterColumnSubType#DropNotNull}</li>
026 *          <li>{@link EAlterColumnSubType#SetNotNull}</li>
027 *          <li>{@link EAlterColumnSubType#SetStatistics}, {@link #getStatisticsValue} returns the statistics value</li>
028 *          <li>{@link EAlterColumnSubType#SetOptions}, {@link #getAttributeOptions} returns the set options</li>
029 *          <li>{@link EAlterColumnSubType#ResetOptions}, {@link #getAttributeOptions} returns the reset options</li>
030 *          <li>{@link EAlterColumnSubType#SetStorage}, {@link #getStorageName} retuns the storage name</li>
031 *          <li>{@link EAlterColumnSubType#SetDataType}, {@link #getNewDataType returns the data type name}</li>
032 *      </ul>
033 *      </li>
034 *      <li>{@link EAlterTableOptionType#ChangeColumn}, MySQL,Hive,Impala change column clause, {@link #getColumnName() old column name},{@link #getNewColumnDef() new column definition}</li>
035 *      <li>{@link EAlterTableOptionType#DropColumn}, check {@link #getColumnNameList()} for dropped columns. {@link #getDropRestrictType()} returns drop restrict type if any.</li>
036 *      <li>{@link EAlterTableOptionType#ModifyColumn}, check {@link #getColumnDefinitionList()} for modified columns</li>
037 *      <li>{@link EAlterTableOptionType#RenameColumn}, rename {@link #getColumnName() old name} to {@link #getNewColumnName() new name}</li>
038 *      <li>{@link EAlterTableOptionType#SetUnUsedColumn}, check {@link #getColumnNameList()} for unused column. Oracle set unused column.</li>
039 *      <li>{@link EAlterTableOptionType#DropUnUsedColumn}, Oracle drop unused columns</li>
040 *      <li>{@link EAlterTableOptionType#DropColumnsContinue}, Oracle drop columns continue</li>
041 *</ul>
042 * <p>Constraint</p>
043 * <ul>
044 *      <li>{@link EAlterTableOptionType#AddConstraint}, check {@link #getConstraintList()} for newly added table level constraints</li>
045 *      <li>{@link EAlterTableOptionType#AddConstraintPK}, {@link #getColumnNameList() primary key column list}. MySQL, {@link #getConstraintName()} returns the constraint name</li>
046 *      <li>{@link EAlterTableOptionType#AddConstraintUnique}, {@link #getColumnNameList() unique key column list}. MyQL,{@link #getConstraintName()} returns the constraint name</li>
047 *      <li>{@link EAlterTableOptionType#AddConstraintFK}, {@link #getColumnNameList() unique key column list},
048 *          {@link #getConstraintName() [constraint name]},
049 *          {@link #getReferencedObjectName() [referenced table name]},
050 *          {@link #getReferencedColumnList() [referenced column list]},MySQL</li>
051 *      <li>{@link EAlterTableOptionType#AddConstraintIndex}, check {@link #getColumnNameList() index column list},
052 *          {@link #getConstraintName() [constraint name]},{@link #getMySQLIndexStorageType() [index storage type]} MySQL add index constraint</li>
053 *      <li>{@link EAlterTableOptionType#DropConstraint}, {@link #getConstraintNameList() constraint name list}, {@link #getDropRestrictType() [drop restrict type]}, Oracle/DB2/Greenplum drop constraint. </li>
054 *      <li>{@link EAlterTableOptionType#AlterConstraintFK}, DB2 alter foreign key. </li>
055 *      <li>{@link EAlterTableOptionType#AlterConstraintCheck}, {@link #getConstraintName() check constraint name }, DB2 alter check constraint. </li>
056 *      <li>{@link EAlterTableOptionType#DropConstraintIndex}, {@link #getConstraintName() constriant name}, MySQL drop index </li>
057 *      <li>{@link EAlterTableOptionType#DropConstraintKey}, {@link #getConstraintName() constriant name}, MySQL drop index </li>
058 *      <li>{@link EAlterTableOptionType#DropConstraintPK}, Oracle/DB2/MySQL drop primary key. </li>
059 *      <li>{@link EAlterTableOptionType#DropConstraintFK},{@link #getConstraintName() [constriant name]}, DB2/MySQL drop foreign key. </li>
060 *      <li>{@link EAlterTableOptionType#DropConstraintUnique}, In Oracle, get {@link #getColumnNameList() unique column list}, In DB2, get {@link #getConstraintName() constraint name}. </li>
061 *      <li>{@link EAlterTableOptionType#DropConstraintCheck}, {@link #getConstraintName() check constraint name}, DB2 drop check constraint. </li>
062 *      <li>{@link EAlterTableOptionType#ModifyConstraint}, check {@link #getConstraintName() constraint name} </li>
063 *      <li>{@link EAlterTableOptionType#RenameConstraint}, rename {@link #getConstraintName() old name} to {@link #getNewConstraintName() new name} </li>
064 *      <li>{@link EAlterTableOptionType#ValidateConstraint},  {@link #getConstraintName() constraint name}, greenplum/postgresql</li>
065 *</ul>
066 * <p>Enable/disable</p>
067 * <ul>
068 *       <li>{@link EAlterTableOptionType#enableTrigger}, {@link #getTriggerName() trigger name}, greenplum/postgresql. </li>
069 *       <li>{@link EAlterTableOptionType#enableAlwaysTrigger}, {@link #getTriggerName() trigger name}, greenplum/postgresql. </li>
070 *       <li>{@link EAlterTableOptionType#enableReplicaTrigger}, {@link #getTriggerName() trigger name}, greenplum/postgresql. </li>
071 *       <li>{@link EAlterTableOptionType#disableTrigger}, {@link #getTriggerName() trigger name}, greenplum/postgresql. </li>
072 *       <li>{@link EAlterTableOptionType#enableRule}, {@link #getRuleName() rule name}, greenplum/postgresql. </li>
073 *       <li>{@link EAlterTableOptionType#enableAlwaysRule}, {@link #getRuleName() rule name}, greenplum/postgresql. </li>
074 *       <li>{@link EAlterTableOptionType#enableReplicaRule}, {@link #getRuleName() rule name}, greenplum/postgresql. </li>
075 *       <li>{@link EAlterTableOptionType#disableRule}, {@link #getRuleName() rule name}, greenplum. </li>
076 * </ul>
077 * <p>Others</p>
078 * <ul>
079 *       <li>{@link EAlterTableOptionType#RenameTable}, {@link #getNewTableName() new table name} </li>
080 *       <li>{@link EAlterTableOptionType#inherit}, {@link #getParentTable() parent table}, greenplum/postgresql. </li>
081 *       <li>{@link EAlterTableOptionType#noInherit}, {@link #getParentTable() parent table}, greenplum/postgresql. </li>
082 *
083 *       <li>{@link EAlterTableOptionType#ofAnyType}, {@link #getAnyTypeName() type name}, greenplum/postgresql. </li>
084 *
085 *       <li>{@link EAlterTableOptionType#ownerTo}, {@link #getNewOwnerName() owner name}, greenplum/postgresql. </li>
086 *       <li>{@link EAlterTableOptionType#setTablespace}, {@link #getNewTablespaceName() tablespace name}, greenplum/postgresql. </li>
087 *
088 *        <li>{@link EAlterTableOptionType#setSchema}, {@link #getSchemaName() schema name}, greenplum. </li>
089 *        <li>{@link EAlterTableOptionType#clusterOn}, {@link #getIndexName() index name}, postgresql. </li>
090 *
091 *  </ul>
092 */
093public class TAlterTableOption extends TParseTreeNode {
094
095    private TPeriodForClause periodForClause;
096
097    public TPeriodForClause getPeriodForClause() {
098        return periodForClause;
099    }
100
101    public void setPeriodForClause(TPeriodForClause periodForClause) {
102        this.periodForClause = periodForClause;
103    }
104
105    public static EAlterTableOptionType getAlterTableOptionTypeByToken(TSourceToken st){
106        if (st == null) return EAlterTableOptionType.RenameTable;
107        if (st.toString().equalsIgnoreCase("index")) return EAlterTableOptionType.renameIndex;
108        if (st.toString().equalsIgnoreCase("key")) return EAlterTableOptionType.renameIndex;
109
110        return EAlterTableOptionType.Unknown;
111    }
112    private TBaseTablePartition tablePartition;
113
114    public TBaseTablePartition getTablePartition() {
115        return tablePartition;
116    }
117
118    public void adjustOptionByToken(TSourceToken st){
119        if (st == null) return;
120
121        switch (getOptionType()){
122            case AddConstraintIndex:
123                if (st.tokencode == TBaseType.rrw_unique){
124                    setOptionType(EAlterTableOptionType.AddConstraintUnique);
125                }
126                break;
127        }
128    }
129    public TMySQLCreateTableOption getMySQLTableOption() {
130        return mySQLTableOption;
131    }
132
133    private  TMySQLCreateTableOption mySQLTableOption;
134
135    public void setPartitionBoundSpec(TPartitionBoundSpecSqlNode partitionBoundSpec) {
136        this.partitionBoundSpec = partitionBoundSpec;
137    }
138
139    public TPartitionBoundSpecSqlNode getPartitionBoundSpec() {
140        return partitionBoundSpec;
141    }
142
143    /**
144     * postgresql PartitionBoundSpec
145     * https://www.postgresql.org/docs/current/sql-createtable.html
146     */
147    private TPartitionBoundSpecSqlNode partitionBoundSpec;
148
149    public static final int COLUMN_POSITION_NONE = 0;
150    public static final int COLUMN_POSITION_FIRST = 1;
151    public static final int COLUMN_POSITION_AFTER = 2;
152
153    private int columnPosition = COLUMN_POSITION_NONE;
154    private TObjectName afterColumnName;
155
156    public TObjectName getAfterColumnName() {
157        return afterColumnName;
158    }
159
160    public int getColumnPosition() {
161        return columnPosition;
162    }
163
164    public void setColumnPosition(TDummy node){
165        if (node == null) return;
166        if (node.node1 != null){
167            this.columnPosition = COLUMN_POSITION_AFTER;
168            this.afterColumnName = (TObjectName)node.node1;
169        }else{
170            this.columnPosition = COLUMN_POSITION_FIRST;
171        }
172    }
173
174    private EAlterColumnSubType AlterColumnSubType = EAlterColumnSubType.unknown;
175
176    /**
177     * subtype of alter column.
178     * @return subtype of alter column.
179     */
180    public EAlterColumnSubType getAlterColumnSubType() {
181        return AlterColumnSubType;
182    }
183
184    private TPTNodeList <TMySQLIndexOption> indexOptionList;
185
186    public void setIndexOptionList(TPTNodeList<TMySQLIndexOption> indexOptionList) {
187        this.indexOptionList = indexOptionList;
188    }
189
190    /**
191     * MySQL, index options
192     * @return index options
193     */
194    public TPTNodeList<TMySQLIndexOption> getIndexOptionList() {
195
196        return indexOptionList;
197    }
198
199    private TPTNodeList <TColumnWithSortOrder > indexCols;
200
201    public void setIndexCols(TPTNodeList<TColumnWithSortOrder > indexCols) {
202        this.indexCols = indexCols;
203    }
204
205    /**
206     * MySQL, column list of foreign key
207     *
208     *
209     * @return column list of foreign key
210     */
211    public TPTNodeList<TColumnWithSortOrder > getIndexCols() {
212
213        return indexCols;
214    }
215
216    private TObjectName serdeName;
217
218    private TResultColumnList mutationUpdateList;
219
220    /**
221     * ClickHouse ALTER TABLE ... UPDATE set list.
222     * Valid when {@link #getOptionType()} is {@link EAlterTableOptionType#clickhouseUpdateMutation}.
223     * @return the SET column assignment list
224     */
225    public TResultColumnList getMutationUpdateList() {
226        return mutationUpdateList;
227    }
228
229    /**
230     * ClickHouse ALTER TABLE ... DELETE WHERE / UPDATE ... WHERE condition.
231     * Valid when {@link #getOptionType()} is {@link EAlterTableOptionType#clickhouseDeleteMutation}
232     * or {@link EAlterTableOptionType#clickhouseUpdateMutation}.
233     * @return the WHERE condition expression
234     */
235    public TExpression getMutationWhereCondition() {
236        return defaultExpr;
237    }
238
239    private TParseTreeNode partitionExpr;
240
241    /**
242     * ClickHouse ALTER TABLE partition expression.
243     * Valid when {@link #getOptionType()} is one of the clickhouse partition operations
244     * (e.g., {@link EAlterTableOptionType#clickhouseDetachPartition},
245     * {@link EAlterTableOptionType#clickhouseAttachPartition}, etc.).
246     * @return the partition expression node (integer, string, identifier, or tuple)
247     */
248    public TParseTreeNode getPartitionExpr() {
249        return partitionExpr;
250    }
251
252    private EKeyReferenceType dropRestrictType = EKeyReferenceType.no_action;
253
254    public void setDropRestrictType(TSourceToken st) {
255        if (st == null) return;
256
257        if (st.tokencode == TBaseType.rrw_cascade ){
258            this.dropRestrictType = EKeyReferenceType.cascade;
259        }else if (st.tokencode == TBaseType.rrw_restrict){
260            this.dropRestrictType = EKeyReferenceType.restrict;
261        }
262
263    }
264
265    /**
266     * drop restrict type such as: cascade, restrict
267     * @return drop restrict type such as: cascade, restrict
268     */
269    public EKeyReferenceType getDropRestrictType() {
270
271        return dropRestrictType;
272    }
273
274    /**
275     * Type of this alter table option
276     * @return Type of this alter table option
277     */
278    public EAlterTableOptionType getOptionType() {
279        return optionType;
280    }
281
282    /**
283     * referenced table in foreign key clause
284     * @return referenced table in foreign key clause
285     */
286    public TTable getReferencedTable() {
287        return referencedTable;
288    }
289
290    private TTable referencedTable = null;
291
292    private TObjectName partitionName;
293
294
295    /**
296     * partition name in PostgreSQL attached partition clause.
297     *
298     * @return partition name
299     */
300    public TObjectName getPartitionName() {
301        return partitionName;
302    }
303
304    private TColumnDefinitionList columnDefinitionList = null;
305    private TConstraintList constraintList = null;
306    private TObjectNameList columnNameList = null;
307    private TObjectName columnName = null;
308    private TObjectName newColumnName = null;
309    private TObjectName constraintName = null;
310    private TObjectName newConstraintName = null;
311    private TObjectName referencedObjectName = null;
312    private TObjectNameList referencedColumnList = null;
313    private TObjectName newTableName = null;
314    private TExpression defaultExpr = null;
315    private TConstant statisticsValue = null;
316    private ArrayList<TAttributeOption> attributeOptions;
317    private TObjectName storageName = null;
318    private TTypeName   newDataType = null;
319    private TObjectName newCollation = null;
320    private TExpression usingExpr = null;
321    private TConstraint tableConstraint = null;
322    private TObjectName indexName = null;
323    private TObjectName triggerName = null;
324    private TObjectName ruleName = null;
325    private TObjectName parentTable = null;
326    private TObjectName anyTypeName = null;
327    private TObjectName newOwnerName = null;
328    private TObjectName newTablespaceName = null;
329    private TObjectName schemaName = null;
330    private TObjectNameList constraintNameList = null;
331    private TExpression partitionExpression1 = null;
332
333    private TObjectName sourceTableName = null;
334
335    /**
336     * redshift alter table... append from source table
337     * @return redshift alter table... append from source table
338     */
339    public TObjectName getSourceTableName() {
340        return sourceTableName;
341    }
342
343    /**
344     * SQL Server, target_partition_number_expression in switch partition clause
345     * @return
346     */
347    public TExpression getPartitionExpression2() {
348        return partitionExpression2;
349    }
350
351    /**
352     * SQL Server, source_partition_number_expression in switch partition clause
353     * @return source_partition_number_expression
354     */
355    public TExpression getPartitionExpression1() {
356        return partitionExpression1;
357    }
358
359    private TExpression partitionExpression2 = null;
360
361    /**
362     * constraint name list when option type is: {@link EAlterTableOptionType#DropConstraint}
363     *
364     * @return constraint name list
365     */
366    public TObjectNameList getConstraintNameList() {
367        return constraintNameList;
368    }
369
370    /**
371     * Greenplum, Schema name when option type is {@link EAlterTableOptionType#setSchema}
372     *
373     * @return schema name
374     */
375    public TObjectName getSchemaName() {
376        return schemaName;
377    }
378
379    public void setNewTablespaceName(TObjectName newTablespaceName) {
380        this.newTablespaceName = newTablespaceName;
381    }
382
383    /**
384    * Greenplum, Postgresql, Snowflake, tablespace name when option type is {@link EAlterTableOptionType#setTablespace}
385     *
386    * @return tablespace name
387    */
388    public TObjectName getNewTablespaceName() {
389
390        return newTablespaceName;
391    }
392
393    public void setNewOwnerName(TObjectName newOwnerName) {
394        this.newOwnerName = newOwnerName;
395    }
396
397    /**
398    * owner name or roleId when option type is {@link EAlterTableOptionType#ownerTo}
399     *
400    * @return owner name or role id
401    */
402    public TObjectName getNewOwnerName() {
403
404        return newOwnerName;
405    }
406
407    public void setAnyTypeName(TObjectName anyTypeName) {
408        this.anyTypeName = anyTypeName;
409    }
410
411     /**
412     * Greenplum, Postgresql,Snowflake, any name when option type is {@link EAlterTableOptionType#ofAnyType}
413      *
414     * @return any name
415     */
416    public TObjectName getAnyTypeName() {
417
418        return anyTypeName;
419    }
420
421    public void setParentTable(TObjectName parentTable) {
422        this.parentTable = parentTable;
423    }
424
425     /**
426     * Greenplum, Postgresql,Snowflake, parent table when option type is {@link EAlterTableOptionType#inherit} or
427      * {@link EAlterTableOptionType#noInherit}
428      *
429     * @return  parent table
430     */
431    public TObjectName getParentTable() {
432
433        return parentTable;
434    }
435
436    public void setRuleName(TObjectName ruleName) {
437        this.ruleName = ruleName;
438    }
439
440    /**
441     * Greenplum, Postgresql,Snowflake, rule name when option type is {@link EAlterTableOptionType#enableRule} or
442     * {@link EAlterTableOptionType#enableAlwaysRule} or  {@link EAlterTableOptionType#enableReplicaRule} or
443     * {@link EAlterTableOptionType#disableRule}
444     *
445     * @return rule name
446     */
447    public TObjectName getRuleName() {
448
449        return ruleName;
450    }
451
452    public void setTriggerName(TObjectName triggerName) {
453        this.triggerName = triggerName;
454    }
455
456
457    /**
458     * Greenplum, Postgresql,Snowflake, trigger name when option type is {@link EAlterTableOptionType#enableTrigger} or
459     * {@link EAlterTableOptionType#enableAlwaysTrigger} or  {@link EAlterTableOptionType#enableReplicaTrigger} or
460     * {@link EAlterTableOptionType#disableTrigger}
461     *
462     * @return trigger name
463     */
464    public TObjectName getTriggerName() {
465
466        return triggerName;
467    }
468
469    public void setIndexName(TObjectName indexName) {
470        this.indexName = indexName;
471    }
472
473    /**
474     * Greenplum, Postgresql, Snowflake, index name when option type is {@link EAlterTableOptionType#clusterOn}
475     *
476     * @return index name
477     */
478    public TObjectName getIndexName() {
479
480        return indexName;
481    }
482
483    private TObjectName newIndexName;
484
485    public TObjectName getNewIndexName() {
486        return newIndexName;
487    }
488
489    public void setTableConstraint(TConstraint tableConstraint) {
490        this.tableConstraint = tableConstraint;
491    }
492
493    /**
494     * table constraint when option type is {@link EAlterTableOptionType#AddConstraint}
495     *
496     * @return table constraint
497     */
498    public TConstraint getTableConstraint() {
499
500        return tableConstraint;
501    }
502
503    public void setUsingExpr(TExpression usingExpr) {
504        this.usingExpr = usingExpr;
505    }
506
507    /**
508     * using expr when option type is {@link gudusoft.gsqlparser.EAlterTableOptionType#AlterColumn} and subtype is {@link EAlterColumnSubType#SetDataType}
509     *
510     * @return using expr
511     */
512    public TExpression getUsingExpr() {
513        return usingExpr;
514    }
515
516    public void setNewCollation(TObjectName newCollation) {
517        this.newCollation = newCollation;
518    }
519
520    /**
521     * new collation when option type is {@link gudusoft.gsqlparser.EAlterTableOptionType#AlterColumn} and subtype is {@link EAlterColumnSubType#SetDataType}
522     *
523     * @return new collation
524     */
525    public TObjectName getNewCollation() {
526
527        return newCollation;
528    }
529
530    public void setNewDataType(TTypeName newDataType) {
531        this.newDataType = newDataType;
532    }
533
534    /**
535     * new data type when option type is {@link gudusoft.gsqlparser.EAlterTableOptionType#AlterColumn} and subtype is {@link EAlterColumnSubType#SetDataType}
536     *
537     * @return new data type
538     */
539    public TTypeName getNewDataType() {
540
541        return newDataType;
542    }
543
544    public void setStorageName(TObjectName storageName) {
545        this.storageName = storageName;
546    }
547
548    /**
549     * storage name when option type is {@link gudusoft.gsqlparser.EAlterTableOptionType#AlterColumn} and subtype is {@link EAlterColumnSubType#SetStorage}
550     *
551     * @return storage name
552     */
553    public TObjectName getStorageName() {
554
555        return storageName;
556    }
557
558    public void setAttributeOptions(ArrayList<TAttributeOption> attributeOptions) {
559        this.attributeOptions = attributeOptions;
560    }
561
562    /**
563     * Greenplum,PostgreSQL,Snowflake, set/reset options when option type is {@link gudusoft.gsqlparser.EAlterTableOptionType#AlterColumn} and subtype is {@link EAlterColumnSubType#SetOptions}
564     * or {@link EAlterColumnSubType#ResetOptions}
565     *
566     * @return set/reset options
567     */
568    public ArrayList<TAttributeOption> getAttributeOptions() {
569
570        return attributeOptions;
571    }
572
573    public void setStatisticsValue(TConstant statisticsValue) {
574        this.statisticsValue = statisticsValue;
575    }
576
577    /**
578     * Greenplum,PostgreSQL,Snowflake, statistics value when option type is {@link gudusoft.gsqlparser.EAlterTableOptionType#AlterColumn} and subtype is {@link EAlterColumnSubType#SetStatistics}
579     *
580     * @return statistcs value
581     */
582    public TConstant getStatisticsValue() {
583
584        return statisticsValue;
585    }
586
587    public void setDefaultExpr(TExpression defaultExpr) {
588        this.defaultExpr = defaultExpr;
589    }
590
591    /**
592     * MySQL, PostgreSQL, expression in set default clause of alter column.
593     *
594     * @return expression in set default clause of alter column.
595     */
596    public TExpression getDefaultExpr() {
597
598        return defaultExpr;
599    }
600
601    private TMySQLIndexStorageType mySQLIndexStorageType = null;
602    private TSourceToken mySQLIndexTypeToken = null;
603
604    public void setMySQLIndexTypeToken(TSourceToken mySQLIndexTypeToken) {
605        this.mySQLIndexTypeToken = mySQLIndexTypeToken;
606
607        if (mySQLIndexTypeToken == null) return;
608
609        switch (getOptionType()){
610            case AddConstraintIndex:
611                if (mySQLIndexTypeToken.tokencode == TBaseType.rrw_unique){
612                    setOptionType(EAlterTableOptionType.AddConstraintUnique);
613                }
614                break;
615        }
616    }
617
618    /**
619     * MySQL, unique, fulltext or spatial keyword used in add key/index clause when option type is {@link EAlterTableOptionType#AddConstraintIndex}
620     *
621     * @return unique, fulltext or spatial keyword
622     */
623    public TSourceToken getMySQLIndexTypeToken() {
624
625        return mySQLIndexTypeToken;
626    }
627
628    public void setMySQLIndexStorageType(TMySQLIndexStorageType mySQLIndexStorageType) {
629        this.mySQLIndexStorageType = mySQLIndexStorageType;
630    }
631
632    /**
633     * @deprecated As of v1.7.2.4. use {@link #getIndexOptionList()} instead.
634     * @return index storage type
635     */
636    public TMySQLIndexStorageType getMySQLIndexStorageType() {
637
638        return mySQLIndexStorageType;
639    }
640
641    /**
642     * set new table name when option type is {@link EAlterTableOptionType#RenameTable }
643     *
644     * @param newTableName the new table name
645     */
646    public void setNewTableName(TObjectName newTableName) {
647        this.newTableName = newTableName;
648        this.newTableName.setDbObjectType(EDbObjectType.table);
649    }
650
651    /**
652     * new table name is valid when option type is one of the following values:
653     * <ul>
654     *     <li>{@link EAlterTableOptionType#RenameTable}, new table in rename to clause</li>
655     *     <li>{@link EAlterTableOptionType#setPrivileges}, Netezza, table name in set privileges to clause </li>
656     *     <li>{@link EAlterTableOptionType#exchangePartition},Hive, Impala, table name in exchange partition with table clause </li>
657     *     <li>{@link EAlterTableOptionType#switchPartition}, SQL Server, Openedge, table name of switch partition to target_table clause </li>
658     * </ul>
659     *
660     * @return new table name
661     */
662    public TObjectName getNewTableName() {
663
664        return newTableName;
665    }
666
667    public void setReferencedColumnList(TObjectNameList referencedColumnList) {
668        this.referencedColumnList = referencedColumnList;
669    }
670
671    public void setReferencedObjectName(TObjectName referencedObjectName) {
672        this.referencedObjectName = referencedObjectName;
673    }
674
675    public void setColumnName(TObjectName columnName) {
676        this.columnName = columnName;
677    }
678
679    /**
680     * List of {@link TConstraint} in add constraint clause, valid when {@link #getOptionType()} is {@link EAlterTableOptionType#AddConstraint}
681     *
682     * @return constraint list
683     */
684    public TConstraintList getConstraintList() {
685        return constraintList;
686    }
687
688    private TObjectNameList nameList = null;
689
690    public TObjectNameList getNameList() {
691        if (this.nameList == null){
692
693            this.nameList = new TObjectNameList();
694        }
695        return nameList;
696    }
697
698    /**
699     * List of {@link TObjectNameList}, valid when {@link #getOptionType()} is:
700     * <p>{@link EAlterTableOptionType#SetUnUsedColumn}
701     * <p>{@link EAlterTableOptionType#DropColumn}
702     * <p>{@link EAlterTableOptionType#AddConstraintIndex}, columns of an index
703     * <p>{@link EAlterTableOptionType#AddConstraintPK}, columns of primary key
704     * <p>{@link EAlterTableOptionType#AddConstraintUnique}, columns of unique key
705     * <p>{@link EAlterTableOptionType#AddConstraintFK}, columns of foreign key
706     * <p>{@link EAlterTableOptionType#DropConstraintUnique}, available in Oracle
707     * @return column name list
708     */
709    public TObjectNameList getColumnNameList() {
710        if (this.columnNameList == null){
711
712            this.columnNameList = new TObjectNameList();
713        }
714        return columnNameList;
715    }
716
717    /**
718     * List of {@link TColumnDefinitionList}, valid when {@link #getOptionType()} is:
719     * <p>{@link EAlterTableOptionType#AddColumn}
720     * <p>{@link EAlterTableOptionType#ModifyColumn}
721     *
722     * @return column definition list
723     */
724    public TColumnDefinitionList getColumnDefinitionList() {
725        return columnDefinitionList;
726    }
727
728    /**
729     * valid when {@link #getOptionType()} is:
730     * <p>{@link EAlterTableOptionType#AlterColumn}
731     * <p>{@link EAlterTableOptionType#RenameColumn}
732     * <p>{@link EAlterTableOptionType#ChangeColumn}
733     *
734     * <p>{@link EAlterTableOptionType#AddConstraintIndex}, index name, optional
735     * @return column name
736     */
737    public TObjectName getColumnName() {
738        return columnName;
739    }
740
741    /**
742     *valid when {@link #getOptionType()} is:
743     * <p>{@link EAlterTableOptionType#AddConstraintPK}, optional
744     * <p>{@link EAlterTableOptionType#AddConstraintUnique}, optional
745     * <p>{@link EAlterTableOptionType#AddConstraintFK}, optional
746     * <p>{@link EAlterTableOptionType#ModifyConstraint}
747     * <p>{@link EAlterTableOptionType#RenameConstraint}
748     * <p>{@link EAlterTableOptionType#DropConstraintFK}
749     * <p>{@link EAlterTableOptionType#DropConstraintUnique}, available in DB2
750     * <p>{@link EAlterTableOptionType#DropConstraintCheck}, available in DB2
751     * <p>{@link EAlterTableOptionType#DropConstraintIndex}, available in DB2
752     * <p>{@link EAlterTableOptionType#DropConstraintKey}, available in DB2
753     * <p>{@link EAlterTableOptionType#AlterConstraintFK}, available in DB2
754     * <p>{@link EAlterTableOptionType#AlterConstraintCheck}, available in DB2
755     * @return constraint name
756     */
757    public TObjectName getConstraintName() {
758        return constraintName;
759    }
760
761    /**
762     * new column name valid when {@link #getOptionType()} is {@link EAlterTableOptionType#RenameColumn}
763     *
764     * @return new column name
765     */
766    public TObjectName getNewColumnName() {
767        return newColumnName;
768    }
769
770    /**
771     * new constraint name valid when {@link #getOptionType()} is {@link EAlterTableOptionType#RenameConstraint}
772     *
773     * @return new constraint name
774     */
775    public TObjectName getNewConstraintName() {
776        return newConstraintName;
777    }
778
779    /**
780     * MySQL, referenced column list in foreign key clause.
781     *
782     * @return referenced column list in foreign key clause.
783     */
784    public TObjectNameList getReferencedColumnList() {
785        return referencedColumnList;
786    }
787
788    /**
789     * referenced table in foreign key clause. Please use {@link #getReferencedTable} instead.
790     *
791     * @return referenced table in foreign key clause
792     */
793    public TObjectName getReferencedObjectName() {
794        return referencedObjectName;
795    }
796
797    public void init(Object arg1){
798        this.optionType = (EAlterTableOptionType)arg1;
799    }
800
801    public void init(Object arg1,Object arg2){
802        init(arg1);
803        switch (optionType){
804            case dropPartitionSpecList:
805            case addPartitionSpecList:
806            case touch:
807            case archive:
808            case unArchive:
809            case setPartitionLocation:
810                this.partitionSpecList = (ArrayList<TPartitionExtensionClause>)arg2;
811                break;
812            case setTableProperties:
813            case unsetTableProperties:
814                this.tableProperties = (TPTNodeList)arg2;
815                break;
816            case setFileFormat:
817                this.fileFormat = (THiveTableFileFormat)arg2;
818                break;
819            case setLocation:
820                this.tableLocation = (TObjectName)arg2;
821                break;
822            case serde:
823                serdeName = (TObjectName)arg2;
824                break;
825            case renamePartition:
826            case addPartition:
827            case dropPartition:
828                if (arg2 instanceof TObjectNameList){
829                    nameList = (TObjectNameList) arg2;
830                }
831                else if (arg2 instanceof TPartitionExtensionClause){
832                    newPartitionSpec= (TPartitionExtensionClause)arg2;
833                }
834                else if (arg2 instanceof ArrayList){
835                    partitionDefinitionList = (ArrayList<TPartitionDefinition>)arg2;
836                }
837                else{
838                    partitionSpecList = (ArrayList<TPartitionExtensionClause>)arg2;
839                    newPartitionSpec = partitionSpecList.get(0);
840                }
841
842                break;
843            case rebuildPartition:
844            case optimizePartition:
845            case repairPartition:
846            case analyzePartition:
847            case checkPartition:
848            case discardPartition:
849            case importPartition:
850            case truncatePartition:
851                nameList = (TObjectNameList) arg2;
852                break;
853            case dropCheck:
854                break;
855            case dropConstraint:
856                break;
857            case alterConstraint:
858                break;
859            case alterCheck:
860                break;
861            case coalescePartition:
862                break;
863            case tableBuckets:
864                numberOfBuckets = (TConstant)arg2;
865                break;
866            case skewedLocation:
867                skewedLocations = (TExpressionList)arg2;
868                break;
869            case clusteredSorted:
870                tableBuckets    = (THiveTableBuckets)arg2;
871                break;
872            case partitionColumn:
873                partitionColumnDef = (TColumnDefinition)arg2;
874                break;
875            case tableSkewed:
876                tableSkewed = (THiveTableSkewed)arg2;
877                break;
878            case setSchema:
879                schemaName = (TObjectName)arg2;
880                break;
881            case DropConstraint:
882                constraintNameList = (TObjectNameList)arg2;
883                if (constraintList != null){
884                    constraintName = constraintNameList.getObjectName(0);
885                }
886                break;
887            case CheckConstraint:
888            case NocheckConstraint:
889                constraintNameList = (TObjectNameList)arg2;
890                if (constraintNameList != null && constraintNameList.size() > 0){
891                    constraintName = constraintNameList.getObjectName(0);
892                }
893                break;
894            case modifyPrimaryKey:
895                indexName = (TObjectName)arg2;
896                break;
897            case switchPartition:
898                newTableName = (TObjectName)arg2;
899                break;
900            case AddConstraintIndex:
901            case AddConstraintPK:
902            case AddConstraintUnique:
903            case AddConstraintFK:
904                if (arg2 instanceof TPTNodeList){
905                    indexCols = (TPTNodeList)arg2;
906                }else{
907                    columnNameList = (TObjectNameList)arg2;
908                }
909                break;
910            case AddConstraintCheck:
911                defaultExpr = (TExpression)arg2;
912                break;
913            case clickhouseDeleteMutation:
914                defaultExpr = (TExpression)arg2;
915                break;
916            case clickhouseDropPartition:
917            case clickhouseDetachPartition:
918            case clickhouseAttachPartition:
919            case clickhouseFreezePartition:
920            case clickhouseUnfreezePartition:
921                partitionExpr = (TParseTreeNode)arg2;
922                break;
923            case setPrivileges:
924                newTableName = (TObjectName)arg2;
925                break;
926            case AddColumn:
927                this.columnDefinitionList = (TColumnDefinitionList)arg2;
928                break;
929            case AlterColumn:
930                this.columnName = (TObjectName)arg2;
931                this.columnName.setDbObjectType(EDbObjectType.column);
932                break;
933            case attachPartition:
934            case detachPartition:
935            case modifySubPartition:
936                partitionName = (TObjectName)arg2;
937                if (partitionName != null) partitionName.setDbObjectType(EDbObjectType.partition);
938                break;
939            case swapWith:
940                newTableName = (TObjectName)arg2;
941                break;
942            case setDataRetentionTimeInDays:
943                break;
944            case setComment:
945                break;
946            case RenameTable:
947                newTableName = (TObjectName)arg2;
948                newTableName.setDbObjectType(EDbObjectType.table);
949                break;
950            case appendFrom:
951                this.sourceTableName = (TObjectName) arg2;
952                break;
953            case AddConstraint:
954                if (arg2 instanceof TConstraint){
955                    this.tableConstraint = (TConstraint) arg2;
956                }else if (arg2 instanceof TConstraintList){
957                    this.constraintList  = (TConstraintList) arg2;
958                }
959                break;
960            case mysqlTableOption:
961                this.mySQLTableOption = (TMySQLCreateTableOption)arg2;
962                break;
963            case renameIndex:
964                this.indexName = (TObjectName)arg2;
965                this.indexName.setDbObjectTypeDirectly(EDbObjectType.index);
966                break;
967            case partition:
968                this.tablePartition = (TBaseTablePartition) arg2;
969                break;
970            case addPeriodForClause:
971                this.periodForClause = (TPeriodForClause)arg2;
972                break;
973            default:
974                if (arg2 instanceof TColumnDefinitionList){
975                   this.columnDefinitionList = (TColumnDefinitionList)arg2;
976                }else if (arg2 instanceof TConstraintList){
977                   this.constraintList = (TConstraintList)arg2;
978                }else if (arg2 instanceof TObjectNameList) {
979
980                   this.columnNameList = (TObjectNameList)arg2;
981                }else if (arg2 instanceof TDummyList) {
982                   TDummyList dlist = (TDummyList)arg2;
983                    TDummy dm = null;
984                   for(int i=0;i<dlist.size();i++){
985                       dm = dlist.getDummyItem(i);
986                       this.getColumnNameList().addObjectName((TObjectName)dm.node1);
987                   }
988
989                   if ((optionType == EAlterTableOptionType.DropColumn)&&(this.getColumnNameList().size() > 0)){
990                       this.columnName = this.getColumnNameList().getObjectName(0);
991                   }
992                }
993                break;
994
995        }
996
997     }
998
999
1000    private String comment;
1001
1002    public String getComment() {
1003        return comment;
1004    }
1005
1006    private TExpression commentExpr;
1007
1008    public TExpression getCommentExpr() {
1009        return commentExpr;
1010    }
1011
1012    public void setNewConstraintName(TObjectName newConstraintName) {
1013        this.newConstraintName = newConstraintName;
1014    }
1015
1016    public void setConstraintName(TDummy pName) {
1017        if (pName == null) return;
1018        if (pName.node1 == null) return;
1019        this.constraintName = (TObjectName)pName.node1;
1020    }
1021    public void setConstraintName(TObjectName constraintName) {
1022
1023        this.constraintName = constraintName;
1024    }
1025
1026    private TColumnDefinition newColumnDef;
1027
1028    /**
1029     *
1030     * @return {@link TColumnDefinition}, valid when {@link #getOptionType()} is:
1031     * <p>{@link EAlterTableOptionType#ChangeColumn}
1032     */
1033
1034    public TColumnDefinition getNewColumnDef() {
1035        return newColumnDef;
1036    }
1037
1038    /**
1039     *
1040     * valid when {@link #getOptionType()} is:
1041     * <p>{@link EAlterTableOptionType#serde}
1042     * @return serde name
1043     */
1044    public TObjectName getSerdeName() {
1045        return serdeName;
1046    }
1047
1048    public void init(Object arg1,Object arg2,Object arg3){
1049        init(arg1);
1050        switch (optionType){
1051            case AlterColumn:
1052                init(arg1,arg2);
1053                AlterColumnSubType = (EAlterColumnSubType)arg3;
1054                break;
1055            case ChangeColumn:
1056                this.columnName = (TObjectName)arg2;
1057                this.newColumnDef = (TColumnDefinition)arg3;
1058
1059                break;
1060            case serde:
1061                serdeName  = (TObjectName)arg2;
1062                tableProperties = (TPTNodeList)arg3;
1063
1064                break;
1065            case exchangePartition:
1066                if ((dbvendor == EDbVendor.dbvoracle)
1067                        ||(dbvendor == EDbVendor.dbvgaussdb)||(dbvendor == EDbVendor.dbvedb)||(dbvendor == EDbVendor.dbvgreenplum)
1068                        ||(dbvendor == EDbVendor.dbvmysql)){
1069                    partitionName = (TObjectName)arg2;
1070                    partitionName.setDbObjectType(EDbObjectType.partition);
1071                }else{
1072                    exchangePartitionSpec = (TPartitionExtensionClause)arg2;
1073                }
1074
1075                newTableName = (TObjectName)arg3;
1076                newTableName.setDbObjectType(EDbObjectType.table);
1077                break;
1078            case RenameColumn:
1079                this.columnName = (TObjectName)arg2;
1080                this.newColumnName = (TObjectName)arg3;
1081                break;
1082            case modifyPrimaryKey:
1083                if (arg2 !=null){
1084                    indexName = (TObjectName)arg2;
1085                }
1086                columnNameList = (TObjectNameList)arg3;
1087                break;
1088            case switchPartition:
1089                newTableName = (TObjectName)arg2;
1090                partitionExpression1 = (TExpression)arg3;
1091                break;
1092            case attachPartition:
1093                partitionName = (TObjectName)arg2;
1094                partitionBoundSpec = (TPartitionBoundSpecSqlNode) arg3;
1095                break;
1096            case reorganizePartition:
1097                nameList = (TObjectNameList) arg2;
1098                partitionDefinitionList = (ArrayList<TPartitionDefinition>)arg3;
1099                break;
1100            case renameIndex:
1101                this.indexName = (TObjectName)arg2;
1102                this.indexName.setDbObjectTypeDirectly(EDbObjectType.index);
1103                this.newIndexName  = (TObjectName)arg3;
1104                this.newIndexName.setDbObjectTypeDirectly(EDbObjectType.index);
1105                break;
1106            case clickhouseUpdateMutation:
1107                this.mutationUpdateList = (TResultColumnList)arg2;
1108                this.defaultExpr = (TExpression)arg3;
1109                break;
1110            case clickhouseFetchPartition:
1111                this.partitionExpr = (TParseTreeNode)arg2;
1112                // arg3 is the FROM path string constant
1113                break;
1114            case clickhouseMovePartition:
1115                this.partitionExpr = (TParseTreeNode)arg2;
1116                this.newTableName = (TObjectName)arg3;
1117                this.newTableName.setDbObjectType(EDbObjectType.table);
1118                break;
1119            case clickhouseReplacePartition:
1120                this.partitionExpr = (TParseTreeNode)arg2;
1121                this.sourceTableName = (TObjectName)arg3;
1122                this.sourceTableName.setDbObjectType(EDbObjectType.table);
1123                break;
1124            default:
1125                this.columnName = (TObjectName)arg2;
1126                this.newColumnName = (TObjectName)arg3;
1127                break;
1128        }
1129    }
1130
1131    public void init(Object arg1,Object arg2,Object arg3,Object arg4) {
1132        init(arg1,arg2,arg3);
1133        switch (optionType) {
1134            case switchPartition:
1135                partitionExpression2 = (TExpression)arg4;
1136                break;
1137            case AlterColumn:
1138                switch (AlterColumnSubType){
1139                    case Comment:
1140                        this.comment = ((TSourceToken)arg4).toString();
1141                        break;
1142                }
1143                break;
1144            default:
1145                break;
1146        }
1147    }
1148
1149    private ArrayList<TPartitionDefinition> partitionDefinitionList;
1150
1151    public ArrayList<TPartitionDefinition> getPartitionDefinitionList() {
1152        return partitionDefinitionList;
1153    }
1154
1155    //private TPTNodeList <TPartitionExtensionClause> partitionSpecList;
1156    private ArrayList<TPartitionExtensionClause> partitionSpecList;
1157
1158    /**
1159     * Hive, Impala, partition spec list in unarchive clause when option type is {@link EAlterTableOptionType#unArchive},
1160     * {@link EAlterTableOptionType#archive}.
1161     * <br>partition spec list in touch clause when option type is {@link EAlterTableOptionType#touch},
1162     * <br>partition spec list in drop clause when option type is {@link EAlterTableOptionType#dropPartitionSpecList},
1163     * <br>partition spec list in add clause when option type is {@link EAlterTableOptionType#addPartitionSpecList},
1164     *
1165     * @return    partition spec list
1166     */
1167    public ArrayList<TPartitionExtensionClause> getPartitionSpecList() {
1168        return partitionSpecList;
1169    }
1170
1171    private TPTNodeList <THiveKeyValueProperty> tableProperties;
1172
1173    /**
1174     * Hive, Impala, table properties of SET/UNSET TBLPROPERTIES clause
1175     * when {@link #getOptionType()} is {@link EAlterTableOptionType#setTableProperties} or
1176     * {@link EAlterTableOptionType#unsetTableProperties}
1177     * <br> table properties of SET SERDE clause when {@link #getOptionType()} is {@link EAlterTableOptionType#serde}
1178     *
1179     * @return  table properties of SET/UNSET TBLPROPERTIES/SET SERDE clause.
1180     */
1181    public TPTNodeList<THiveKeyValueProperty> getTableProperties() {
1182        return tableProperties;
1183    }
1184
1185    private THiveTableFileFormat fileFormat;
1186
1187    /**
1188     * Hive,Impala,file format of SET FILEFORMAT clause valid when {@link #getOptionType()} is {@link EAlterTableOptionType#setFileFormat}
1189     *
1190     * @return      file format of SET FILEFORMAT
1191     */
1192    public THiveTableFileFormat getFileFormat() {
1193        return fileFormat;
1194    }
1195
1196    private TObjectName tableLocation;
1197
1198    /**
1199     * Hive,Impala,  file location of SET LOCATION clause valid when {@link #getOptionType()} is:
1200     * {@link EAlterTableOptionType#setLocation}
1201     *
1202     * @return   file location of SET LOCATION clause
1203     */
1204    public TObjectName getTableLocation() {
1205        return tableLocation;
1206    }
1207
1208    private TPartitionDefinition partitionDefinition;
1209
1210    public TPartitionDefinition getPartitionDefinition() {
1211        return partitionDefinition;
1212    }
1213
1214    /**
1215     * Hive, Impala, new partition spec in RENAME TO clause
1216     * valid when {@link #getOptionType()} is:
1217     * {@link EAlterTableOptionType#renamePartition}
1218     *
1219     * @return  new partition spec in RENAME TO clause
1220     */
1221    public TPartitionExtensionClause getNewPartitionSpec() {
1222        return newPartitionSpec;
1223    }
1224
1225
1226    private TPartitionExtensionClause newPartitionSpec;
1227
1228    private TConstant numberOfBuckets;
1229
1230    /**
1231     * Hive, Impala, number of buckets of INTO BUCKETS clause
1232     * valid when {@link #getOptionType()} is:
1233     * {@link EAlterTableOptionType#tableBuckets}
1234     *
1235     * @return  number of buckets of INTO BUCKETS clause
1236     */
1237    public TConstant getNumberOfBuckets() {
1238        return numberOfBuckets;
1239    }
1240
1241    private TExpressionList skewedLocations;
1242
1243    /**
1244     * Hive, Impala, skewed location of SET SKEWED LOCATION clause
1245     * valid when {@link #getOptionType()} is:
1246     * {@link EAlterTableOptionType#skewedLocation}
1247     *
1248     * @return  skewed location of SET SKEWED LOCATION clause
1249     */
1250    public TExpressionList getSkewedLocations() {
1251        return skewedLocations;
1252    }
1253
1254    private THiveTableBuckets tableBuckets;
1255
1256
1257    /**
1258     * Hive, Impala, clustered by ... [sorted by ...] into number buckets
1259     * valid when {@link #getOptionType()} is:
1260     * {@link EAlterTableOptionType#clusteredSorted}
1261     *
1262     * @return    table buckets
1263     */
1264    public THiveTableBuckets getTableBuckets() {
1265        return tableBuckets;
1266    }
1267
1268    private TColumnDefinition partitionColumnDef;
1269
1270    /**
1271     * Hive,Impala, column of  PARTITION COLUMN ( columnDef ) clause
1272     * valid when {@link #getOptionType()} is:
1273     * {@link EAlterTableOptionType#clusteredSorted}
1274     *
1275     * @return  column of  PARTITION COLUMN ( columnDef ) clause
1276     */
1277    public TColumnDefinition getPartitionColumnDef() {
1278        return partitionColumnDef;
1279    }
1280
1281    private THiveTableSkewed     tableSkewed;
1282
1283    /**
1284     * Hive, Impala,   tableSkewed clause syntax like: SKEWED BY '(' columnNameList ')' ON '(' skewedValueElement ')'
1285     * valid when {@link #getOptionType()} is:
1286     * {@link EAlterTableOptionType#tableSkewed}
1287     *
1288     * @return         tableSkewed clause
1289     */
1290    public THiveTableSkewed getTableSkewed() {
1291        return tableSkewed;
1292    }
1293
1294    /**
1295     * Hive,Impala, partition spec of EXCHANGE partitionSpec WITH TABLE tableName clause.
1296     * valid when {@link #getOptionType()} is:
1297     * {@link EAlterTableOptionType#exchangePartition}
1298     *
1299     * @return     partition spec of EXCHANGE partitionSpec WITH TABLE tableName clause.
1300     */
1301    public TPartitionExtensionClause getExchangePartitionSpec() {
1302        return exchangePartitionSpec;
1303    }
1304
1305    private TPartitionExtensionClause exchangePartitionSpec;
1306
1307    private EAlterTableOptionType optionType = EAlterTableOptionType.Unknown;
1308
1309
1310    public void doParse(TCustomSqlStatement psql, ESqlClause plocation){
1311
1312        switch (optionType){
1313            case AddColumn:
1314                getColumnDefinitionList().doParse(psql,plocation);
1315                for(int i=0;i<getColumnDefinitionList().size();i++){
1316                    TColumnDefinition cd = getColumnDefinitionList().getColumn(i);
1317
1318                    cd.getColumnName().setLocation(ESqlClause.createTable);
1319                    cd.getColumnName().setLinkedColumnDef(cd);
1320                    psql.tables.getTable(0).getLinkedColumns().addObjectName(cd.getColumnName());
1321                    cd.getColumnName().setSourceTable(psql.tables.getTable(0));
1322
1323                }
1324                break;
1325            case ModifyColumn:
1326                if (getColumnDefinitionList() != null){
1327                    getColumnDefinitionList().doParse(psql,plocation);
1328                    for(int i=0;i<getColumnDefinitionList().size();i++){
1329                        TColumnDefinition cd = getColumnDefinitionList().getColumn(i);
1330
1331                        cd.getColumnName().setLocation(ESqlClause.createTable);
1332                        cd.getColumnName().setLinkedColumnDef(cd);
1333                        psql.tables.getTable(0).getLinkedColumns().addObjectName(cd.getColumnName());
1334                        cd.getColumnName().setSourceTable(psql.tables.getTable(0));
1335
1336                    }
1337                }
1338                break;
1339            case DropColumn:
1340                for(int i=0;i<getColumnNameList().size();i++){
1341                    TObjectName dropedColumn = getColumnNameList().getObjectName(i);
1342                    psql.tables.getTable(0).getLinkedColumns().addObjectName(dropedColumn);
1343                    dropedColumn.setSourceTable(psql.tables.getTable(0));
1344                }
1345                break;
1346            case ChangeColumn:
1347                psql.tables.getTable(0).getLinkedColumns().addObjectName(getColumnName());
1348                getColumnName().setSourceTable(psql.tables.getTable(0));
1349
1350                TObjectName newColumn = newColumnDef.getColumnName();
1351                psql.tables.getTable(0).getLinkedColumns().addObjectName(newColumn);
1352                newColumn.setSourceTable(psql.tables.getTable(0));
1353
1354                break;
1355            case AddConstraintPK:
1356            case AddConstraintUnique:
1357                if (getIndexCols() != null){
1358                    for(int i=0;i<getIndexCols().size();i++){
1359                        psql.tables.getTable(0).getLinkedColumns().addObjectName(getIndexCols().getElement(i).getColumnName());
1360                        getIndexCols().getElement(i).getColumnName().setSourceTable(psql.tables.getTable(0));
1361                    }
1362                }
1363                else if (getColumnNameList().size() > 0){
1364                    for (int i=0;i<getColumnNameList().size();i++){
1365                        TObjectName columnName = getColumnNameList().getObjectName(i);
1366                        psql.tables.getTable(0).getLinkedColumns().addObjectName(columnName);
1367                        columnName.setSourceTable(psql.tables.getTable(0));
1368                    }
1369                }
1370                break;
1371            case AddConstraintFK:
1372                if (getIndexCols() != null){
1373                    for(int i=0;i<getIndexCols().size();i++){
1374                        psql.tables.getTable(0).getLinkedColumns().addObjectName(getIndexCols().getElement(i).getColumnName());
1375                        getIndexCols().getElement(i).getColumnName().setSourceTable(psql.tables.getTable(0));
1376                    }
1377                }
1378                else if (getColumnNameList().size() > 0){
1379                    for (int i=0;i<getColumnNameList().size();i++){
1380                        TObjectName columnName = getColumnNameList().getObjectName(i);
1381                        psql.tables.getTable(0).getLinkedColumns().addObjectName(columnName);
1382                        columnName.setSourceTable(psql.tables.getTable(0));
1383                    }
1384                }
1385
1386                if (referencedObjectName != null){
1387                    referencedObjectName.setDbObjectType(EDbObjectType.table);
1388                    referencedTable = psql.analyzeTablename(referencedObjectName);
1389                    if (getReferencedColumnList() != null) {
1390                        for (int i = 0; i < getReferencedColumnList().size(); i++) {
1391                            TObjectName refColumn = getReferencedColumnList().getObjectName(i);
1392                            referencedTable.getLinkedColumns().addObjectName(refColumn);
1393                            refColumn.setSourceTable(referencedTable);
1394                        }
1395                    }
1396
1397                }
1398                break;
1399            case RenameTable:
1400                TTable lcTable = new TTable(newTableName);
1401                lcTable.setPropertyFromObjectName(newTableName, ETableEffectType.tetAlterTableRename);
1402                psql.addToTables(lcTable);
1403                break;
1404            case AddConstraint:
1405                if (tableConstraint != null){
1406                    tableConstraint.doParse(psql,ESqlClause.unknown);
1407                }else if (constraintList != null){
1408                    constraintList.doParse(psql,ESqlClause.unknown);
1409                }
1410                break;
1411        }
1412
1413    }
1414
1415    private TExpression retenttionTimeInDays;
1416
1417    public TExpression getRetenttionTimeInDays() {
1418        return retenttionTimeInDays;
1419    }
1420
1421    public void accept(TParseTreeVisitor v)
1422    {
1423        v.preVisit(this);
1424        v.postVisit(this);
1425    }
1426
1427    public void acceptChildren(TParseTreeVisitor v)
1428    {
1429        v.preVisit(this);
1430        switch (optionType){
1431            case AlterColumn:
1432                columnName.acceptChildren(v);
1433                if (newDataType != null){
1434                    newDataType.acceptChildren(v);
1435                }
1436                break;
1437            case AddColumn:
1438                for(int i=0;i<getColumnDefinitionList().size();i++){
1439                    TColumnDefinition cd = getColumnDefinitionList().getColumn(i);
1440                    cd.acceptChildren(v);
1441                }
1442                break;
1443            case ModifyColumn:
1444                if (getColumnDefinitionList() != null){
1445                    for(int i=0;i<getColumnDefinitionList().size();i++){
1446                        TColumnDefinition cd = getColumnDefinitionList().getColumn(i);
1447                        cd.acceptChildren(v);
1448                    }
1449                }
1450                break;
1451            case DropColumn:
1452                for(int i=0;i<getColumnNameList().size();i++){
1453                    TObjectName dropedColumn = getColumnNameList().getObjectName(i);
1454                    dropedColumn.acceptChildren(v);
1455                }
1456                break;
1457            case ChangeColumn:
1458                break;
1459            case AddConstraintPK:
1460            case AddConstraintUnique:
1461                break;
1462            case AddConstraintFK:
1463                break;
1464            case RenameTable:
1465                break;
1466        }
1467        v.postVisit(this);
1468    }
1469
1470    public void setSerdeName(TObjectName serdeName) {
1471        this.serdeName = serdeName;
1472    }
1473
1474    public void setDropRestrictType(EKeyReferenceType dropRestrictType) {
1475        this.dropRestrictType = dropRestrictType;
1476    }
1477
1478    public void setColumnDefinitionList(TColumnDefinitionList columnDefinitionList) {
1479        this.columnDefinitionList = columnDefinitionList;
1480    }
1481
1482    public void setConstraintList(TConstraintList constraintList) {
1483        this.constraintList = constraintList;
1484    }
1485
1486    public void setColumnNameList(TObjectNameList columnNameList) {
1487        this.columnNameList = columnNameList;
1488    }
1489
1490    public void setNewColumnName(TObjectName newColumnName) {
1491        this.newColumnName = newColumnName;
1492    }
1493
1494    public void setSchemaName(TObjectName schemaName) {
1495        this.schemaName = schemaName;
1496    }
1497
1498    public void setConstraintNameList(TObjectNameList constraintNameList) {
1499        this.constraintNameList = constraintNameList;
1500    }
1501
1502    public void setPartitionExpression1(TExpression partitionExpression1) {
1503        this.partitionExpression1 = partitionExpression1;
1504    }
1505
1506    public void setPartitionExpression2(TExpression partitionExpression2) {
1507        this.partitionExpression2 = partitionExpression2;
1508    }
1509
1510    public void setNewColumnDef(TColumnDefinition newColumnDef) {
1511        this.newColumnDef = newColumnDef;
1512    }
1513
1514//    public void setPartitionSpecList(TPTNodeList<TPartitionExtensionClause> partitionSpecList) {
1515//        this.partitionSpecList = partitionSpecList;
1516//    }
1517
1518    public void setTableProperties(TPTNodeList<THiveKeyValueProperty> tableProperties) {
1519        this.tableProperties = tableProperties;
1520    }
1521
1522    public void setFileFormat(THiveTableFileFormat fileFormat) {
1523        this.fileFormat = fileFormat;
1524    }
1525
1526    public void setTableLocation(TObjectName tableLocation) {
1527        this.tableLocation = tableLocation;
1528    }
1529
1530    public void setNewPartitionSpec(TPartitionExtensionClause newPartitionSpec) {
1531        this.newPartitionSpec = newPartitionSpec;
1532    }
1533
1534    public void setNumberOfBuckets(TConstant numberOfBuckets) {
1535        this.numberOfBuckets = numberOfBuckets;
1536    }
1537
1538    public void setSkewedLocations(TExpressionList skewedLocations) {
1539        this.skewedLocations = skewedLocations;
1540    }
1541
1542    public void setTableBuckets(THiveTableBuckets tableBuckets) {
1543        this.tableBuckets = tableBuckets;
1544    }
1545
1546    public void setPartitionColumnDef(TColumnDefinition partitionColumnDef) {
1547        this.partitionColumnDef = partitionColumnDef;
1548    }
1549
1550    public void setTableSkewed(THiveTableSkewed tableSkewed) {
1551        this.tableSkewed = tableSkewed;
1552    }
1553
1554    public void setExchangePartitionSpec(TPartitionExtensionClause exchangePartitionSpec) {
1555        this.exchangePartitionSpec = exchangePartitionSpec;
1556    }
1557
1558    public void setOptionType(EAlterTableOptionType optionType) {
1559        this.optionType = optionType;
1560    }
1561}