forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColumn.h
More file actions
143 lines (108 loc) · 4.88 KB
/
Copy pathColumn.h
File metadata and controls
143 lines (108 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2025 The DAPHNE Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <runtime/local/datastructures/DataObjectFactory.h>
#include <runtime/local/datastructures/Structure.h>
#include <runtime/local/datastructures/ValueTypeUtils.h>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <cstddef>
/**
* @brief A data structure that represents a single column in columnar query processing, all elements have the same
* value type and are stored contiguously in memory.
*/
template <typename ValueType> class Column : public Structure {
// `using`, so that we do not need to prefix each occurrence of these fields from the super-classes.
using Structure::numCols;
using Structure::numRows;
std::shared_ptr<ValueType[]> values{};
// Grant DataObjectFactory access to the private constructors and destructors.
template <class DataType, typename... ArgTypes> friend DataType *DataObjectFactory::create(ArgTypes...);
template <class DataType> friend void DataObjectFactory::destroy(const DataType *obj);
Column(size_t numRows, bool zero)
: Structure(numRows, 1), values(std::shared_ptr<ValueType[]>(new ValueType[numRows])) {
if (zero)
std::fill(values.get(), values.get() + numRows, ValueTypeUtils::defaultValue<ValueType>);
}
Column(size_t numRows, std::shared_ptr<ValueType[]> &values) : Structure(numRows, 1), values(values) {}
~Column() override = default;
void printValue(std::ostream &os, ValueType val) const {
if constexpr (std::is_same<ValueType, int8_t>::value || std::is_same<ValueType, uint8_t>::value)
os << static_cast<uint32_t>(val);
else
os << val;
}
public:
template <typename NewValueType> using WithValueType = Column<NewValueType>;
/**
* @brief The common type of all values in this column.
*/
using VT = ValueType;
static std::string getName() { return "Column"; }
size_t getNumDims() const override { return 1; }
size_t getNumItems() const override { return this->numRows; }
void print(std::ostream &os) const override {
os << "Column(" << numRows << ", " << ValueTypeUtils::cppNameFor<ValueType> << ')' << std::endl;
for (size_t r = 0; r < numRows; r++) {
printValue(os, values.get()[r]);
os << std::endl;
}
}
Column<ValueType> *sliceRow(size_t rl, size_t ru) const override {
throw std::runtime_error("slicing has not been implemented for Column yet");
}
Column<ValueType> *sliceCol(size_t cl, size_t cu) const override {
throw std::runtime_error("slicing has not been implemented for Column yet");
}
Column<ValueType> *slice(size_t rl, size_t ru, size_t cl, size_t cu) const override {
throw std::runtime_error("slicing has not been implemented for Column yet");
}
size_t serialize(std::vector<char> &buf) const override {
throw std::runtime_error("serialization has not been implemented for Column yet");
}
void shrinkNumRows(size_t numRows) {
if (numRows > this->numRows)
throw std::runtime_error("Column (shrinkNumRows): number of rows can only be shrunk");
// TODO Here we could reduce the allocated size of the values array.
this->numRows = numRows;
}
const ValueType *getValues() const { return values.get(); }
ValueType *getValues() { return values.get(); }
std::shared_ptr<ValueType[]> getValuesSharedPtr() const { return values; }
bool operator==(const Column<ValueType> &rhs) const {
// Note that we do not use the generic `get` interface here since this operator is meant to be used for writing
// tests for, besides others, those generic interfaces.
if (this == &rhs)
return true;
const size_t numRows = this->getNumRows();
if (numRows != rhs.getNumRows())
return false;
const ValueType *valuesLhs = this->getValues();
const ValueType *valuesRhs = rhs.getValues();
if (valuesLhs == valuesRhs)
return true;
for (size_t r = 0; r < numRows; r++)
if (valuesLhs[r] != valuesRhs[r])
return false;
return true;
}
};
template <typename ValueType> std::ostream &operator<<(std::ostream &os, const Column<ValueType> &obj) {
obj.print(os);
return os;
}