OmniSciDB  16c4e035a1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Calcite.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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 /*
18  * File: Calcite.cpp
19  * Author: michael
20  *
21  * Created on November 23, 2015, 9:33 AM
22  */
23 
24 #include "Calcite.h"
25 #include "Catalog/Catalog.h"
26 #include "Logger/Logger.h"
29 #include "Shared/ThriftClient.h"
30 #include "Shared/fixautotools.h"
31 #include "Shared/measure.h"
33 
34 #include <thrift/protocol/TBinaryProtocol.h>
35 #include <thrift/transport/TSocket.h>
36 #include <thrift/transport/TTransportUtils.h>
37 #include <type_traits>
38 
39 #ifdef _MSC_VER
40 #include <process.h>
41 #endif
42 
43 #include "gen-cpp/CalciteServer.h"
44 
45 #include "rapidjson/document.h"
46 
47 #include <utility>
48 
49 using namespace rapidjson;
50 using namespace apache::thrift;
51 using namespace apache::thrift::protocol;
52 using namespace apache::thrift::transport;
53 
54 namespace {
55 template <typename XDEBUG_OPTION,
56  typename REMOTE_DEBUG_OPTION,
57  typename... REMAINING_ARGS>
58 int wrapped_execlp(char const* path,
59  XDEBUG_OPTION&& x_debug,
60  REMOTE_DEBUG_OPTION&& remote_debug,
61  REMAINING_ARGS&&... standard_args) {
62 #ifdef ENABLE_JAVA_REMOTE_DEBUG
63  return execlp(
64  path, x_debug, remote_debug, std::forward<REMAINING_ARGS>(standard_args)...);
65 #else
66  return execlp(path, std::forward<REMAINING_ARGS>(standard_args)...);
67 #endif
68 }
69 } // namespace
70 
71 static void start_calcite_server_as_daemon(const int db_port,
72  const int port,
73  const std::string& data_dir,
74  const size_t calcite_max_mem,
75  const std::string& ssl_trust_store,
76  const std::string& ssl_trust_password_X,
77  const std::string& ssl_keystore,
78  const std::string& ssl_keystore_password_X,
79  const std::string& ssl_key_file,
80  const std::string& db_config_file,
81  const std::string& udf_filename) {
82  auto root_abs_path = omnisci::get_root_abs_path();
83  std::string const xDebug = "-Xdebug";
84  std::string const remoteDebug =
85  "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005";
86  std::string xmxP = "-Xmx" + std::to_string(calcite_max_mem) + "m";
87  std::string jarP = "-jar";
88  std::string jarD =
89  root_abs_path + "/bin/calcite-1.0-SNAPSHOT-jar-with-dependencies.jar";
90  std::string extensionsP = "-e";
91  std::string extensionsD = root_abs_path + "/QueryEngine/";
92  std::string dataP = "-d";
93  std::string dataD = data_dir;
94  std::string localPortP = "-p";
95  std::string localPortD = std::to_string(port);
96  std::string dbPortP = "-m";
97  std::string dbPortD = std::to_string(db_port);
98  std::string TrustStoreP = "-T";
99  std::string TrustPasswdP = "-P";
100  std::string ConfigFileP = "-c";
101  std::string KeyStoreP = "-Y";
102  std::string KeyStorePasswdP = "-Z";
103  // FIXME: this path should be getting pulled from logger rather than hardcoded
104  std::string logDirectory = "-DMAPD_LOG_DIR=" + data_dir + "/mapd_log/";
105  std::string userDefinedFunctionsP = "";
106  std::string userDefinedFunctionsD = "";
107 
108  if (!udf_filename.empty()) {
109  userDefinedFunctionsP += "-u";
110  userDefinedFunctionsD += udf_filename;
111  }
112 
113  // If a config file hasn't been supplied then put the password in the params
114  // otherwise send an empty string and Calcite should get it from the config file.
115  std::string key_store_password = (db_config_file == "") ? ssl_keystore_password_X : "";
116  std::string trust_store_password = (db_config_file == "") ? ssl_trust_password_X : "";
117 #ifdef _MSC_VER
118  // TODO: enable UDF support
119  std::vector<std::string> args_vec;
120  args_vec.push_back("java");
121  args_vec.push_back(xDebug);
122  args_vec.push_back(remoteDebug);
123  args_vec.push_back(xmxP);
124  args_vec.push_back(logDirectory);
125  args_vec.push_back(jarP);
126  args_vec.push_back(jarD);
127  args_vec.push_back(extensionsP);
128  args_vec.push_back(extensionsD);
129  args_vec.push_back(dataP);
130  args_vec.push_back(dataD);
131  args_vec.push_back(localPortP);
132  args_vec.push_back(localPortD);
133  args_vec.push_back(dbPortP);
134  args_vec.push_back(dbPortD);
135  if (!ssl_trust_store.empty()) {
136  args_vec.push_back(TrustStoreP);
137  args_vec.push_back(ssl_trust_store);
138  }
139  if (!trust_store_password.empty()) {
140  args_vec.push_back(TrustPasswdP);
141  args_vec.push_back(trust_store_password);
142  }
143  if (!ssl_keystore.empty()) {
144  args_vec.push_back(KeyStoreP);
145  args_vec.push_back(ssl_keystore);
146  }
147  if (!key_store_password.empty()) {
148  args_vec.push_back(KeyStorePasswdP);
149  args_vec.push_back(key_store_password);
150  }
151  if (!db_config_file.empty()) {
152  args_vec.push_back(ConfigFileP);
153  args_vec.push_back(db_config_file);
154  }
155  std::string args{boost::algorithm::join(args_vec, " ")};
156  STARTUPINFO startup_info;
157  PROCESS_INFORMATION proc_info;
158  ZeroMemory(&startup_info, sizeof(startup_info));
159  startup_info.cb = sizeof(startup_info);
160  ZeroMemory(&proc_info, sizeof(proc_info));
161  LOG(INFO) << "Startup command: " << args;
162  std::wstring wargs = std::wstring(args.begin(), args.end());
163  const auto ret = CreateProcess(NULL,
164  (LPWSTR)wargs.c_str(),
165  NULL,
166  NULL,
167  false,
168  0,
169  NULL,
170  NULL,
171  &startup_info,
172  &proc_info);
173  if (ret == 0) {
174  LOG(FATAL) << "Failed to start Calcite server " << GetLastError();
175  }
176 #else
177  int pid = fork();
178  if (pid == 0) {
179  int i;
180 
181  if (udf_filename.empty()) {
182  i = wrapped_execlp("java",
183  xDebug.c_str(),
184  remoteDebug.c_str(),
185  xmxP.c_str(),
186  logDirectory.c_str(),
187  jarP.c_str(),
188  jarD.c_str(),
189  extensionsP.c_str(),
190  extensionsD.c_str(),
191  dataP.c_str(),
192  dataD.c_str(),
193  localPortP.c_str(),
194  localPortD.c_str(),
195  dbPortP.c_str(),
196  dbPortD.c_str(),
197  TrustStoreP.c_str(),
198  ssl_trust_store.c_str(),
199  TrustPasswdP.c_str(),
200  trust_store_password.c_str(),
201  KeyStoreP.c_str(),
202  ssl_keystore.c_str(),
203  KeyStorePasswdP.c_str(),
204  key_store_password.c_str(),
205  ConfigFileP.c_str(),
206  db_config_file.c_str(),
207  (char*)0);
208  } else {
209  i = wrapped_execlp("java",
210  xDebug.c_str(),
211  remoteDebug.c_str(),
212  xmxP.c_str(),
213  logDirectory.c_str(),
214  jarP.c_str(),
215  jarD.c_str(),
216  extensionsP.c_str(),
217  extensionsD.c_str(),
218  dataP.c_str(),
219  dataD.c_str(),
220  localPortP.c_str(),
221  localPortD.c_str(),
222  dbPortP.c_str(),
223  dbPortD.c_str(),
224  TrustStoreP.c_str(),
225  ssl_trust_store.c_str(),
226  TrustPasswdP.c_str(),
227  trust_store_password.c_str(),
228  KeyStoreP.c_str(),
229  ssl_keystore.c_str(),
230  KeyStorePasswdP.c_str(),
231  key_store_password.c_str(),
232  ConfigFileP.c_str(),
233  db_config_file.c_str(),
234  userDefinedFunctionsP.c_str(),
235  userDefinedFunctionsD.c_str(),
236  (char*)0);
237  }
238 
239  if (i) {
240  int errsv = errno;
241  LOG(FATAL) << "Failed to start Calcite server [errno=" << errsv
242  << "]: " << strerror(errsv);
243  } else {
244  LOG(INFO) << "Successfully started Calcite server";
245  }
246  }
247 #endif
248 }
249 
250 std::pair<std::shared_ptr<CalciteServerClient>, std::shared_ptr<TTransport>>
252  const auto transport = connMgr_->open_buffered_client_transport("localhost",
253  port,
254  ssl_ca_file_,
255  true,
256  service_keepalive_,
257  service_timeout_,
258  service_timeout_,
259  service_timeout_);
260  try {
261  transport->open();
262 
263  } catch (TException& tx) {
264  throw tx;
265  } catch (std::exception& ex) {
266  throw ex;
267  }
268  std::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
269  std::shared_ptr<CalciteServerClient> client;
270  client.reset(new CalciteServerClient(protocol));
271  std::pair<std::shared_ptr<CalciteServerClient>, std::shared_ptr<TTransport>> ret;
272  return std::make_pair(client, transport);
273 }
274 
275 void Calcite::runServer(const int db_port,
276  const int port,
277  const std::string& data_dir,
278  const size_t calcite_max_mem,
279  const std::string& udf_filename) {
280  LOG(INFO) << "Running Calcite server as a daemon";
281 
282  // ping server to see if for any reason there is an orphaned one
283  int ping_time = ping();
284  if (ping_time > -1) {
285  // we have an orphaned server shut it down
286  LOG(ERROR)
287  << "Appears to be orphaned Calcite server already running, shutting it down";
288  LOG(ERROR) << "Please check that you are not trying to run two servers on same port";
289  LOG(ERROR) << "Attempting to shutdown orphaned Calcite server";
290  try {
291  auto clientP = getClient(remote_calcite_port_);
292  clientP.first->shutdown();
293  clientP.second->close();
294  LOG(ERROR) << "orphaned Calcite server shutdown";
295 
296  } catch (TException& tx) {
297  LOG(ERROR) << "Failed to shutdown orphaned Calcite server, reason: " << tx.what();
298  }
299  }
300 
301  // start the calcite server as a seperate process
303  port,
304  data_dir,
305  calcite_max_mem,
306  ssl_trust_store_,
307  ssl_trust_password_,
308  ssl_keystore_,
309  ssl_keystore_password_,
310  ssl_key_file_,
311  db_config_file_,
312  udf_filename);
313 
314  // check for new server for 30 seconds max
315  std::this_thread::sleep_for(std::chrono::milliseconds(200));
316  int retry_max = 300;
317  for (int i = 2; i <= retry_max; i++) {
318  int ping_time = ping(i, retry_max);
319  if (ping_time > -1) {
320  LOG(INFO) << "Calcite server start took " << i * 100 << " ms ";
321  LOG(INFO) << "ping took " << ping_time << " ms ";
322  server_available_ = true;
323  return;
324  } else {
325  // wait 100 ms
326  std::this_thread::sleep_for(std::chrono::milliseconds(100));
327  }
328  }
329  server_available_ = false;
330  LOG(FATAL) << "Could not connect to Calcite remote server running on port [" << port
331  << "]";
332 }
333 
334 // ping existing server
335 // return -1 if no ping response
336 // params set to default values in header
337 int Calcite::ping(int retry_num, int max_retry) {
338  try {
339  auto ms = measure<>::execution([&]() {
340  auto clientP = getClient(remote_calcite_port_);
341  clientP.first->ping();
342  clientP.second->close();
343  });
344  return ms;
345 
346  } catch (TException& tx) {
347  if (retry_num >= max_retry) {
348  LOG(ERROR) << "Problems connecting to Calcite. Thrift error - " << tx.what();
349  }
350  return -1;
351  }
352 }
353 
354 Calcite::Calcite(const int db_port,
355  const int calcite_port,
356  const std::string& data_dir,
357  const size_t calcite_max_mem,
358  const size_t service_timeout,
359  const bool service_keepalive,
360  const std::string& udf_filename)
361  : server_available_(false)
362  , service_timeout_(service_timeout)
363  , service_keepalive_(service_keepalive) {
364  init(db_port, calcite_port, data_dir, calcite_max_mem, udf_filename);
365 }
366 
367 void Calcite::init(const int db_port,
368  const int calcite_port,
369  const std::string& data_dir,
370  const size_t calcite_max_mem,
371  const std::string& udf_filename) {
372  LOG(INFO) << "Creating Calcite Handler, Calcite Port is " << calcite_port
373  << " base data dir is " << data_dir;
374  connMgr_ = std::make_shared<ThriftClientConnection>();
375  if (calcite_port < 0) {
376  CHECK(false) << "JNI mode no longer supported.";
377  }
378  if (calcite_port == 0) {
379  // dummy process for initdb
380  remote_calcite_port_ = calcite_port;
381  server_available_ = false;
382  } else {
383  remote_calcite_port_ = calcite_port;
384  runServer(db_port, calcite_port, data_dir, calcite_max_mem, udf_filename);
385  server_available_ = true;
386  }
387 }
388 
389 Calcite::Calcite(const SystemParameters& system_parameters,
390  const std::string& data_dir,
391  const std::string& udf_filename)
392  : service_timeout_(system_parameters.calcite_timeout)
393  , service_keepalive_(system_parameters.calcite_keepalive)
394  , ssl_trust_store_(system_parameters.ssl_trust_store)
395  , ssl_trust_password_(system_parameters.ssl_trust_password)
396  , ssl_key_file_(system_parameters.ssl_key_file)
397  , ssl_keystore_(system_parameters.ssl_keystore)
398  , ssl_keystore_password_(system_parameters.ssl_keystore_password)
399  , ssl_ca_file_(system_parameters.ssl_trust_ca_file)
400  , db_config_file_(system_parameters.config_file) {
401  init(system_parameters.omnisci_server_port,
402  system_parameters.calcite_port,
403  data_dir,
404  system_parameters.calcite_max_mem,
405  udf_filename);
406 }
407 
408 void Calcite::updateMetadata(std::string catalog, std::string table) {
409  if (server_available_) {
410  auto ms = measure<>::execution([&]() {
411  auto clientP = getClient(remote_calcite_port_);
412  clientP.first->updateMetadata(catalog, table);
413  clientP.second->close();
414  });
415  LOG(INFO) << "Time to updateMetadata " << ms << " (ms)";
416  } else {
417  LOG(INFO) << "Not routing to Calcite, server is not up";
418  }
419 }
420 
422  std::vector<std::vector<std::string>> tableOrViewNames,
423  AccessPrivileges tablePrivs,
424  AccessPrivileges viewPrivs) {
425  // TODO MAT this needs to be able to check privileges from other catalogs
426  Catalog_Namespace::Catalog& catalog = session_info.getCatalog();
427 
428  for (auto tableOrViewName : tableOrViewNames) {
429  const TableDescriptor* tableMeta =
430  catalog.getMetadataForTable(tableOrViewName[0], false);
431 
432  if (!tableMeta) {
433  throw std::runtime_error("unknown table of view: " + tableOrViewName[0]);
434  }
435 
436  DBObjectKey key;
437  key.dbId = catalog.getCurrentDB().dbId;
440  key.objectId = tableMeta->tableId;
441  AccessPrivileges privs = tableMeta->isView ? viewPrivs : tablePrivs;
442  DBObject dbobject(key, privs, tableMeta->userId);
443  std::vector<DBObject> privObjects{dbobject};
444 
445  if (!privs.hasAny()) {
446  throw std::runtime_error("Operation not supported for object " +
447  tableOrViewName[0]);
448  }
449 
450  if (!Catalog_Namespace::SysCatalog::instance().checkPrivileges(
451  session_info.get_currentUser(), privObjects)) {
452  throw std::runtime_error("Violation of access privileges: user " +
453  session_info.get_currentUser().userLoggable() +
454  " has no proper privileges for object " +
455  tableOrViewName[0]);
456  }
457  }
458 }
459 
460 TPlanResult Calcite::process(query_state::QueryStateProxy query_state_proxy,
461  std::string sql_string,
462  const TQueryParsingOption& query_parsing_option,
463  const TOptimizationOption& optimization_option,
464  const std::string& calcite_session_id) {
465  TPlanResult result = processImpl(query_state_proxy,
466  std::move(sql_string),
467  query_parsing_option,
468  optimization_option,
469  calcite_session_id);
470  if (query_parsing_option.check_privileges && !query_parsing_option.is_explain) {
471  checkAccessedObjectsPrivileges(query_state_proxy, result);
472  }
473  return result;
474 }
475 
477  query_state::QueryStateProxy query_state_proxy,
478  TPlanResult plan) const {
479  AccessPrivileges NOOP;
480  // check the individual tables
481  auto const session_ptr = query_state_proxy.getQueryState().getConstSessionInfo();
482  checkPermissionForTables(*session_ptr,
483  plan.primary_accessed_objects.tables_selected_from,
486  checkPermissionForTables(*session_ptr,
487  plan.primary_accessed_objects.tables_inserted_into,
489  NOOP);
490  checkPermissionForTables(*session_ptr,
491  plan.primary_accessed_objects.tables_updated_in,
493  NOOP);
494  checkPermissionForTables(*session_ptr,
495  plan.primary_accessed_objects.tables_deleted_from,
497  NOOP);
498 }
499 
500 std::vector<TCompletionHint> Calcite::getCompletionHints(
501  const Catalog_Namespace::SessionInfo& session_info,
502  const std::vector<std::string>& visible_tables,
503  const std::string sql_string,
504  const int cursor) {
505  std::vector<TCompletionHint> hints;
506  auto& cat = session_info.getCatalog();
507  const auto user = session_info.get_currentUser().userName;
508  const auto session = session_info.get_session_id();
509  const auto catalog = cat.getCurrentDB().dbName;
510  auto client = getClient(remote_calcite_port_);
511  client.first->getCompletionHints(
512  hints, user, session, catalog, visible_tables, sql_string, cursor);
513  return hints;
514 }
515 
516 std::vector<std::string> Calcite::get_db_objects(const std::string ra) {
517  std::vector<std::string> v_db_obj;
518  Document document;
519  document.Parse(ra.c_str());
520  const Value& rels = document["rels"];
521  CHECK(rels.IsArray());
522  for (auto& v : rels.GetArray()) {
523  std::string relOp(v["relOp"].GetString());
524  if (!relOp.compare("EnumerableTableScan")) {
525  std::string x;
526  auto t = v["table"].GetArray();
527  x = t[1].GetString();
528  v_db_obj.push_back(x);
529  }
530  }
531 
532  return v_db_obj;
533 }
534 
536  const std::string sql_string,
537  const TQueryParsingOption& query_parsing_option,
538  const TOptimizationOption& optimization_option,
539  const std::string& calcite_session_id) {
540  query_state::Timer timer = query_state_proxy.createTimer(__func__);
541  const auto& user_session_info = query_state_proxy.getQueryState().getConstSessionInfo();
542  const auto& cat = user_session_info->getCatalog();
543  const std::string user = getInternalSessionProxyUserName();
544  const std::string catalog = cat.getCurrentDB().dbName;
545  LOG(INFO) << "User " << user << " catalog " << catalog << " sql '" << sql_string << "'";
546  LOG(IR) << "SQL query\n" << sql_string << "\nEnd of SQL query";
547  LOG(PTX) << "SQL query\n" << sql_string << "\nEnd of SQL query";
548 
549  std::vector<TRestriction> trestrictions;
550 
551  TPlanResult ret;
552  if (server_available_) {
553  try {
554  // calcite_session_id would be an empty string when accessed by internal resources
555  // that would not access `process` through handler instance, like for eg: Unit
556  // Tests. In these cases we would use the session_id from query state.
557  auto ms = measure<>::execution([&]() {
558  auto clientP = getClient(remote_calcite_port_);
559  clientP.first->process(ret,
560  user,
561  calcite_session_id.empty()
562  ? user_session_info->get_session_id()
563  : calcite_session_id,
564  catalog,
565  sql_string,
566  query_parsing_option,
567  optimization_option,
568  trestrictions);
569  clientP.second->close();
570  });
571 
572  // LOG(INFO) << ret.plan_result;
573  LOG(INFO) << "Time in Thrift "
574  << (ms > ret.execution_time_ms ? ms - ret.execution_time_ms : 0)
575  << " (ms), Time in Java Calcite server " << ret.execution_time_ms
576  << " (ms)";
577  } catch (InvalidParseRequest& e) {
578  throw std::invalid_argument(e.whyUp);
579  } catch (const std::exception& ex) {
580  LOG(FATAL)
581  << "Error occurred trying to communicate with Calcite server, the error was: '"
582  << ex.what() << "', omnisci_server restart will be required";
583  return ret; // satisfy return-type warning
584  }
585  } else {
586  LOG(FATAL) << "Not routing to Calcite, server is not up";
587  }
588  return ret;
589 }
590 
592  if (server_available_) {
593  TPlanResult ret;
594  std::string whitelist;
595 
596  auto clientP = getClient(remote_calcite_port_);
597  clientP.first->getExtensionFunctionWhitelist(whitelist);
598  clientP.second->close();
599  VLOG(1) << whitelist;
600  return whitelist;
601  } else {
602  LOG(FATAL) << "Not routing to Calcite, server is not up";
603  return "";
604  }
605  CHECK(false);
606  return "";
607 }
608 
610  if (server_available_) {
611  TPlanResult ret;
612  std::string whitelist;
613 
614  auto clientP = getClient(remote_calcite_port_);
615  clientP.first->getUserDefinedFunctionWhitelist(whitelist);
616  clientP.second->close();
617  VLOG(1) << "User defined functions whitelist loaded from Calcite: " << whitelist;
618  return whitelist;
619  } else {
620  LOG(FATAL) << "Not routing to Calcite, server is not up";
621  return "";
622  }
623  UNREACHABLE();
624  return "";
625 }
626 
628  std::call_once(shutdown_once_flag_,
629  [this, log]() { this->inner_close_calcite_server(log); });
630 }
631 
633  if (server_available_) {
634  LOG_IF(INFO, log) << "Shutting down Calcite server";
635  try {
636  auto clientP = getClient(remote_calcite_port_);
637  clientP.first->shutdown();
638  clientP.second->close();
639  } catch (const std::exception& e) {
640  if (std::string(e.what()) != "connect() failed: Connection refused" &&
641  std::string(e.what()) != "socket open() error: Connection refused" &&
642  std::string(e.what()) != "No more data to read.") {
643  std::cerr << "Error shutting down Calcite server: " << e.what() << std::endl;
644  } // else Calcite already shut down
645  }
646  LOG_IF(INFO, log) << "shut down Calcite";
647  server_available_ = false;
648  }
649 }
650 
652  close_calcite_server(false);
653 }
654 
656  if (server_available_) {
657  TPlanResult ret;
658  std::string whitelist;
659  auto clientP = getClient(remote_calcite_port_);
660  clientP.first->getRuntimeExtensionFunctionWhitelist(whitelist);
661  clientP.second->close();
662  VLOG(1) << "Runtime extension functions whitelist loaded from Calcite: " << whitelist;
663  return whitelist;
664  } else {
665  LOG(FATAL) << "Not routing to Calcite, server is not up";
666  return "";
667  }
668  UNREACHABLE();
669  return "";
670 }
671 
673  const std::vector<TUserDefinedFunction>& udfs,
674  const std::vector<TUserDefinedTableFunction>& udtfs,
675  bool isruntime) {
676  if (server_available_) {
677  auto clientP = getClient(remote_calcite_port_);
678  clientP.first->setRuntimeExtensionFunctions(udfs, udtfs, isruntime);
679  clientP.second->close();
680  } else {
681  LOG(FATAL) << "Not routing to Calcite, server is not up";
682  }
683 }
684 
685 TQueryParsingOption Calcite::getCalciteQueryParsingOption(bool legacy_syntax,
686  bool is_explain,
687  bool check_privileges) {
688  TQueryParsingOption query_parsing_info;
689  query_parsing_info.legacy_syntax = legacy_syntax;
690  query_parsing_info.is_explain = is_explain;
691  query_parsing_info.check_privileges = check_privileges;
692  return query_parsing_info;
693 }
694 
696  bool is_view_optimize,
697  bool enable_watchdog,
698  const std::vector<TFilterPushDownInfo>& filter_push_down_info) {
699  TOptimizationOption optimization_option;
700  optimization_option.filter_push_down_info = filter_push_down_info;
701  optimization_option.is_view_optimize = is_view_optimize;
702  optimization_option.enable_watchdog = enable_watchdog;
703  return optimization_option;
704 }
std::vector< std::string > get_db_objects(const std::string ra)
Definition: Calcite.cpp:516
int wrapped_execlp(char const *path, XDEBUG_OPTION &&x_debug, REMOTE_DEBUG_OPTION &&remote_debug, REMAINING_ARGS &&...standard_args)
Definition: Calcite.cpp:58
TPlanResult processImpl(query_state::QueryStateProxy, std::string sql_string, const TQueryParsingOption &query_parsing_option, const TOptimizationOption &optimization_option, const std::string &calcite_session_id)
Definition: Calcite.cpp:535
std::string cat(Ts &&...args)
std::once_flag shutdown_once_flag_
Definition: Calcite.h:140
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:114
static TimeT::rep execution(F func, Args &&...args)
Definition: sample.cpp:29
void init(const int db_port, const int port, const std::string &data_dir, const size_t calcite_max_mem, const std::string &udf_filename)
Definition: Calcite.cpp:367
#define LOG(tag)
Definition: Logger.h:205
static const AccessPrivileges INSERT_INTO_TABLE
Definition: DBObject.h:163
std::string join(T const &container, std::string const &delim)
#define UNREACHABLE()
Definition: Logger.h:255
bool hasAny() const
Definition: DBObject.h:142
void setRuntimeExtensionFunctions(const std::vector< TUserDefinedFunction > &udfs, const std::vector< TUserDefinedTableFunction > &udtfs, bool isruntime=true)
Definition: Calcite.cpp:672
int32_t objectId
Definition: DBObject.h:57
std::shared_ptr< ThriftClientConnection > connMgr_
Definition: Calcite.h:128
Timer createTimer(char const *event_name)
Definition: QueryState.cpp:138
static const AccessPrivileges SELECT_FROM_TABLE
Definition: DBObject.h:162
std::string to_string(char const *&&v)
#define LOG_IF(severity, condition)
Definition: Logger.h:301
void updateMetadata(std::string catalog, std::string table)
Definition: Calcite.cpp:408
Calcite(const int db_port, const int port, const std::string &data_dir, const size_t calcite_max_mem, const size_t service_timeout, const bool service_keepalive, const std::string &udf_filename="")
Definition: Calcite.cpp:354
This file contains the class specification and related data structures for Catalog.
void runServer(const int db_port, const int port, const std::string &data_dir, const size_t calcite_max_mem, const std::string &udf_filename)
Definition: Calcite.cpp:275
void checkPermissionForTables(const Catalog_Namespace::SessionInfo &session_info, std::vector< std::vector< std::string >> tableOrViewNames, AccessPrivileges tablePrivs, AccessPrivileges viewPrivs)
Definition: Calcite.cpp:421
static SysCatalog & instance()
Definition: SysCatalog.h:312
static void start_calcite_server_as_daemon(const int db_port, const int port, const std::string &data_dir, const size_t calcite_max_mem, const std::string &ssl_trust_store, const std::string &ssl_trust_password_X, const std::string &ssl_keystore, const std::string &ssl_keystore_password_X, const std::string &ssl_key_file, const std::string &db_config_file, const std::string &udf_filename)
Definition: Calcite.cpp:71
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:225
std::vector< TCompletionHint > getCompletionHints(const Catalog_Namespace::SessionInfo &session_info, const std::vector< std::string > &visible_tables, const std::string sql_string, const int cursor)
Definition: Calcite.cpp:500
QueryState & getQueryState()
Definition: QueryState.h:181
TQueryParsingOption getCalciteQueryParsingOption(bool legacy_syntax, bool is_explain, bool check_privileges)
Definition: Calcite.cpp:685
static const AccessPrivileges DELETE_FROM_TABLE
Definition: DBObject.h:165
int ping(int retry_num=0, int max_retry=50)
Definition: Calcite.cpp:337
std::string getUserDefinedFunctionWhitelist()
Definition: Calcite.cpp:609
std::string get_session_id() const
Definition: SessionInfo.h:76
static const AccessPrivileges SELECT_FROM_VIEW
Definition: DBObject.h:182
Catalog & getCatalog() const
Definition: SessionInfo.h:65
int remote_calcite_port_
Definition: Calcite.h:132
int32_t dbId
Definition: DBObject.h:56
std::string const getInternalSessionProxyUserName()
Definition: Calcite.h:95
void inner_close_calcite_server(bool log)
Definition: Calcite.cpp:632
bool server_available_
Definition: Calcite.h:129
bool g_enable_watchdog false
Definition: Execute.cpp:76
#define CHECK(condition)
Definition: Logger.h:211
TOptimizationOption getCalciteOptimizationOption(bool is_view_optimize, bool enable_watchdog, const std::vector< TFilterPushDownInfo > &filter_push_down_info)
Definition: Calcite.cpp:695
char * t
std::string get_root_abs_path()
std::pair< std::shared_ptr< CalciteServerClient >, std::shared_ptr< TTransport > > getClient(int port)
Definition: Calcite.cpp:251
int32_t permissionType
Definition: DBObject.h:55
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.
TPlanResult process(query_state::QueryStateProxy, std::string sql_string, const TQueryParsingOption &query_parsing_option, const TOptimizationOption &optimization_option, const std::string &calcite_session_id="")
Definition: Calcite.cpp:460
static const AccessPrivileges UPDATE_IN_TABLE
Definition: DBObject.h:164
void checkAccessedObjectsPrivileges(query_state::QueryStateProxy query_state_prox, TPlanResult plan) const
Definition: Calcite.cpp:476
std::string userLoggable() const
Definition: SysCatalog.cpp:127
std::shared_ptr< Catalog_Namespace::SessionInfo const > getConstSessionInfo() const
Definition: QueryState.cpp:88
~Calcite()
Definition: Calcite.cpp:651
std::string getExtensionFunctionWhitelist()
Definition: Calcite.cpp:591
const UserMetadata & get_currentUser() const
Definition: SessionInfo.h:71
std::string getRuntimeExtensionFunctionWhitelist()
Definition: Calcite.cpp:655
#define VLOG(n)
Definition: Logger.h:305
void close_calcite_server(bool log=true)
Definition: Calcite.cpp:627