OmniSciDB  17c254d2f8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ParserWrapper.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 /*
18  * File: ParserWrapper.cpp
19  * Author: michael
20  *
21  * Created on Feb 23, 2016, 9:33 AM
22  */
23 
24 #include "ParserWrapper.h"
25 #include "Shared/measure.h"
26 
27 #include <boost/algorithm/string.hpp>
28 
29 using namespace std;
30 
31 const std::vector<std::string> ParserWrapper::ddl_cmd = {"ARCHIVE",
32  "ALTER",
33  "COPY",
34  "GRANT",
35  "CREATE",
36  "DROP",
37  "DUMP",
38  "OPTIMIZE",
39  "RESTORE",
40  "REVOKE",
41  "SHOW",
42  "TRUNCATE"};
43 
44 const std::vector<std::string> ParserWrapper::update_dml_cmd = {
45  "INSERT",
46  "DELETE",
47  "UPDATE",
48  "UPSERT",
49 };
50 
51 const std::string ParserWrapper::explain_str = {"explain"};
52 const std::string ParserWrapper::calcite_explain_str = {"explain calcite"};
53 const std::string ParserWrapper::optimized_explain_str = {"explain optimized"};
54 const std::string ParserWrapper::plan_explain_str = {"explain plan"};
55 const std::string ParserWrapper::optimize_str = {"optimize"};
56 const std::string ParserWrapper::validate_str = {"validate"};
57 
58 extern bool g_enable_fsi;
59 
60 ParserWrapper::ParserWrapper(std::string query_string) {
61  query_type_ = QueryType::SchemaRead;
62  if (boost::istarts_with(query_string, calcite_explain_str)) {
63  actual_query = boost::trim_copy(query_string.substr(calcite_explain_str.size()));
64  ParserWrapper inner{actual_query};
65  if (inner.is_ddl || inner.is_update_dml) {
66  explain_type_ = ExplainType::Other;
67  return;
68  } else {
69  explain_type_ = ExplainType::Calcite;
70  return;
71  }
72  }
73 
74  if (boost::istarts_with(query_string, optimized_explain_str)) {
75  actual_query = boost::trim_copy(query_string.substr(optimized_explain_str.size()));
76  ParserWrapper inner{actual_query};
77  if (inner.is_ddl || inner.is_update_dml) {
78  explain_type_ = ExplainType::Other;
79  return;
80  } else {
81  explain_type_ = ExplainType::OptimizedIR;
82  return;
83  }
84  }
85 
86  if (boost::istarts_with(query_string, plan_explain_str)) {
87  actual_query = boost::trim_copy(query_string.substr(plan_explain_str.size()));
88  ParserWrapper inner{actual_query};
89  if (inner.is_ddl || inner.is_update_dml) {
90  explain_type_ = ExplainType::Other;
91  return;
92  } else {
93  explain_type_ = ExplainType::ExecutionPlan;
94  return;
95  }
96  }
97 
98  if (boost::istarts_with(query_string, explain_str)) {
99  actual_query = boost::trim_copy(query_string.substr(explain_str.size()));
100  ParserWrapper inner{actual_query};
101  if (inner.is_ddl || inner.is_update_dml) {
102  explain_type_ = ExplainType::Other;
103  return;
104  } else {
105  explain_type_ = ExplainType::IR;
106  return;
107  }
108  }
109 
110  if (boost::istarts_with(query_string, optimize_str)) {
111  query_type_ = QueryType::SchemaWrite;
112  is_optimize = true;
113  return;
114  }
115 
116  if (boost::istarts_with(query_string, validate_str)) {
117  is_validate = true;
118  return;
119  }
120  query_type_ = QueryType::Read;
121  for (std::string ddl : ddl_cmd) {
122  is_ddl = boost::istarts_with(query_string, ddl);
123  if (is_ddl) {
124  query_type_ = QueryType::SchemaWrite;
125  if (g_enable_fsi) {
126  boost::regex fsi_regex{R"(((CREATE|DROP)\s+(SERVER|FOREIGN\s+TABLE).*))",
127  boost::regex::extended | boost::regex::icase};
128  if (boost::regex_match(query_string, fsi_regex)) {
129  is_calcite_ddl_ = true;
130  is_legacy_ddl_ = false;
131  return;
132  }
133  }
134  if (ddl == "CREATE") {
135  boost::regex ctas_regex{R"(CREATE\s+TABLE.*AS.*SELECT.*)",
136  boost::regex::extended | boost::regex::icase};
137  if (boost::regex_match(query_string, ctas_regex)) {
138  is_ctas = true;
139  }
140  } else if (ddl == "COPY") {
141  is_copy = true;
142  // now check if it is COPY TO
143  boost::regex copy_to{R"(COPY\s*\(([^#])(.+)\)\s+TO\s+.*)",
144  boost::regex::extended | boost::regex::icase};
145  if (boost::regex_match(query_string, copy_to)) {
146  query_type_ = QueryType::Read;
147  is_copy_to = true;
148  } else {
149  query_type_ = QueryType::Write;
150  }
151  } else if (ddl == "SHOW") {
152  query_type_ = QueryType::SchemaRead;
153  boost::regex show_create_table_regex{
154  R"(SHOW\s+CREATE\s+TABLE\s+.*)",
155  boost::regex::extended | boost::regex::icase};
156  if (!boost::regex_match(query_string, show_create_table_regex)) {
157  is_calcite_ddl_ = true;
158  is_legacy_ddl_ = false;
159  return;
160  }
161  }
162 
163  is_legacy_ddl_ = !is_calcite_ddl_;
164  return;
165  }
166  }
167 
168  for (int i = 0; i < update_dml_cmd.size(); i++) {
169  is_update_dml = boost::istarts_with(query_string, ParserWrapper::update_dml_cmd[i]);
170  if (is_update_dml) {
171  query_type_ = QueryType::Write;
172  dml_type_ = (DMLType)(i);
173  break;
174  }
175  }
176 
177  if (dml_type_ == DMLType::Insert) {
178  boost::regex insert_regex{R"(INSERT\s+INTO.*VALUES\s*\(.*)",
179  boost::regex::extended | boost::regex::icase};
180  if (!boost::regex_match(query_string, insert_regex)) {
181  boost::regex itas_regex{R"(INSERT\s+INTO.*SELECT.*)",
182  boost::regex::extended | boost::regex::icase};
183  if (boost::regex_match(query_string, itas_regex)) {
184  is_itas = true;
185  }
186  }
187  }
188 }
189 
191 
193  return {explain_type_ == ExplainType::IR,
194  explain_type_ == ExplainType::OptimizedIR,
195  explain_type_ == ExplainType::ExecutionPlan,
196  explain_type_ == ExplainType::Calcite};
197 }
Classes used to wrap parser calls for calcite redirection.
static const std::string optimize_str
virtual ~ParserWrapper()
static const std::string optimized_explain_str
ExplainInfo getExplainInfo() const
static const std::string calcite_explain_str
static const std::vector< std::string > ddl_cmd
static const std::string validate_str
static const std::string explain_str
static const std::vector< std::string > update_dml_cmd
static const std::string plan_explain_str
ParserWrapper(std::string query_string)
bool g_enable_fsi
Definition: Catalog.cpp:90