OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ParserUtils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 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 #include "QueryRunner.h"
18 
19 #include "Calcite/Calcite.h"
20 #include "Catalog/Catalog.h"
21 #include "Parser/ParserWrapper.h"
22 #include "Parser/parser.h"
23 #include "Planner/Planner.h"
25 #include "gen-cpp/CalciteServer.h"
26 
27 namespace QueryRunner {
28 
29 Planner::RootPlan* QueryRunner::parsePlanLegacy(const std::string& query_str) {
30  const auto& cat = session_info_->getCatalog();
31  SQLParser parser;
32  std::list<std::unique_ptr<Parser::Stmt>> parse_trees;
33  std::string last_parsed;
34  if (parser.parse(query_str, parse_trees, last_parsed)) {
35  throw std::runtime_error("Failed to parse query: " + query_str);
36  }
37  CHECK_EQ(parse_trees.size(), size_t(1));
38  const auto& stmt = parse_trees.front();
39  Parser::DDLStmt* ddl = dynamic_cast<Parser::DDLStmt*>(stmt.get());
40  CHECK(!ddl);
41  Parser::DMLStmt* dml = dynamic_cast<Parser::DMLStmt*>(stmt.get());
42  Analyzer::Query query;
43  dml->analyze(cat, query);
44  Planner::Optimizer optimizer(query, cat);
45  return optimizer.optimize();
46 }
47 
48 Planner::RootPlan* QueryRunner::parsePlanCalcite(QueryStateProxy query_state_proxy) {
49  auto const& query_state = query_state_proxy.getQueryState();
50  ParserWrapper pw{query_state.getQueryStr()};
51  if (pw.isOtherExplain() || pw.is_ddl || pw.is_update_dml) {
52  return parsePlanLegacy(query_state.getQueryStr());
53  }
54 
55  const auto& cat = query_state.getConstSessionInfo()->getCatalog();
56  auto calcite_mgr = cat.getCalciteMgr();
57  const auto query_ra =
58  calcite_mgr
59  ->process(query_state_proxy,
60  pg_shim(query_state.getQueryStr()),
61  {},
62  true,
63  false,
64  false)
65  .plan_result; // if we want to be able to check plans we may want to calc this
66  return translate_query(query_ra, cat);
67 }
68 
69 Planner::RootPlan* QueryRunner::parsePlan(QueryStateProxy query_state_proxy) {
70  return parsePlanCalcite(query_state_proxy);
71 }
72 
73 } // namespace QueryRunner
virtual void analyze(const Catalog_Namespace::Catalog &catalog, Analyzer::Query &query) const =0
Classes used to wrap parser calls for calcite redirection.
#define CHECK_EQ(x, y)
Definition: Logger.h:198
const Catalog_Namespace::Catalog & getCatalog() const
Definition: Planner.h:293
Planner::RootPlan * parsePlanCalcite(QueryStateProxy)
Definition: ParserUtils.cpp:48
This file contains the class specification and related data structures for Catalog.
CHECK(cgen_state)
std::shared_ptr< Calcite > getCalciteMgr() const
Definition: Catalog.h:178
RootPlan * optimize()
Definition: Planner.cpp:43
QueryState & getQueryState()
Definition: QueryState.h:172
Planner::RootPlan * parsePlanLegacy(const std::string &query_str)
Definition: ParserUtils.cpp:29
Defines data structures for query plan nodes.
Planner::RootPlan * translate_query(const std::string &query, const Catalog_Namespace::Catalog &cat)
std::shared_ptr< Catalog_Namespace::SessionInfo > session_info_
Definition: QueryRunner.h:188
std::string pg_shim(const std::string &query)