17 package com.mapd.parser.extension.ddl;
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;
39 import java.util.List;
41 import java.util.Objects;
48 public final SqlIdentifier
name;
54 new SqlSpecialOperator(
"CREATE TABLE", SqlKind.CREATE_TABLE);
64 super(
OPERATOR, pos,
false, ifNotExists);
66 this.name = Objects.requireNonNull(
name);
67 this.options = withOptions;
77 public void unparse(SqlWriter writer,
int leftPrec,
int rightPrec) {
78 writer.keyword(
"CREATE");
80 writer.keyword(
"TEMPORARY");
82 writer.keyword(
"TABLE");
84 writer.keyword(
"IF NOT EXISTS");
86 name.unparse(writer, leftPrec, rightPrec);
88 SqlWriter.Frame frame = writer.startList(
"(",
")");
91 c.unparse(writer, 0, 0);
93 writer.endList(frame);
97 writer.newlineAndIndent();
98 query.unparse(writer, 0, 0);
105 Map<String, Object> map = jsonBuilder.map();
107 jsonBuilder.put(map,
"command",
"CREATE_TABLE");
108 jsonBuilder.put(map,
"name", this.name.toString());
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());
127 List<Object> elements_list = jsonBuilder.list();
130 if (!(elementNode instanceof SqlCall)) {
131 throw new CalciteException(
"Column definition for table " + this.
name.toString()
132 +
" is invalid: " + elementNode.toString(),
135 elements_list.add(elementNode);
138 jsonBuilder.put(map,
"elements", elements_list);
139 jsonBuilder.put(map,
"temporary", this.temporary);
140 jsonBuilder.put(map,
"ifNotExists", this.ifNotExists);
142 map.put(
"options", this.options);
144 Map<String, Object> payload = jsonBuilder.map();
145 payload.put(
"payload", map);
150 return jsonBuilder.toJsonString(payload);
final HeavyDBOptionsMap options
SqlCreateTable(SqlParserPos pos, boolean temporary, boolean ifNotExists, SqlIdentifier name, SqlNodeList columnList, HeavyDBOptionsMap withOptions, SqlNode query)
List< SqlNode > getOperandList()
final SqlNodeList columnList
static final SqlOperator OPERATOR
void unparse(SqlWriter writer, int leftPrec, int rightPrec)