OmniSciDB  94e8789169
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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;
28 import org.apache.calcite.sql.SqlOperator;
29 import org.apache.calcite.sql.SqlSpecialOperator;
30 import org.apache.calcite.sql.SqlWriter;
31 import org.apache.calcite.sql.parser.SqlParserPos;
33 import org.apache.calcite.util.ImmutableNullableList;
34 import org.apache.calcite.util.JsonBuilder;
35 
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Objects;
39 
43 public class SqlCreateTable extends SqlCreate {
44  public final SqlIdentifier name;
45  public final SqlNodeList columnList;
46  public final SqlNode query;
47  private final OmniSciOptionsMap options;
48 
49  private static final SqlOperator OPERATOR =
50  new SqlSpecialOperator("CREATE TABLE", SqlKind.CREATE_TABLE);
51 
53  protected SqlCreateTable(SqlParserPos pos,
54  boolean replace,
55  boolean ifNotExists,
56  SqlIdentifier name,
57  SqlNodeList columnList,
58  OmniSciOptionsMap withOptions,
59  SqlNode query) {
60  super(OPERATOR, pos, replace, ifNotExists);
61  this.name = Objects.requireNonNull(name);
62  this.options = withOptions;
63  this.columnList = columnList; // may be null
64  this.query = query; // for "CREATE TABLE ... AS query"; may be null
65  }
66 
67  public List<SqlNode> getOperandList() {
68  return ImmutableNullableList.of(name, columnList, query);
69  }
70 
71  @Override
72  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
73  writer.keyword("CREATE");
74  writer.keyword("TABLE");
75  if (ifNotExists) {
76  writer.keyword("IF NOT EXISTS");
77  }
78  name.unparse(writer, leftPrec, rightPrec);
79  if (columnList != null) {
80  SqlWriter.Frame frame = writer.startList("(", ")");
81  for (SqlNode c : columnList) {
82  writer.sep(",");
83  c.unparse(writer, 0, 0);
84  }
85  writer.endList(frame);
86  }
87  if (query != null) {
88  writer.keyword("AS");
89  writer.newlineAndIndent();
90  query.unparse(writer, 0, 0);
91  }
92  }
93 
94  @Override
95  public String toString() {
96  JsonBuilder jsonBuilder = new EscapedStringJsonBuilder();
97  Map<String, Object> map = jsonBuilder.map();
98 
99  jsonBuilder.put(map, "name", this.name.toString());
100  jsonBuilder.put(
101  map, "query", this.query == null ? this.query : this.query.toString());
102 
103  List<Object> elements_list = jsonBuilder.list();
104  for (SqlNode elementNode : this.columnList) {
105  if (!(elementNode instanceof SqlCall)) {
106  throw new CalciteException("Column definition for table " + this.name.toString()
107  + " is invalid: " + elementNode.toString(),
108  null);
109  }
110  elements_list.add(elementNode);
111  }
112  jsonBuilder.put(map, "elements", elements_list);
113 
114  jsonBuilder.put(map, "ifNotExists", this.ifNotExists);
115 
116  map.put("command", "CREATE_TABLE");
117  map.put("options", this.options);
118  Map<String, Object> payload = jsonBuilder.map();
119  payload.put("payload", map);
120  return jsonBuilder.toJsonString(payload);
121  }
122 }
SqlCreateTable(SqlParserPos pos, boolean replace, boolean ifNotExists, SqlIdentifier name, SqlNodeList columnList, OmniSciOptionsMap withOptions, SqlNode query)
void unparse(SqlWriter writer, int leftPrec, int rightPrec)