18 package com.mapd.parser.extension.ddl;
20 import com.google.gson.annotations.Expose;
22 import org.apache.calcite.runtime.CalciteException;
23 import org.apache.calcite.sql.SqlBasicTypeNameSpec;
24 import org.apache.calcite.sql.SqlCall;
25 import org.apache.calcite.sql.SqlDataTypeSpec;
26 import org.apache.calcite.sql.SqlDdl;
27 import org.apache.calcite.sql.SqlIdentifier;
28 import org.apache.calcite.sql.SqlKind;
29 import org.apache.calcite.sql.SqlNode;
30 import org.apache.calcite.sql.SqlNodeList;
32 import org.apache.calcite.sql.SqlSpecialOperator;
33 import org.apache.calcite.sql.SqlWriter;
34 import org.apache.calcite.sql.parser.SqlParserPos;
35 import org.apache.calcite.sql.util.SqlBasicVisitor;
36 import org.apache.calcite.sql.util.SqlVisitor;
38 import org.apache.calcite.util.JsonBuilder;
40 import java.util.List;
49 new SqlSpecialOperator(
"ALTER_TABLE", SqlKind.OTHER_DDL);
84 private SqlParserPos
pos;
162 this.tableName = name.toString();
172 final Map<String, String>
options) {
181 this.command = OPERATOR.getName();
191 public void unparse(SqlWriter writer,
int leftPrec,
int rightPrec) {
192 writer.keyword(
"ALTER");
193 writer.keyword(
"TABLE");
200 Map<String, Object> map = jsonBuilder.map();
202 map.put(
"command",
"ALTER_TABLE");
203 map.put(
"tableName", this.tableName.toString());
206 map.put(
"alterType",
"RENAME_TABLE");
207 map.put(
"newTableName", this.newTableName.toString());
210 map.put(
"alterType",
"RENAME_COLUMN");
211 map.put(
"columnName", this.columnName.toString());
212 map.put(
"newColumnName", this.newColumnName.toString());
215 map.put(
"alterType",
"ADD_COLUMN");
217 List<Object> elements_list = jsonBuilder.list();
219 if (!(elementNode instanceof SqlCall)) {
220 throw new CalciteException(
"Column definition for table "
222 +
" is invalid: " + elementNode.toString(),
225 elements_list.add(elementNode);
227 map.put(
"columnData", elements_list);
232 map.put(
"alterType",
"ALTER_COLUMN");
234 List<Object> elements_list = jsonBuilder.list();
236 if (!(elementNode instanceof SqlCall)) {
237 throw new CalciteException(
"Column definition " + this.
tableName.toString()
238 +
" is invalid: " + elementNode.toString(),
242 SqlVisitor<Object> sqlVisitor =
new SqlBasicVisitor<Object>() {
244 public Object visit(SqlIdentifier
id) {
245 column_name = id.toString();
249 public Object visit(SqlDataTypeSpec spec) {
250 if (!(spec.getTypeNameSpec() instanceof SqlBasicTypeNameSpec)) {
251 throw new CalciteException(
"Type definition for column "
252 + column_name.toString()
253 +
" is invalid: " + spec.toString(),
259 String column_name =
"<UNSET>";
261 elementNode.accept(sqlVisitor);
263 elements_list.add(elementNode);
265 map.put(
"alterData", elements_list);
269 map.put(
"alterType",
"DROP_COLUMN");
270 map.put(
"columnData", this.columnList.toString());
273 map.put(
"alterType",
"ALTER_OPTIONS");
274 map.put(
"options", this.options);
278 Map<String, Object> payload = jsonBuilder.map();
279 payload.put(
"payload", map);
280 return jsonBuilder.toJsonString(payload);
void unparse(SqlWriter writer, int leftPrec, int rightPrec)
void alterTableName(final String newTableName)
void addColumnList(final SqlNodeList columnList)
SqlAlterTable(final SqlParserPos pos, final SqlIdentifier name)
void setPos(final SqlParserPos pos)
void alterColumnName(final String columnName, final String newColumnName)
void addAlterColumnList(final SqlNodeList columnList)
void dropColumn(final SqlNodeList columnList)
void setTableName(final String tableName)
Map< String, String > options
List< SqlNode > getOperandList()
SqlAlterTable(final SqlParserPos pos, final AlterType alterType, final String tableName, final String newTableName, final String columnName, final String newColumnName, final SqlNodeList columnList, final Map< String, String > options)
static final SqlOperator OPERATOR