Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hdfs-scanner-ir.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 "exec/hdfs-scanner.h"
16 #include "runtime/row-batch.h"
17 #include "util/string-parser.h"
19 
20 #include "common/names.h"
21 
22 using namespace impala;
23 
24 // Functions in this file are cross compiled to IR with clang. These functions
25 // are modified at runtime with a query specific codegen'd WriteTuple
26 
27 // This function will output tuples to the row batch from parsed field locations.
28 // The fields locations should be aligned to the start of the tuple (field at 0 is
29 // the first materialized slot).
30 // This function takes more arguments than are strictly necessary (they could be
31 // computed inside this function) but this is done to minimize the clang dependencies,
32 // specifically, calling function on the scan node.
33 int HdfsScanner::WriteAlignedTuples(MemPool* pool, TupleRow* tuple_row, int row_size,
34  FieldLocation* fields, int num_tuples, int max_added_tuples,
35  int slots_per_tuple, int row_idx_start) {
36 
37  DCHECK(tuple_ != NULL);
38  uint8_t* tuple_row_mem = reinterpret_cast<uint8_t*>(tuple_row);
39  uint8_t* tuple_mem = reinterpret_cast<uint8_t*>(tuple_);
40  Tuple* tuple = reinterpret_cast<Tuple*>(tuple_mem);
41 
42  uint8_t error[slots_per_tuple];
43  memset(error, 0, sizeof(error));
44 
45  int tuples_returned = 0;
46 
47  // Loop through the fields and materialize all the tuples
48  for (int i = 0; i < num_tuples; ++i) {
49  uint8_t error_in_row = false;
50  // Materialize a single tuple. This function will be replaced by a codegen'd
51  // function.
52  if (WriteCompleteTuple(pool, fields, tuple, tuple_row, template_tuple_,
53  error, &error_in_row)) {
54  ++tuples_returned;
55  tuple_mem += tuple_byte_size_;
56  tuple_row_mem += row_size;
57  tuple = reinterpret_cast<Tuple*>(tuple_mem);
58  tuple_row = reinterpret_cast<TupleRow*>(tuple_row_mem);
59  }
60 
61  // Report parse errors
62  if (UNLIKELY(error_in_row)) {
63  if (!ReportTupleParseError(fields, error, i + row_idx_start)) {
64  return -1;
65  }
66  }
67 
68  // Advance to the start of the next tuple
69  fields += slots_per_tuple;
70 
71  if (tuples_returned == max_added_tuples) {
72  break;
73  }
74  }
75 
76  return tuples_returned;
77 }
78 
80  return conjunct_ctxs_[idx];
81 }
82 
83 // Define the string parsing functions for llvm. Stamp out the templated functions
84 #ifdef IR_COMPILE
85 extern "C"
86 bool IrStringToBool(const char* s, int len, StringParser::ParseResult* result) {
87  return StringParser::StringToBool(s, len, result);
88 }
89 
90 int8_t IrStringToInt8(const char* s, int len, StringParser::ParseResult* result) {
91  return StringParser::StringToInt<int8_t>(s, len, result);
92 }
93 
94 extern "C"
95 int16_t IrStringToInt16(const char* s, int len, StringParser::ParseResult* result) {
96  return StringParser::StringToInt<int16_t>(s, len, result);
97 }
98 
99 extern "C"
100 int32_t IrStringToInt32(const char* s, int len, StringParser::ParseResult* result) {
101  return StringParser::StringToInt<int32_t>(s, len, result);
102 }
103 
104 extern "C"
105 int64_t IrStringToInt64(const char* s, int len, StringParser::ParseResult* result) {
106  return StringParser::StringToInt<int64_t>(s, len, result);
107 }
108 
109 extern "C"
110 float IrStringToFloat(const char* s, int len, StringParser::ParseResult* result) {
111  return StringParser::StringToFloat<float>(s, len, result);
112 }
113 
114 extern "C"
115 double IrStringToDouble(const char* s, int len, StringParser::ParseResult* result) {
116  return StringParser::StringToFloat<double>(s, len, result);
117 }
118 
119 extern "C"
120 bool IrIsNullString(const char* data, int len) {
121  return data == NULL || (len == 2 && data[0] == '\\' && data[1] == 'N');
122 }
123 
124 extern "C"
125 bool IrGenericIsNullString(const char* s, int slen, const char* n, int nlen) {
126  return s == NULL || (slen == nlen && StringCompare(s, slen, n, nlen, slen) == 0);
127 }
128 #endif
ExprContext * GetConjunctCtx(int idx) const
int tuple_byte_size_
Fixed size of each tuple, in bytes.
Definition: hdfs-scanner.h:167
A tuple with 0 materialised slots is represented as NULL.
Definition: tuple.h:48
std::vector< ExprContext * > conjunct_ctxs_
Definition: hdfs-scanner.h:154
ObjectPool pool
static bool StringToBool(const char *s, int len, ParseResult *result)
Parses a string for 'true' or 'false', case insensitive.
Definition: string-parser.h:87
bool WriteCompleteTuple(MemPool *pool, FieldLocation *fields, Tuple *tuple, TupleRow *tuple_row, Tuple *template_tuple, uint8_t *error_fields, uint8_t *error_in_row)
#define UNLIKELY(expr)
Definition: compiler-util.h:33
int WriteAlignedTuples(MemPool *pool, TupleRow *tuple_row_mem, int row_size, FieldLocation *fields, int num_tuples, int max_added_tuples, int slots_per_tuple, int row_start_indx)
Tuple * tuple_
Current tuple pointer into tuple_mem_.
Definition: hdfs-scanner.h:170
static int StringCompare(const char *s1, int n1, const char *s2, int n2, int len)
bool ReportTupleParseError(FieldLocation *fields, uint8_t *errors, int row_idx)