OmniSciDB  dfae7c3b14
ParserWrapper.h
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 
25 #pragma once
26 
27 #include <boost/regex.hpp>
28 #include <string>
29 #include <vector>
30 
31 struct ExplainInfo {
32  bool explain;
36 
37  static ExplainInfo defaults() { return ExplainInfo{false, false, false, false}; }
38 
39  bool justExplain() const { return explain || explain_plan || explain_optimized; }
40 
41  bool justCalciteExplain() const { return calcite_explain; }
42 };
43 
45  public:
46  // HACK: This needs to go away as calcite takes over parsing
47  enum class DMLType : int { Insert = 0, Delete, Update, Upsert, NotDML };
48 
49  enum class ExplainType { None, IR, OptimizedIR, Calcite, ExecutionPlan, Other };
50 
51  enum class QueryType { Unknown, Read, Write, SchemaRead, SchemaWrite };
52 
53  ParserWrapper(std::string query_string);
54  std::string process(std::string user,
55  std::string passwd,
56  std::string catalog,
57  std::string sql_string,
58  const bool legacy_syntax);
59  virtual ~ParserWrapper();
60 
61  bool is_ddl = false;
62  // is_update_dml does not imply UPDATE,
63  // but rather any of the statement types: INSERT DELETE UPDATE UPSERT
64  bool is_update_dml = false;
65  bool is_ctas = false;
66  bool is_itas = false;
67  bool is_copy = false;
68  bool is_copy_to = false;
69  bool is_optimize = false;
70  bool is_validate = false;
71  std::string actual_query;
72 
73  DMLType getDMLType() const { return dml_type_; }
74 
75  ExplainInfo getExplainInfo() const;
76 
77  ExplainType getExplainType() const { return explain_type_; }
78 
79  QueryType getQueryType() const { return query_type_; }
80 
81  bool isCalciteExplain() const { return explain_type_ == ExplainType::Calcite; }
82 
83  bool isPlanExplain() const { return explain_type_ == ExplainType::ExecutionPlan; }
84 
85  bool isSelectExplain() const {
86  return explain_type_ == ExplainType::Calcite || explain_type_ == ExplainType::IR ||
87  explain_type_ == ExplainType::OptimizedIR ||
88  explain_type_ == ExplainType::ExecutionPlan;
89  }
90 
91  bool isIRExplain() const {
92  return explain_type_ == ExplainType::IR || explain_type_ == ExplainType::OptimizedIR;
93  }
94 
95  bool isCalcitePathPermissable(bool read_only_mode = false) {
96  return is_calcite_ddl_ || (!is_legacy_ddl_ && !is_optimize && !is_validate &&
97  isCalcitePermissableDml(read_only_mode) &&
98  !(explain_type_ == ExplainType::Other));
99  }
100 
101  bool isOtherExplain() const { return explain_type_ == ExplainType::Other; }
102 
103  bool isCalcitePermissableDml(bool read_only_mode) {
104  if (read_only_mode) {
105  return !is_update_dml; // If we're read-only, no update/delete DML is permissable
106  }
107  return !is_update_dml || (getDMLType() == ParserWrapper::DMLType::Delete) ||
108  (getDMLType() == ParserWrapper::DMLType::Update);
109  }
110 
111  bool isCalciteDdl() const { return is_calcite_ddl_; }
112 
113  private:
114  DMLType dml_type_ = DMLType::NotDML;
115  ExplainType explain_type_ = ExplainType::None;
116  QueryType query_type_ = QueryType::Unknown;
117 
118  static const std::vector<std::string> ddl_cmd;
119  static const std::vector<std::string> update_dml_cmd;
120  static const std::string explain_str;
121  static const std::string calcite_explain_str;
122  static const std::string optimized_explain_str;
123  static const std::string plan_explain_str;
124  static const std::string optimize_str;
125  static const std::string validate_str;
126 
127  bool is_legacy_ddl_ = false;
128  bool is_calcite_ddl_ = false;
129 };
static const std::string optimize_str
DMLType getDMLType() const
Definition: ParserWrapper.h:73
static ExplainInfo defaults()
Definition: ParserWrapper.h:37
bool isIRExplain() const
Definition: ParserWrapper.h:91
static const std::string optimized_explain_str
static const std::string calcite_explain_str
std::string actual_query
Definition: ParserWrapper.h:71
bool isCalcitePathPermissable(bool read_only_mode=false)
Definition: ParserWrapper.h:95
QueryType getQueryType() const
Definition: ParserWrapper.h:79
ExplainType getExplainType() const
Definition: ParserWrapper.h:77
bool explain_plan
Definition: ParserWrapper.h:34
bool isOtherExplain() const
bool isSelectExplain() const
Definition: ParserWrapper.h:85
static const std::vector< std::string > ddl_cmd
bool justCalciteExplain() const
Definition: ParserWrapper.h:41
bool calcite_explain
Definition: ParserWrapper.h:35
static const std::string validate_str
bool isPlanExplain() const
Definition: ParserWrapper.h:83
static const std::string explain_str
static const std::vector< std::string > update_dml_cmd
static const std::string plan_explain_str
bool isCalcitePermissableDml(bool read_only_mode)
bool explain_optimized
Definition: ParserWrapper.h:33
bool isCalciteExplain() const
Definition: ParserWrapper.h:81
bool justExplain() const
Definition: ParserWrapper.h:39
bool isCalciteDdl() const