001package gudusoft.gsqlparser.stmt;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.nodes.*;
005import gudusoft.gsqlparser.nodes.flink.TFlinkWatermarkClause;
006import gudusoft.gsqlparser.nodes.flink.TFlinkWithClause;
007import gudusoft.gsqlparser.nodes.hive.*;
008import gudusoft.gsqlparser.nodes.netezza.TExternalTableOption;
009import gudusoft.gsqlparser.nodes.oracle.TPhysicalProperties;
010import gudusoft.gsqlparser.nodes.postgresql.TInheritsClause;
011import gudusoft.gsqlparser.nodes.postgresql.TPartitionBoundSpecSqlNode;
012import gudusoft.gsqlparser.nodes.teradata.TIndexDefinition;
013import gudusoft.gsqlparser.nodes.teradata.TTeradataPeriodForClause;
014
015import java.util.*;
016
017/**
018 * SQL create table statement.
019 * <br>
020 * {@link #getTargetTable} returns the created table. {@link #getColumnList()} returns columns created in the table.
021 * <br> {@link #getTableConstraints()} returns table level constraints if any.
022 * <br> {@link #getSubQuery()} returns select statement that generate data for this table.
023 * <br><br>Example:
024 * <pre>
025 *     CREATE TABLE dbo.Employee (EmployeeID int PRIMARY KEY CLUSTERED);
026 * </pre>
027 * Table:dbo.Employee can be fetched from {@link #getTargetTable} or the first element of {@link #tables}
028 * <br> column definitions: can be fetched from {@link #getColumnList}
029 * <br>
030 * table constraints: {@link #getTableConstraints}
031 *
032 */
033public class TCreateTableSqlStatement extends TCustomSqlStatement {
034
035
036    public TInheritsClause getInheritsClause() {
037        return inheritsClause;
038    }
039
040    private TInheritsClause inheritsClause;
041
042    private TBaseTablePartition tablePartition;
043
044
045    public TBaseTablePartition getTablePartition() {
046        return tablePartition;
047    }
048
049    public void setPartitionBoundSpec(TPartitionBoundSpecSqlNode partitionBoundSpec) {
050        this.partitionBoundSpec = partitionBoundSpec;
051    }
052
053    public TPartitionBoundSpecSqlNode getPartitionBoundSpec() {
054        return partitionBoundSpec;
055    }
056
057    /**
058     * postgresql PartitionBoundSpec
059     * https://www.postgresql.org/docs/current/sql-createtable.html
060     */
061    private TPartitionBoundSpecSqlNode partitionBoundSpec;
062
063    public TTableProperties getTableProperties() {
064        return tableProperties;
065    }
066
067    public void setTableProperties(TTableProperties tableProperties) {
068        this.tableProperties = tableProperties;
069    }
070
071    private TTableProperties tableProperties;
072
073    public enum TableSourceType  {normal,subquery,like,clone,copy,partitionOf,forExchangeWith};
074    private TableSourceType tableSourceType = TableSourceType.normal;
075
076    private List<String> externalTableOptionList = null;
077
078    /**
079     * sql server create external table with options
080     * @return
081     */
082    public  List<String> getExternalTableOptionNames(){
083        return  TBaseType.getOptionNames(externalTableOptionList);
084    }
085
086    /**
087     * sql server create external table with option.
088     *
089     * @param optionName
090     * @return
091     */
092    public String getExternalTableOption(String optionName){
093        return TBaseType.getOption(externalTableOptionList,optionName);
094    }
095
096    public TableSourceType getTableSourceType() {
097        return tableSourceType;
098    }
099
100    private TObjectName cloneSourceTable = null;
101
102    public TObjectName getCloneSourceTable() {
103        return cloneSourceTable;
104    }
105
106    private TExternalTableOption externalTableOption = null;
107
108    public TExternalTableOption getExternalTableOption() {
109        return externalTableOption;
110    }
111
112    private TExpression partitionByExpr;
113
114    public void setPartitionByExpr(TExpression partitionByExpr) {
115        this.partitionByExpr = partitionByExpr;
116    }
117
118    /**
119     * Bigquery partition by expr
120     *
121     * @return expr
122     */
123    public TExpression getPartitionByExpr() {
124        return partitionByExpr;
125    }
126
127    private String awsSnsTopic = null;
128
129    public void setAwsSnsTopic(String awsSnsTopic) {
130        this.awsSnsTopic = awsSnsTopic;
131    }
132
133    public String getAwsSnsTopic() {
134        return awsSnsTopic;
135    }
136
137    private TObjectNameList partitionColumnList;
138
139    public void setPartitionColumnList(TObjectNameList partitionColumnList) {
140        this.partitionColumnList = partitionColumnList;
141    }
142
143    public TObjectNameList getPartitionColumnList() {
144        return partitionColumnList;
145    }
146
147    public void setFileFormatName(String fileFormatName) {
148        this.fileFormatName = fileFormatName;
149    }
150
151    public void setFileFormatType(String fileFormatType) {
152        this.fileFormatType = fileFormatType;
153    }
154
155    public String getFileFormatName() {
156        return fileFormatName;
157    }
158
159    public String getFileFormatType() {
160        return fileFormatType;
161    }
162
163    private String fileFormatName = null;
164    private String fileFormatType = null;
165
166    private String regex_pattern;
167
168    public void setRegex_pattern(String regex_pattern) {
169        this.regex_pattern = regex_pattern;
170    }
171
172    public String getRegex_pattern() {
173        return regex_pattern;
174    }
175
176    private TStageLocation stageLocation;
177
178    public void setStageLocation(TStageLocation stageLocation) {
179        this.stageLocation = stageLocation;
180    }
181
182    public TStageLocation getStageLocation() {
183        return stageLocation;
184    }
185
186    public void setTableOptions(ArrayList<TCreateTableOption> tableOptions) {
187        this.tableOptions = tableOptions;
188    }
189
190    public ArrayList<TCreateTableOption> getTableOptions() {
191        return tableOptions;
192    }
193
194    private ArrayList<TCreateTableOption> tableOptions;
195
196    public void setOnFilegroup(TDummy onFilegroup) {
197        this.onFilegroup = onFilegroup;
198    }
199
200    public TDummy getOnFilegroup() {
201        return onFilegroup;
202    }
203
204    private TDummy onFilegroup = null;
205
206    /**
207     * Oracle physical properties
208     * @return Oracle physical properties
209     */
210    public TPhysicalProperties getPhysicalProperties() {
211        return physicalProperties;
212    }
213
214    private TPhysicalProperties physicalProperties;
215
216    private EnumSet<ETableKind> tableKinds = EnumSet.noneOf(ETableKind.class);
217
218    public void setTableKinds(EnumSet<ETableKind> tableKinds) {
219        this.tableKinds = tableKinds;
220    }
221
222    /**
223     * Type of this table
224     * @return Type of this table
225     */
226    public EnumSet<ETableKind> getTableKinds() {
227        return tableKinds;
228    }
229
230    private TTable asTable;
231
232    /**
233     * Netezza, Teradata, as table name.
234     * @return as table name
235     */
236    public TTable getAsTable() {
237        return asTable;
238    }
239
240    private boolean externalTable;
241
242    /**
243     * @deprecated As of v1.9.7.2 , replace by {@link #isExternal}
244     * <br>
245     * Netezza, whether it is a external table
246     * @return true if it's netezza create external table
247     */
248    public boolean isExternalTable() {
249        return externalTable;
250    }
251
252    private ArrayList <TIndexDefinition> indexDefinitions;
253
254    /**
255     * Teradata index definition
256     *
257     * <pre>UNIQUE PRIMARY INDEX (storeid, productid, salesdate)</pre>
258     * @return  list of index definition
259     */
260    public ArrayList<TIndexDefinition> getIndexDefinitions() {
261        return indexDefinitions;
262    }
263
264    /**
265     * Teradata PERIOD FOR clauses that define derived temporal columns.
266     * A table can have multiple PERIOD FOR clauses (e.g., one for VALIDTIME and one for TRANSACTIONTIME).
267     */
268    private ArrayList<TTeradataPeriodForClause> periodForClauses;
269
270    /**
271     * Returns the list of Teradata PERIOD FOR clauses in this CREATE TABLE statement.
272     * PERIOD FOR clauses define derived columns that represent time periods from two date/timestamp columns.
273     *
274     * @return list of PERIOD FOR clauses, may be null or empty if none are defined
275     */
276    public ArrayList<TTeradataPeriodForClause> getPeriodForClauses() {
277        return periodForClauses;
278    }
279
280    /**
281     * Flink WATERMARK clauses that define event-time processing.
282     * A table typically has at most one WATERMARK clause.
283     */
284    private ArrayList<TFlinkWatermarkClause> watermarkClauses;
285
286    /**
287     * Returns the list of Flink WATERMARK clauses in this CREATE TABLE statement.
288     * WATERMARK clauses define how watermarks are generated for event-time processing.
289     *
290     * @return list of WATERMARK clauses, may be null or empty if none are defined
291     */
292    public ArrayList<TFlinkWatermarkClause> getWatermarkClauses() {
293        return watermarkClauses;
294    }
295
296    /**
297     * Returns the first Flink WATERMARK clause in this CREATE TABLE statement.
298     * Typically only one WATERMARK clause is defined per table.
299     *
300     * @return the first WATERMARK clause, or null if none are defined
301     */
302    public TFlinkWatermarkClause getWatermarkClause() {
303        return (watermarkClauses != null && !watermarkClauses.isEmpty())
304            ? watermarkClauses.get(0)
305            : null;
306    }
307
308    private boolean external;
309    private boolean ifNotExists;
310    private TObjectName tableLocation;
311    private TObjectName tableComment;
312    private THiveTableProperties hiveTableProperties;
313    private THiveTablePartition hiveTablePartition;
314    private THiveTableBuckets hiveTableBuckets;
315    private THiveTableSkewed hiveTableSkewed;
316    private THiveRowFormat hiveRowFormat;
317    private THiveTableFileFormat hiveTableFileFormat;
318    private TObjectName likeTableName;
319
320    /**
321     * Flink WITH clause containing connector properties.
322     */
323    private TFlinkWithClause flinkWithClause;
324
325    /**
326     * Get the Flink WITH clause containing connector properties.
327     *
328     * <p>Flink SQL uses the WITH clause to specify connector properties:</p>
329     * <pre>
330     * CREATE TABLE my_table (
331     *   id BIGINT,
332     *   name STRING
333     * ) WITH (
334     *   'connector' = 'kafka',
335     *   'topic' = 'my-topic',
336     *   'properties.bootstrap.servers' = 'localhost:9092',
337     *   'format' = 'json'
338     * );
339     * </pre>
340     *
341     * @return the Flink WITH clause, or null if not present
342     */
343    public TFlinkWithClause getFlinkWithClause() {
344        return flinkWithClause;
345    }
346
347    /**
348     * Set the Flink WITH clause.
349     * @param flinkWithClause the Flink WITH clause
350     */
351    public void setFlinkWithClause(TFlinkWithClause flinkWithClause) {
352        this.flinkWithClause = flinkWithClause;
353    }
354
355    /**
356     * Hive, Impala,Netezza,snowflake whether it is an external table
357     * @return whether it is an external table
358     */
359    public boolean isExternal() {
360        return external;
361    }
362
363    /**
364     * Hive, impala table row format
365     * @return table row format
366     */
367    public THiveRowFormat getHiveRowFormat() {
368        return hiveRowFormat;
369    }
370
371    /**
372     * Hive, Impala table buckets
373     * @return table buckets
374     */
375    public THiveTableBuckets getHiveTableBuckets() {
376        return hiveTableBuckets;
377    }
378
379    /**
380     * Hive, Impala table file format
381     * @return table file format
382     */
383    public THiveTableFileFormat getHiveTableFileFormat() {
384        return hiveTableFileFormat;
385    }
386
387    /**
388     * Hive, Impala table partition
389     * @return table partition
390     */
391    public THiveTablePartition getHiveTablePartition() {
392        return hiveTablePartition;
393    }
394
395    /**
396     * Hive, Impala table properties
397     * @return table properties
398     */
399    public THiveTableProperties getHiveTableProperties() {
400        return hiveTableProperties;
401    }
402
403    /**
404     * Hive, Impala skewed by clause.
405     * @return skewed by clause.
406     */
407    public THiveTableSkewed getHiveTableSkewed() {
408        return hiveTableSkewed;
409    }
410
411    /**
412     * Hive, Impala, if not exists clause
413     * @return true if not exists clause is used in create table statement
414     */
415    public boolean isIfNotExists() {
416        return ifNotExists;
417    }
418
419    /**
420     * Greenplum, table used in like clause.
421     * <br>Hive, Impala, table used in like clause.
422     * @return table used in like clause.
423     */
424    public TObjectName getLikeTableName() {
425        return likeTableName;
426    }
427
428    /**
429     * Hive, Impala, comment of the table.
430     * @return comment of the table.
431     */
432    public TObjectName getTableComment() {
433        return tableComment;
434    }
435
436    /**
437     * Hive, Impala, location of the table.
438     * @return location of the table.
439     */
440    public TObjectName getTableLocation() {
441        return tableLocation;
442    }
443
444    private TObjectName rowTypeName;
445    private TObjectName superTableName;
446
447    /**
448     * Informix, typename used in of type clause.
449     *
450     * @return row type name
451     */
452    public TObjectName getRowTypeName() {
453        return rowTypeName;
454    }
455
456    /**
457     * Informix, table name used in the under clause.
458     * @return table name used in the under clause.
459     */
460    public TObjectName getSuperTableName() {
461        return superTableName;
462    }
463
464    /**
465     * Data of the created table is derived from this select statement.
466     *
467     * @return {@link gudusoft.gsqlparser.stmt.TSelectSqlStatement} used in create table.
468     */
469    public TSelectSqlStatement getSubQuery() {
470        return subQuery;
471    }
472
473    private TSelectSqlStatement subQuery = null;
474    private TExecuteSqlStatement executePreparedStatement = null;//greenplum
475
476    public TExecuteSqlStatement getExecutePreparedStatement() {
477        return executePreparedStatement;
478    }
479
480
481
482    private TPTNodeList <TMySQLCreateTableOption> mySQLTableOptionList;
483
484    /**
485     * MySQL, option used in create table such as engine, auto_increment and etc.
486     * @return option used in create table such as engine, auto_increment and etc.
487     */
488    public TPTNodeList<TMySQLCreateTableOption> getMySQLTableOptionList() {
489        return mySQLTableOptionList;
490    }
491
492    public TCreateTableSqlStatement(EDbVendor dbvendor) {
493        super(dbvendor);
494        sqlstatementtype = ESqlStatementType.sstcreatetable;
495    }
496
497    void buildsql() {
498    }
499
500    void clear() {
501    }
502
503    String getasprettytext() {
504        return "";
505    }
506
507    void iterate(TVisitorAbs pvisitor) {
508    }
509
510    private TObjectName tableName = null;
511
512    /**
513     * The first table in {@link #tables}, this is the same table as {@link #getTargetTable}
514     *
515     * @return The first table in {@link #tables}
516     */
517    public TObjectName getTableName() {
518        return tables.getTable(0).getTableName();
519    }
520
521    private boolean webTable = false;
522
523    public boolean isWebTable() {
524        return webTable;
525    }
526
527
528    private boolean readable;
529
530    public boolean isReadable() {
531        return readable;
532    }
533
534    public boolean isWritable() {
535        return writable;
536    }
537
538    private boolean writable;
539
540    private TConstant executeCmd;
541
542
543    public TConstant getExecuteCmd() {
544        return executeCmd;
545    }
546
547
548    private ArrayList<TConstant> locationFiles;
549
550    public void setLocationFiles(ArrayList<TConstant> locationFiles) {
551        this.locationFiles = locationFiles;
552    }
553
554    public ArrayList<TConstant> getLocationFiles() {
555        return locationFiles;
556    }
557
558    public int doParseStatement(TCustomSqlStatement psql) {
559       if (rootNode == null) return -1;
560       TCreateTableSqlNode createTableNode = (TCreateTableSqlNode)rootNode;
561       super.doParseStatement(psql);
562       this.tableKinds = createTableNode.getTableKinds();
563       this.tableSourceType = createTableNode.getTableSourceType();
564       this.setTargetTable(createTableNode.getTable());
565
566
567       // move to TMetadataCollector
568
569//       TSQLTable sqlTable = null;
570//        if (getSqlEnv() != null)  {
571//            if (createTableNode.getTable() != null){
572//                sqlTable = getSqlEnv().addTable(createTableNode.getTable().getFullName(),true);
573//            }
574//
575//            if (getSqlEnv().getDefaultCatalogName() != null){
576//                if (createTableNode.getTable().getTableName().getDatabaseToken() == null){
577//                    createTableNode.getTable().getTableName().setDatabaseToken(new TSourceToken(getSqlEnv().getDefaultCatalogName()),true);
578//                }
579//            }
580//
581//            createTableNode.getTable().getTableName().setSqlEnv(getSqlEnv());
582//        }
583
584        // end of move to TMetadataCollector
585
586       externalTable = createTableNode.isExternalTable();
587       physicalProperties = createTableNode.getPhysicalProperties();
588       tableProperties = createTableNode.getTableProperties();
589       this.tablePartition = createTableNode.getTablePartition();
590
591       if (createTableNode.getNetezzaExternalTableOption() != null){
592           this.externalTableOption = new TExternalTableOption(createTableNode.getNetezzaExternalTableOption().toString());
593       }
594
595       mySQLTableOptionList = createTableNode.getMySQLTableOptionList();
596       tableOptions = createTableNode.getTableOptions();
597       if (tableOptions != null){
598           for(int i=0;i<tableOptions.size();i++){
599               tableOptions.get(i).doParse(this,ESqlClause.unknown);
600           }
601       }
602
603       superTableName = createTableNode.getSuperTableName();
604       rowTypeName = createTableNode.getRowTypeName();
605       onFilegroup = createTableNode.getOnFilegroup();
606       cloneSourceTable = createTableNode.getCloneSourceTable();
607       if (cloneSourceTable != null){
608           cloneSourceTable.setDbObjectType(this.dbvendor,EDbObjectType.table);
609       }
610       likeTableName = createTableNode.getLikeTableName();
611       if (likeTableName != null){
612           likeTableName.setDbObjectType(this.dbvendor,EDbObjectType.table);
613       }
614       partitionBoundSpec = createTableNode.getPartitionBoundSpec();
615
616       tables.addTable(createTableNode.getTable());
617       asTable = createTableNode.getAsTable();
618        if (asTable != null) tables.addTable(asTable);
619       if ((createTableNode.getTableElementList() != null)&&(createTableNode.getTableElementList().size() >0 )){
620           TTableElement te;
621           TColumnDefinition cd;
622           Set<String> columnNames = new LinkedHashSet<String>();
623           for(int i=0; i<createTableNode.getTableElementList().size();i++){
624              te = createTableNode.getTableElementList().getTableElement(i);
625               if (te.getType() == TTableElement.type_column_def) {
626                   cd = te.getColumnDefinition();
627                   this.getColumnList().addColumn(cd);
628
629                   cd.doParse(this, ESqlClause.createTable);
630                   this.getTargetTable().getObjectNameReferences().addObjectName(cd.getColumnName());
631                   cd.getColumnName().setLocation(ESqlClause.createTable);
632                   cd.getColumnName().setLinkedColumnDef(cd);
633                   getTargetTable().getLinkedColumns().addObjectName(cd.getColumnName());
634                   cd.getColumnName().setSourceTable(getTargetTable());
635                   // move to TMetadataCollector
636//                   if (sqlTable != null){
637//                       sqlTable.addColumn(cd.getColumnName().toString(),cd.getDatatype());
638//                       //getTargetTable().setSqlTable(sqlTable);
639//                   }
640                   if (!columnNames.add(cd.getColumnName().toString())){
641                       // find duplicate colum name
642                       TSourceToken st = cd.getColumnName().getStartToken();
643                       this.parseerrormessagehandle(new TSyntaxError(st.getAstext(), st.lineNo, st.columnNo
644                               ,"duplicate column name", EErrorType.spfatalerror
645                               , TBaseType.MSG_ERROR_DUPLICATED_COLUMN_NAME,this,st.posinlist));
646
647                   }
648               }
649               else  if (te.getType() == TTableElement.type_table_like){
650                   likeTableName = te.getParentTable();
651               }else  if (te.getType() == TTableElement.type_supplemental_logging){
652
653               }else  if (te.getType() == TTableElement.type_period_for_clause){
654                   // Teradata PERIOD FOR clause - derived temporal column
655                   if (periodForClauses == null) {
656                       periodForClauses = new ArrayList<TTeradataPeriodForClause>();
657                   }
658                   periodForClauses.add(te.getPeriodForClause());
659               }else  if (te.getType() == TTableElement.type_flink_watermark){
660                   // Flink WATERMARK clause for event-time processing
661                   if (watermarkClauses == null) {
662                       watermarkClauses = new ArrayList<TFlinkWatermarkClause>();
663                   }
664                   watermarkClauses.add(te.getFlinkWatermarkClause());
665               }else{
666                   te.getConstraint().doParse(this,ESqlClause.unknown);
667                   this.getTableConstraints().addConstraint(te.getConstraint());
668               }
669           }
670       }
671
672       // column name list used when create table using a subquery.
673       TColumnDefinition t = null;
674       if ((createTableNode.getColumnList() != null)&&(createTableNode.getColumnList().size() > 0)){
675           for(int i=0;i<createTableNode.getColumnList().size();i++){
676               t = new TColumnDefinition(createTableNode.getColumnList().getObjectName(i));
677               t.setStartToken(t.getColumnName().getStartToken());
678               t.setEndToken(t.getColumnName().getEndToken());
679               this.getColumnList().addColumn(t);
680               this.getTargetTable().getObjectNameReferences().addObjectName(createTableNode.getColumnList().getObjectName(i));
681               getTargetTable().getLinkedColumns().addObjectName(createTableNode.getColumnList().getObjectName(i));
682               createTableNode.getColumnList().getObjectName(i).setSourceTable(getTargetTable());
683               // move to TMetadataCollector
684//               if (sqlTable != null){
685//                   sqlTable.addColumn(createTableNode.getColumnList().getObjectName(i).toString());
686//               }
687           }
688       }
689
690       if((subQuery == null) && (createTableNode.getSubQueryNode() != null) && (createTableNode.getSubQueryNode().getRelationExpr() == null)){
691           subQuery = new TSelectSqlStatement(this.dbvendor);
692           subQuery.rootNode = createTableNode.getSubQueryNode();
693           subQuery.doParseStatement(this);
694
695
696           // link column alias in subquery to table in create statement if columnList is not specified
697           if (createTableNode.getColumnList() == null){
698               TSelectSqlStatement viewQuery = subQuery;
699               if (subQuery.isCombinedQuery()){
700                   viewQuery = subQuery.getRightStmt();
701               }
702
703                        if (viewQuery.getResultColumnList() != null) {
704               for(int i=0;i<viewQuery.getResultColumnList().size();i++){
705                   TResultColumn resultColumn = viewQuery.getResultColumnList().getResultColumn(i);
706                   TObjectName aliasName = null;
707                   if (resultColumn.getAliasClause() != null){
708                       if (resultColumn.getAliasClause().getColumns() != null){
709                           // select explode(str_to_map(regexp_replace(regexp_replace(regexp_replace(lower(t.input), '', ''), '',''), '', ''), '', '')) as (`key`, `value`) from B t1
710                            for (int j=0;j<resultColumn.getAliasClause().getColumns().size();j++){
711                                TObjectName aliasName1 =  resultColumn.getAliasClause().getColumns().getObjectName(j);
712                                aliasName1.setLocation(ESqlClause.columnAlias);
713                                resultColumn.getAliasNameList().add(aliasName1);
714                            }
715                       }else {
716                           aliasName = TObjectName.createObjectName (this.dbvendor, EDbObjectType.column,resultColumn.getAliasClause().getAliasName().getStartToken());
717                           aliasName.getReferencedObjects().addObjectName(resultColumn.getAliasClause().getAliasName());
718                           aliasName.setLocation(resultColumn.getAliasClause().getAliasName().getLocation());
719                       }
720                   }else{
721                       if (resultColumn.getExpr().getObjectOperand() != null){
722                           aliasName =  TObjectName.createObjectName (this.dbvendor, EDbObjectType.column,resultColumn.getExpr().getObjectOperand().getPartToken());
723                           aliasName.getReferencedObjects().addObjectName(resultColumn.getExpr().getObjectOperand());
724                           aliasName.setLocation(resultColumn.getExpr().getObjectOperand().getLocation());
725                       }
726                   }
727
728                   if (aliasName != null){
729                       if (aliasName.getLocation() == ESqlClause.unknown){
730                           aliasName.setLocation(ESqlClause.columnAlias);
731                       }
732                       getTargetTable().getLinkedColumns().addObjectName(aliasName);
733                       aliasName.setSourceTable(getTargetTable());
734                       resultColumn.setAliasName(aliasName.toString());
735
736                       // move to TMetadataCollector
737//                       if (sqlTable != null){
738//                           sqlTable.addColumn(TSQLEnv.getObjectName(aliasName.toString()));
739//                       }
740                   }
741               }
742                        }
743           }
744       }else if ((createTableNode.getSubQueryNode() != null) && (createTableNode.getSubQueryNode().getRelationExpr() != null)){
745            // CREATE TABLE a AS TABLE b
746           asTable = new TTable(createTableNode.getSubQueryNode().getRelationExpr().getRelationName());
747           tables.addTable(asTable);
748       }
749
750       if (createTableNode.getExecuteSqlNode() != null){
751           executePreparedStatement = new TExecuteSqlStatement(this.dbvendor);
752           executePreparedStatement.rootNode = createTableNode.getExecuteSqlNode();
753           executePreparedStatement.doParseStatement(this);
754       }
755
756        external = createTableNode.isExternal();
757       if (!external && tableKinds.contains(ETableKind.etkExternal)){
758           external = true;
759       }
760       if (external){
761           if (createTableNode.getOptionStartParenthesis() != null){
762               externalTableOptionList = TBaseType.getArrayListBetweenTokens(createTableNode.getOptionStartParenthesis(),createTableNode.getOptionEndParenthesis(),false);
763           }
764       }
765        ifNotExists = createTableNode.isIfNotExists();
766        if (createTableNode.getTableComment() != null){
767            tableComment = createTableNode.getTableComment();
768        }
769
770        locationFiles = createTableNode.getLocationFiles();
771        readable = createTableNode.isReadable();
772        writable = createTableNode.isWritable();
773        executeCmd = createTableNode.getExecuteCmd();
774        webTable = createTableNode.isWebTable();
775
776        tableLocation = createTableNode.getTableLocation();
777        hiveTableProperties = createTableNode.getHiveTableProperties();
778        hiveTablePartition = createTableNode.getHiveTablePartition();
779        hiveTableBuckets = createTableNode.getHiveTableBuckets();
780        hiveRowFormat = createTableNode.getHiveRowFormat();
781        hiveTableSkewed = createTableNode.getHiveTableSkewed();
782        hiveTableFileFormat = createTableNode.getHiveTableFileFormat();
783        flinkWithClause = createTableNode.getFlinkWithClause();
784        if (likeTableName == null){
785            likeTableName = createTableNode.getLikeTableName();
786        }
787        indexDefinitions = createTableNode.getIndexDefinitions();
788        if (indexDefinitions != null){
789            for(int i=0;i<indexDefinitions.size();i++){
790                indexDefinitions.get(i).doParse(this,ESqlClause.tableIndexOption);
791            }
792        }
793
794        inheritsClause = createTableNode.getInheritsClause();
795        if (inheritsClause != null) {
796            for(int i=0;i<inheritsClause.getParentTables().size();i++){
797                inheritsClause.getParentTables().getObjectName(i).setDbObjectType(this.dbvendor, EDbObjectType.table);
798            }
799        }
800        return 0;
801    }
802
803    /**
804     * table level constraints.
805     * @return List of table constraints of this table.
806     */
807    public TConstraintList getTableConstraints() {
808        if (this.tableConstraints == null){
809            this.tableConstraints = new TConstraintList();
810
811        }
812        
813        return tableConstraints;
814    }
815
816    /**
817     *  columns created in create table statement.
818     *
819     * @return List of column definitions of this table.
820     */
821    public TColumnDefinitionList getColumnList() {
822        if (this.columnList == null){
823            this.columnList = new TColumnDefinitionList();
824
825        }
826        return columnList;
827    }
828
829    private TColumnDefinitionList columnList = null;
830    private TConstraintList tableConstraints = null;
831
832    public void accept(TParseTreeVisitor v){
833        v.preVisit(this);
834        v.postVisit(this);
835    }
836
837    public void acceptChildren(TParseTreeVisitor v){
838        v.preVisit(this);
839        //tables.acceptChildren(v);
840        if (getTargetTable() != null) {
841            getTargetTable().acceptChildren(v);
842        }
843        this.getColumnList().acceptChildren(v);
844        if ((this.getTableConstraints() != null)&&(this.getTableConstraints().size()>0)){
845            this.getTableConstraints().acceptChildren(v);
846        }
847        if (this.getSubQuery() != null){
848            this.getSubQuery().acceptChildren(v);
849        }
850        if (getHiveTablePartition() != null){
851            getHiveTablePartition().acceptChildren(v);
852        }
853        v.postVisit(this);
854    }
855
856    public void setColumnList(TColumnDefinitionList columnList) {
857        this.columnList = columnList;
858    }
859
860    public void setAsTable(TTable asTable) {
861        this.asTable = asTable;
862    }
863
864    public void setExternalTable(boolean externalTable) {
865        this.externalTable = externalTable;
866    }
867
868    public void setIndexDefinitions(ArrayList<TIndexDefinition> indexDefinitions) {
869        this.indexDefinitions = indexDefinitions;
870    }
871
872    public void setExternal(boolean external) {
873        this.external = external;
874    }
875
876    public void setIfNotExists(boolean ifNotExists) {
877        this.ifNotExists = ifNotExists;
878    }
879
880    public void setTableLocation(TObjectName tableLocation) {
881        this.tableLocation = tableLocation;
882    }
883
884    public void setTableComment(TObjectName tableComment) {
885        this.tableComment = tableComment;
886    }
887
888    public void setHiveTableProperties(THiveTableProperties hiveTableProperties) {
889        this.hiveTableProperties = hiveTableProperties;
890    }
891
892    public void setHiveTablePartition(THiveTablePartition hiveTablePartition) {
893        this.hiveTablePartition = hiveTablePartition;
894    }
895
896    public void setHiveTableBuckets(THiveTableBuckets hiveTableBuckets) {
897        this.hiveTableBuckets = hiveTableBuckets;
898    }
899
900    public void setHiveTableSkewed(THiveTableSkewed hiveTableSkewed) {
901        this.hiveTableSkewed = hiveTableSkewed;
902    }
903
904    public void setHiveRowFormat(THiveRowFormat hiveRowFormat) {
905        this.hiveRowFormat = hiveRowFormat;
906    }
907
908    public void setHiveTableFileFormat(THiveTableFileFormat hiveTableFileFormat) {
909        this.hiveTableFileFormat = hiveTableFileFormat;
910    }
911
912    public void setLikeTableName(TObjectName likeTableName) {
913        this.likeTableName = likeTableName;
914    }
915
916    public void setRowTypeName(TObjectName rowTypeName) {
917        this.rowTypeName = rowTypeName;
918    }
919
920    public void setSuperTableName(TObjectName superTableName) {
921        this.superTableName = superTableName;
922    }
923
924    public void setSubQuery(TSelectSqlStatement subQuery) {
925        this.subQuery = subQuery;
926    }
927
928    public void setExecutePreparedStatement(TExecuteSqlStatement executePreparedStatement) {
929        this.executePreparedStatement = executePreparedStatement;
930    }
931
932    public void setMySQLTableOptionList(TPTNodeList<TMySQLCreateTableOption> mySQLTableOptionList) {
933        this.mySQLTableOptionList = mySQLTableOptionList;
934    }
935
936    public void setTableName(TObjectName tableName) {
937        this.tableName = tableName;
938    }
939
940    public void setTableConstraints(TConstraintList tableConstraints) {
941        this.tableConstraints = tableConstraints;
942    }
943}