001package gudusoft.gsqlparser.nodes;
002
003
004import gudusoft.gsqlparser.*;
005import gudusoft.gsqlparser.nodes.flink.TFlinkWithClause;
006import gudusoft.gsqlparser.nodes.hive.*;
007import gudusoft.gsqlparser.nodes.oracle.TPhysicalProperties;
008import gudusoft.gsqlparser.nodes.postgresql.TPartitionBoundSpecSqlNode;
009import gudusoft.gsqlparser.nodes.teradata.TIndexDefinition;
010import gudusoft.gsqlparser.stmt.TCreateTableSqlStatement;
011import gudusoft.gsqlparser.nodes.postgresql.TInheritsClause;
012
013import java.util.ArrayList;
014import java.util.EnumSet;
015
016public class TCreateTableSqlNode extends TParseTreeNode {
017
018    private TDistributeBy distributeBy;
019
020    public void setDistributeBy(TDistributeBy distributeBy) {
021        this.distributeBy = distributeBy;
022    }
023
024    public TDistributeBy getDistributeBy() {
025        return distributeBy;
026    }
027
028    private TBaseTablePartition tablePartition;
029
030    public void setTablePartition(TBaseTablePartition tablePartition) {
031        this.tablePartition = tablePartition;
032    }
033
034    public TBaseTablePartition getTablePartition() {
035        return tablePartition;
036    }
037
038    public void setPartitionBoundSpec(TPartitionBoundSpecSqlNode partitionBoundSpec) {
039        this.partitionBoundSpec = partitionBoundSpec;
040    }
041
042    public TPartitionBoundSpecSqlNode getPartitionBoundSpec() {
043        return partitionBoundSpec;
044    }
045
046    /**
047     * postgresql PartitionBoundSpec
048     * https://www.postgresql.org/docs/current/sql-createtable.html
049     */
050    private TPartitionBoundSpecSqlNode partitionBoundSpec;
051
052    public TTableProperties getTableProperties() {
053        return tableProperties;
054    }
055
056    public void setTableProperties(TTableProperties tableProperties) {
057        this.tableProperties = tableProperties;
058    }
059
060    private TTableProperties tableProperties;
061
062    /** Oracle SHARING clause, including its specified attribute and original token range. */
063    private TParseTreeNode sharingClause;
064
065    /** @return the SHARING clause, or null when no sharing attribute was specified */
066    public TParseTreeNode getSharingClause() {
067        return sharingClause;
068    }
069
070    public void setSharingClause(TParseTreeNode sharingClause) {
071        this.sharingClause = sharingClause;
072    }
073
074
075    private ArrayList<TConstant> locationFiles;
076
077    public void setLocationFiles(ArrayList<TConstant> locationFiles) {
078        this.locationFiles = locationFiles;
079    }
080
081    public ArrayList<TConstant> getLocationFiles() {
082        return locationFiles;
083    }
084
085    public void setOptionStartParenthesis(TSourceToken optionStartParenthesis) {
086        this.optionStartParenthesis = optionStartParenthesis;
087    }
088
089    public void setOptionEndParenthesis(TSourceToken optionEndParenthesis) {
090        this.optionEndParenthesis = optionEndParenthesis;
091    }
092
093    public TSourceToken getOptionStartParenthesis() {
094        return optionStartParenthesis;
095    }
096
097    public TSourceToken getOptionEndParenthesis() {
098        return optionEndParenthesis;
099    }
100
101    private TSourceToken optionStartParenthesis;
102    private TSourceToken optionEndParenthesis;
103
104    private TCreateTableSqlStatement.TableSourceType tableSourceType = TCreateTableSqlStatement.TableSourceType.normal;
105
106    private TObjectName cloneSourceTable = null;
107
108    private TDummy netezzaExternalTableOption = null;
109
110    public void setCloneSourceTable(TObjectName cloneSourceTable) {
111        this.cloneSourceTable = cloneSourceTable;
112        this.tableSourceType = TCreateTableSqlStatement.TableSourceType.clone;
113    }
114
115    public TObjectName getCloneSourceTable() {
116        return cloneSourceTable;
117    }
118
119    public void setNetezzaExternalTableOption(TDummy netezzaExternalTableOption) {
120        this.netezzaExternalTableOption = netezzaExternalTableOption;
121    }
122
123    public TDummy getNetezzaExternalTableOption() {
124        return netezzaExternalTableOption;
125    }
126
127    public TObjectName getComment(){
128        return this.tableComment;
129    }
130    /**
131     * Takes the location of the table from a {@link ECreateTableOption#etoLocation} option,
132     * {@code LOCATION 's3://bucket/path/'}, when it is not set yet. {@link #setTableOptions} calls it; a grammar
133     * that does not keep its options on the node (Spark SQL) calls it directly.
134     */
135    public void setTableLocationFromOptions(ArrayList<TCreateTableOption> tableOptions) {
136        if (tableOptions == null) return;
137        for (TCreateTableOption option : tableOptions) {
138            if (this.tableLocation == null && option != null
139                    && option.getCreateTableOptionType() == ECreateTableOption.etoLocation) {
140                this.tableLocation = option.getTableLocation();
141            }
142        }
143    }
144
145    public void setTableOptions(ArrayList<TCreateTableOption> tableOptions, boolean enableAppend) {
146        if (tableOptions == null) return;
147        for(TCreateTableOption option:tableOptions){
148            if (option.getCreateTableOptionType() == ECreateTableOption.etoComment){
149                this.tableComment = option.getComment();
150            }
151        }
152        setTableLocationFromOptions(tableOptions);
153
154        if ((enableAppend) && (this.tableOptions != null)){
155            this.tableOptions.addAll(tableOptions);
156        }else{
157            this.tableOptions = tableOptions;
158        }
159    }
160
161    public void setTableOptions(ArrayList<TCreateTableOption> tableOptions) {
162
163        this.setTableOptions(tableOptions,false);
164//        if (tableOptions == null) return;
165//        for(TCreateTableOption option:tableOptions){
166//            if (option.getCreateTableOptionType() == ECreateTableOption.etoComment){
167//                this.tableComment = option.getComment();
168//            }
169//        }
170//
171//        this.tableOptions = tableOptions;
172
173    }
174
175    public void appendTableOption(TCreateTableOption tableOption) {
176        if (tableOption == null) return;
177        if (tableOptions == null){
178            tableOptions = new ArrayList<>();
179        }
180        tableOptions.add(tableOption);
181    }
182
183    public void addTableOptionByToken(TSourceToken st, ECreateTableOption createTableOption){
184        if (st == null) return;
185
186        TCreateTableOption newOption = new TCreateTableOption();
187        newOption.init(createTableOption,st);
188        switch (createTableOption){
189            case etoFileFormat:
190                newOption.setFileFormatType(st);
191                break;
192            case etoEncoding:
193                newOption.setEncoding(st);
194                break;
195        }
196
197        if (tableOptions == null){
198            tableOptions = new ArrayList<>();
199        }
200
201        tableOptions.add(newOption);
202    }
203    public ArrayList<TCreateTableOption> getTableOptions() {
204        return tableOptions;
205    }
206
207    private ArrayList<TCreateTableOption> tableOptions;
208
209    public void setOnFilegroup(TDummy onFilegroup) {
210        this.onFilegroup = onFilegroup;
211    }
212
213    public TDummy getOnFilegroup() {
214        return onFilegroup;
215    }
216
217    private TDummy onFilegroup = null;
218
219    private TPhysicalProperties physicalProperties;
220
221    public void setPhysicalProperties(TPhysicalProperties physicalProperties) {
222        this.physicalProperties = physicalProperties;
223    }
224
225    public void setTableOption(TCreateTableOption option){
226        if (option == null) return;
227        if (this.tableOptions == null){
228            this.tableOptions = new ArrayList<>();
229        }
230        this.tableOptions.add(option);
231    }
232
233    /**
234     * Oracle physical properties
235     *
236     * @return Oracle physical properties
237     */
238    public TPhysicalProperties getPhysicalProperties() {
239        return physicalProperties;
240    }
241
242    public void setTableKindByToken(TSourceToken st){
243        if (st == null) return;
244        if (st.toString().equalsIgnoreCase("set")){
245            tableKinds.add(ETableKind.etkSet);
246        }else if (st.toString().equalsIgnoreCase("multiset")){
247            tableKinds.add(ETableKind.etkMultiset);
248        }else if (st.toString().equalsIgnoreCase("volatile")){
249            tableKinds.add(ETableKind.etkVolatile);
250        }else if (st.toString().equalsIgnoreCase("global")){
251            tableKinds.add(ETableKind.etkGlobalTemporary);
252        }else if (st.toString().equalsIgnoreCase("temporary")){
253            tableKinds.add(ETableKind.etkTemporary);
254        }else if (st.toString().equalsIgnoreCase("temp")){
255            tableKinds.add(ETableKind.etkTemp);
256        }else if (st.toString().equalsIgnoreCase("external")){
257            tableKinds.add(ETableKind.etkExternal);
258            this.external = true;
259        }
260    }
261    public void setTableKindByNode(TDummy node) {
262        if ((TDummy) node.node1 == null) return;
263        TDummy node2 = (TDummy) node.node1;
264        setTableKindByToken(node2.st1);
265        setTableKindByToken(node2.st2);
266    }
267
268    private EnumSet<ETableKind> tableKinds = EnumSet.noneOf(ETableKind.class);
269
270    public void setTableKinds(EnumSet<ETableKind> tableKinds) {
271        this.tableKinds = tableKinds;
272    }
273
274    public EnumSet<ETableKind> getTableKinds() {
275
276        return tableKinds;
277    }
278
279    //private TObjectName asTableName;
280    private TTable asTable = null;
281
282    public TTable getAsTable() {
283        return asTable;
284    }
285
286    public void setAsTableName(TObjectName asTableName) {
287        asTable = new TTable();
288        asTableName.setObjectType(TObjectName.ttobjTable);
289        asTable.setTableName(asTableName);
290        asTable.setTableType(ETableSource.objectname);
291        asTable.setEffectType(ETableEffectType.tetCreateAs);
292    }
293
294
295
296    public void setExternalTable(boolean externalTable) {
297        this.externalTable = externalTable;
298    }
299
300    /**
301     * @deprecated As of v1.9.7.2 , replace by {@link #isExternal}
302     *
303     * @return true if it's an external table
304     */
305    public boolean isExternalTable() {
306
307        return externalTable;
308    }
309
310    private boolean externalTable;
311
312    private ArrayList <TIndexDefinition> indexDefinitions;
313
314    public void setIndexDefinitions(ArrayList<TIndexDefinition> indexDefinitions) {
315        this.indexDefinitions = indexDefinitions;
316    }
317
318    public ArrayList<TIndexDefinition> getIndexDefinitions() {
319
320        return indexDefinitions;
321    }
322
323    private boolean readable = false;
324    private boolean writable = false;
325    private boolean webTable = false;
326
327    public void setWebTable(boolean webTable) {
328        this.webTable = webTable;
329    }
330
331    public boolean isWebTable() {
332        return webTable;
333    }
334
335    private TConstant executeCmd;
336
337    public void setExecuteCmd(TConstant executeCmd) {
338        this.executeCmd = executeCmd;
339    }
340
341    public TConstant getExecuteCmd() {
342        return executeCmd;
343    }
344
345    public void setReadWritableByToken(TSourceToken st){
346        if (st == null) return;
347        if (st.toString().equalsIgnoreCase("readable")){
348            readable = true;
349        }else if (st.toString().equalsIgnoreCase("writable")){
350            writable = true;
351        }
352    }
353
354    public boolean isReadable() {
355        return readable;
356    }
357
358    public boolean isWritable() {
359        return writable;
360    }
361
362    private boolean external;
363    private boolean ifNotExists;
364    private TObjectName tableLocation;
365    private TObjectName tableComment;
366    private THiveTableProperties hiveTableProperties;
367    private THiveTablePartition hiveTablePartition;
368    private THiveTableBuckets hiveTableBuckets;
369    private THiveTableSkewed hiveTableSkewed;
370    private THiveRowFormat hiveRowFormat;
371    private THiveTableFileFormat hiveTableFileFormat;
372    private TObjectName likeTableName;
373
374    public void setDummyOfTemp(TDummy dummy){
375        if (dummy == null) return;
376        if (dummy.st1.toString().equalsIgnoreCase("external")){
377            this.external = true;
378        }
379    }
380
381    public void setExternal(boolean external) {
382        this.external = external;
383    }
384
385    public void setHiveRowFormat(THiveRowFormat hiveRowFormat) {
386        this.hiveRowFormat = hiveRowFormat;
387    }
388
389    public void setHiveTableBuckets(THiveTableBuckets hiveTableBuckets) {
390        this.hiveTableBuckets = hiveTableBuckets;
391    }
392
393    public void setHiveTableFileFormat(THiveTableFileFormat hiveTableFileFormat) {
394        this.hiveTableFileFormat = hiveTableFileFormat;
395    }
396
397    public void setHiveTablePartition(THiveTablePartition hiveTablePartition) {
398        this.hiveTablePartition = hiveTablePartition;
399    }
400
401    public void setHiveTableProperties(THiveTableProperties hiveTableProperties) {
402        this.hiveTableProperties = hiveTableProperties;
403    }
404
405    public void setHiveTableSkewed(THiveTableSkewed hiveTableSkewed) {
406        this.hiveTableSkewed = hiveTableSkewed;
407    }
408
409    public void setIfNotExists(boolean ifNotExists) {
410        this.ifNotExists = ifNotExists;
411    }
412
413    public void setLikeTableName(TObjectName likeTableName) {
414        if ((likeTableName != null)&&(tableSourceType == TCreateTableSqlStatement.TableSourceType.normal)){
415           tableSourceType = TCreateTableSqlStatement.TableSourceType.like;
416        }
417        this.likeTableName = likeTableName;
418    }
419
420    public void setTableComment(TObjectName tableComment) {
421        this.tableComment = tableComment;
422    }
423
424    public void setTableLocation(TObjectName tableLocation) {
425        this.tableLocation = tableLocation;
426    }
427
428    public boolean isExternal() {
429
430        return external;
431    }
432
433    public THiveRowFormat getHiveRowFormat() {
434        return hiveRowFormat;
435    }
436
437    public THiveTableBuckets getHiveTableBuckets() {
438        return hiveTableBuckets;
439    }
440
441    public THiveTableFileFormat getHiveTableFileFormat() {
442        return hiveTableFileFormat;
443    }
444
445    public THiveTablePartition getHiveTablePartition() {
446        return hiveTablePartition;
447    }
448
449    public THiveTableProperties getHiveTableProperties() {
450        return hiveTableProperties;
451    }
452
453    public THiveTableSkewed getHiveTableSkewed() {
454        return hiveTableSkewed;
455    }
456
457    public boolean isIfNotExists() {
458        return ifNotExists;
459    }
460
461    public TObjectName getLikeTableName() {
462        return likeTableName;
463    }
464
465    public TObjectName getTableComment() {
466        return tableComment;
467    }
468
469    public TObjectName getTableLocation() {
470        return tableLocation;
471    }
472
473    private TObjectName rowTypeName;
474    private TObjectName superTableName;
475
476    public void setRowTypeName(TObjectName rowTypeName) {
477        this.rowTypeName = rowTypeName;
478    }
479
480    public void setSuperTableName(TObjectName superTableName) {
481        this.superTableName = superTableName;
482    }
483
484    public TObjectName getRowTypeName() {
485
486        return rowTypeName;
487    }
488
489    public TObjectName getSuperTableName() {
490        return superTableName;
491    }
492
493
494    private TPTNodeList <TMySQLCreateTableOption> mySQLTableOptionList;
495
496    public void setMySQLTableOptionList(TPTNodeList<TMySQLCreateTableOption> mySQLTableOptionList) {
497        this.mySQLTableOptionList = mySQLTableOptionList;
498    }
499
500    public TPTNodeList<TMySQLCreateTableOption> getMySQLTableOptionList() {
501
502        return mySQLTableOptionList;
503    }
504
505    private TTable table = null;
506
507    public void setTableElementList(TTableElementList tableElementList) {
508        this.tableElementList = tableElementList;
509    }
510
511    private TTableElementList tableElementList = null;
512
513    public TObjectNameList getColumnList() {
514        return columnList;
515    }
516
517    public TTableElementList getTableElementList() {
518        return tableElementList;
519    }
520
521    public TTable getTable() {
522
523        return table;
524    }
525
526    public void setColumnList(TObjectNameList columnList) {
527        this.columnList = columnList;
528    }
529
530    public TSelectSqlNode getSubQueryNode() {
531        return subQueryNode;
532    }
533
534    private TObjectNameList columnList = null;
535    private TSelectSqlNode subQueryNode = null;
536    private TStatementSqlNode refreshUsingStatementNode = null;
537    private boolean usingTemplate = false;
538
539    /**
540     * Snowflake custom incremental dynamic-table refresh DML.
541     *
542     * @return the MERGE or INSERT statement from REFRESH USING, or null
543     */
544    public TStatementSqlNode getRefreshUsingStatementNode() {
545        return refreshUsingStatementNode;
546    }
547
548    public void setRefreshUsingStatementNode(
549            TStatementSqlNode refreshUsingStatementNode) {
550        this.refreshUsingStatementNode = refreshUsingStatementNode;
551        if (refreshUsingStatementNode != null) {
552            tableSourceType = TCreateTableSqlStatement.TableSourceType.refreshUsing;
553        }
554    }
555
556    /**
557     * Snowflake: CREATE TABLE ... USING TEMPLATE &lt;query&gt;. When true, the
558     * subquery (see {@link #getSubQueryNode()}) is a schema-inference template
559     * whose result derives the table's column definitions, rather than a
560     * CREATE TABLE AS SELECT that also populates the table with data.
561     *
562     * @return true if the table is created via USING TEMPLATE
563     */
564    public boolean isUsingTemplate() {
565        return usingTemplate;
566    }
567
568    public void setUsingTemplate(boolean usingTemplate) {
569        this.usingTemplate = usingTemplate;
570        if (usingTemplate) {
571            // Distinguish a schema-inference template from a CTAS subquery so that
572            // downstream consumers (lineage, metadata) do not treat the template
573            // query as data that populates the target table. The query object is
574            // still exposed via getSubQueryNode() for resolving its own sources.
575            this.tableSourceType = TCreateTableSqlStatement.TableSourceType.usingTemplate;
576        }
577    }
578
579    public void setExecuteSqlNode(TExecuteSqlNode executeSqlNode) {
580        this.executeSqlNode = executeSqlNode;
581    }
582
583    public TExecuteSqlNode getExecuteSqlNode() {
584
585        return executeSqlNode;
586    }
587
588    private TExecuteSqlNode executeSqlNode = null;//greenplum
589
590    public void setSubQueryNode(TSelectSqlNode subQueryNode) {
591        if (subQueryNode != null){
592            this.subQueryNode = subQueryNode;
593            tableSourceType = TCreateTableSqlStatement.TableSourceType.subquery;
594        }
595
596    }
597
598    public void init(Object arg1)
599    {
600        table = new TTable();
601        ((TObjectName)arg1).setObjectType(TObjectName.ttobjTable);
602        table.setTableName((TObjectName)arg1);
603        table.setTableType(ETableSource.objectname);
604        table.setEffectType(ETableEffectType.tetCreate);
605        table.setGsqlparser(((TObjectName)arg1).getGsqlparser());
606        if (table.getTableName().toString().startsWith("##")){
607            // SQL Server global temporary table (##tablename)
608            this.getTableKinds().add(ETableKind.etkGlobalTemporary);
609            table.getTableName().setTableKind(ETableKind.etkGlobalTemporary);
610        } else if (table.getTableName().toString().startsWith("#")){
611            // SQL Server local temporary table (#tablename)
612            this.getTableKinds().add(ETableKind.etkTemporary);
613            table.getTableName().setTableKind(ETableKind.etkTemporary);
614        }
615    }
616
617    public TCreateTableSqlStatement.TableSourceType getTableSourceType() {
618        return tableSourceType;
619    }
620
621    public void init(Object arg1, Object arg2){
622        tableSourceType = (TCreateTableSqlStatement.TableSourceType)arg1;
623        init(arg2);
624    }
625
626    public void setInheritsClause(TInheritsClause inheritsClause) {
627        this.inheritsClause = inheritsClause;
628    }
629
630    public TInheritsClause getInheritsClause() {
631        return inheritsClause;
632    }
633
634    private TInheritsClause inheritsClause;
635
636    /**
637     * Flink WITH clause for connector properties
638     */
639    private TFlinkWithClause flinkWithClause;
640
641    /**
642     * Get the Flink WITH clause containing connector properties.
643     * @return the Flink WITH clause, or null if not present
644     */
645    public TFlinkWithClause getFlinkWithClause() {
646        return flinkWithClause;
647    }
648
649    /**
650     * Set the Flink WITH clause.
651     * @param flinkWithClause the Flink WITH clause
652     */
653    public void setFlinkWithClause(TFlinkWithClause flinkWithClause) {
654        this.flinkWithClause = flinkWithClause;
655    }
656}