OmniSciDB  5ade3759e0
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 #include "Shared/ConfigResolve.h"
32 
33 enum class CalciteDMLPathSelection : int {
34  Unsupported = 0,
35  OnlyUpdates = 1,
36  OnlyDeletes = 2,
38 };
39 
41  int selector = 0;
42  if (std::is_same<CalciteDeletePathSelector, PreprocessorTrue>::value) {
43  selector |= 0x02;
44  }
45  if (std::is_same<CalciteUpdatePathSelector, PreprocessorTrue>::value) {
46  selector |= 0x01;
47  }
48  return static_cast<CalciteDMLPathSelection>(selector);
49 }
50 
52  public:
53  // HACK: This needs to go away as calcite takes over parsing
54  enum class DMLType : int { Insert = 0, Delete, Update, Upsert, NotDML };
55 
56  enum class ExplainType { None, IR, OptimizedIR, Calcite, Other };
57 
58  ParserWrapper(std::string query_string);
59  std::string process(std::string user,
60  std::string passwd,
61  std::string catalog,
62  std::string sql_string,
63  const bool legacy_syntax);
64  virtual ~ParserWrapper();
65 
66  bool is_ddl = false;
67  bool is_update_dml = false;
68  bool is_ctas = false;
69  bool is_itas = false;
70  bool is_copy = false;
71  bool is_copy_to = false;
72  bool is_optimize = false;
73  bool is_validate = false;
74  std::string actual_query;
75 
76  DMLType getDMLType() const { return dml_type_; }
77 
78  ExplainType getExplainType() const { return explain_type_; }
79 
80  bool isCalciteExplain() const { return explain_type_ == ExplainType::Calcite; }
81 
82  bool isSelectExplain() const {
83  return explain_type_ == ExplainType::Calcite || explain_type_ == ExplainType::IR ||
84  explain_type_ == ExplainType::OptimizedIR;
85  }
86 
87  bool isIRExplain() const {
88  return explain_type_ == ExplainType::IR || explain_type_ == ExplainType::OptimizedIR;
89  }
90 
91  bool isCalcitePathPermissable(bool read_only_mode = false) {
92  return (!is_ddl && !is_optimize && !is_validate &&
93  isCalcitePermissableDml(read_only_mode) &&
94  !(explain_type_ == ExplainType::Other));
95  }
96 
97  bool isOtherExplain() const { return explain_type_ == ExplainType::Other; }
98 
99  bool isCalcitePermissableDml(bool read_only_mode) {
100  if (read_only_mode) {
101  return !is_update_dml; // If we're read-only, no DML is permissable
102  }
103 
104  // TODO: A good place for if-constexpr
105  switch (yield_dml_path_selector()) {
107  return !is_update_dml || (getDMLType() == ParserWrapper::DMLType::Update);
109  return !is_update_dml || (getDMLType() == ParserWrapper::DMLType::Delete);
111  return !is_update_dml || (getDMLType() == ParserWrapper::DMLType::Delete) ||
112  (getDMLType() == ParserWrapper::DMLType::Update);
114  default:
115  return false;
116  }
117  }
118 
119  private:
120  DMLType dml_type_ = DMLType::NotDML;
121  ExplainType explain_type_ = ExplainType::None;
122 
123  static const std::vector<std::string> ddl_cmd;
124  static const std::vector<std::string> update_dml_cmd;
125  static const std::string explain_str;
126  static const std::string calcite_explain_str;
127  static const std::string optimized_explain_str;
128  static const std::string optimize_str;
129  static const std::string validate_str;
130 };
static const std::string optimize_str
DMLType getDMLType() const
Definition: ParserWrapper.h:76
CalciteDMLPathSelection
Definition: ParserWrapper.h:33
bool isIRExplain() const
Definition: ParserWrapper.h:87
static const std::string optimized_explain_str
static const std::string calcite_explain_str
std::string actual_query
Definition: ParserWrapper.h:74
bool isCalcitePathPermissable(bool read_only_mode=false)
Definition: ParserWrapper.h:91
ExplainType getExplainType() const
Definition: ParserWrapper.h:78
bool isOtherExplain() const
Definition: ParserWrapper.h:97
bool isSelectExplain() const
Definition: ParserWrapper.h:82
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
bool isCalcitePermissableDml(bool read_only_mode)
Definition: ParserWrapper.h:99
constexpr CalciteDMLPathSelection yield_dml_path_selector()
Definition: ParserWrapper.h:40
bool isCalciteExplain() const
Definition: ParserWrapper.h:80