OmniSciDB  b28c0d5765
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqlInsertValues.java
Go to the documentation of this file.
1 package com.mapd.parser.extension.ddl;
2 
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;
12 
13 import java.util.List;
14 import java.util.Map;
15 
16 import javax.annotation.Nonnull;
17 
18 public class SqlInsertValues extends SqlDdl {
19  public final SqlNode name;
20  public SqlNode values;
21  public final SqlNodeList columnList;
22 
23  private static final SqlOperator OPERATOR =
24  new SqlSpecialOperator("INSERT_INTO_TABLE_AS_SELECT", SqlKind.OTHER_DDL);
25 
27  SqlParserPos pos, SqlNode name, SqlNode values, SqlNodeList columnList) {
28  super(OPERATOR, pos);
29  this.name = name;
30  this.values = values;
31  this.columnList = columnList;
32  }
33 
34  @Nonnull
35  @Override
36  public List<SqlNode> getOperandList() {
37  return ImmutableNullableList.of(name, columnList, values);
38  }
39 
40  @Override
41  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
42  writer.keyword("INSERT");
43  writer.keyword("INTO");
44  name.unparse(writer, leftPrec, rightPrec);
45  if (columnList != null) {
46  SqlWriter.Frame frame = writer.startList("(", ")");
47  for (SqlNode c : columnList) {
48  writer.sep(",");
49  c.unparse(writer, 0, 0);
50  }
51  writer.endList(frame);
52  }
53  writer.keyword("VALUES");
54  SqlWriter.Frame frame = writer.startList("(", ")");
55  values.unparse(writer, leftPrec, rightPrec);
56  writer.endList(frame);
57  }
58 
59  @Override
60  public String toString() {
61  JsonBuilder jsonBuilder = new EscapedStringJsonBuilder();
62  Map<String, Object> map = jsonBuilder.map();
63 
64  map.put("command", "INSERT_VALUES_INTO_TABLE");
65  map.put("name", this.name.toString());
66 
67  if (columnList != null) {
68  List<Object> col_list = jsonBuilder.list();
69  for (SqlNode col : this.columnList) {
70  col_list.add(col.toString());
71  }
72  jsonBuilder.put(map, "columns", col_list);
73  }
74 
75  List<Object> rows = jsonBuilder.list();
76  for (SqlNode row_node : ((SqlBasicCall) values).getOperands()) {
77  rows.add(toJson(row_node, jsonBuilder));
78  }
79  jsonBuilder.put(map, "values", rows);
80  Map<String, Object> payload = jsonBuilder.map();
81  payload.put("payload", map);
82  return jsonBuilder.toJsonString(payload);
83  }
84 
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);
90  } else {
91  throw new RuntimeException(
92  "Unexpected node in values statement: " + node.toString());
93  }
94  }
95 
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());
104  }
105  return map;
106  }
107 
108  private Object toJson(SqlBasicCall call, JsonBuilder jsonBuilder) {
109  if (call.getOperator().kind == SqlKind.ARRAY_VALUE_CONSTRUCTOR) {
110  return arrayToJson(call, jsonBuilder);
111  } else if (call.getOperator().kind == SqlKind.ROW) {
112  return rowToJson(call, jsonBuilder);
113  } else {
114  throw new RuntimeException(
115  "Unexpected sql call: " + call.getOperator().kind.toString());
116  }
117  }
118 
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));
123  }
124  return values;
125  }
126 
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));
132  }
133  map.put("array", elements);
134  return map;
135  }
136 }
Object rowToJson(SqlBasicCall row, JsonBuilder jsonBuilder)
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)
SqlInsertValues(SqlParserPos pos, SqlNode name, SqlNode values, SqlNodeList columnList)