OmniSciDB  95562058bd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TargetInfo.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 
22 #ifndef QUERYENGINE_TARGETINFO_H
23 #define QUERYENGINE_TARGETINFO_H
24 
25 #include "sqldefs.h"
26 #include "sqltypes.h"
27 
28 #include "../Analyzer/Analyzer.h"
29 
30 inline const Analyzer::AggExpr* cast_to_agg_expr(const Analyzer::Expr* target_expr) {
31  return dynamic_cast<const Analyzer::AggExpr*>(target_expr);
32 }
33 
35  const std::shared_ptr<Analyzer::Expr> target_expr) {
36  return dynamic_cast<const Analyzer::AggExpr*>(target_expr.get());
37 }
38 
39 struct TargetInfo {
40  bool is_agg;
46 #ifndef __CUDACC__
47  public:
48  inline std::string toString() const {
49  auto result = std::string("TargetInfo(");
50  result += "is_agg=" + std::string(is_agg ? "true" : "false") + ", ";
51  result += "sql_type=" + sql_type.to_string() + ", ";
52  result += "agg_arg_type=" + agg_arg_type.to_string() + ", ";
53  result += "skip_null_val=" + std::string(skip_null_val ? "true" : "false") + ", ";
54  result += "is_distinct=" + std::string(is_distinct ? "true" : "false") + ")";
55  return result;
56  }
57 #endif
58 };
59 
64 inline bool is_agg_domain_range_equivalent(const SQLAgg& agg_kind) {
65  switch (agg_kind) {
66  case kMIN:
67  case kMAX:
68  case kSINGLE_VALUE:
69  case kSAMPLE:
70  return true;
71  default:
72  break;
73  }
74  return false;
75 }
76 
77 template <class PointerType>
78 inline TargetInfo get_target_info(const PointerType target_expr,
79  const bool bigint_count) {
80  const auto agg_expr = cast_to_agg_expr(target_expr);
81  bool notnull = target_expr->get_type_info().get_notnull();
82  if (!agg_expr) {
83  auto target_ti = target_expr ? get_logical_type_info(target_expr->get_type_info())
84  : SQLTypeInfo(kBIGINT, notnull);
85  return {false, kMIN, target_ti, SQLTypeInfo(kNULLT, false), false, false};
86  }
87  const auto agg_type = agg_expr->get_aggtype();
88  const auto agg_arg = agg_expr->get_arg();
89  if (!agg_arg) {
90  CHECK_EQ(kCOUNT, agg_type);
91  CHECK(!agg_expr->get_is_distinct());
92  return {true,
93  kCOUNT,
94  SQLTypeInfo(bigint_count ? kBIGINT : kINT, notnull),
95  SQLTypeInfo(kNULLT, false),
96  false,
97  false};
98  }
99 
100  const auto& agg_arg_ti = agg_arg->get_type_info();
101  bool is_distinct{false};
102  if (agg_expr->get_aggtype() == kCOUNT) {
103  is_distinct = agg_expr->get_is_distinct();
104  }
105 
106  if (agg_type == kAVG) {
107  // Upcast the target type for AVG, so that the integer argument does not overflow the
108  // sum
109  return {true,
110  agg_expr->get_aggtype(),
111  agg_arg_ti.is_integer() ? SQLTypeInfo(kBIGINT, agg_arg_ti.get_notnull())
112  : agg_arg_ti,
113  agg_arg_ti,
114  !agg_arg_ti.get_notnull(),
115  is_distinct};
116  }
117 
118  return {
119  true,
120  agg_expr->get_aggtype(),
121  agg_type == kCOUNT
122  ? SQLTypeInfo((is_distinct || bigint_count) ? kBIGINT : kINT, notnull)
123  : agg_expr->get_type_info(),
124  agg_arg_ti,
125  agg_type == kCOUNT && agg_arg_ti.is_varlen() ? false : !agg_arg_ti.get_notnull(),
126  is_distinct};
127 }
128 
129 inline bool is_distinct_target(const TargetInfo& target_info) {
130  return target_info.is_distinct || target_info.agg_kind == kAPPROX_COUNT_DISTINCT;
131 }
132 
133 inline bool takes_float_argument(const TargetInfo& target_info) {
134  return target_info.is_agg &&
135  (target_info.agg_kind == kAVG || target_info.agg_kind == kSUM ||
136  target_info.agg_kind == kMIN || target_info.agg_kind == kMAX ||
137  target_info.agg_kind == kSINGLE_VALUE) &&
138  target_info.agg_arg_type.get_type() == kFLOAT;
139 }
140 
141 #endif // QUERYENGINE_TARGETINFO_H
const Analyzer::Expr * agg_arg(const Analyzer::Expr *expr)
SQLAgg
Definition: sqldefs.h:71
#define CHECK_EQ(x, y)
Definition: Logger.h:205
SQLTypeInfo sql_type
Definition: TargetInfo.h:42
TargetInfo get_target_info(const PointerType target_expr, const bool bigint_count)
Definition: TargetInfo.h:78
SQLTypeInfo get_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:820
Constants for Builtin SQL Types supported by OmniSci.
bool takes_float_argument(const TargetInfo &target_info)
Definition: TargetInfo.h:133
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:259
bool skip_null_val
Definition: TargetInfo.h:44
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:43
Definition: sqldefs.h:73
std::string to_string() const
Definition: sqltypes.h:395
bool is_agg
Definition: TargetInfo.h:40
std::string toString() const
Definition: TargetInfo.h:48
Definition: sqldefs.h:75
bool is_distinct_target(const TargetInfo &target_info)
Definition: TargetInfo.h:129
SQLAgg agg_kind
Definition: TargetInfo.h:41
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
const Analyzer::AggExpr * cast_to_agg_expr(const Analyzer::Expr *target_expr)
Definition: TargetInfo.h:30
Definition: sqldefs.h:76
#define CHECK(condition)
Definition: Logger.h:197
Common Enum definitions for SQL processing.
Definition: sqltypes.h:47
bool is_distinct
Definition: TargetInfo.h:45
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:266
Definition: sqldefs.h:74
Definition: sqldefs.h:72
bool is_distinct(const size_t input_idx, const RelAlgNode *node)
bool is_agg_domain_range_equivalent(const SQLAgg &agg_kind)
Definition: TargetInfo.h:64