forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimed_evaluate.h
More file actions
136 lines (112 loc) · 4.06 KB
/
Copy pathtimed_evaluate.h
File metadata and controls
136 lines (112 loc) · 4.06 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
#include <memory>
#include <vector>
#include "benchmark/benchmark.h"
#include "gandiva/arrow.h"
#include "gandiva/filter.h"
#include "gandiva/projector.h"
#include "gandiva/tests/generate_data.h"
#pragma once
#define THOUSAND (1024)
#define MILLION (1024 * 1024)
#define NUM_BATCHES 16
namespace gandiva {
template <typename C_TYPE>
std::vector<C_TYPE> GenerateData(int num_records, DataGenerator<C_TYPE>& data_generator) {
std::vector<C_TYPE> data;
for (int i = 0; i < num_records; i++) {
data.push_back(data_generator.GenerateData());
}
return data;
}
class BaseEvaluator {
public:
virtual ~BaseEvaluator() = default;
virtual Status Evaluate(arrow::RecordBatch& batch, arrow::MemoryPool* pool) = 0;
};
class ProjectEvaluator : public BaseEvaluator {
public:
explicit ProjectEvaluator(std::shared_ptr<Projector> projector)
: projector_(projector) {}
Status Evaluate(arrow::RecordBatch& batch, arrow::MemoryPool* pool) override {
arrow::ArrayVector outputs;
return projector_->Evaluate(batch, pool, &outputs);
}
private:
std::shared_ptr<Projector> projector_;
};
class FilterEvaluator : public BaseEvaluator {
public:
explicit FilterEvaluator(std::shared_ptr<Filter> filter) : filter_(filter) {}
Status Evaluate(arrow::RecordBatch& batch, arrow::MemoryPool* pool) override {
if (selection_ == nullptr || selection_->GetMaxSlots() < batch.num_rows()) {
auto status = SelectionVector::MakeInt16(batch.num_rows(), pool, &selection_);
if (!status.ok()) {
return status;
}
}
return filter_->Evaluate(batch, selection_);
}
private:
std::shared_ptr<Filter> filter_;
std::shared_ptr<SelectionVector> selection_;
};
template <typename TYPE, typename C_TYPE>
Status TimedEvaluate(SchemaPtr schema, BaseEvaluator& evaluator,
DataGenerator<C_TYPE>& data_generator, arrow::MemoryPool* pool,
int num_records, int batch_size, benchmark::State& state) {
int num_remaining = num_records;
int num_fields = schema->num_fields();
int num_calls = 0;
Status status;
// Generate batches of data
std::shared_ptr<arrow::RecordBatch> batches[NUM_BATCHES];
for (int i = 0; i < NUM_BATCHES; i++) {
// generate data for all columns in the schema
std::vector<ArrayPtr> columns;
for (int col = 0; col < num_fields; col++) {
std::vector<C_TYPE> data = GenerateData<C_TYPE>(batch_size, data_generator);
std::vector<bool> validity(batch_size, true);
ArrayPtr col_data =
MakeArrowArray<TYPE, C_TYPE>(schema->field(col)->type(), data, validity);
columns.push_back(col_data);
}
// make the record batch
std::shared_ptr<arrow::RecordBatch> batch =
arrow::RecordBatch::Make(schema, batch_size, columns);
batches[i] = batch;
}
for (auto _ : state) {
int num_in_batch = batch_size;
num_remaining = num_records;
while (num_remaining > 0) {
if (batch_size > num_remaining) {
num_in_batch = num_remaining;
}
status = evaluator.Evaluate(*(batches[num_calls % NUM_BATCHES]), pool);
if (!status.ok()) {
state.SkipWithError("Evaluation of the batch failed");
return status;
}
num_calls++;
num_remaining -= num_in_batch;
}
}
return Status::OK();
}
} // namespace gandiva