OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TargetValue.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 
17 /*
18  * @file TargetValue.h
19  * @author Alex Suhan <alex@mapd.com>
20  * @brief High-level representation of SQL values.
21  *
22  * Copyright (c) 2014 MapD Technologies, Inc. All rights reserved.
23  */
24 
25 #ifndef QUERYENGINE_TARGETVALUE_H
26 #define QUERYENGINE_TARGETVALUE_H
27 
28 #include <boost/optional.hpp>
29 #include <boost/variant.hpp>
30 #include "Shared/Logger.h"
31 
32 #include <Shared/sqltypes.h>
33 
34 #include <cstdint>
35 #include <string>
36 #include <vector>
37 
39  int64_t i1;
40  int64_t i2;
41 
42  enum class ITVType { Int, Pair, Str, Arr, Null };
43 
45 
46  explicit InternalTargetValue(const int64_t i1_) : i1(i1_), ty(ITVType::Int) {}
47 
48  explicit InternalTargetValue(const int64_t i1_, const int64_t i2_)
49  : i1(i1_), i2(i2_), ty(ITVType::Pair) {}
50 
51  explicit InternalTargetValue(const std::string* s)
52  : i1(reinterpret_cast<int64_t>(s)), ty(ITVType::Str) {}
53 
54  explicit InternalTargetValue(const std::vector<int64_t>* v)
55  : i1(reinterpret_cast<int64_t>(v)), ty(ITVType::Arr) {}
56 
57  explicit InternalTargetValue() : ty(ITVType::Null) {}
58 
59  std::string strVal() const { return *reinterpret_cast<std::string*>(i1); }
60 
61  std::vector<int64_t> arrVal() const {
62  return *reinterpret_cast<std::vector<int64_t>*>(i1);
63  }
64 
65  bool isInt() const { return ty == ITVType::Int; }
66 
67  bool isPair() const { return ty == ITVType::Pair; }
68 
69  bool isNull() const { return ty == ITVType::Null; }
70 
71  bool isStr() const { return ty == ITVType::Str; }
72 
73  bool operator<(const InternalTargetValue& other) const {
74  switch (ty) {
75  case ITVType::Int:
76  CHECK(other.ty == ITVType::Int);
77  return i1 < other.i1;
78  case ITVType::Pair:
79  CHECK(other.ty == ITVType::Pair);
80  if (i1 != other.i1) {
81  return i1 < other.i1;
82  }
83  return i2 < other.i2;
84  case ITVType::Str:
85  CHECK(other.ty == ITVType::Str);
86  return strVal() < other.strVal();
87  case ITVType::Null:
88  return false;
89  default:
90  abort();
91  }
92  }
93 
94  bool operator==(const InternalTargetValue& other) const {
95  return !(*this < other || other < *this);
96  }
97 };
98 
100  std::shared_ptr<std::vector<double>> coords;
101 
102  GeoPointTargetValue(const std::vector<double>& coords)
103  : coords(std::make_shared<std::vector<double>>(coords)) {}
104 };
105 
107  std::shared_ptr<std::vector<double>> coords;
108 
109  GeoLineStringTargetValue(const std::vector<double>& coords)
110  : coords(std::make_shared<std::vector<double>>(coords)) {}
111 };
112 
114  std::shared_ptr<std::vector<double>> coords;
115  std::shared_ptr<std::vector<int32_t>> ring_sizes;
116 
117  GeoPolyTargetValue(const std::vector<double>& coords,
118  const std::vector<int32_t>& ring_sizes)
119  : coords(std::make_shared<std::vector<double>>(coords))
120  , ring_sizes(std::make_shared<std::vector<int32_t>>(ring_sizes)) {}
121 };
122 
124  std::shared_ptr<std::vector<double>> coords;
125  std::shared_ptr<std::vector<int32_t>> ring_sizes;
126  std::shared_ptr<std::vector<int32_t>> poly_rings;
127 
128  GeoMultiPolyTargetValue(const std::vector<double>& coords,
129  const std::vector<int32_t>& ring_sizes,
130  const std::vector<int32_t>& poly_rings)
131  : coords(std::make_shared<std::vector<double>>(coords))
132  , ring_sizes(std::make_shared<std::vector<int32_t>>(ring_sizes))
133  , poly_rings(std::make_shared<std::vector<int32_t>>(poly_rings)) {}
134 };
135 
137  std::shared_ptr<VarlenDatum> coords_data;
138 };
139 
141  std::shared_ptr<VarlenDatum> coords_data;
142 };
143 
145  std::shared_ptr<VarlenDatum> coords_data;
146  std::shared_ptr<VarlenDatum> ring_sizes_data;
147 };
148 
150  std::shared_ptr<VarlenDatum> coords_data;
151  std::shared_ptr<VarlenDatum> ring_sizes_data;
152  std::shared_ptr<VarlenDatum> poly_rings_data;
153 };
154 
155 using NullableString = boost::variant<std::string, void*>;
156 using ScalarTargetValue = boost::variant<int64_t, double, float, NullableString>;
157 using ArrayTargetValue = boost::optional<std::vector<ScalarTargetValue>>;
158 using GeoTargetValue = boost::variant<GeoPointTargetValue,
162 using GeoTargetValuePtr = boost::variant<GeoPointTargetValuePtr,
166 using TargetValue = boost::
167  variant<ScalarTargetValue, ArrayTargetValue, GeoTargetValue, GeoTargetValuePtr>;
168 
169 #endif // QUERYENGINE_TARGETVALUE_H
bool operator<(const InternalTargetValue &other) const
Definition: TargetValue.h:73
std::string strVal() const
Definition: TargetValue.h:59
bool isPair() const
Definition: TargetValue.h:67
InternalTargetValue(const int64_t i1_)
Definition: TargetValue.h:46
GeoPolyTargetValue(const std::vector< double > &coords, const std::vector< int32_t > &ring_sizes)
Definition: TargetValue.h:117
bool isStr() const
Definition: TargetValue.h:71
std::shared_ptr< VarlenDatum > coords_data
Definition: TargetValue.h:145
GeoMultiPolyTargetValue(const std::vector< double > &coords, const std::vector< int32_t > &ring_sizes, const std::vector< int32_t > &poly_rings)
Definition: TargetValue.h:128
boost::variant< GeoPointTargetValue, GeoLineStringTargetValue, GeoPolyTargetValue, GeoMultiPolyTargetValue > GeoTargetValue
Definition: TargetValue.h:161
std::shared_ptr< std::vector< int32_t > > ring_sizes
Definition: TargetValue.h:125
std::shared_ptr< VarlenDatum > poly_rings_data
Definition: TargetValue.h:152
Constants for Builtin SQL Types supported by MapD.
std::shared_ptr< std::vector< double > > coords
Definition: TargetValue.h:124
InternalTargetValue(const std::string *s)
Definition: TargetValue.h:51
std::shared_ptr< std::vector< int32_t > > poly_rings
Definition: TargetValue.h:126
std::shared_ptr< std::vector< double > > coords
Definition: TargetValue.h:107
GeoLineStringTargetValue(const std::vector< double > &coords)
Definition: TargetValue.h:109
GeoPointTargetValue(const std::vector< double > &coords)
Definition: TargetValue.h:102
CHECK(cgen_state)
boost::optional< std::vector< ScalarTargetValue >> ArrayTargetValue
Definition: TargetValue.h:157
std::shared_ptr< std::vector< int32_t > > ring_sizes
Definition: TargetValue.h:115
std::shared_ptr< VarlenDatum > ring_sizes_data
Definition: TargetValue.h:146
std::shared_ptr< std::vector< double > > coords
Definition: TargetValue.h:100
bool isNull() const
Definition: TargetValue.h:69
InternalTargetValue(const int64_t i1_, const int64_t i2_)
Definition: TargetValue.h:48
std::vector< int64_t > arrVal() const
Definition: TargetValue.h:61
std::shared_ptr< VarlenDatum > coords_data
Definition: TargetValue.h:141
boost::variant< std::string, void * > NullableString
Definition: TargetValue.h:155
bool isInt() const
Definition: TargetValue.h:65
std::shared_ptr< VarlenDatum > coords_data
Definition: TargetValue.h:137
boost::variant< ScalarTargetValue, ArrayTargetValue, GeoTargetValue, GeoTargetValuePtr > TargetValue
Definition: TargetValue.h:167
std::shared_ptr< VarlenDatum > coords_data
Definition: TargetValue.h:150
InternalTargetValue(const std::vector< int64_t > *v)
Definition: TargetValue.h:54
std::shared_ptr< std::vector< double > > coords
Definition: TargetValue.h:114
bool operator==(const InternalTargetValue &other) const
Definition: TargetValue.h:94
std::shared_ptr< VarlenDatum > ring_sizes_data
Definition: TargetValue.h:151
boost::variant< int64_t, double, float, NullableString > ScalarTargetValue
Definition: TargetValue.h:156
boost::variant< GeoPointTargetValuePtr, GeoLineStringTargetValuePtr, GeoPolyTargetValuePtr, GeoMultiPolyTargetValuePtr > GeoTargetValuePtr
Definition: TargetValue.h:165