OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqlCreateModel.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;
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;
38 
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Objects;
42 
46 public class SqlCreateModel extends SqlCreate {
47  public final SqlIdentifier modelType;
48  public final SqlIdentifier name;
49  public final SqlNode query;
50  private final HeavyDBOptionsMap options;
51 
52  private static final SqlOperator OPERATOR =
53  new SqlSpecialOperator("CREATE MODEL", SqlKind.OTHER_DDL);
54 
56  protected SqlCreateModel(SqlParserPos pos,
57  boolean replace,
58  boolean ifNotExists,
59  SqlIdentifier modelType,
60  SqlIdentifier name,
61  HeavyDBOptionsMap withOptions,
62  SqlNode query) {
63  super(OPERATOR, pos, replace, ifNotExists);
64  this.modelType = Objects.requireNonNull(modelType);
65  this.name = Objects.requireNonNull(name);
66  this.options = withOptions;
67  this.query = query; // for "CREATE TABLE ... AS query"; may be null
68  }
69 
70  public List<SqlNode> getOperandList() {
71  return ImmutableNullableList.of(modelType, name, query);
72  }
73 
74  @Override
75  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
76  if (getReplace()) {
77  writer.keyword("CREATE OR REPLACE");
78  } else {
79  writer.keyword("CREATE");
80  }
81  writer.keyword("MODEL");
82  if (ifNotExists) {
83  writer.keyword("IF NOT EXISTS");
84  }
85  writer.keyword("OF TYPE");
86  modelType.unparse(writer, leftPrec, rightPrec);
87  name.unparse(writer, leftPrec, rightPrec);
88  if (query != null) {
89  writer.keyword("AS");
90  writer.newlineAndIndent();
91  query.unparse(writer, 0, 0);
92  }
93  }
94 
95  @Override
96  public String toString() {
97  JsonBuilder jsonBuilder = new EscapedStringJsonBuilder();
98  Map<String, Object> map = jsonBuilder.map();
99 
100  jsonBuilder.put(map, "command", "CREATE_MODEL");
101  jsonBuilder.put(map, "type", this.modelType.toString());
102  jsonBuilder.put(map, "name", this.name.toString());
103 
104  if (query != null) {
105  // By default ... toString() seems to single-quote too much stuff
106  // for the SELECT stmt to be executed later
107  // ->
108  // use PrettyWriter to output a cleaner SQL statement
109  //
110  SqlWriterConfig c = SqlPrettyWriter.config()
111  .withDialect(CalciteSqlDialect.DEFAULT)
112  .withQuoteAllIdentifiers(false)
113  .withSelectListItemsOnSeparateLines(false)
114  .withWhereListItemsOnSeparateLines(false)
115  .withValuesListNewline(false);
116  SqlPrettyWriter writer = new SqlPrettyWriter(c);
117  this.query.unparse(writer, 0, 0);
118  jsonBuilder.put(map, "query", writer.toString());
119  }
120 
121  jsonBuilder.put(map, "replace", getReplace());
122  jsonBuilder.put(map, "ifNotExists", this.ifNotExists);
123 
124  map.put("options", this.options);
125 
126  Map<String, Object> payload = jsonBuilder.map();
127  payload.put("payload", map);
128 
129  // To Debug:
130  // System.out.println(jsonBuilder.toJsonString(payload))
131 
132  return jsonBuilder.toJsonString(payload);
133  }
134 }
void unparse(SqlWriter writer, int leftPrec, int rightPrec)
SqlCreateModel(SqlParserPos pos, boolean replace, boolean ifNotExists, SqlIdentifier modelType, SqlIdentifier name, HeavyDBOptionsMap withOptions, SqlNode query)