OmniSciDB  04ee39c94c
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());
43  dml->analyze(cat, query);
44  Planner::Optimizer optimizer(query, cat);
45  return optimizer.optimize();
46 }
47 
48 Planner::RootPlan* QueryRunner::parsePlanCalcite(const std::string& query_str) {
49  ParserWrapper pw{query_str};
50  if (pw.isOtherExplain() || pw.is_ddl || pw.is_update_dml) {
51  return parsePlanLegacy(query_str);
52  }
53 
54  const auto& cat = session_info_->getCatalog();
55  auto calcite_mgr = cat.getCalciteMgr();
56  const auto query_ra =
57  calcite_mgr->process(*session_info_,
58  pg_shim(query_str),
59  {},
60  true,
61  false,
62  false)
63  .plan_result; // if we want to be able to check plans we may want to calc this
64  return translate_query(query_ra, cat);
65 }
66 
67 Planner::RootPlan* QueryRunner::parsePlan(const std::string& query_str) {
68  return parsePlanCalcite(query_str);
69 }
70 
71 } // 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:195
This file contains the class specification and related data structures for Catalog.
const Catalog_Namespace::Catalog & getCatalog() const
Definition: Planner.h:293
RootPlan * optimize()
Definition: Planner.cpp:43
Defines data structures for query plan nodes.
Planner::RootPlan * translate_query(const std::string &query, const Catalog_Namespace::Catalog &cat)
#define CHECK(condition)
Definition: Logger.h:187
std::string pg_shim(const std::string &query)