Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
string-value.h
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 
16 #ifndef IMPALA_RUNTIME_STRING_VALUE_H
17 #define IMPALA_RUNTIME_STRING_VALUE_H
18 
19 #include <string.h>
20 #include <string>
21 
22 #include "common/logging.h"
23 #include "udf/udf.h"
24 #include "util/hash-util.h"
25 #include "runtime/types.h"
26 
27 namespace impala {
28 
33 struct StringValue {
37  char* ptr;
38  int len;
39 
40  StringValue(char* ptr, int len): ptr(ptr), len(len) {
41  DCHECK_GE(len, 0);
42  }
43  StringValue(): ptr(NULL), len(0) {}
44 
47  StringValue(const std::string& s)
48  : ptr(const_cast<char*>(s.c_str())), len(s.size()) {
49  }
50 
55  StringValue(const char* s)
56  : ptr(const_cast<char*>(s)), len(strlen(s)) {
57  }
58 
63  int Compare(const StringValue& other) const;
64 
66  bool Eq(const StringValue& other) const;
67  bool operator==(const StringValue& other) const { return Eq(other); }
69  bool Ne(const StringValue& other) const { return !Eq(other); }
70  bool operator!=(const StringValue& other) const { return Ne(other); }
72  bool Le(const StringValue& other) const { return Compare(other) <= 0; }
73  bool operator<=(const StringValue& other) const { return Le(other); }
75  bool Ge(const StringValue& other) const { return Compare(other) >= 0; }
76  bool operator>=(const StringValue& other) const { return Ge(other); }
78  bool Lt(const StringValue& other) const { return Compare(other) < 0; }
79  bool operator<(const StringValue& other) const { return Lt(other); }
81  bool Gt(const StringValue& other) const { return Compare(other) > 0; }
82  bool operator>(const StringValue& other) const { return Gt(other); }
83 
84  std::string DebugString() const;
85 
87  StringValue Substring(int start_pos) const;
88 
94  StringValue Substring(int start_pos, int new_len) const;
95 
97  StringValue Trim() const;
98 
100  *sv = impala_udf::StringVal(reinterpret_cast<uint8_t*>(ptr), len);
101  }
102 
104  return StringValue(reinterpret_cast<char*>(sv.ptr), sv.len);
105  }
106 
109  inline static void PadWithSpaces(char* cptr, int64_t cptr_len, int64_t num_chars);
110 
112  inline static int64_t UnpaddedCharLength(const char* cptr, int64_t len);
113 
117  inline static char* CharSlotToPtr(void* slot, const ColumnType& type);
118  inline static const char* CharSlotToPtr(const void* slot, const ColumnType& type);
119 
121  static const char* LLVM_CLASS_NAME;
122 };
123 
125 inline std::size_t hash_value(const StringValue& v) {
126  return HashUtil::Hash(v.ptr, v.len, 0);
127 }
128 
129 std::ostream& operator<<(std::ostream& os, const StringValue& string_value);
130 
131 }
132 
133 #endif
bool Eq(const StringValue &other) const
==
bool Ge(const StringValue &other) const
>=
Definition: string-value.h:75
bool operator<(const StringValue &other) const
Definition: string-value.h:79
bool operator!=(const StringValue &other) const
Definition: string-value.h:70
bool operator<=(const StringValue &other) const
Definition: string-value.h:73
bool operator>(const StringValue &other) const
Definition: string-value.h:82
bool Lt(const StringValue &other) const
<
Definition: string-value.h:78
static void PadWithSpaces(char *cptr, int64_t cptr_len, int64_t num_chars)
uint8_t * ptr
Definition: udf.h:523
std::size_t hash_value(const Decimal4Value &v)
This function must be called 'hash_value' to be picked up by boost.
StringValue Trim() const
Trims leading and trailing spaces.
static int64_t UnpaddedCharLength(const char *cptr, int64_t len)
Returns number of characters in a char array (ignores trailing spaces)
bool operator==(const StringValue &other) const
Definition: string-value.h:67
int Compare(const StringValue &other) const
static uint32_t Hash(const void *data, int32_t bytes, uint32_t seed)
Definition: hash-util.h:135
bool Le(const StringValue &other) const
<=
Definition: string-value.h:72
bool operator>=(const StringValue &other) const
Definition: string-value.h:76
static StringValue FromStringVal(const impala_udf::StringVal &sv)
Definition: string-value.h:103
StringValue(const std::string &s)
Definition: string-value.h:47
StringValue Substring(int start_pos) const
Returns the substring starting at start_pos until the end of string.
static char * CharSlotToPtr(void *slot, const ColumnType &type)
static const char * LLVM_CLASS_NAME
For C++/IR interop, we need to be able to look up types by name.
Definition: string-value.h:121
StringValue(char *ptr, int len)
Definition: string-value.h:40
bool Gt(const StringValue &other) const
>
Definition: string-value.h:81
ostream & operator<<(ostream &os, const map< TNetworkAddress, llama::TAllocatedResource > &resources)
void ToStringVal(impala_udf::StringVal *sv) const
Definition: string-value.h:99
StringValue(const char *s)
Definition: string-value.h:55
bool Ne(const StringValue &other) const
!=
Definition: string-value.h:69
std::string DebugString() const
Definition: string-value.cc:24