OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sqldefs.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 
23 #pragma once
24 
25 #include <cstdio>
26 #include <ostream>
27 #include <sstream>
28 
29 // must not change the order without keeping the array in OperExpr::to_string
30 // in sync.
31 enum SQLOps {
32  kEQ = 0,
34  kNE,
35  kLT,
36  kGT,
37  kLE,
38  kGE,
40  kOR,
55  kIN,
59 };
60 
61 #define IS_COMPARISON(X) \
62  ((X) == kEQ || (X) == kBW_EQ || (X) == kBBOX_INTERSECT || (X) == kNE || (X) == kLT || \
63  (X) == kGT || (X) == kLE || (X) == kGE)
64 #define IS_LOGIC(X) ((X) == kAND || (X) == kOR)
65 #define IS_ARITHMETIC(X) \
66  ((X) == kMINUS || (X) == kPLUS || (X) == kMULTIPLY || (X) == kDIVIDE || (X) == kMODULO)
67 #define COMMUTE_COMPARISON(X) \
68  ((X) == kLT ? kGT : (X) == kLE ? kGE : (X) == kGT ? kLT : (X) == kGE ? kLE : (X))
69 #define IS_UNARY(X) \
70  ((X) == kNOT || (X) == kUMINUS || (X) == kISNULL || (X) == kEXISTS || (X) == kCAST || \
71  (X) == kENCODE_TEXT)
72 #define IS_EQUIVALENCE(X) ((X) == kEQ || (X) == kBW_EQ || (X) == kBBOX_INTERSECT)
73 
75 
76 enum SQLAgg {
90 };
91 
92 enum class SqlStringOpKind {
93  /* Unary */
94  LOWER = 1,
95  UPPER,
96  INITCAP,
97  REVERSE,
98  /* Binary */
99  REPEAT,
100  CONCAT,
101  RCONCAT,
102  /* Ternary */
103  LPAD,
104  RPAD,
105  TRIM,
106  LTRIM,
107  RTRIM,
108  SUBSTRING,
109  OVERLAY,
110  REPLACE,
111  SPLIT_PART,
112  /* 6 args */
115  REGEXP_COUNT,
116  JSON_VALUE,
119  URL_ENCODE,
120  URL_DECODE,
121  TRY_STRING_CAST, // string-to-numeric
122  POSITION, // string-to-numeric
123  JAROWINKLER_SIMILARITY, // string-to-numeric
124  LEVENSHTEIN_DISTANCE, // string-to-numeric
125  HASH, // string-to-numeric
126  INVALID
127 };
128 
130  MIN = 0,
131  MAX,
132  AVG,
133  SUM,
134  COUNT,
135  ROW_NUMBER,
136  RANK,
137  DENSE_RANK,
138  PERCENT_RANK,
139  CUME_DIST,
140  NTILE,
141  LAG,
142  LAG_IN_FRAME,
143  LEAD,
145  FIRST_VALUE,
147  LAST_VALUE,
149  NTH_VALUE,
151  FORWARD_FILL,
153  COUNT_IF,
154  SUM_IF,
155  SUM_INTERNAL, // For deserialization from Calcite only. Gets rewritten to a regular
156  // SUM.
158  UNKNOWN,
159 };
160 
161 constexpr char const* toString(SqlWindowFunctionKind const kind) {
162  constexpr char const* strings[]{"MIN",
163  "MAX",
164  "AVG",
165  "SUM",
166  "COUNT",
167  "ROW_NUMBER",
168  "RANK",
169  "DENSE_RANK",
170  "PERCENT_RANK",
171  "CUME_DIST",
172  "NTILE",
173  "LAG",
174  "LAG_IN_FRAME",
175  "LEAD",
176  "LEAD_IN_FRAME",
177  "FIRST_VALUE",
178  "FIRST_VALUE_IN_FRAME",
179  "LAST_VALUE",
180  "LAST_VALUE_IN_FRAME",
181  "NTH_VALUE",
182  "NTH_VALUE_IN_FRAME",
183  "FORWARD_FILL",
184  "BACKWARD_FILL",
185  "COUNT_IF",
186  "SUM_IF",
187  "SUM_INTERNAL",
188  "CONDITIONAL_CHANGE_EVENT",
189  "UNKNOWN"};
190  constexpr size_t nstrings = ((sizeof strings) / (sizeof *strings));
191  constexpr size_t max_str_idx = nstrings - 1;
192  static_assert(max_str_idx == size_t(SqlWindowFunctionKind::UNKNOWN));
193  return strings[size_t(kind)];
194 }
195 
196 #ifndef __CUDACC__
197 inline std::ostream& operator<<(std::ostream& os, SqlWindowFunctionKind const kind) {
198  return os << toString(kind);
199 }
200 #endif
201 
205  CURRENT_ROW,
208  UNKNOWN
209 };
210 
211 constexpr char const* toString(SqlWindowFrameBoundType const kind) {
212  constexpr char const* strings[]{"UNBOUNDED_PRECEDING",
213  "EXPR_PRECEDING",
214  "CURRENT_ROW",
215  "EXPR_FOLLOWING",
216  "UNBOUNDED_FOLLOWING",
217  "UNKNOWN"};
218  constexpr size_t nstrings = ((sizeof strings) / (sizeof *strings));
219  constexpr size_t max_str_idx = nstrings - 1;
220  static_assert(max_str_idx == size_t(SqlWindowFrameBoundType::UNKNOWN));
221  return strings[size_t(kind)];
222 }
223 
224 #ifndef __CUDACC__
225 inline std::ostream& operator<<(std::ostream& os, SqlWindowFrameBoundType const kind) {
226  return os << toString(kind);
227 }
228 #endif
229 
231 
232 // StorageOption::kCPU and ::kGPU conflict with libtorch's c10::DeviceType::kCPU
233 // Keep it as enum class rather than plain enum
234 enum class StorageOption { kDISK = 0, kGPU = 1, kCPU = 2 };
235 
236 enum ViewRefreshOption { kMANUAL = 0, kAUTO = 1, kIMMEDIATE = 2 };
237 
238 enum class JoinType {
239  INNER,
240  LEFT,
241  SEMI,
242  ANTI,
245  INVALID
246 };
247 
248 #if !(defined(__CUDACC__) || defined(NO_BOOST))
249 
250 #include <sstream>
251 #include <string>
252 #include "Logger/Logger.h"
253 
254 inline std::string toString(const JoinType& join_type) {
255  switch (join_type) {
256  case JoinType::INNER:
257  return "INNER";
258  case JoinType::LEFT:
259  return "LEFT";
260  case JoinType::SEMI:
261  return "SEMI";
262  case JoinType::ANTI:
263  return "ANTI";
265  return "WINDOW_FUNCTION";
267  return "WINDOW_FUNCTION_FRAMING";
268  default:
269  return "INVALID";
270  }
271 }
272 
273 inline std::string toString(const SQLQualifier& qualifier) {
274  switch (qualifier) {
275  case kONE:
276  return "ONE";
277  case kANY:
278  return "ANY";
279  case kALL:
280  return "ALL";
281  }
282  LOG(FATAL) << "Invalid SQLQualifier: " << qualifier;
283  return "";
284 }
285 
286 inline std::string toString(const SQLAgg& kind) {
287  switch (kind) {
288  case kAVG:
289  return "AVG";
290  case kMIN:
291  return "MIN";
292  case kMAX:
293  return "MAX";
294  case kSUM:
295  return "SUM";
296  case kCOUNT:
297  return "COUNT";
299  return "APPROX_COUNT_DISTINCT";
300  case kAPPROX_QUANTILE:
301  return "APPROX_PERCENTILE";
302  case kSAMPLE:
303  return "SAMPLE";
304  case kSINGLE_VALUE:
305  return "SINGLE_VALUE";
306  case kMODE:
307  return "MODE";
308  case kCOUNT_IF:
309  return "COUNT_IF";
310  case kSUM_IF:
311  return "SUM_IF";
312  case kINVALID_AGG:
313  return "INVALID";
314  default:
315  UNREACHABLE() << "Invalid aggregate kind: " << kind;
316  return {};
317  }
318 }
319 
321  switch (agg_type) {
322  case kCOUNT_IF:
323  return kCOUNT;
324  case kSUM_IF:
325  return kSUM;
326  default:
327  return agg_type;
328  }
329 }
330 
331 inline std::string toString(const SQLOps& op) {
332  switch (op) {
333  case kEQ:
334  return "EQ";
335  case kBW_EQ:
336  return "BW_EQ";
337  case kNE:
338  return "NE";
339  case kLT:
340  return "LT";
341  case kGT:
342  return "GT";
343  case kLE:
344  return "LE";
345  case kGE:
346  return "GE";
347  case kAND:
348  return "AND";
349  case kOR:
350  return "OR";
351  case kNOT:
352  return "NOT";
353  case kMINUS:
354  return "MINUS";
355  case kPLUS:
356  return "PLUS";
357  case kMULTIPLY:
358  return "MULTIPLY";
359  case kDIVIDE:
360  return "DIVIDE";
361  case kMODULO:
362  return "MODULO";
363  case kUMINUS:
364  return "UMINUS";
365  case kISNULL:
366  return "ISNULL";
367  case kISNOTNULL:
368  return "ISNOTNULL";
369  case kEXISTS:
370  return "EXISTS";
371  case kCAST:
372  return "CAST";
373  case kARRAY_AT:
374  return "ARRAY_AT";
375  case kUNNEST:
376  return "UNNEST";
377  case kFUNCTION:
378  return "FUNCTION";
379  case kIN:
380  return "IN";
381  case kBBOX_INTERSECT:
382  return "BOUNDING_BOX_INTERSECT";
383  case kENCODE_TEXT:
384  return "ENCODE_TEXT";
385  case kINVALID_OP:
386  return "INVALID_OP";
387  }
388  LOG(FATAL) << "Invalid operation kind: " << op;
389  return "";
390 }
391 
392 inline std::ostream& operator<<(std::ostream& os, const SqlStringOpKind kind) {
393  switch (kind) {
395  return os << "LOWER";
397  return os << "UPPER";
399  return os << "INITCAP";
401  return os << "REVERSE";
403  return os << "REPEAT";
405  return os << "CONCAT";
407  return os << "RCONCAT";
409  return os << "LPAD";
411  return os << "RPAD";
413  return os << "TRIM";
415  return os << "LTRIM";
417  return os << "RTRIM";
419  return os << "SUBSTRING";
421  return os << "OVERLAY";
423  return os << "REPLACE";
425  return os << "SPLIT_PART";
427  return os << "REGEXP_REPLACE";
429  return os << "REGEXP_SUBSTR";
431  return os << "REGEXP_COUNT";
433  return os << "JSON_VALUE";
435  return os << "BASE64_ENCODE";
437  return os << "BASE64_DECODE";
439  return os << "URL_ENCODE";
441  return os << "URL_DECODE";
443  return os << "TRY_STRING_CAST";
445  return os << "POSITION";
447  return os << "JAROWINKLER_SIMILARITY";
449  return os << "LEVENSHTEIN_DISTANCE";
451  return os << "HASH";
453  return os << "INVALID";
454  }
455  LOG(FATAL) << "Invalid string operation";
456  // Make compiler happy
457  return os << "INVALID";
458 }
459 
460 inline SqlStringOpKind name_to_string_op_kind(const std::string& func_name) {
461  if (func_name == "LOWER") {
462  return SqlStringOpKind::LOWER;
463  }
464  if (func_name == "UPPER") {
465  return SqlStringOpKind::UPPER;
466  }
467  if (func_name == "INITCAP") {
469  }
470  if (func_name == "REVERSE") {
472  }
473  if (func_name == "REPEAT") {
475  }
476  if (func_name == "||") {
478  }
479  if (func_name == "LPAD") {
480  return SqlStringOpKind::LPAD;
481  }
482  if (func_name == "RPAD") {
483  return SqlStringOpKind::RPAD;
484  }
485  if (func_name == "TRIM") {
486  return SqlStringOpKind::TRIM;
487  }
488  if (func_name == "LTRIM") {
489  return SqlStringOpKind::LTRIM;
490  }
491  if (func_name == "RTRIM") {
492  return SqlStringOpKind::RTRIM;
493  }
494  if (func_name == "SUBSTRING") {
496  }
497  if (func_name == "OVERLAY") {
499  }
500  if (func_name == "REPLACE") {
502  }
503  if (func_name == "SPLIT_PART") {
505  }
506  if (func_name == "REGEXP_REPLACE") {
508  }
509  if (func_name == "REGEXP_SUBSTR") {
511  }
512  if (func_name == "REGEXP_MATCH") {
514  }
515  if (func_name == "REGEXP_COUNT") {
517  }
518  if (func_name == "JSON_VALUE") {
520  }
521  if (func_name == "BASE64_ENCODE") {
523  }
524  if (func_name == "BASE64_DECODE") {
526  }
527  if (func_name == "URL_ENCODE") {
529  }
530  if (func_name == "URL_DECODE") {
532  }
533  if (func_name == "TRY_CAST") {
535  }
536  if (func_name == "POSITION") {
538  }
539  if (func_name == "JAROWINKLER_SIMILARITY") {
541  }
542  if (func_name == "LEVENSHTEIN_DISTANCE") {
544  }
545  if (func_name == "HASH") {
546  return SqlStringOpKind::HASH;
547  }
548  LOG(FATAL) << "Invalid string function " << func_name << ".";
550 }
551 
552 inline bool string_op_returns_string(const SqlStringOpKind kind) {
553  switch (kind) {
560  return false;
561  default:
562  return true;
563  }
564 }
565 
566 #endif // #if !(defined(__CUDACC__) || defined(NO_BOOST))
Definition: sqldefs.h:74
SqlWindowFrameBoundType
Definition: sqldefs.h:202
SQLAgg
Definition: sqldefs.h:76
ViewRefreshOption
Definition: sqldefs.h:236
SqlStringOpKind name_to_string_op_kind(const std::string &func_name)
Definition: sqldefs.h:460
JoinType
Definition: sqldefs.h:238
SQLQualifier
Definition: sqldefs.h:74
#define LOG(tag)
Definition: Logger.h:285
std::ostream & operator<<(std::ostream &os, const SessionInfo &session_info)
Definition: SessionInfo.cpp:57
SQLOps
Definition: sqldefs.h:31
Definition: sqldefs.h:37
Definition: sqldefs.h:38
Definition: sqldefs.h:40
#define UNREACHABLE()
Definition: Logger.h:338
SqlStringOpKind
Definition: sqldefs.h:92
Definition: sqldefs.h:51
Definition: sqldefs.h:32
Definition: sqldefs.h:43
Definition: sqldefs.h:78
Definition: sqldefs.h:39
Definition: sqldefs.h:80
Definition: sqldefs.h:74
std::string toString(const Executor::ExtModuleKinds &kind)
Definition: Execute.h:1703
StorageOption
Definition: sqldefs.h:234
SQLStmtType
Definition: sqldefs.h:230
Definition: sqldefs.h:36
Definition: sqldefs.h:42
bool string_op_returns_string(const SqlStringOpKind kind)
Definition: sqldefs.h:552
Definition: sqldefs.h:74
Definition: sqldefs.h:34
Definition: sqldefs.h:81
Definition: sqldefs.h:236
SqlWindowFunctionKind
Definition: sqldefs.h:129
Definition: sqldefs.h:55
Definition: sqldefs.h:33
Definition: sqldefs.h:35
Definition: sqldefs.h:79
Definition: sqldefs.h:77
Definition: sqldefs.h:41
Definition: sqldefs.h:86
SQLAgg get_non_conditional_agg_type(SQLAgg const agg_type)
Definition: sqldefs.h:320