OmniSciDB  04ee39c94c
MapDTable.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package com.mapd.calcite.parser;
7 
11 import com.mapd.thrift.server.TColumnType;
12 import com.mapd.thrift.server.TDatumType;
13 import com.mapd.thrift.server.TTableDetails;
14 import com.mapd.thrift.server.TTypeInfo;
15 
16 import org.apache.calcite.config.CalciteConnectionConfig;
17 import org.apache.calcite.rel.type.RelDataType;
18 import org.apache.calcite.rel.type.RelDataTypeFactory;
19 import org.apache.calcite.schema.Schema;
20 import org.apache.calcite.schema.Statistic;
21 import org.apache.calcite.schema.Statistics;
22 import org.apache.calcite.schema.Table;
23 import org.apache.calcite.sql.SqlCall;
24 import org.apache.calcite.sql.SqlNode;
25 import org.apache.calcite.sql.type.SqlTypeName;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 
29 import java.util.concurrent.atomic.AtomicLong;
30 
35 public class MapDTable implements Table {
36  private static final AtomicLong VERSION_PROVIDER = new AtomicLong();
37 
38  final static Logger MAPDLOGGER = LoggerFactory.getLogger(MapDTable.class);
39  private final TTableDetails rowInfo;
40  private final long version = VERSION_PROVIDER.incrementAndGet();
41 
42  public long getVersion() {
43  return version;
44  }
45 
46  public MapDTable(TTableDetails ri) {
47  rowInfo = ri;
48  }
49 
50  @Override
51  public RelDataType getRowType(RelDataTypeFactory rdtf) {
52  RelDataTypeFactory.FieldInfoBuilder builder = rdtf.builder();
53  for (TColumnType tct : rowInfo.getRow_desc()) {
54  MAPDLOGGER.debug("'" + tct.col_name + "'"
55  + " \t" + tct.getCol_type().getEncoding() + " \t"
56  + tct.getCol_type().getFieldValue(TTypeInfo._Fields.TYPE) + " \t"
57  + tct.getCol_type().nullable + " \t" + tct.getCol_type().is_array + " \t"
58  + tct.getCol_type().precision + " \t" + tct.getCol_type().scale);
59  builder.add(tct.col_name, createType(tct, rdtf));
60  }
61  return builder.build();
62  }
63 
64  @Override
65  public Statistic getStatistic() {
66  return Statistics.UNKNOWN;
67  }
68 
69  @Override
70  public Schema.TableType getJdbcTableType() {
71  return Schema.TableType.TABLE;
72  }
73 
74  private RelDataType createType(TColumnType value, RelDataTypeFactory typeFactory) {
75  RelDataType cType = getRelDataType(value.col_type.type,
76  value.col_type.precision,
77  value.col_type.scale,
78  typeFactory);
79 
80  if (value.col_type.is_array) {
81  cType = typeFactory.createArrayType(
82  typeFactory.createTypeWithNullability(cType, true), -1);
83  }
84 
85  if (value.col_type.isNullable()) {
86  return typeFactory.createTypeWithNullability(cType, true);
87  } else {
88  return cType;
89  }
90  }
91 
92  // Convert our TDataumn type in to a base calcite SqlType
93  // todo confirm whether it is ok to ignore thinsg like lengths
94  // since we do not use them on the validator side of the calcite 'fence'
95  private RelDataType getRelDataType(
96  TDatumType dType, int precision, int scale, RelDataTypeFactory typeFactory) {
97  switch (dType) {
98  case TINYINT:
99  return typeFactory.createSqlType(SqlTypeName.TINYINT);
100  case SMALLINT:
101  return typeFactory.createSqlType(SqlTypeName.SMALLINT);
102  case INT:
103  return typeFactory.createSqlType(SqlTypeName.INTEGER);
104  case BIGINT:
105  return typeFactory.createSqlType(SqlTypeName.BIGINT);
106  case FLOAT:
107  return typeFactory.createSqlType(SqlTypeName.FLOAT);
108  case DECIMAL:
109  return typeFactory.createSqlType(SqlTypeName.DECIMAL, precision, scale);
110  case DOUBLE:
111  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
112  case STR:
113  return typeFactory.createSqlType(SqlTypeName.VARCHAR, 50);
114  case TIME:
115  return typeFactory.createSqlType(SqlTypeName.TIME);
116  case TIMESTAMP:
117  return typeFactory.createSqlType(SqlTypeName.TIMESTAMP, precision);
118  case DATE:
119  return typeFactory.createSqlType(SqlTypeName.DATE);
120  case BOOL:
121  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
122  case INTERVAL_DAY_TIME:
123  return typeFactory.createSqlType(SqlTypeName.INTERVAL_DAY);
124  case INTERVAL_YEAR_MONTH:
125  return typeFactory.createSqlType(SqlTypeName.INTERVAL_YEAR_MONTH);
126  case POINT:
127  return typeFactory.createSqlType(SqlTypeName.ANY);
128  // return new PointSqlType();
129  case LINESTRING:
130  return typeFactory.createSqlType(SqlTypeName.ANY);
131  // return new LinestringSqlType();
132  case POLYGON:
133  return typeFactory.createSqlType(SqlTypeName.ANY);
134  // return new PolygonSqlType();
135  case MULTIPOLYGON:
136  return typeFactory.createSqlType(SqlTypeName.ANY);
137  default:
138  throw new AssertionError(dType.name());
139  }
140  }
141 
142  @Override
143  public boolean isRolledUp(String string) {
144  // will set to false by default
145  return false;
146  }
147 
148  @Override
150  String string, SqlCall sc, SqlNode sn, CalciteConnectionConfig ccc) {
151  throw new UnsupportedOperationException(
152  "rolledUpColumnValidInsideAgg Not supported yet.");
153  }
154 }
static final Logger MAPDLOGGER
Definition: MapDTable.java:38
boolean isRolledUp(String string)
Definition: MapDTable.java:143
Schema.TableType getJdbcTableType()
Definition: MapDTable.java:70
static final AtomicLong VERSION_PROVIDER
Definition: MapDTable.java:36
boolean rolledUpColumnValidInsideAgg(String string, SqlCall sc, SqlNode sn, CalciteConnectionConfig ccc)
Definition: MapDTable.java:149
RelDataType getRelDataType(TDatumType dType, int precision, int scale, RelDataTypeFactory typeFactory)
Definition: MapDTable.java:95
RelDataType createType(TColumnType value, RelDataTypeFactory typeFactory)
Definition: MapDTable.java:74
final TTableDetails rowInfo
Definition: MapDTable.java:39
RelDataType getRowType(RelDataTypeFactory rdtf)
Definition: MapDTable.java:51