1 package com.mapd.parser.extension.ddl;
3 import org.apache.calcite.rel.type.RelDataTypeField;
4 import org.apache.calcite.sql.*;
5 import org.apache.calcite.sql.dialect.CalciteSqlDialect;
6 import org.apache.calcite.sql.parser.SqlParserPos;
7 import org.apache.calcite.sql.pretty.SqlPrettyWriter;
9 import org.apache.calcite.util.ImmutableNullableList;
10 import org.apache.calcite.util.JsonBuilder;
11 import org.checkerframework.checker.nullness.qual.Nullable;
13 import java.util.List;
16 import javax.annotation.Nonnull;
24 new SqlSpecialOperator(
"INSERT_INTO_TABLE_AS_SELECT", SqlKind.OTHER_DDL);
41 public void unparse(SqlWriter writer,
int leftPrec,
int rightPrec) {
42 writer.keyword(
"INSERT");
43 writer.keyword(
"INTO");
44 name.unparse(writer, leftPrec, rightPrec);
46 SqlWriter.Frame frame = writer.startList(
"(",
")");
49 c.unparse(writer, 0, 0);
51 writer.endList(frame);
53 writer.keyword(
"VALUES");
54 SqlWriter.Frame frame = writer.startList(
"(",
")");
55 values.unparse(writer, leftPrec, rightPrec);
56 writer.endList(frame);
62 Map<String, Object> map = jsonBuilder.map();
64 map.put(
"command",
"INSERT_VALUES_INTO_TABLE");
65 map.put(
"name", this.name.toString());
68 List<Object> col_list = jsonBuilder.list();
70 col_list.add(col.toString());
72 jsonBuilder.put(map,
"columns", col_list);
75 List<Object>
rows = jsonBuilder.list();
76 for (SqlNode row_node : ((SqlBasicCall)
values).getOperands()) {
77 rows.add(
toJson(row_node, jsonBuilder));
79 jsonBuilder.put(map,
"values",
rows);
80 Map<String, Object> payload = jsonBuilder.map();
81 payload.put(
"payload", map);
82 return jsonBuilder.toJsonString(payload);
85 private Object
toJson(SqlNode node, JsonBuilder jsonBuilder) {
86 if (node instanceof SqlLiteral) {
87 return toJson((SqlLiteral) node, jsonBuilder);
88 }
else if (node instanceof SqlBasicCall) {
89 return toJson((SqlBasicCall) node, jsonBuilder);
91 throw new RuntimeException(
92 "Unexpected node in values statement: " + node.toString());
96 private Object
toJson(SqlLiteral literal, JsonBuilder jsonBuilder) {
97 final Map<String, @Nullable Object> map = jsonBuilder.map();
98 map.put(
"literal", literal.toValue());
99 map.put(
"type", literal.getTypeName().
toString());
100 if (literal instanceof SqlNumericLiteral) {
101 SqlNumericLiteral numeric = (SqlNumericLiteral) literal;
102 map.put(
"scale", numeric.getScale());
103 map.put(
"precision", numeric.getPrec());
108 private Object
toJson(SqlBasicCall call, JsonBuilder jsonBuilder) {
109 if (call.getOperator().kind == SqlKind.ARRAY_VALUE_CONSTRUCTOR) {
111 }
else if (call.getOperator().kind == SqlKind.ROW) {
114 throw new RuntimeException(
115 "Unexpected sql call: " + call.getOperator().kind.toString());
119 private Object
rowToJson(SqlBasicCall row, JsonBuilder jsonBuilder) {
120 List<Object>
values = jsonBuilder.list();
121 for (SqlNode operand : row.getOperands()) {
122 values.add(
toJson(operand, jsonBuilder));
127 private Object
arrayToJson(SqlBasicCall array, JsonBuilder jsonBuilder) {
128 final Map<String, @Nullable Object> map = jsonBuilder.map();
129 List<Object> elements = jsonBuilder.list();
130 for (SqlNode operand : array.getOperands()) {
131 elements.add(
toJson(operand, jsonBuilder));
133 map.put(
"array", elements);
Object rowToJson(SqlBasicCall row, JsonBuilder jsonBuilder)
static final SqlOperator OPERATOR
void unparse(SqlWriter writer, int leftPrec, int rightPrec)
Object toJson(SqlNode node, JsonBuilder jsonBuilder)
Object toJson(SqlBasicCall call, JsonBuilder jsonBuilder)
Object arrayToJson(SqlBasicCall array, JsonBuilder jsonBuilder)
Object toJson(SqlLiteral literal, JsonBuilder jsonBuilder)
final SqlNodeList columnList
SqlInsertValues(SqlParserPos pos, SqlNode name, SqlNode values, SqlNodeList columnList)
List< SqlNode > getOperandList()