OmniSciDB  1dac507f6e
 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::optimize_str = {"optimize"};
55 const std::string ParserWrapper::validate_str = {"validate"};
56 
57 ParserWrapper::ParserWrapper(std::string query_string) {
58  if (boost::istarts_with(query_string, calcite_explain_str)) {
59  actual_query = boost::trim_copy(query_string.substr(calcite_explain_str.size()));
60  ParserWrapper inner{actual_query};
61  if (inner.is_ddl || inner.is_update_dml) {
62  explain_type_ = ExplainType::Other;
63  return;
64  } else {
65  explain_type_ = ExplainType::Calcite;
66  return;
67  }
68  }
69 
70  if (boost::istarts_with(query_string, optimized_explain_str)) {
71  actual_query = boost::trim_copy(query_string.substr(optimized_explain_str.size()));
72  ParserWrapper inner{actual_query};
73  if (inner.is_ddl || inner.is_update_dml) {
74  explain_type_ = ExplainType::Other;
75  return;
76  } else {
77  explain_type_ = ExplainType::OptimizedIR;
78  return;
79  }
80  }
81 
82  if (boost::istarts_with(query_string, explain_str)) {
83  actual_query = boost::trim_copy(query_string.substr(explain_str.size()));
84  ParserWrapper inner{actual_query};
85  if (inner.is_ddl || inner.is_update_dml) {
86  explain_type_ = ExplainType::Other;
87  return;
88  } else {
89  explain_type_ = ExplainType::IR;
90  return;
91  }
92  }
93 
94  if (boost::istarts_with(query_string, optimize_str)) {
95  is_optimize = true;
96  return;
97  }
98 
99  if (boost::istarts_with(query_string, validate_str)) {
100  is_validate = true;
101  return;
102  }
103 
104  for (std::string ddl : ddl_cmd) {
105  is_ddl = boost::istarts_with(query_string, ddl);
106  if (is_ddl) {
107  if (ddl == "CREATE") {
108  boost::regex ctas_regex{R"(CREATE\s+TABLE.*AS.*SELECT.*)",
109  boost::regex::extended | boost::regex::icase};
110  if (boost::regex_match(query_string, ctas_regex)) {
111  is_ctas = true;
112  }
113  } else if (ddl == "COPY") {
114  is_copy = true;
115  // now check if it is COPY TO
116  boost::regex copy_to{R"(COPY\s*\(([^#])(.+)\)\s+TO\s+.*)",
117  boost::regex::extended | boost::regex::icase};
118  if (boost::regex_match(query_string, copy_to)) {
119  is_copy_to = true;
120  }
121  }
122  return;
123  }
124  }
125 
126  for (int i = 0; i < update_dml_cmd.size(); i++) {
127  is_update_dml = boost::istarts_with(query_string, ParserWrapper::update_dml_cmd[i]);
128  if (is_update_dml) {
129  dml_type_ = (DMLType)(i);
130  break;
131  }
132  }
133 
134  if (dml_type_ == DMLType::Insert) {
135  boost::regex insert_regex{R"(INSERT\s+INTO.*VALUES\s*\(.*)",
136  boost::regex::extended | boost::regex::icase};
137  if (!boost::regex_match(query_string, insert_regex)) {
138  boost::regex itas_regex{R"(INSERT\s+INTO.*SELECT.*)",
139  boost::regex::extended | boost::regex::icase};
140  if (boost::regex_match(query_string, itas_regex)) {
141  is_itas = true;
142  }
143  }
144  }
145 }
146 
Classes used to wrap parser calls for calcite redirection.
static const std::string optimize_str
virtual ~ParserWrapper()
static const std::string optimized_explain_str
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
ParserWrapper(std::string query_string)