OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqlCreateTable.java
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to you under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package com.mapd.parser.extension.ddl;
18 
20 
21 import org.apache.calcite.runtime.CalciteException;
22 import org.apache.calcite.sql.SqlCall;
23 import org.apache.calcite.sql.SqlCreate;
24 import org.apache.calcite.sql.SqlIdentifier;
25 import org.apache.calcite.sql.SqlKind;
26 import org.apache.calcite.sql.SqlNode;
27 import org.apache.calcite.sql.SqlNodeList;
29 import org.apache.calcite.sql.SqlSpecialOperator;
30 import org.apache.calcite.sql.SqlWriter;
31 import org.apache.calcite.sql.SqlWriterConfig;
32 import org.apache.calcite.sql.dialect.CalciteSqlDialect;
33 import org.apache.calcite.sql.parser.SqlParserPos;
34 import org.apache.calcite.sql.pretty.SqlPrettyWriter;
36 import org.apache.calcite.util.ImmutableNullableList;
37 import org.apache.calcite.util.JsonBuilder;
38 
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Objects;
42 
46 public class SqlCreateTable extends SqlCreate {
47  public final boolean temporary;
48  public final SqlIdentifier name;
49  public final SqlNodeList columnList;
50  public SqlNode query = null;
51  private final HeavyDBOptionsMap options;
52 
53  private static final SqlOperator OPERATOR =
54  new SqlSpecialOperator("CREATE TABLE", SqlKind.CREATE_TABLE);
55 
57  protected SqlCreateTable(SqlParserPos pos,
58  boolean temporary,
59  boolean ifNotExists,
60  SqlIdentifier name,
61  SqlNodeList columnList,
62  HeavyDBOptionsMap withOptions,
63  SqlNode query) {
64  super(OPERATOR, pos, false, ifNotExists);
65  this.temporary = temporary;
66  this.name = Objects.requireNonNull(name);
67  this.options = withOptions;
68  this.columnList = columnList; // may be null
69  this.query = query; // for "CREATE TABLE ... AS query"; may be null
70  }
71 
72  public List<SqlNode> getOperandList() {
73  return ImmutableNullableList.of(name, columnList, query);
74  }
75 
76  @Override
77  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
78  writer.keyword("CREATE");
79  if (temporary) {
80  writer.keyword("TEMPORARY");
81  }
82  writer.keyword("TABLE");
83  if (ifNotExists) {
84  writer.keyword("IF NOT EXISTS");
85  }
86  name.unparse(writer, leftPrec, rightPrec);
87  if (columnList != null) {
88  SqlWriter.Frame frame = writer.startList("(", ")");
89  for (SqlNode c : columnList) {
90  writer.sep(",");
91  c.unparse(writer, 0, 0);
92  }
93  writer.endList(frame);
94  }
95  if (query != null) {
96  writer.keyword("AS");
97  writer.newlineAndIndent();
98  query.unparse(writer, 0, 0);
99  }
100  }
101 
102  @Override
103  public String toString() {
104  JsonBuilder jsonBuilder = new EscapedStringJsonBuilder();
105  Map<String, Object> map = jsonBuilder.map();
106 
107  jsonBuilder.put(map, "command", "CREATE_TABLE");
108  jsonBuilder.put(map, "name", this.name.toString());
109 
110  if (query != null) {
111  // By default ... toString() seems to single-quote too much stuff
112  // for the SELECT stmt to be executed later
113  // ->
114  // use PrettyWriter to output a cleaner SQL statement
115  //
116  SqlWriterConfig c = SqlPrettyWriter.config()
117  .withDialect(CalciteSqlDialect.DEFAULT)
118  .withQuoteAllIdentifiers(false)
119  .withSelectListItemsOnSeparateLines(false)
120  .withWhereListItemsOnSeparateLines(false)
121  .withValuesListNewline(false);
122  SqlPrettyWriter writer = new SqlPrettyWriter(c);
123  this.query.unparse(writer, 0, 0);
124  jsonBuilder.put(map, "query", writer.toString());
125  }
126 
127  List<Object> elements_list = jsonBuilder.list();
128  if (columnList != null) {
129  for (SqlNode elementNode : this.columnList) {
130  if (!(elementNode instanceof SqlCall)) {
131  throw new CalciteException("Column definition for table " + this.name.toString()
132  + " is invalid: " + elementNode.toString(),
133  null);
134  }
135  elements_list.add(elementNode);
136  }
137  }
138  jsonBuilder.put(map, "elements", elements_list);
139  jsonBuilder.put(map, "temporary", this.temporary);
140  jsonBuilder.put(map, "ifNotExists", this.ifNotExists);
141 
142  map.put("options", this.options);
143 
144  Map<String, Object> payload = jsonBuilder.map();
145  payload.put("payload", map);
146 
147  // To Debug:
148  // System.out.println(jsonBuilder.toJsonString(payload))
149 
150  return jsonBuilder.toJsonString(payload);
151  }
152 }
SqlCreateTable(SqlParserPos pos, boolean temporary, boolean ifNotExists, SqlIdentifier name, SqlNodeList columnList, HeavyDBOptionsMap withOptions, SqlNode query)
void unparse(SqlWriter writer, int leftPrec, int rightPrec)