OmniSciDB  21ac014ffc
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 inline bool target_expr_has_varlen_projection(const Analyzer::Expr* target_expr) {
40  return !(dynamic_cast<const Analyzer::GeoExpr*>(target_expr) == nullptr);
41 }
42 
44  const std::shared_ptr<Analyzer::Expr> target_expr) {
45  return !(dynamic_cast<const Analyzer::GeoExpr*>(target_expr.get()) == nullptr);
46 }
47 
48 struct TargetInfo {
49  bool is_agg;
56 #ifndef __CUDACC__
57  public:
58  inline std::string toString() const {
59  auto result = std::string("TargetInfo(");
60  result += "is_agg=" + std::string(is_agg ? "true" : "false") + ", ";
61  result += "agg_kind=" + ::toString(agg_kind) + ", ";
62  result += "sql_type=" + sql_type.to_string() + ", ";
63  result += "agg_arg_type=" + agg_arg_type.to_string() + ", ";
64  result += "skip_null_val=" + std::string(skip_null_val ? "true" : "false") + ", ";
65  result += "is_distinct=" + std::string(is_distinct ? "true" : "false") + ")";
66  result +=
67  "is_varlen_projection=" + std::string(is_varlen_projection ? "true" : "false") +
68  ")";
69  return result;
70  }
71 #endif
72 };
73 
78 inline bool is_agg_domain_range_equivalent(const SQLAgg& agg_kind) {
79  switch (agg_kind) {
80  case kMIN:
81  case kMAX:
82  case kSINGLE_VALUE:
83  case kSAMPLE:
84  return true;
85  default:
86  break;
87  }
88  return false;
89 }
90 
91 template <class PointerType>
92 inline TargetInfo get_target_info(const PointerType target_expr,
93  const bool bigint_count) {
94  const auto agg_expr = cast_to_agg_expr(target_expr);
95  bool notnull = target_expr->get_type_info().get_notnull();
96  if (!agg_expr) {
98  auto target_ti = target_expr ? get_logical_type_info(target_expr->get_type_info())
99  : SQLTypeInfo(kBIGINT, notnull);
100  return {false,
101  kMIN,
102  target_ti,
103  SQLTypeInfo(kNULLT, false),
104  false,
105  false,
106  is_varlen_projection};
107  }
108  const auto agg_type = agg_expr->get_aggtype();
109  const auto agg_arg = agg_expr->get_arg();
110  if (!agg_arg) {
111  CHECK_EQ(kCOUNT, agg_type);
112  CHECK(!agg_expr->get_is_distinct());
113  return {true,
114  kCOUNT,
115  SQLTypeInfo(bigint_count ? kBIGINT : kINT, notnull),
116  SQLTypeInfo(kNULLT, false),
117  false,
118  false,
119  false};
120  }
121 
122  const auto& agg_arg_ti = agg_arg->get_type_info();
123  bool is_distinct{false};
124  if (agg_expr->get_aggtype() == kCOUNT) {
125  is_distinct = agg_expr->get_is_distinct();
126  }
127 
128  if (agg_type == kAVG) {
129  // Upcast the target type for AVG, so that the integer argument does not overflow the
130  // sum
131  return {true,
132  agg_expr->get_aggtype(),
133  agg_arg_ti.is_integer() ? SQLTypeInfo(kBIGINT, agg_arg_ti.get_notnull())
134  : agg_arg_ti,
135  agg_arg_ti,
136  !agg_arg_ti.get_notnull(),
137  is_distinct,
138  false};
139  }
140 
141  return {
142  true,
143  agg_expr->get_aggtype(),
144  agg_type == kCOUNT
145  ? SQLTypeInfo((is_distinct || bigint_count) ? kBIGINT : kINT, notnull)
146  : agg_expr->get_type_info(),
147  agg_arg_ti,
148  agg_type == kCOUNT && agg_arg_ti.is_varlen() ? false : !agg_arg_ti.get_notnull(),
149  is_distinct,
150  false};
151 }
152 
153 inline bool is_distinct_target(const TargetInfo& target_info) {
154  return target_info.is_distinct || target_info.agg_kind == kAPPROX_COUNT_DISTINCT;
155 }
156 
157 inline bool takes_float_argument(const TargetInfo& target_info) {
158  return target_info.is_agg &&
159  (target_info.agg_kind == kAVG || target_info.agg_kind == kSUM ||
160  target_info.agg_kind == kMIN || target_info.agg_kind == kMAX ||
161  target_info.agg_kind == kSINGLE_VALUE) &&
162  target_info.agg_arg_type.get_type() == kFLOAT;
163 }
164 
165 #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:214
SQLTypeInfo sql_type
Definition: TargetInfo.h:51
TargetInfo get_target_info(const PointerType target_expr, const bool bigint_count)
Definition: TargetInfo.h:92
SQLTypeInfo get_logical_type_info(const SQLTypeInfo &type_info)
Definition: sqltypes.h:925
Constants for Builtin SQL Types supported by OmniSci.
bool takes_float_argument(const TargetInfo &target_info)
Definition: TargetInfo.h:157
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:323
bool skip_null_val
Definition: TargetInfo.h:53
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:52
bool target_expr_has_varlen_projection(const Analyzer::Expr *target_expr)
Definition: TargetInfo.h:39
bool is_varlen_projection
Definition: TargetInfo.h:55
Definition: sqldefs.h:73
bool is_varlen_projection(const Analyzer::Expr *target_expr, const SQLTypeInfo &ti)
std::string to_string() const
Definition: sqltypes.h:466
bool is_agg
Definition: TargetInfo.h:49
std::string toString() const
Definition: TargetInfo.h:58
Definition: sqldefs.h:75
bool is_distinct_target(const TargetInfo &target_info)
Definition: TargetInfo.h:153
SQLAgg agg_kind
Definition: TargetInfo.h:50
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:206
Common Enum definitions for SQL processing.
Definition: sqltypes.h:44
bool is_distinct
Definition: TargetInfo.h:54
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:330
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:78