OmniSciDB  72180abbfe
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MetaConnect.java
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 package com.mapd.metadata;
17 
18 import com.google.common.collect.ImmutableList;
19 import com.google.gson.Gson;
20 import com.google.gson.JsonArray;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonObject;
28 import com.omnisci.thrift.server.OmniSci;
29 import com.omnisci.thrift.server.TColumnType;
30 import com.omnisci.thrift.server.TDatumType;
31 import com.omnisci.thrift.server.TEncodingType;
32 import com.omnisci.thrift.server.TOmniSciException;
33 import com.omnisci.thrift.server.TTableDetails;
34 import com.omnisci.thrift.server.TTypeInfo;
35 
36 import org.apache.calcite.schema.Table;
37 import org.apache.thrift.TException;
38 import org.apache.thrift.protocol.TBinaryProtocol;
39 import org.apache.thrift.protocol.TProtocol;
40 import org.apache.thrift.transport.TServerSocket;
41 import org.apache.thrift.transport.TSocket;
42 import org.apache.thrift.transport.TTransport;
43 import org.apache.thrift.transport.TTransportException;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 
47 import java.io.File;
48 import java.io.FileInputStream;
49 import java.io.IOException;
50 import java.sql.Connection;
51 import java.sql.DriverManager;
52 import java.sql.ResultSet;
53 import java.sql.SQLException;
54 import java.sql.Statement;
55 import java.util.HashSet;
56 import java.util.List;
57 import java.util.Map;
58 import java.util.Map.Entry;
59 import java.util.Set;
60 import java.util.concurrent.ConcurrentHashMap;
61 
66 public class MetaConnect {
67  final static Logger MAPDLOGGER = LoggerFactory.getLogger(MetaConnect.class);
68  private final String dataDir;
69  private final String db;
70  private final MapDUser currentUser;
71  private final int mapdPort;
72  private Connection catConn;
73  private final MapDParser parser;
74 
75  private static final int KBOOLEAN = 1;
76  private static final int KCHAR = 2;
77  private static final int KVARCHAR = 3;
78  private static final int KNUMERIC = 4;
79  private static final int KDECIMAL = 5;
80  private static final int KINT = 6;
81  private static final int KSMALLINT = 7;
82  private static final int KFLOAT = 8;
83  private static final int KDOUBLE = 9;
84  private static final int KTIME = 10;
85  private static final int KTIMESTAMP = 11;
86  private static final int KBIGINT = 12;
87  private static final int KTEXT = 13;
88  private static final int KDATE = 14;
89  private static final int KARRAY = 15;
90  private static final int KINTERVAL_DAY_TIME = 16;
91  private static final int KINTERVAL_YEAR_MONTH = 17;
92  private static final int KPOINT = 18;
93  private static final int KLINESTRING = 19;
94  private static final int KPOLYGON = 20;
95  private static final int KMULTIPOLYGON = 21;
96  private static final int KTINYINT = 22;
97 
98  private static volatile Map<String, Set<String>> MAPD_DATABASE_TO_TABLES =
99  new ConcurrentHashMap<>();
100  private static volatile Map<List<String>, Table> MAPD_TABLE_DETAILS =
101  new ConcurrentHashMap<>();
103 
104  public MetaConnect(int mapdPort,
105  String dataDir,
106  MapDUser currentMapDUser,
109  this.dataDir = dataDir;
110  if (currentMapDUser != null) {
111  this.db = currentMapDUser.getDB();
112  } else {
113  this.db = null;
114  }
115  this.currentUser = currentMapDUser;
116  this.mapdPort = mapdPort;
117  this.parser = parser;
118  this.sock_transport_properties = skT;
119  }
120 
121  private void connectToDBCatalog() {
122  try {
123  // try {
124  Class.forName("org.sqlite.JDBC");
125  } catch (ClassNotFoundException ex) {
126  String err = "Could not find class for metadata connection; DB: '" + db
127  + "' data dir '" + dataDir + "', error was " + ex.getMessage();
128  MAPDLOGGER.error(err);
129  throw new RuntimeException(err);
130  }
131  String connectURL = "jdbc:sqlite:" + dataDir + "/mapd_catalogs/" + db;
132  try {
133  catConn = DriverManager.getConnection(connectURL);
134  } catch (SQLException ex) {
135  String err = "Could not establish a connection for metadata; DB: '" + db
136  + "' data dir '" + dataDir + "', error was " + ex.getMessage();
137  MAPDLOGGER.error(err);
138  throw new RuntimeException(err);
139  }
140  MAPDLOGGER.debug("Opened database successfully");
141  }
142 
143  private void disconnectFromDBCatalog() {
144  try {
145  catConn.close();
146  } catch (SQLException ex) {
147  String err = "Could not disconnect for metadata; DB: '" + db + "' data dir '"
148  + dataDir + "', error was " + ex.getMessage();
149  MAPDLOGGER.error(err);
150  throw new RuntimeException(err);
151  }
152  }
153 
154  public Table getTable(String tableName) {
155  List<String> dbTable = ImmutableList.of(db.toUpperCase(), tableName.toUpperCase());
156  Table cTable = MAPD_TABLE_DETAILS.get(dbTable);
157  if (cTable != null) {
158  return cTable;
159  }
160 
161  TTableDetails td = get_table_details(tableName);
162 
163  if (td.getView_sql() == null || td.getView_sql().isEmpty()) {
164  MAPDLOGGER.debug("Processing a table");
165  Table rTable = new MapDTable(td);
166  MAPD_TABLE_DETAILS.putIfAbsent(dbTable, rTable);
167  return rTable;
168  } else {
169  MAPDLOGGER.debug("Processing a view");
170  Table rTable = new MapDView(getViewSql(tableName), td, parser);
171  MAPD_TABLE_DETAILS.putIfAbsent(dbTable, rTable);
172  return rTable;
173  }
174  }
175 
176  public Set<String> getTables() {
177  Set<String> mSet = MAPD_DATABASE_TO_TABLES.get(db.toUpperCase());
178  if (mSet != null) {
179  return mSet;
180  }
181 
182  if (mapdPort == -1) {
183  // use sql
185  Set<String> ts = getTables_SQL();
187  MAPD_DATABASE_TO_TABLES.putIfAbsent(db.toUpperCase(), ts);
188  return ts;
189  }
190  // use thrift direct to local server
191  try {
192  TProtocol protocol = null;
193  TTransport transport =
194  sock_transport_properties.openClientTransport("localhost", mapdPort);
195  if (!transport.isOpen()) transport.open();
196  protocol = new TBinaryProtocol(transport);
197 
198  OmniSci.Client client = new OmniSci.Client(protocol);
199 
200  List<String> tablesList = client.get_tables(currentUser.getSession());
201  Set<String> ts = new HashSet<String>(tablesList.size());
202  for (String tableName : tablesList) {
203  ts.add(tableName);
204  }
205 
206  transport.close();
207  MAPD_DATABASE_TO_TABLES.putIfAbsent(db.toUpperCase(), ts);
208  return ts;
209 
210  } catch (TTransportException ex) {
211  MAPDLOGGER.error("TTransportException on port [" + mapdPort + "]");
212  MAPDLOGGER.error(ex.toString());
213  throw new RuntimeException(ex.toString());
214  } catch (TOmniSciException ex) {
215  MAPDLOGGER.error(ex.toString());
216  throw new RuntimeException(ex.toString());
217  } catch (TException ex) {
218  MAPDLOGGER.error(ex.toString());
219  throw new RuntimeException(ex.toString());
220  }
221  }
222 
223  private Set<String> getTables_SQL() {
225  Set<String> tableSet = new HashSet<String>();
226  Statement stmt = null;
227  ResultSet rs = null;
228  String sqlText = "";
229  try {
230  stmt = catConn.createStatement();
231 
232  // get the tables
233  rs = stmt.executeQuery("SELECT name FROM mapd_tables ");
234  while (rs.next()) {
235  tableSet.add(rs.getString("name"));
236  /*--*/
237  MAPDLOGGER.debug("Object name = " + rs.getString("name"));
238  }
239  rs.close();
240  stmt.close();
241 
242  } catch (Exception e) {
243  String err = "error trying to get all the tables, error was " + e.getMessage();
244  MAPDLOGGER.error(err);
245  throw new RuntimeException(err);
246  }
248 
249  try {
250  // open temp table json file
251  final String filePath = dataDir + "/mapd_catalogs/" + db + "_temp_tables.json";
252  MAPDLOGGER.debug("Opening temp table file at " + filePath);
253  String tempTablesJsonStr;
254  try {
255  File tempTablesFile = new File(filePath);
256  FileInputStream tempTablesStream = new FileInputStream(tempTablesFile);
257  byte[] data = new byte[(int) tempTablesFile.length()];
258  tempTablesStream.read(data);
259  tempTablesStream.close();
260 
261  tempTablesJsonStr = new String(data, "UTF-8");
262  } catch (java.io.FileNotFoundException e) {
263  return tableSet;
264  }
265 
266  Gson gson = new Gson();
267  JsonObject fileParentObject = gson.fromJson(tempTablesJsonStr, JsonObject.class);
268  for (Entry<String, JsonElement> member : fileParentObject.entrySet()) {
269  String tableName = member.getKey();
270  tableSet.add(tableName);
271  /*--*/
272  MAPDLOGGER.debug("Temp table object name = " + tableName);
273  }
274 
275  } catch (Exception e) {
276  String err = "error trying to load temporary tables from json file, error was "
277  + e.getMessage();
278  MAPDLOGGER.error(err);
279  throw new RuntimeException(err);
280  }
281 
282  return tableSet;
283  }
284 
285  public TTableDetails get_table_details(String tableName) {
286  if (mapdPort == -1) {
287  // use sql
289  TTableDetails td = get_table_detail_SQL(tableName);
291  return td;
292  }
293  // use thrift direct to local server
294  try {
295  TProtocol protocol = null;
296 
297  TTransport transport =
298  sock_transport_properties.openClientTransport("localhost", mapdPort);
299  if (!transport.isOpen()) transport.open();
300  protocol = new TBinaryProtocol(transport);
301 
302  OmniSci.Client client = new OmniSci.Client(protocol);
303 
304  TTableDetails td =
305  client.get_internal_table_details(currentUser.getSession(), tableName);
306 
307  transport.close();
308 
309  return td;
310 
311  } catch (TTransportException ex) {
312  MAPDLOGGER.error(ex.toString());
313  throw new RuntimeException(ex.toString());
314  } catch (TOmniSciException ex) {
315  MAPDLOGGER.error(ex.toString());
316  throw new RuntimeException(ex.toString());
317  } catch (TException ex) {
318  MAPDLOGGER.error(ex.toString());
319  throw new RuntimeException(ex.toString());
320  }
321  }
322 
323  public static final int get_physical_cols(int type) {
324  switch (type) {
325  case KPOINT:
326  return 1; // coords
327  case KLINESTRING:
328  return 2; // coords, bounds
329  case KPOLYGON:
330  return 4; // coords, ring_sizes, bounds, render_group
331  case KMULTIPOLYGON:
332  return 5; // coords, ring_sizes, poly_rings, bounds, render_group
333  default:
334  break;
335  }
336  return 0;
337  }
338 
339  public static final boolean is_geometry(int type) {
340  return type == KPOINT || type == KLINESTRING || type == KPOLYGON
341  || type == KMULTIPOLYGON;
342  }
343 
344  private TTableDetails get_table_detail_SQL(String tableName) {
345  TTableDetails td = new TTableDetails();
346  td.getRow_descIterator();
347  int id = getTableId(tableName);
348  if (id == -1) {
349  try {
350  return get_table_detail_JSON(tableName);
351  } catch (Exception e) {
352  String err = "Table '" + tableName + "' does not exist for DB '" + db + "'";
353  MAPDLOGGER.error(err);
354  throw new RuntimeException(err);
355  }
356  }
357 
358  // read data from table
359  Statement stmt = null;
360  ResultSet rs = null;
361  try {
362  stmt = catConn.createStatement();
363  MAPDLOGGER.debug("table id is " + id);
364  MAPDLOGGER.debug("table name is " + tableName);
365  String query = String.format(
366  "SELECT * FROM mapd_columns where tableid = %d and not is_deletedcol order by columnid;",
367  id);
368  MAPDLOGGER.debug(query);
369  rs = stmt.executeQuery(query);
370  int skip_physical_cols = 0;
371  while (rs.next()) {
372  String colName = rs.getString("name");
373  MAPDLOGGER.debug("name = " + colName);
374  int colType = rs.getInt("coltype");
375  MAPDLOGGER.debug("coltype = " + colType);
376  int colSubType = rs.getInt("colsubtype");
377  MAPDLOGGER.debug("colsubtype = " + colSubType);
378  int colDim = rs.getInt("coldim");
379  MAPDLOGGER.debug("coldim = " + colDim);
380  int colScale = rs.getInt("colscale");
381  MAPDLOGGER.debug("colscale = " + colScale);
382  boolean isNotNull = rs.getBoolean("is_notnull");
383  MAPDLOGGER.debug("is_notnull = " + isNotNull);
384  boolean isSystemCol = rs.getBoolean("is_systemcol");
385  MAPDLOGGER.debug("is_systemcol = " + isSystemCol);
386  boolean isVirtualCol = rs.getBoolean("is_virtualcol");
387  MAPDLOGGER.debug("is_vitrualcol = " + isVirtualCol);
388  MAPDLOGGER.debug("");
389  TColumnType tct = new TColumnType();
390  TTypeInfo tti = new TTypeInfo();
391  TDatumType tdt;
392 
393  if (colType == KARRAY) {
394  tti.is_array = true;
395  tdt = typeToThrift(colSubType);
396  } else {
397  tti.is_array = false;
398  tdt = typeToThrift(colType);
399  }
400 
401  tti.nullable = !isNotNull;
402  tti.encoding = TEncodingType.NONE;
403  tti.type = tdt;
404  tti.scale = colScale;
405  tti.precision = colDim;
406 
407  tct.col_name = colName;
408  tct.col_type = tti;
409  tct.is_system = isSystemCol;
410 
411  if (skip_physical_cols <= 0) skip_physical_cols = get_physical_cols(colType);
412  if (is_geometry(colType) || skip_physical_cols-- <= 0) td.addToRow_desc(tct);
413  }
414  } catch (Exception e) {
415  String err = "error trying to read from mapd_columns, error was " + e.getMessage();
416  MAPDLOGGER.error(err);
417  throw new RuntimeException(err);
418  } finally {
419  if (rs != null) {
420  try {
421  rs.close();
422  } catch (SQLException ex) {
423  String err = "Could not close resultset, error was " + ex.getMessage();
424  MAPDLOGGER.error(err);
425  throw new RuntimeException(err);
426  }
427  }
428  if (stmt != null) {
429  try {
430  stmt.close();
431  } catch (SQLException ex) {
432  String err = "Could not close stmt, error was " + ex.getMessage();
433  MAPDLOGGER.error(err);
434  throw new RuntimeException(err);
435  }
436  }
437  }
438  if (isView(tableName)) {
439  td.setView_sqlIsSet(true);
440  td.setView_sql(getViewSqlViaSql(id));
441  }
442  return td;
443  }
444 
445  private TTableDetails get_table_detail_JSON(String tableName)
446  throws IOException, RuntimeException {
447  TTableDetails td = new TTableDetails();
448  td.getRow_descIterator();
449 
450  // open table json file
451  final String filePath = dataDir + "/mapd_catalogs/" + db + "_temp_tables.json";
452  MAPDLOGGER.debug("Opening temp table file at " + filePath);
453 
454  String tempTablesJsonStr;
455  try {
456  File tempTablesFile = new File(filePath);
457  FileInputStream tempTablesStream = new FileInputStream(tempTablesFile);
458  byte[] data = new byte[(int) tempTablesFile.length()];
459  tempTablesStream.read(data);
460  tempTablesStream.close();
461 
462  tempTablesJsonStr = new String(data, "UTF-8");
463  } catch (java.io.FileNotFoundException e) {
464  throw new RuntimeException("Failed to read temporary tables file.");
465  }
466 
467  Gson gson = new Gson();
468  JsonObject fileParentObject = gson.fromJson(tempTablesJsonStr, JsonObject.class);
469  if (fileParentObject == null) {
470  throw new IOException("Malformed temporary tables file.");
471  }
472 
473  JsonObject tableObject = fileParentObject.getAsJsonObject(tableName);
474  if (tableObject == null) {
475  throw new RuntimeException(
476  "Failed to find table " + tableName + " in temporary tables file.");
477  }
478 
479  String jsonTableName = tableObject.get("name").getAsString();
480  assert (tableName == jsonTableName);
481  int id = tableObject.get("id").getAsInt();
482  MAPDLOGGER.debug("table id is " + id);
483  MAPDLOGGER.debug("table name is " + tableName);
484 
485  JsonArray jsonColumns = tableObject.getAsJsonArray("columns");
486  assert (jsonColumns != null);
487 
488  int skip_physical_cols = 0;
489  for (JsonElement columnElement : jsonColumns) {
490  JsonObject columnObject = columnElement.getAsJsonObject();
491 
492  String colName = columnObject.get("name").getAsString();
493  MAPDLOGGER.debug("name = " + colName);
494  int colType = columnObject.get("coltype").getAsInt();
495  MAPDLOGGER.debug("coltype = " + colType);
496  int colSubType = columnObject.get("colsubtype").getAsInt();
497  MAPDLOGGER.debug("colsubtype = " + colSubType);
498  int colDim = columnObject.get("coldim").getAsInt();
499  MAPDLOGGER.debug("coldim = " + colDim);
500  int colScale = columnObject.get("colscale").getAsInt();
501  MAPDLOGGER.debug("colscale = " + colScale);
502  boolean isNotNull = columnObject.get("is_notnull").getAsBoolean();
503  MAPDLOGGER.debug("is_notnull = " + isNotNull);
504  boolean isSystemCol = columnObject.get("is_systemcol").getAsBoolean();
505  MAPDLOGGER.debug("is_systemcol = " + isSystemCol);
506  boolean isVirtualCol = columnObject.get("is_virtualcol").getAsBoolean();
507  MAPDLOGGER.debug("is_vitrualcol = " + isVirtualCol);
508  boolean isDeletedCol = columnObject.get("is_deletedcol").getAsBoolean();
509  MAPDLOGGER.debug("is_deletedcol = " + isDeletedCol);
510  MAPDLOGGER.debug("");
511 
512  if (isDeletedCol) {
513  MAPDLOGGER.debug("Skipping delete column.");
514  continue;
515  }
516 
517  TColumnType tct = new TColumnType();
518  TTypeInfo tti = new TTypeInfo();
519  TDatumType tdt;
520 
521  if (colType == KARRAY) {
522  tti.is_array = true;
523  tdt = typeToThrift(colSubType);
524  } else {
525  tti.is_array = false;
526  tdt = typeToThrift(colType);
527  }
528 
529  tti.nullable = !isNotNull;
530  tti.encoding = TEncodingType.NONE;
531  tti.type = tdt;
532  tti.scale = colScale;
533  tti.precision = colDim;
534 
535  tct.col_name = colName;
536  tct.col_type = tti;
537  tct.is_system = isSystemCol;
538 
539  if (skip_physical_cols <= 0) skip_physical_cols = get_physical_cols(colType);
540  if (is_geometry(colType) || skip_physical_cols-- <= 0) td.addToRow_desc(tct);
541  }
542 
543  return td;
544  }
545 
546  private int getTableId(String tableName) {
547  Statement stmt = null;
548  ResultSet rs = null;
549  int tableId = -1;
550  try {
551  stmt = catConn.createStatement();
552  rs = stmt.executeQuery(String.format(
553  "SELECT tableid FROM mapd_tables where name = '%s' COLLATE NOCASE;",
554  tableName));
555  while (rs.next()) {
556  tableId = rs.getInt("tableid");
557  MAPDLOGGER.debug("tableId = " + tableId);
558  MAPDLOGGER.debug("");
559  }
560  rs.close();
561  stmt.close();
562  } catch (Exception e) {
563  String err = "Error trying to read from metadata table mapd_tables;DB: " + db
564  + " data dir " + dataDir + ", error was " + e.getMessage();
565  MAPDLOGGER.error(err);
566  throw new RuntimeException(err);
567  } finally {
568  if (rs != null) {
569  try {
570  rs.close();
571  } catch (SQLException ex) {
572  String err = "Could not close resultset, error was " + ex.getMessage();
573  MAPDLOGGER.error(err);
574  throw new RuntimeException(err);
575  }
576  }
577  if (stmt != null) {
578  try {
579  stmt.close();
580  } catch (SQLException ex) {
581  String err = "Could not close stmt, error was " + ex.getMessage();
582  MAPDLOGGER.error(err);
583  throw new RuntimeException(err);
584  }
585  }
586  }
587  return (tableId);
588  }
589 
590  private boolean isView(String tableName) {
591  Statement stmt;
592  ResultSet rs;
593  int viewFlag = 0;
594  try {
595  stmt = catConn.createStatement();
596  rs = stmt.executeQuery(String.format(
597  "SELECT isview FROM mapd_tables where name = '%s' COLLATE NOCASE;",
598  tableName));
599  while (rs.next()) {
600  viewFlag = rs.getInt("isview");
601  MAPDLOGGER.debug("viewFlag = " + viewFlag);
602  MAPDLOGGER.debug("");
603  }
604  rs.close();
605  stmt.close();
606  } catch (Exception e) {
607  String err = "error trying to read from mapd_views, error was " + e.getMessage();
608  MAPDLOGGER.error(err);
609  throw new RuntimeException(err);
610  }
611  return (viewFlag == 1);
612  }
613 
614  private String getViewSql(String tableName) {
615  String sqlText;
616  if (mapdPort == -1) {
617  // use sql
619  sqlText = getViewSqlViaSql(getTableId(tableName));
621  } else {
622  // use thrift direct to local server
623  try {
624  TProtocol protocol = null;
625 
626  TTransport transport =
627  sock_transport_properties.openClientTransport("localhost", mapdPort);
628  if (!transport.isOpen()) transport.open();
629  protocol = new TBinaryProtocol(transport);
630 
631  OmniSci.Client client = new OmniSci.Client(protocol);
632 
633  TTableDetails td = client.get_table_details(currentUser.getSession(), tableName);
634 
635  transport.close();
636 
637  sqlText = td.getView_sql();
638 
639  } catch (TTransportException ex) {
640  MAPDLOGGER.error(ex.toString());
641  throw new RuntimeException(ex.toString());
642  } catch (TOmniSciException ex) {
643  MAPDLOGGER.error(ex.toString());
644  throw new RuntimeException(ex.toString());
645  } catch (TException ex) {
646  MAPDLOGGER.error(ex.toString());
647  throw new RuntimeException(ex.toString());
648  }
649  }
650  /* return string without the sqlite's trailing semicolon */
651  if (sqlText.charAt(sqlText.length() - 1) == ';') {
652  return (sqlText.substring(0, sqlText.length() - 1));
653  } else {
654  return (sqlText);
655  }
656  }
657 
658  // we assume there is already a DB connection here
659  private String getViewSqlViaSql(int tableId) {
660  Statement stmt;
661  ResultSet rs;
662  String sqlText = "";
663  try {
664  stmt = catConn.createStatement();
665  rs = stmt.executeQuery(String.format(
666  "SELECT sql FROM mapd_views where tableid = '%s' COLLATE NOCASE;",
667  tableId));
668  while (rs.next()) {
669  sqlText = rs.getString("sql");
670  MAPDLOGGER.debug("View definition = " + sqlText);
671  MAPDLOGGER.debug("");
672  }
673  rs.close();
674  stmt.close();
675  } catch (Exception e) {
676  String err = "error trying to read from mapd_views, error was " + e.getMessage();
677  MAPDLOGGER.error(err);
678  throw new RuntimeException(err);
679  }
680  if (sqlText == null || sqlText.length() == 0) {
681  String err = "No view text found";
682  MAPDLOGGER.error(err);
683  throw new RuntimeException(err);
684  }
685  return sqlText;
686  }
687 
688  private TDatumType typeToThrift(int type) {
689  switch (type) {
690  case KBOOLEAN:
691  return TDatumType.BOOL;
692  case KTINYINT:
693  return TDatumType.TINYINT;
694  case KSMALLINT:
695  return TDatumType.SMALLINT;
696  case KINT:
697  return TDatumType.INT;
698  case KBIGINT:
699  return TDatumType.BIGINT;
700  case KFLOAT:
701  return TDatumType.FLOAT;
702  case KNUMERIC:
703  case KDECIMAL:
704  return TDatumType.DECIMAL;
705  case KDOUBLE:
706  return TDatumType.DOUBLE;
707  case KTEXT:
708  case KVARCHAR:
709  case KCHAR:
710  return TDatumType.STR;
711  case KTIME:
712  return TDatumType.TIME;
713  case KTIMESTAMP:
714  return TDatumType.TIMESTAMP;
715  case KDATE:
716  return TDatumType.DATE;
717  case KINTERVAL_DAY_TIME:
718  return TDatumType.INTERVAL_DAY_TIME;
720  return TDatumType.INTERVAL_YEAR_MONTH;
721  case KPOINT:
722  return TDatumType.POINT;
723  case KLINESTRING:
724  return TDatumType.LINESTRING;
725  case KPOLYGON:
726  return TDatumType.POLYGON;
727  case KMULTIPOLYGON:
728  return TDatumType.MULTIPOLYGON;
729  default:
730  return null;
731  }
732  }
733 
734  public void updateMetaData(String schema, String table) {
735  // Check if table is specified, if not we are dropping an entire DB so need to
736  // remove all
737  // tables for that DB
738  if (table.equals("")) {
739  // Drop db and all tables
740  // iterate through all and remove matching schema
741  Set<List<String>> all = new HashSet<>(MAPD_TABLE_DETAILS.keySet());
742  for (List<String> keys : all) {
743  if (keys.get(0).equals(schema.toUpperCase())) {
744  MAPDLOGGER.debug("removing schema " + keys.get(0) + " table " + keys.get(1));
745  MAPD_TABLE_DETAILS.remove(keys);
746  }
747  }
748  } else {
749  MAPDLOGGER.debug("removing schema " + schema.toUpperCase() + " table "
750  + table.toUpperCase());
751  MAPD_TABLE_DETAILS.remove(
752  ImmutableList.of(schema.toUpperCase(), table.toUpperCase()));
753  }
754  // Invalidate views
755  Set<List<String>> all = new HashSet<>(MAPD_TABLE_DETAILS.keySet());
756  for (List<String> keys : all) {
757  if (keys.get(0).equals(schema.toUpperCase())) {
758  Table ttable = MAPD_TABLE_DETAILS.get(keys);
759  if (ttable instanceof MapDView) {
760  MAPDLOGGER.debug(
761  "removing view in schema " + keys.get(0) + " view " + keys.get(1));
762  MAPD_TABLE_DETAILS.remove(keys);
763  }
764  }
765  }
766  // now remove schema
767  MAPDLOGGER.debug("removing schema " + schema.toUpperCase());
768  MAPD_DATABASE_TO_TABLES.remove(schema.toUpperCase());
769  }
770 }
Table getTable(String tableName)
static final int get_physical_cols(int type)
MetaConnect(int mapdPort, String dataDir, MapDUser currentMapDUser, MapDParser parser, SockTransportProperties skT)
static final int KMULTIPOLYGON
void updateMetaData(String schema, String table)
static volatile Map< List< String >, Table > MAPD_TABLE_DETAILS
String getViewSql(String tableName)
static final boolean is_geometry(int type)
String getViewSqlViaSql(int tableId)
TTableDetails get_table_details(String tableName)
static final int KINTERVAL_YEAR_MONTH
static final Logger MAPDLOGGER
final SockTransportProperties sock_transport_properties
static final int KLINESTRING
int64_t const int32_t sz assert(dest)
TTableDetails get_table_detail_SQL(String tableName)
TDatumType typeToThrift(int type)
int getTableId(String tableName)
TTableDetails get_table_detail_JSON(String tableName)
static final int KINTERVAL_DAY_TIME
boolean isView(String tableName)
static volatile Map< String, Set< String > > MAPD_DATABASE_TO_TABLES