OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SqlColumnDeclaration.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 
19 import com.google.common.collect.ImmutableList;
23 
24 import org.apache.calcite.avatica.SqlType;
25 import org.apache.calcite.schema.ColumnStrategy;
26 import org.apache.calcite.sql.SqlBasicTypeNameSpec;
27 import org.apache.calcite.sql.SqlCall;
28 import org.apache.calcite.sql.SqlDataTypeSpec;
29 import org.apache.calcite.sql.SqlIdentifier;
30 import org.apache.calcite.sql.SqlKind;
31 import org.apache.calcite.sql.SqlNode;
32 import org.apache.calcite.sql.SqlOperator;
33 import org.apache.calcite.sql.SqlSpecialOperator;
34 import org.apache.calcite.sql.SqlTypeNameSpec;
35 import org.apache.calcite.sql.SqlWriter;
36 import org.apache.calcite.sql.parser.SqlParserPos;
37 import org.apache.calcite.sql.type.SqlTypeName;
39 
40 import java.util.List;
41 import java.util.Map;
42 
49 public class SqlColumnDeclaration extends SqlCall {
50  private static final SqlSpecialOperator OPERATOR =
51  new SqlSpecialOperator("COLUMN_DECL", SqlKind.COLUMN_DECL);
52 
53  public final SqlIdentifier name;
55  public final SqlNode expression;
56  public final ColumnStrategy strategy;
57 
59  SqlColumnDeclaration(SqlParserPos pos,
60  SqlIdentifier name,
62  SqlNode expression,
63  ColumnStrategy strategy) {
64  super(pos);
65  this.name = name;
66  this.dataType = dataType;
67  this.expression = expression;
68  this.strategy = strategy;
69  }
70 
71  @Override
72  public SqlOperator getOperator() {
73  return OPERATOR;
74  }
75 
76  @Override
77  public List<SqlNode> getOperandList() {
78  return ImmutableList.of(name, dataType);
79  }
80 
81  @Override
82  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
83  name.unparse(writer, 0, 0);
84  dataType.unparse(writer, 0, 0);
85  if (dataType.getNullable() != null && !dataType.getNullable()) {
86  writer.keyword("NOT NULL");
87  }
88  if (expression != null) {
89  switch (strategy) {
90  case VIRTUAL:
91  case STORED:
92  writer.keyword("AS");
93  exp(writer);
94  writer.keyword(strategy.name());
95  break;
96  case DEFAULT:
97  writer.keyword("DEFAULT");
98  exp(writer);
99  break;
100  default:
101  throw new AssertionError("unexpected: " + strategy);
102  }
103  }
104  }
105 
106  private void exp(SqlWriter writer) {
107  if (writer.isAlwaysUseParentheses()) {
108  expression.unparse(writer, 0, 0);
109  } else {
110  writer.sep("(");
111  expression.unparse(writer, 0, 0);
112  writer.sep(")");
113  }
114  }
115 
116  @Override
117  public String toString() {
119  Map<String, Object> map = jsonBuilder.map();
120 
121  jsonBuilder.put(map, "type", "SQL_COLUMN_DECLARATION");
122 
123  jsonBuilder.put(map, "name", this.name == null ? null : this.name.toString());
124 
125  jsonBuilder.put(map,
126  "expression",
127  this.expression == null ? null : this.expression.toString());
128  jsonBuilder.put(map, "nullable", this.strategy == ColumnStrategy.NULLABLE);
129  jsonBuilder.put(map, "encodingType", this.dataType.getEncodingString());
130  jsonBuilder.put(map, "encodingSize", this.dataType.getEncodingSize());
131 
132  SqlTypeNameSpec dataTypeSpec = this.dataType.getTypeNameSpec();
133  if (dataTypeSpec instanceof OmniSciGeoTypeNameSpec) {
134  map = ((OmniSciGeoTypeNameSpec) dataTypeSpec).toJsonMap(map);
135  } else {
136  boolean isText = false;
137  if (dataTypeSpec instanceof OmniSciTypeNameSpec) {
138  OmniSciTypeNameSpec omniSciDataTypeSpec = (OmniSciTypeNameSpec) dataTypeSpec;
139  if (omniSciDataTypeSpec.getIsArray()) {
140  jsonBuilder.put(map, "arraySize", omniSciDataTypeSpec.getArraySize());
141  }
142  isText = omniSciDataTypeSpec.getIsText();
143  }
144  jsonBuilder.put(
145  map, "precision", ((SqlBasicTypeNameSpec) dataTypeSpec).getPrecision());
146  jsonBuilder.put(map, "scale", ((SqlBasicTypeNameSpec) dataTypeSpec).getScale());
147  if (isText) {
148  jsonBuilder.put(map, "sqltype", "TEXT");
149  } else {
150  jsonBuilder.put(map,
151  "sqltype",
152  this.dataType == null ? null : dataTypeSpec.getTypeName().toString());
153  }
154  }
155  return jsonBuilder.toJsonString(map);
156  }
157 }
SqlColumnDeclaration(SqlParserPos pos, SqlIdentifier name, OmniSciSqlDataTypeSpec dataType, SqlNode expression, ColumnStrategy strategy)
void unparse(SqlWriter writer, int leftPrec, int rightPrec)