OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LockMgr.cpp
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 
17 #include "LockMgr.h"
21 #include "gen-cpp/CalciteServer.h"
22 
23 namespace Lock_Namespace {
24 
25 using namespace rapidjson;
26 
27 void getTableNames(std::map<std::string, bool>& tableNames, const std::string query_ra) {
28  rapidjson::Document query_ast;
29  query_ast.Parse(query_ra.c_str());
30  CHECK(!query_ast.HasParseError());
31  CHECK(query_ast.IsObject());
32  getTableNames(tableNames, query_ast);
33 }
34 
35 void getTableNames(std::map<std::string, bool>& tableNames, const Value& value) {
36  if (value.IsArray()) {
37  for (SizeType i = 0; i < value.Size(); ++i) {
38  getTableNames(tableNames, value[i]);
39  }
40  return;
41  } else if (value.IsObject()) {
42  for (auto mit = value.MemberBegin(); mit != value.MemberEnd(); ++mit) {
43  getTableNames(tableNames, mit->value);
44  }
45  } else {
46  return;
47  }
48 
49  if (value.FindMember("rels") == value.MemberEnd()) {
50  return;
51  }
52  const auto& rels = value["rels"];
53  CHECK(rels.IsArray());
54  for (SizeType i = 0; i < rels.Size(); ++i) {
55  const auto& rel = rels[i];
56  const auto& relop = json_str(rel["relOp"]);
57  if (rel.FindMember("table") != rel.MemberEnd()) {
58  if ("EnumerableTableScan" == relop || "LogicalTableModify" == relop) {
59  const auto t = rel["table"].GetArray();
60  CHECK(t[1].IsString());
61  tableNames[t[1].GetString()] |= "LogicalTableModify" == relop;
62  }
63  }
64  }
65 }
66 
68  const std::string& tableName) {
69  if (const auto tdp = cat.getMetadataForTable(tableName, false)) {
70  ChunkKey chunk_key{cat.getCurrentDB().dbId, tdp->tableId};
71  return chunk_key;
72  } else {
73  throw std::runtime_error("Table " + tableName + " does not exist.");
74  }
75 }
76 
77 std::string parse_to_ra(query_state::QueryStateProxy query_state_proxy,
78  const std::string& query_str) {
79  auto const session = query_state_proxy.getQueryState().getConstSessionInfo();
80  auto const& cat = session->getCatalog();
81  return cat.getCalciteMgr()
82  ->process(query_state_proxy, query_str, {}, true, false, false)
83  .plan_result;
84 }
85 
86 } // namespace Lock_Namespace
std::vector< int > ChunkKey
Definition: types.h:35
void getTableNames(std::map< std::string, bool > &tableNames, const std::string query_ra)
Definition: LockMgr.cpp:27
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:81
std::string parse_to_ra(query_state::QueryStateProxy query_state_proxy, const std::string &query_str)
Definition: LockMgr.cpp:77
const std::string json_str(const rapidjson::Value &obj) noexcept
Definition: JsonAccessors.h:44
ChunkKey getTableChunkKey(const Catalog_Namespace::Catalog &cat, const std::string &tableName)
Definition: LockMgr.cpp:67
CHECK(cgen_state)
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:176
QueryState & getQueryState()
Definition: QueryState.h:172
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.
std::shared_ptr< Catalog_Namespace::SessionInfo const > getConstSessionInfo() const
Definition: QueryState.cpp:75