OmniSciDB  0bd2ec9cf4
 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.mapd.thrift.server.MapD;
29 import com.mapd.thrift.server.TColumnType;
30 import com.mapd.thrift.server.TDatumType;
31 import com.mapd.thrift.server.TEncodingType;
32 import com.mapd.thrift.server.TMapDException;
33 import com.mapd.thrift.server.TTableDetails;
34 import com.mapd.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  MapD.Client client = new MapD.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 (TMapDException 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  MapD.Client client = new MapD.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 (TMapDException 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 
410  if (skip_physical_cols <= 0) skip_physical_cols = get_physical_cols(colType);
411  if (is_geometry(colType) || skip_physical_cols-- <= 0) td.addToRow_desc(tct);
412  }
413  } catch (Exception e) {
414  String err = "error trying to read from mapd_columns, error was " + e.getMessage();
415  MAPDLOGGER.error(err);
416  throw new RuntimeException(err);
417  } finally {
418  if (rs != null) {
419  try {
420  rs.close();
421  } catch (SQLException ex) {
422  String err = "Could not close resultset, error was " + ex.getMessage();
423  MAPDLOGGER.error(err);
424  throw new RuntimeException(err);
425  }
426  }
427  if (stmt != null) {
428  try {
429  stmt.close();
430  } catch (SQLException ex) {
431  String err = "Could not close stmt, error was " + ex.getMessage();
432  MAPDLOGGER.error(err);
433  throw new RuntimeException(err);
434  }
435  }
436  }
437  if (isView(tableName)) {
438  td.setView_sqlIsSet(true);
439  td.setView_sql(getViewSqlViaSql(id));
440  }
441  return td;
442  }
443 
444  private TTableDetails get_table_detail_JSON(String tableName)
445  throws IOException, RuntimeException {
446  TTableDetails td = new TTableDetails();
447  td.getRow_descIterator();
448 
449  // open table json file
450  final String filePath = dataDir + "/mapd_catalogs/" + db + "_temp_tables.json";
451  MAPDLOGGER.debug("Opening temp table file at " + filePath);
452 
453  String tempTablesJsonStr;
454  try {
455  File tempTablesFile = new File(filePath);
456  FileInputStream tempTablesStream = new FileInputStream(tempTablesFile);
457  byte[] data = new byte[(int) tempTablesFile.length()];
458  tempTablesStream.read(data);
459  tempTablesStream.close();
460 
461  tempTablesJsonStr = new String(data, "UTF-8");
462  } catch (java.io.FileNotFoundException e) {
463  throw new RuntimeException("Failed to read temporary tables file.");
464  }
465 
466  Gson gson = new Gson();
467  JsonObject fileParentObject = gson.fromJson(tempTablesJsonStr, JsonObject.class);
468  if (fileParentObject == null) {
469  throw new IOException("Malformed temporary tables file.");
470  }
471 
472  JsonObject tableObject = fileParentObject.getAsJsonObject(tableName);
473  if (tableObject == null) {
474  throw new RuntimeException(
475  "Failed to find table " + tableName + " in temporary tables file.");
476  }
477 
478  String jsonTableName = tableObject.get("name").getAsString();
479  assert (tableName == jsonTableName);
480  int id = tableObject.get("id").getAsInt();
481  MAPDLOGGER.debug("table id is " + id);
482  MAPDLOGGER.debug("table name is " + tableName);
483 
484  JsonArray jsonColumns = tableObject.getAsJsonArray("columns");
485  assert (jsonColumns != null);
486 
487  int skip_physical_cols = 0;
488  for (JsonElement columnElement : jsonColumns) {
489  JsonObject columnObject = columnElement.getAsJsonObject();
490 
491  String colName = columnObject.get("name").getAsString();
492  MAPDLOGGER.debug("name = " + colName);
493  int colType = columnObject.get("coltype").getAsInt();
494  MAPDLOGGER.debug("coltype = " + colType);
495  int colSubType = columnObject.get("colsubtype").getAsInt();
496  MAPDLOGGER.debug("colsubtype = " + colSubType);
497  int colDim = columnObject.get("coldim").getAsInt();
498  MAPDLOGGER.debug("coldim = " + colDim);
499  int colScale = columnObject.get("colscale").getAsInt();
500  MAPDLOGGER.debug("colscale = " + colScale);
501  boolean isNotNull = columnObject.get("is_notnull").getAsBoolean();
502  MAPDLOGGER.debug("is_notnull = " + isNotNull);
503  boolean isSystemCol = columnObject.get("is_systemcol").getAsBoolean();
504  MAPDLOGGER.debug("is_systemcol = " + isSystemCol);
505  boolean isVirtualCol = columnObject.get("is_virtualcol").getAsBoolean();
506  MAPDLOGGER.debug("is_vitrualcol = " + isVirtualCol);
507  boolean isDeletedCol = columnObject.get("is_deletedcol").getAsBoolean();
508  MAPDLOGGER.debug("is_deletedcol = " + isDeletedCol);
509  MAPDLOGGER.debug("");
510 
511  if (isDeletedCol) {
512  MAPDLOGGER.debug("Skipping delete column.");
513  continue;
514  }
515 
516  TColumnType tct = new TColumnType();
517  TTypeInfo tti = new TTypeInfo();
518  TDatumType tdt;
519 
520  if (colType == KARRAY) {
521  tti.is_array = true;
522  tdt = typeToThrift(colSubType);
523  } else {
524  tti.is_array = false;
525  tdt = typeToThrift(colType);
526  }
527 
528  tti.nullable = !isNotNull;
529  tti.encoding = TEncodingType.NONE;
530  tti.type = tdt;
531  tti.scale = colScale;
532  tti.precision = colDim;
533 
534  tct.col_name = colName;
535  tct.col_type = tti;
536 
537  if (skip_physical_cols <= 0) skip_physical_cols = get_physical_cols(colType);
538  if (is_geometry(colType) || skip_physical_cols-- <= 0) td.addToRow_desc(tct);
539  }
540 
541  return td;
542  }
543 
544  private int getTableId(String tableName) {
545  Statement stmt = null;
546  ResultSet rs = null;
547  int tableId = -1;
548  try {
549  stmt = catConn.createStatement();
550  rs = stmt.executeQuery(String.format(
551  "SELECT tableid FROM mapd_tables where name = '%s' COLLATE NOCASE;",
552  tableName));
553  while (rs.next()) {
554  tableId = rs.getInt("tableid");
555  MAPDLOGGER.debug("tableId = " + tableId);
556  MAPDLOGGER.debug("");
557  }
558  rs.close();
559  stmt.close();
560  } catch (Exception e) {
561  String err = "Error trying to read from metadata table mapd_tables;DB: " + db
562  + " data dir " + dataDir + ", error was " + e.getMessage();
563  MAPDLOGGER.error(err);
564  throw new RuntimeException(err);
565  } finally {
566  if (rs != null) {
567  try {
568  rs.close();
569  } catch (SQLException ex) {
570  String err = "Could not close resultset, error was " + ex.getMessage();
571  MAPDLOGGER.error(err);
572  throw new RuntimeException(err);
573  }
574  }
575  if (stmt != null) {
576  try {
577  stmt.close();
578  } catch (SQLException ex) {
579  String err = "Could not close stmt, error was " + ex.getMessage();
580  MAPDLOGGER.error(err);
581  throw new RuntimeException(err);
582  }
583  }
584  }
585  return (tableId);
586  }
587 
588  private boolean isView(String tableName) {
589  Statement stmt;
590  ResultSet rs;
591  int viewFlag = 0;
592  try {
593  stmt = catConn.createStatement();
594  rs = stmt.executeQuery(String.format(
595  "SELECT isview FROM mapd_tables where name = '%s' COLLATE NOCASE;",
596  tableName));
597  while (rs.next()) {
598  viewFlag = rs.getInt("isview");
599  MAPDLOGGER.debug("viewFlag = " + viewFlag);
600  MAPDLOGGER.debug("");
601  }
602  rs.close();
603  stmt.close();
604  } catch (Exception e) {
605  String err = "error trying to read from mapd_views, error was " + e.getMessage();
606  MAPDLOGGER.error(err);
607  throw new RuntimeException(err);
608  }
609  return (viewFlag == 1);
610  }
611 
612  private String getViewSql(String tableName) {
613  String sqlText;
614  if (mapdPort == -1) {
615  // use sql
617  sqlText = getViewSqlViaSql(getTableId(tableName));
619  } else {
620  // use thrift direct to local server
621  try {
622  TProtocol protocol = null;
623 
624  TTransport transport =
625  sock_transport_properties.openClientTransport("localhost", mapdPort);
626  if (!transport.isOpen()) transport.open();
627  protocol = new TBinaryProtocol(transport);
628 
629  MapD.Client client = new MapD.Client(protocol);
630 
631  TTableDetails td = client.get_table_details(currentUser.getSession(), tableName);
632 
633  transport.close();
634 
635  sqlText = td.getView_sql();
636 
637  } catch (TTransportException ex) {
638  MAPDLOGGER.error(ex.toString());
639  throw new RuntimeException(ex.toString());
640  } catch (TMapDException ex) {
641  MAPDLOGGER.error(ex.toString());
642  throw new RuntimeException(ex.toString());
643  } catch (TException ex) {
644  MAPDLOGGER.error(ex.toString());
645  throw new RuntimeException(ex.toString());
646  }
647  }
648  /* return string without the sqlite's trailing semicolon */
649  if (sqlText.charAt(sqlText.length() - 1) == ';') {
650  return (sqlText.substring(0, sqlText.length() - 1));
651  } else {
652  return (sqlText);
653  }
654  }
655 
656  // we assume there is already a DB connection here
657  private String getViewSqlViaSql(int tableId) {
658  Statement stmt;
659  ResultSet rs;
660  String sqlText = "";
661  try {
662  stmt = catConn.createStatement();
663  rs = stmt.executeQuery(String.format(
664  "SELECT sql FROM mapd_views where tableid = '%s' COLLATE NOCASE;",
665  tableId));
666  while (rs.next()) {
667  sqlText = rs.getString("sql");
668  MAPDLOGGER.debug("View definition = " + sqlText);
669  MAPDLOGGER.debug("");
670  }
671  rs.close();
672  stmt.close();
673  } catch (Exception e) {
674  String err = "error trying to read from mapd_views, error was " + e.getMessage();
675  MAPDLOGGER.error(err);
676  throw new RuntimeException(err);
677  }
678  if (sqlText == null || sqlText.length() == 0) {
679  String err = "No view text found";
680  MAPDLOGGER.error(err);
681  throw new RuntimeException(err);
682  }
683  return sqlText;
684  }
685 
686  private TDatumType typeToThrift(int type) {
687  switch (type) {
688  case KBOOLEAN:
689  return TDatumType.BOOL;
690  case KTINYINT:
691  return TDatumType.TINYINT;
692  case KSMALLINT:
693  return TDatumType.SMALLINT;
694  case KINT:
695  return TDatumType.INT;
696  case KBIGINT:
697  return TDatumType.BIGINT;
698  case KFLOAT:
699  return TDatumType.FLOAT;
700  case KNUMERIC:
701  case KDECIMAL:
702  return TDatumType.DECIMAL;
703  case KDOUBLE:
704  return TDatumType.DOUBLE;
705  case KTEXT:
706  case KVARCHAR:
707  case KCHAR:
708  return TDatumType.STR;
709  case KTIME:
710  return TDatumType.TIME;
711  case KTIMESTAMP:
712  return TDatumType.TIMESTAMP;
713  case KDATE:
714  return TDatumType.DATE;
715  case KINTERVAL_DAY_TIME:
716  return TDatumType.INTERVAL_DAY_TIME;
718  return TDatumType.INTERVAL_YEAR_MONTH;
719  case KPOINT:
720  return TDatumType.POINT;
721  case KLINESTRING:
722  return TDatumType.LINESTRING;
723  case KPOLYGON:
724  return TDatumType.POLYGON;
725  case KMULTIPOLYGON:
726  return TDatumType.MULTIPOLYGON;
727  default:
728  return null;
729  }
730  }
731 
732  public void updateMetaData(String schema, String table) {
733  // Check if table is specified, if not we are dropping an entire DB so need to
734  // remove all
735  // tables for that DB
736  if (table.equals("")) {
737  // Drop db and all tables
738  // iterate through all and remove matching schema
739  Set<List<String>> all = new HashSet<>(MAPD_TABLE_DETAILS.keySet());
740  for (List<String> keys : all) {
741  if (keys.get(0).equals(schema.toUpperCase())) {
742  MAPDLOGGER.debug("removing schema " + keys.get(0) + " table " + keys.get(1));
743  MAPD_TABLE_DETAILS.remove(keys);
744  }
745  }
746  } else {
747  MAPDLOGGER.debug("removing schema " + schema.toUpperCase() + " table "
748  + table.toUpperCase());
749  MAPD_TABLE_DETAILS.remove(
750  ImmutableList.of(schema.toUpperCase(), table.toUpperCase()));
751  }
752  // Invalidate views
753  Set<List<String>> all = new HashSet<>(MAPD_TABLE_DETAILS.keySet());
754  for (List<String> keys : all) {
755  if (keys.get(0).equals(schema.toUpperCase())) {
756  Table ttable = MAPD_TABLE_DETAILS.get(keys);
757  if (ttable instanceof MapDView) {
758  MAPDLOGGER.debug(
759  "removing view in schema " + keys.get(0) + " view " + keys.get(1));
760  MAPD_TABLE_DETAILS.remove(keys);
761  }
762  }
763  }
764  // now remove schema
765  MAPDLOGGER.debug("removing schema " + schema.toUpperCase());
766  MAPD_DATABASE_TO_TABLES.remove(schema.toUpperCase());
767  }
768 }
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