OmniSciDB  72c90bc290
 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 replace,
59  boolean temporary,
60  boolean ifNotExists,
61  SqlIdentifier name,
62  SqlNodeList columnList,
63  HeavyDBOptionsMap withOptions,
64  SqlNode query) {
65  super(OPERATOR, pos, replace, ifNotExists);
66  this.temporary = temporary;
67  this.name = Objects.requireNonNull(name);
68  this.options = withOptions;
69  this.columnList = columnList; // may be null
70  this.query = query; // for "CREATE TABLE ... AS query"; may be null
71  }
72 
73  public List<SqlNode> getOperandList() {
74  return ImmutableNullableList.of(name, columnList, query);
75  }
76 
77  @Override
78  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
79  writer.keyword("CREATE");
80  if (temporary) {
81  writer.keyword("TEMPORARY");
82  }
83  writer.keyword("TABLE");
84  if (ifNotExists) {
85  writer.keyword("IF NOT EXISTS");
86  }
87  name.unparse(writer, leftPrec, rightPrec);
88  if (columnList != null) {
89  SqlWriter.Frame frame = writer.startList("(", ")");
90  for (SqlNode c : columnList) {
91  writer.sep(",");
92  c.unparse(writer, 0, 0);
93  }
94  writer.endList(frame);
95  }
96  if (query != null) {
97  writer.keyword("AS");
98  writer.newlineAndIndent();
99  query.unparse(writer, 0, 0);
100  }
101  }
102 
103  @Override
104  public String toString() {
105  JsonBuilder jsonBuilder = new EscapedStringJsonBuilder();
106  Map<String, Object> map = jsonBuilder.map();
107 
108  jsonBuilder.put(map, "command", "CREATE_TABLE");
109  jsonBuilder.put(map, "name", this.name.toString());
110 
111  if (query != null) {
112  // By default ... toString() seems to single-quote too much stuff
113  // for the SELECT stmt to be executed later
114  // ->
115  // use PrettyWriter to output a cleaner SQL statement
116  //
117  SqlWriterConfig c = SqlPrettyWriter.config()
118  .withDialect(CalciteSqlDialect.DEFAULT)
119  .withQuoteAllIdentifiers(false)
120  .withSelectListItemsOnSeparateLines(false)
121  .withWhereListItemsOnSeparateLines(false)
122  .withValuesListNewline(false);
123  SqlPrettyWriter writer = new SqlPrettyWriter(c);
124  this.query.unparse(writer, 0, 0);
125  jsonBuilder.put(map, "query", writer.toString());
126  }
127 
128  List<Object> elements_list = jsonBuilder.list();
129  if (columnList != null) {
130  for (SqlNode elementNode : this.columnList) {
131  if (!(elementNode instanceof SqlCall)) {
132  throw new CalciteException("Column definition for table " + this.name.toString()
133  + " is invalid: " + elementNode.toString(),
134  null);
135  }
136  elements_list.add(elementNode);
137  }
138  }
139  jsonBuilder.put(map, "elements", elements_list);
140 
141  jsonBuilder.put(map, "temporary", this.temporary);
142  jsonBuilder.put(map, "ifNotExists", this.ifNotExists);
143 
144  map.put("options", this.options);
145 
146  Map<String, Object> payload = jsonBuilder.map();
147  payload.put("payload", map);
148 
149  // To Debug:
150  // System.out.println(jsonBuilder.toJsonString(payload))
151 
152  return jsonBuilder.toJsonString(payload);
153  }
154 }
SqlCreateTable(SqlParserPos pos, boolean replace, boolean temporary, boolean ifNotExists, SqlIdentifier name, SqlNodeList columnList, HeavyDBOptionsMap withOptions, SqlNode query)
void unparse(SqlWriter writer, int leftPrec, int rightPrec)