OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Datum.h
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2022 HEAVY.AI, Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
24 #pragma once
25 
26 #include "funcannotations.h"
27 
28 #include <type_traits>
29 
30 #ifndef __CUDACC__
31 #include <string_view>
32 #endif
33 
34 #ifndef __CUDACC__
35 static_assert(!std::is_trivial<std::string_view>::value);
36 #endif
37 
38 // Since std::string_view is not a Trivial class, we use StringView instead,
39 // which is both Trivial and has Standard-layout (aka POD).
40 // This is like std::string_view but can be used in the context of cuda and llvm.
41 struct StringView {
42  char const* ptr_;
43  uint64_t len_;
44 
45 #ifndef __CUDACC__
46  std::string_view stringView() const {
47  return {ptr_, len_};
48  }
49 #endif
50 };
51 
52 static_assert(sizeof(char) == sizeof(int8_t));
53 static_assert(std::is_standard_layout<StringView>::value);
54 static_assert(std::is_trivial<StringView>::value);
55 
56 struct VarlenDatum {
57  size_t length;
58  int8_t* pointer;
59  bool is_null;
60 
61  DEVICE VarlenDatum() : length(0), pointer(nullptr), is_null(true) {}
62  DEVICE virtual ~VarlenDatum() {}
63 
64  VarlenDatum(const size_t l, int8_t* p, const bool n)
65  : length(l), pointer(p), is_null(n) {}
66 };
67 
68 static_assert(!std::is_standard_layout<VarlenDatum>::value);
69 static_assert(!std::is_trivial<VarlenDatum>::value);
70 
71 union Datum {
72  int8_t boolval;
73  int8_t tinyintval;
74  int16_t smallintval;
75  int32_t intval;
76  int64_t bigintval;
77  float floatval;
78  double doubleval;
80 #ifndef __CUDACC__
81  std::string* stringval; // string value
82 #endif
83 };
84 
85 template <typename T>
87  static_assert(std::is_same_v<T, bool> || std::is_same_v<T, int8_t> ||
88  std::is_same_v<T, int16_t> || std::is_same_v<T, int32_t> ||
89  std::is_same_v<T, int64_t> || std::is_same_v<T, float> ||
90  std::is_same_v<T, double> || std::is_same_v<T, VarlenDatum*>
91 #ifndef __CUDACC__
92  || std::is_same_v<T, std::string*>
93 #endif
94  ,
95  "Type T must be one of the allowed types");
96  Datum d;
97  if constexpr (std::is_same_v<T, bool>) {
98  d.boolval = static_cast<int8_t>(val);
99  } else if constexpr (std::is_same_v<T, int8_t>) {
100  d.tinyintval = val;
101  } else if constexpr (std::is_same_v<T, int16_t>) {
102  d.smallintval = val;
103  } else if constexpr (std::is_same_v<T, int32_t>) {
104  d.intval = val;
105  } else if constexpr (std::is_same_v<T, int64_t>) {
106  d.bigintval = val;
107  } else if constexpr (std::is_same_v<T, float>) {
108  d.floatval = val;
109  } else if constexpr (std::is_same_v<T, double>) {
110  d.doubleval = val;
111  } else if constexpr (std::is_same_v<T, VarlenDatum*>) {
112  // deleting `arrayval` is caller's responsibility
113  d.arrayval = val;
114 #ifndef __CUDACC__
115  } else if constexpr (std::is_same_v<T, std::string*>) {
116  // deleting `stringval` is caller's responsibility
117  d.stringval = val;
118 #endif
119  }
120  return d;
121 }
int8_t tinyintval
Definition: Datum.h:73
DEVICE VarlenDatum()
Definition: Datum.h:61
bool is_null
Definition: Datum.h:59
Datum make_datum(T val)
Definition: Datum.h:86
int8_t boolval
Definition: Datum.h:72
VarlenDatum * arrayval
Definition: Datum.h:79
uint64_t len_
Definition: Datum.h:43
int32_t intval
Definition: Datum.h:75
int8_t * pointer
Definition: Datum.h:58
#define DEVICE
float floatval
Definition: Datum.h:77
VarlenDatum(const size_t l, int8_t *p, const bool n)
Definition: Datum.h:64
int64_t bigintval
Definition: Datum.h:76
int16_t smallintval
Definition: Datum.h:74
std::string_view stringView() const
Definition: Datum.h:46
bool g_enable_smem_group_by true
std::string * stringval
Definition: Datum.h:81
virtual DEVICE ~VarlenDatum()
Definition: Datum.h:62
constexpr double n
Definition: Utm.h:38
char const * ptr_
Definition: Datum.h:42
Definition: Datum.h:71
double doubleval
Definition: Datum.h:78
size_t length
Definition: Datum.h:57