OmniSciDB  085a039ca4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ChunkMetadata.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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 #pragma once
18 
19 #include <cstddef>
20 #include "../Shared/sqltypes.h"
21 #include "Shared/StringTransform.h"
22 #include "Shared/types.h"
23 
24 #include "Logger/Logger.h"
25 
26 struct ChunkStats {
29  bool has_nulls;
30 };
31 
32 struct ChunkMetadata {
34  size_t numBytes;
35  size_t numElements;
37 
38  std::string dump() const {
40  // Unencoded strings have no min/max.
41  if (type.is_string() && type.get_compression() == kENCODING_NONE) {
42  return "type: " + sqlType.get_type_name() + " numBytes: " + to_string(numBytes) +
43  " numElements " + to_string(numElements) + " min: <invalid>" +
44  " max: <invalid>" + " has_nulls: " + to_string(chunkStats.has_nulls);
45  } else if (type.is_string()) {
46  return "type: " + sqlType.get_type_name() + " numBytes: " + to_string(numBytes) +
47  " numElements " + to_string(numElements) +
48  " min: " + to_string(chunkStats.min.intval) +
49  " max: " + to_string(chunkStats.max.intval) +
50  " has_nulls: " + to_string(chunkStats.has_nulls);
51  } else {
52  return "type: " + sqlType.get_type_name() + " numBytes: " + to_string(numBytes) +
53  " numElements " + to_string(numElements) +
54  " min: " + DatumToString(chunkStats.min, type) +
55  " max: " + DatumToString(chunkStats.max, type) +
56  " has_nulls: " + to_string(chunkStats.has_nulls);
57  }
58  }
59 
60  std::string toString() const { return dump(); }
61 
62  ChunkMetadata(const SQLTypeInfo& sql_type,
63  const size_t num_bytes,
64  const size_t num_elements,
65  const ChunkStats& chunk_stats)
66  : sqlType(sql_type)
67  , numBytes(num_bytes)
68  , numElements(num_elements)
69  , chunkStats(chunk_stats) {}
70 
72 
73  template <typename T>
74  void fillChunkStats(const T min, const T max, const bool has_nulls) {
75  chunkStats.has_nulls = has_nulls;
76  switch (sqlType.get_type()) {
77  case kBOOLEAN: {
80  break;
81  }
82  case kTINYINT: {
85  break;
86  }
87  case kSMALLINT: {
90  break;
91  }
92  case kINT: {
93  chunkStats.min.intval = min;
94  chunkStats.max.intval = max;
95  break;
96  }
97  case kBIGINT:
98  case kNUMERIC:
99  case kDECIMAL: {
100  chunkStats.min.bigintval = min;
101  chunkStats.max.bigintval = max;
102  break;
103  }
104  case kTIME:
105  case kTIMESTAMP:
106  case kDATE: {
107  chunkStats.min.bigintval = min;
108  chunkStats.max.bigintval = max;
109  break;
110  }
111  case kFLOAT: {
112  chunkStats.min.floatval = min;
113  chunkStats.max.floatval = max;
114  break;
115  }
116  case kDOUBLE: {
117  chunkStats.min.doubleval = min;
118  chunkStats.max.doubleval = max;
119  break;
120  }
121  case kVARCHAR:
122  case kCHAR:
123  case kTEXT:
125  chunkStats.min.intval = min;
126  chunkStats.max.intval = max;
127  }
128  break;
129  default: {
130  break;
131  }
132  }
133  }
134 
135  void fillChunkStats(const Datum min, const Datum max, const bool has_nulls) {
136  chunkStats.has_nulls = has_nulls;
137  chunkStats.min = min;
138  chunkStats.max = max;
139  }
140 
141  bool operator==(const ChunkMetadata& that) const {
142  return sqlType == that.sqlType && numBytes == that.numBytes &&
143  numElements == that.numElements &&
145  that.chunkStats.min,
148  that.chunkStats.max,
151  }
152 };
153 
154 inline int64_t extract_min_stat_int_type(const ChunkStats& stats, const SQLTypeInfo& ti) {
155  return extract_int_type_from_datum(stats.min, ti);
156 }
157 
158 inline int64_t extract_max_stat_int_type(const ChunkStats& stats, const SQLTypeInfo& ti) {
159  return extract_int_type_from_datum(stats.max, ti);
160 }
161 
162 inline double extract_min_stat_fp_type(const ChunkStats& stats, const SQLTypeInfo& ti) {
163  return extract_fp_type_from_datum(stats.min, ti);
164 }
165 
166 inline double extract_max_stat_fp_type(const ChunkStats& stats, const SQLTypeInfo& ti) {
167  return extract_fp_type_from_datum(stats.max, ti);
168 }
169 
170 using ChunkMetadataMap = std::map<int, std::shared_ptr<ChunkMetadata>>;
171 using ChunkMetadataVector =
172  std::vector<std::pair<ChunkKey, std::shared_ptr<ChunkMetadata>>>;
int8_t tinyintval
Definition: sqltypes.h:212
std::string DatumToString(Datum d, const SQLTypeInfo &ti)
Definition: Datum.cpp:392
Definition: sqltypes.h:49
double extract_max_stat_fp_type(const ChunkStats &stats, const SQLTypeInfo &ti)
ChunkMetadata(const SQLTypeInfo &sql_type, const size_t num_bytes, const size_t num_elements, const ChunkStats &chunk_stats)
Definition: ChunkMetadata.h:62
void fillChunkStats(const T min, const T max, const bool has_nulls)
Definition: ChunkMetadata.h:74
bool has_nulls
Definition: ChunkMetadata.h:29
std::string dump() const
Definition: ChunkMetadata.h:38
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
int32_t intval
Definition: sqltypes.h:214
int64_t extract_max_stat_int_type(const ChunkStats &stats, const SQLTypeInfo &ti)
std::string to_string(char const *&&v)
ChunkStats chunkStats
Definition: ChunkMetadata.h:36
void fillChunkStats(const Datum min, const Datum max, const bool has_nulls)
std::map< int, std::shared_ptr< ChunkMetadata >> ChunkMetadataMap
float floatval
Definition: sqltypes.h:216
bool DatumEqual(const Datum a, const Datum b, const SQLTypeInfo &ti)
Definition: Datum.cpp:342
double extract_min_stat_fp_type(const ChunkStats &stats, const SQLTypeInfo &ti)
int64_t extract_int_type_from_datum(const Datum datum, const SQLTypeInfo &ti)
Definition: Datum.cpp:455
int64_t bigintval
Definition: sqltypes.h:215
int64_t extract_min_stat_int_type(const ChunkStats &stats, const SQLTypeInfo &ti)
std::vector< std::pair< ChunkKey, std::shared_ptr< ChunkMetadata >>> ChunkMetadataVector
int16_t smallintval
Definition: sqltypes.h:213
Definition: sqltypes.h:52
Definition: sqltypes.h:53
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:337
std::string toString() const
Definition: ChunkMetadata.h:60
std::string get_type_name() const
Definition: sqltypes.h:443
Definition: sqltypes.h:41
double extract_fp_type_from_datum(const Datum datum, const SQLTypeInfo &ti)
Definition: Datum.cpp:481
Definition: sqltypes.h:45
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:865
SQLTypeInfo sqlType
Definition: ChunkMetadata.h:33
bool operator==(const ChunkMetadata &that) const
bool is_array() const
Definition: sqltypes.h:518
double doubleval
Definition: sqltypes.h:217
size_t numElements
Definition: ChunkMetadata.h:35