OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HeavyDBTable.java
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.mapd.calcite.parser;
18 
22 
23 import org.apache.calcite.config.CalciteConnectionConfig;
24 import org.apache.calcite.rel.type.RelDataType;
25 import org.apache.calcite.rel.type.RelDataTypeFactory;
26 import org.apache.calcite.schema.Schema;
27 import org.apache.calcite.schema.Statistic;
28 import org.apache.calcite.schema.Statistics;
29 import org.apache.calcite.schema.Table;
30 import org.apache.calcite.sql.SqlCall;
31 import org.apache.calcite.sql.SqlNode;
32 import org.apache.calcite.sql.type.SqlTypeName;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 
36 import java.util.HashSet;
37 import java.util.concurrent.atomic.AtomicLong;
38 import java.util.stream.Collectors;
39 
40 import ai.heavy.thrift.server.TColumnType;
41 import ai.heavy.thrift.server.TDatumType;
42 import ai.heavy.thrift.server.TTableDetails;
43 import ai.heavy.thrift.server.TTypeInfo;
44 
45 public class HeavyDBTable implements Table {
46  private static final AtomicLong VERSION_PROVIDER = new AtomicLong();
47 
48  final static Logger HEAVYDBLOGGER = LoggerFactory.getLogger(HeavyDBTable.class);
49  private final TTableDetails rowInfo;
50  private final long version = VERSION_PROVIDER.incrementAndGet();
51  private final HashSet<String> systemColumnNames;
52 
53  public long getVersion() {
54  return version;
55  }
56 
57  public HeavyDBTable(TTableDetails ri) {
58  rowInfo = ri;
59  systemColumnNames = rowInfo.row_desc.stream()
60  .filter(row_desc -> row_desc.is_system)
61  .map(row_desc -> row_desc.col_name)
62  .collect(Collectors.toCollection(HashSet::new));
63  }
64 
65  @Override
66  public RelDataType getRowType(RelDataTypeFactory rdtf) {
67  RelDataTypeFactory.Builder builder = rdtf.builder();
68  for (TColumnType tct : rowInfo.getRow_desc()) {
69  HEAVYDBLOGGER.debug("'" + tct.col_name + "'"
70  + " \t" + tct.getCol_type().getEncoding() + " \t"
71  + tct.getCol_type().getFieldValue(TTypeInfo._Fields.TYPE) + " \t"
72  + tct.getCol_type().nullable + " \t" + tct.getCol_type().is_array + " \t"
73  + tct.getCol_type().precision + " \t" + tct.getCol_type().scale);
74  builder.add(tct.col_name, createType(tct, rdtf));
75  }
76  return builder.build();
77  }
78 
79  @Override
80  public Statistic getStatistic() {
81  return Statistics.UNKNOWN;
82  }
83 
84  @Override
85  public Schema.TableType getJdbcTableType() {
86  return Schema.TableType.TABLE;
87  }
88 
89  private RelDataType createType(TColumnType value, RelDataTypeFactory typeFactory) {
90  RelDataType cType = getRelDataType(value.col_type.type,
91  value.col_type.precision,
92  value.col_type.scale,
93  typeFactory);
94 
95  if (value.col_type.is_array) {
96  cType = typeFactory.createArrayType(
97  typeFactory.createTypeWithNullability(cType, true), -1);
98  }
99 
100  if (value.col_type.isNullable()) {
101  return typeFactory.createTypeWithNullability(cType, true);
102  } else {
103  return cType;
104  }
105  }
106 
107  // Convert our TDataumn type in to a base calcite SqlType
108  // todo confirm whether it is ok to ignore thinsg like lengths
109  // since we do not use them on the validator side of the calcite 'fence'
110  private RelDataType getRelDataType(
111  TDatumType dType, int precision, int scale, RelDataTypeFactory typeFactory) {
112  switch (dType) {
113  case TINYINT:
114  return typeFactory.createSqlType(SqlTypeName.TINYINT);
115  case SMALLINT:
116  return typeFactory.createSqlType(SqlTypeName.SMALLINT);
117  case INT:
118  return typeFactory.createSqlType(SqlTypeName.INTEGER);
119  case BIGINT:
120  return typeFactory.createSqlType(SqlTypeName.BIGINT);
121  case FLOAT:
122  return typeFactory.createSqlType(SqlTypeName.FLOAT);
123  case DECIMAL:
124  return typeFactory.createSqlType(SqlTypeName.DECIMAL, precision, scale);
125  case DOUBLE:
126  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
127  case STR:
128  return typeFactory.createSqlType(SqlTypeName.VARCHAR, 50);
129  case TIME:
130  return typeFactory.createSqlType(SqlTypeName.TIME);
131  case TIMESTAMP:
132  return typeFactory.createSqlType(SqlTypeName.TIMESTAMP, precision);
133  case DATE:
134  return typeFactory.createSqlType(SqlTypeName.DATE);
135  case BOOL:
136  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
137  case INTERVAL_DAY_TIME:
138  return typeFactory.createSqlType(SqlTypeName.INTERVAL_DAY);
139  case INTERVAL_YEAR_MONTH:
140  return typeFactory.createSqlType(SqlTypeName.INTERVAL_YEAR_MONTH);
141  case POINT:
142  return typeFactory.createSqlType(SqlTypeName.ANY);
143  // return new PointSqlType();
144  case MULTIPOINT:
145  return typeFactory.createSqlType(SqlTypeName.ANY);
146  // return new MultipointSqlType();
147  case LINESTRING:
148  return typeFactory.createSqlType(SqlTypeName.ANY);
149  // return new LinestringSqlType();
150  case MULTILINESTRING:
151  return typeFactory.createSqlType(SqlTypeName.ANY);
152  // return new MultilinestringSqlType();
153  case POLYGON:
154  return typeFactory.createSqlType(SqlTypeName.ANY);
155  // return new PolygonSqlType();
156  case MULTIPOLYGON:
157  return typeFactory.createSqlType(SqlTypeName.ANY);
158  // return new MultipolygonSqlType();
159  default:
160  throw new AssertionError(dType.name());
161  }
162  }
163 
164  @Override
165  public boolean isRolledUp(String string) {
166  // will set to false by default
167  return false;
168  }
169 
170  @Override
172  String string, SqlCall sc, SqlNode sn, CalciteConnectionConfig ccc) {
173  throw new UnsupportedOperationException(
174  "rolledUpColumnValidInsideAgg Not supported yet.");
175  }
176 
177  public boolean isSystemColumn(final String columnName) {
178  return systemColumnNames.contains(columnName);
179  }
180 }
RelDataType getRelDataType(TDatumType dType, int precision, int scale, RelDataTypeFactory typeFactory)
boolean rolledUpColumnValidInsideAgg(String string, SqlCall sc, SqlNode sn, CalciteConnectionConfig ccc)
final HashSet< String > systemColumnNames
RelDataType createType(TColumnType value, RelDataTypeFactory typeFactory)
static final AtomicLong VERSION_PROVIDER
void getFieldValue(T &result, TargetValue *field)
Definition: DBETypes.cpp:28
RelDataType getRowType(RelDataTypeFactory rdtf)
boolean isSystemColumn(final String columnName)