Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
row-batch-list-test.cc
Go to the documentation of this file.
1 // Copyright 2013 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <cstdlib>
16 #include <cstdio>
17 #include <iostream>
18 #include <vector>
19 
20 #include <gtest/gtest.h>
21 
22 #include "exec/row-batch-list.h"
23 #include "runtime/descriptors.h"
24 #include "runtime/mem-pool.h"
25 #include "runtime/mem-tracker.h"
26 #include "runtime/string-value.h"
27 #include "runtime/tuple-row.h"
28 #include "util/cpu-info.h"
29 #include "util/runtime-profile.h"
31 
32 #include "common/names.h"
33 
34 namespace impala {
35 
37  public:
39 
40  protected:
44 
45  virtual void SetUp() {
46  DescriptorTblBuilder builder(&pool_);
47  builder.DeclareTuple() << TYPE_INT;
48  DescriptorTbl* desc_tbl = builder.Build();
49  vector<bool> nullable_tuples(1, false);
50  vector<TTupleId> tuple_id(1, (TTupleId) 0);
51  desc_ = pool_.Add(new RowDescriptor(*desc_tbl, tuple_id, nullable_tuples));
52  }
53 
54  RowBatch* CreateRowBatch(int start, int end) {
55  int num_rows = end - start + 1;
56  RowBatch* batch = pool_.Add(new RowBatch(*desc_, num_rows, &tracker_));
57  int32_t* tuple_mem = reinterpret_cast<int32_t*>(
58  batch->tuple_data_pool()->Allocate(sizeof(int32_t) * num_rows));
59 
60  for (int i = start; i <= end; ++i) {
61  int idx = batch->AddRow();
62  TupleRow* row = batch->GetRow(idx);
63  *tuple_mem = i;
64  row->SetTuple(0, reinterpret_cast<Tuple*>(tuple_mem));
65 
66  batch->CommitLastRow();
67  tuple_mem++;
68  }
69  return batch;
70  }
71 
72  // Validate that row contains the expected value
73  void ValidateMatch(TupleRow* row, int32_t expected) {
74  EXPECT_EQ(expected, *reinterpret_cast<int32_t*>(row->GetTuple(0)));
75  }
76 
77  void FullScan(RowBatchList* list, int start, int end) {
78  EXPECT_LT(start, end);
80  int i = start;
81  while (!it.AtEnd()) {
82  EXPECT_TRUE(it.GetRow() != NULL);
83  ValidateMatch(it.GetRow(), i);
84  it.Next();
85  ++i;
86  }
87  EXPECT_EQ(end, i - 1);
88  }
89 };
90 
91 // This tests inserts the rows [0->5] to list. It validates that they are all there.
92 TEST_F(RowBatchListTest, BasicTest) {
93  RowBatchList row_list;
94  RowBatch* batch = CreateRowBatch(0, 5);
95  row_list.AddRowBatch(batch);
96  EXPECT_EQ(row_list.total_num_rows(), 6);
97 
98  // Do a full table scan and validate returned pointers
99  FullScan(&row_list, 0, 5);
100 }
101 
102 // This tests an empty batch is handled correctly.
103 TEST_F(RowBatchListTest, EmptyBatchTest) {
104  RowBatchList row_list;
105  RowBatch* batch = pool_.Add(new RowBatch(*desc_, 1, &tracker_));
106  row_list.AddRowBatch(batch);
107  EXPECT_EQ(row_list.total_num_rows(), 0);
109  EXPECT_TRUE(it.AtEnd());
110 }
111 
112 // This tests inserts 100 row batches of 1024 rows each to list. It validates that they
113 // are all there.
114 TEST_F(RowBatchListTest, MultipleRowBatchesTest) {
115  RowBatchList row_list;
116 
117  int BATCH_SIZE = 1024;
118  for (int batch_idx = 0; batch_idx < 100; ++batch_idx) {
119  int batch_start = batch_idx * BATCH_SIZE;
120  int batch_end = batch_start + BATCH_SIZE - 1;
121  RowBatch* batch = CreateRowBatch(batch_start, batch_end);
122  row_list.AddRowBatch(batch);
123  EXPECT_EQ(row_list.total_num_rows(), batch_end + 1);
124  FullScan(&row_list, 0, batch_end);
125  }
126 }
127 
128 }
129 
130 int main(int argc, char** argv) {
131  ::testing::InitGoogleTest(&argc, argv);
133  return RUN_ALL_TESTS();
134 }
TupleRowIterator Iterator()
Returns a new iterator over all the tuple rows.
TEST_F(InstructionCounterTest, Count)
Tuple * GetTuple(int tuple_idx)
Definition: tuple-row.h:30
A simple iterator used to scan over all the rows stored in the list.
void AddRowBatch(RowBatch *row_batch)
TupleRow * GetRow(int row_idx)
Definition: row-batch.h:140
const int BATCH_SIZE
bool AtEnd()
Returns true if this iterator is at the end, i.e. GetRow() cannot be called.
void FullScan(RowBatchList *list, int start, int end)
void ValidateMatch(TupleRow *row, int32_t expected)
This class is thread-safe.
Definition: mem-tracker.h:61
void CommitLastRow()
Definition: row-batch.h:109
MemPool * tuple_data_pool()
Definition: row-batch.h:148
uint64_t Test(T *ht, const ProbeTuple *input, uint64_t num_tuples)
void SetTuple(int tuple_idx, Tuple *tuple)
Definition: tuple-row.h:34
TupleDescBuilder & DeclareTuple()
int main(int argc, char **argv)
static void Init()
Initialize CpuInfo.
Definition: cpu-info.cc:75
void Next()
Increments the iterator. No-op if the iterator is at the end.
RowBatch * CreateRowBatch(int start, int end)
uint8_t * Allocate(int size)
Definition: mem-pool.h:92
int64_t total_num_rows()
Returns the total number of rows in all row batches.