Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
tuple.cc
Go to the documentation of this file.
1 // Copyright 2012 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 "runtime/tuple.h"
16 
17 #include <vector>
18 
19 #include "exprs/expr.h"
20 #include "exprs/expr-context.h"
21 #include "runtime/descriptors.h"
22 #include "runtime/mem-pool.h"
23 #include "runtime/raw-value.h"
24 #include "runtime/tuple-row.h"
25 #include "runtime/string-value.h"
26 #include "util/debug-util.h"
27 
28 #include "common/names.h"
29 
30 namespace impala {
31 
32  const char* Tuple::LLVM_CLASS_NAME = "class.impala::Tuple";
33 
34 Tuple* Tuple::DeepCopy(const TupleDescriptor& desc, MemPool* pool, bool convert_ptrs) {
35  Tuple* result = reinterpret_cast<Tuple*>(pool->Allocate(desc.byte_size()));
36  DeepCopy(result, desc, pool, convert_ptrs);
37  return result;
38 }
39 
41  bool convert_ptrs) {
42  memcpy(dst, this, desc.byte_size());
43  // allocate in the same pool and then copy all non-null string slots
44  for (vector<SlotDescriptor*>::const_iterator i = desc.string_slots().begin();
45  i != desc.string_slots().end(); ++i) {
46  DCHECK((*i)->type().IsVarLen());
47  if (!dst->IsNull((*i)->null_indicator_offset())) {
48  StringValue* string_v = dst->GetStringSlot((*i)->tuple_offset());
49  int offset = pool->GetCurrentOffset();
50  char* string_copy = reinterpret_cast<char*>(pool->Allocate(string_v->len));
51  memcpy(string_copy, string_v->ptr, string_v->len);
52  string_v->ptr = (convert_ptrs ? reinterpret_cast<char*>(offset) : string_copy);
53  }
54  }
55 }
56 
57 void Tuple::DeepCopy(const TupleDescriptor& desc, char** data, int* offset,
58  bool convert_ptrs) {
59  Tuple* dst = reinterpret_cast<Tuple*>(*data);
60  memcpy(dst, this, desc.byte_size());
61  *data += desc.byte_size();
62  *offset += desc.byte_size();
63  for (vector<SlotDescriptor*>::const_iterator i = desc.string_slots().begin();
64  i != desc.string_slots().end(); ++i) {
65  DCHECK((*i)->type().IsVarLen());
66  if (!dst->IsNull((*i)->null_indicator_offset())) {
67  StringValue* string_v = dst->GetStringSlot((*i)->tuple_offset());
68  memcpy(*data, string_v->ptr, string_v->len);
69  string_v->ptr = (convert_ptrs ? reinterpret_cast<char*>(*offset) : *data);
70  *data += string_v->len;
71  *offset += string_v->len;
72  }
73  }
74 }
75 
76 template <bool collect_string_vals>
78  TupleRow* row, const TupleDescriptor& desc,
79  const vector<ExprContext*>& materialize_expr_ctxs, MemPool* pool,
80  vector<StringValue*>* non_null_var_len_values, int* total_var_len) {
81  if (collect_string_vals) {
82  non_null_var_len_values->clear();
83  *total_var_len = 0;
84  }
85  memset(this, 0, desc.num_null_bytes());
86  // Evaluate the output_slot_exprs and place the results in the tuples.
87  int mat_expr_index = 0;
88  for (int i = 0; i < desc.slots().size(); ++i) {
89  SlotDescriptor* slot_desc = desc.slots()[i];
90  if (!slot_desc->is_materialized()) continue;
91  // The FE ensures we don't get any TYPE_NULL expressions by picking an arbitrary type
92  // when necessary, but does not do this for slot descs.
93  // TODO: revisit this logic in the FE
94  DCHECK(slot_desc->type().type == TYPE_NULL ||
95  slot_desc->type() == materialize_expr_ctxs[mat_expr_index]->root()->type());
96  void* src = materialize_expr_ctxs[mat_expr_index]->GetValue(row);
97  if (src != NULL) {
98  void* dst = GetSlot(slot_desc->tuple_offset());
99  RawValue::Write(src, dst, slot_desc->type(), pool);
100  if (collect_string_vals && slot_desc->type().IsVarLen()) {
101  StringValue* string_val = reinterpret_cast<StringValue*>(dst);
102  non_null_var_len_values->push_back(string_val);
103  *total_var_len += string_val->len;
104  }
105  } else {
106  SetNull(slot_desc->null_indicator_offset());
107  }
108  ++mat_expr_index;
109  }
110 
111  DCHECK_EQ(mat_expr_index, materialize_expr_ctxs.size());
112 }
113 
114 template void Tuple::MaterializeExprs<false>(TupleRow* row, const TupleDescriptor& desc,
115  const vector<ExprContext*>& materialize_expr_ctxs, MemPool* pool,
116  vector<StringValue*>* non_null_var_values, int* total_var_len);
117 
118 template void Tuple::MaterializeExprs<true>(TupleRow* row, const TupleDescriptor& desc,
119  const vector<ExprContext*>& materialize_expr_ctxs, MemPool* pool,
120  vector<StringValue*>* non_null_var_values, int* total_var_len);
121 }
bool IsVarLen() const
Definition: types.h:172
void SetNull(const NullIndicatorOffset &offset)
Definition: tuple.h:101
A tuple with 0 materialised slots is represented as NULL.
Definition: tuple.h:48
void * GetSlot(int offset)
Definition: tuple.h:118
int num_null_bytes() const
Definition: descriptors.h:301
const std::vector< SlotDescriptor * > & slots() const
Definition: descriptors.h:302
int byte_size() const
Definition: descriptors.h:300
const NullIndicatorOffset & null_indicator_offset() const
Definition: descriptors.h:89
Tuple * DeepCopy(const TupleDescriptor &desc, MemPool *pool, bool convert_ptrs=false)
Definition: tuple.cc:34
bool IsNull(const NullIndicatorOffset &offset) const
Definition: tuple.h:112
const std::vector< SlotDescriptor * > & string_slots() const
Definition: descriptors.h:303
PrimitiveType type
Definition: types.h:60
static const char * LLVM_CLASS_NAME
For C++/IR interop, we need to be able to look up types by name.
Definition: tuple.h:134
const ColumnType & type() const
Definition: descriptors.h:78
ObjectPool pool
int GetCurrentOffset() const
Definition: mem-pool.h:163
static void Write(const void *value, Tuple *tuple, const SlotDescriptor *slot_desc, MemPool *pool)
Definition: raw-value.cc:303
StringValue * GetStringSlot(int offset)
Definition: tuple.h:128
uint8_t offset[7 *64-sizeof(uint64_t)]
int tuple_offset() const
Definition: descriptors.h:88
void MaterializeExprs(TupleRow *row, const TupleDescriptor &desc, const std::vector< ExprContext * > &materialize_expr_ctxs, MemPool *pool, std::vector< StringValue * > *non_null_var_len_values=NULL, int *total_var_len=NULL)
bool is_materialized() const
Definition: descriptors.h:92
uint8_t * Allocate(int size)
Definition: mem-pool.h:92