OmniSciDB  06b3bd477c
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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.omnisci.thrift.server.TColumnType;
12 import com.omnisci.thrift.server.TDatumType;
13 import com.omnisci.thrift.server.TTableDetails;
14 import com.omnisci.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.HashSet;
30 import java.util.concurrent.atomic.AtomicLong;
31 import java.util.stream.Collectors;
32 
37 public class MapDTable implements Table {
38  private static final AtomicLong VERSION_PROVIDER = new AtomicLong();
39 
40  final static Logger MAPDLOGGER = LoggerFactory.getLogger(MapDTable.class);
41  private final TTableDetails rowInfo;
42  private final long version = VERSION_PROVIDER.incrementAndGet();
43  private final HashSet<String> systemColumnNames;
44 
45  public long getVersion() {
46  return version;
47  }
48 
49  public MapDTable(TTableDetails ri) {
50  rowInfo = ri;
51  systemColumnNames = rowInfo.row_desc.stream()
52  .filter(row_desc -> row_desc.is_system)
53  .map(row_desc -> row_desc.col_name)
54  .collect(Collectors.toCollection(HashSet::new));
55  }
56 
57  @Override
58  public RelDataType getRowType(RelDataTypeFactory rdtf) {
59  RelDataTypeFactory.FieldInfoBuilder builder = rdtf.builder();
60  for (TColumnType tct : rowInfo.getRow_desc()) {
61  MAPDLOGGER.debug("'" + tct.col_name + "'"
62  + " \t" + tct.getCol_type().getEncoding() + " \t"
63  + tct.getCol_type().getFieldValue(TTypeInfo._Fields.TYPE) + " \t"
64  + tct.getCol_type().nullable + " \t" + tct.getCol_type().is_array + " \t"
65  + tct.getCol_type().precision + " \t" + tct.getCol_type().scale);
66  builder.add(tct.col_name, createType(tct, rdtf));
67  }
68  return builder.build();
69  }
70 
71  @Override
72  public Statistic getStatistic() {
73  return Statistics.UNKNOWN;
74  }
75 
76  @Override
77  public Schema.TableType getJdbcTableType() {
78  return Schema.TableType.TABLE;
79  }
80 
81  private RelDataType createType(TColumnType value, RelDataTypeFactory typeFactory) {
82  RelDataType cType = getRelDataType(value.col_type.type,
83  value.col_type.precision,
84  value.col_type.scale,
85  typeFactory);
86 
87  if (value.col_type.is_array) {
88  cType = typeFactory.createArrayType(
89  typeFactory.createTypeWithNullability(cType, true), -1);
90  }
91 
92  if (value.col_type.isNullable()) {
93  return typeFactory.createTypeWithNullability(cType, true);
94  } else {
95  return cType;
96  }
97  }
98 
99  // Convert our TDataumn type in to a base calcite SqlType
100  // todo confirm whether it is ok to ignore thinsg like lengths
101  // since we do not use them on the validator side of the calcite 'fence'
102  private RelDataType getRelDataType(
103  TDatumType dType, int precision, int scale, RelDataTypeFactory typeFactory) {
104  switch (dType) {
105  case TINYINT:
106  return typeFactory.createSqlType(SqlTypeName.TINYINT);
107  case SMALLINT:
108  return typeFactory.createSqlType(SqlTypeName.SMALLINT);
109  case INT:
110  return typeFactory.createSqlType(SqlTypeName.INTEGER);
111  case BIGINT:
112  return typeFactory.createSqlType(SqlTypeName.BIGINT);
113  case FLOAT:
114  return typeFactory.createSqlType(SqlTypeName.FLOAT);
115  case DECIMAL:
116  return typeFactory.createSqlType(SqlTypeName.DECIMAL, precision, scale);
117  case DOUBLE:
118  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
119  case STR:
120  return typeFactory.createSqlType(SqlTypeName.VARCHAR, 50);
121  case TIME:
122  return typeFactory.createSqlType(SqlTypeName.TIME);
123  case TIMESTAMP:
124  return typeFactory.createSqlType(SqlTypeName.TIMESTAMP, precision);
125  case DATE:
126  return typeFactory.createSqlType(SqlTypeName.DATE);
127  case BOOL:
128  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
129  case INTERVAL_DAY_TIME:
130  return typeFactory.createSqlType(SqlTypeName.INTERVAL_DAY);
131  case INTERVAL_YEAR_MONTH:
132  return typeFactory.createSqlType(SqlTypeName.INTERVAL_YEAR_MONTH);
133  case POINT:
134  return typeFactory.createSqlType(SqlTypeName.ANY);
135  // return new PointSqlType();
136  case LINESTRING:
137  return typeFactory.createSqlType(SqlTypeName.ANY);
138  // return new LinestringSqlType();
139  case POLYGON:
140  return typeFactory.createSqlType(SqlTypeName.ANY);
141  // return new PolygonSqlType();
142  case MULTIPOLYGON:
143  return typeFactory.createSqlType(SqlTypeName.ANY);
144  default:
145  throw new AssertionError(dType.name());
146  }
147  }
148 
149  @Override
150  public boolean isRolledUp(String string) {
151  // will set to false by default
152  return false;
153  }
154 
155  @Override
157  String string, SqlCall sc, SqlNode sn, CalciteConnectionConfig ccc) {
158  throw new UnsupportedOperationException(
159  "rolledUpColumnValidInsideAgg Not supported yet.");
160  }
161 
162  public boolean isSystemColumn(final String columnName) {
163  return systemColumnNames.contains(columnName);
164  }
165 }
static final Logger MAPDLOGGER
Definition: MapDTable.java:40
boolean isRolledUp(String string)
Definition: MapDTable.java:150
Schema.TableType getJdbcTableType()
Definition: MapDTable.java:77
static final AtomicLong VERSION_PROVIDER
Definition: MapDTable.java:38
boolean rolledUpColumnValidInsideAgg(String string, SqlCall sc, SqlNode sn, CalciteConnectionConfig ccc)
Definition: MapDTable.java:156
final HashSet< String > systemColumnNames
Definition: MapDTable.java:43
RelDataType getRelDataType(TDatumType dType, int precision, int scale, RelDataTypeFactory typeFactory)
Definition: MapDTable.java:102
RelDataType createType(TColumnType value, RelDataTypeFactory typeFactory)
Definition: MapDTable.java:81
boolean isSystemColumn(final String columnName)
Definition: MapDTable.java:162
final TTableDetails rowInfo
Definition: MapDTable.java:41
RelDataType getRowType(RelDataTypeFactory rdtf)
Definition: MapDTable.java:58