Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
expr-test.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 <map>
16 #include <math.h>
17 #include <string>
18 #include <time.h>
19 
20 #include <boost/assign/list_of.hpp>
21 #include <boost/date_time/c_local_time_adjustor.hpp>
22 #include <boost/date_time/posix_time/posix_time.hpp>
23 #include <boost/lexical_cast.hpp>
24 #include <boost/random/mersenne_twister.hpp>
25 #include <boost/unordered_map.hpp>
26 #include <gtest/gtest.h>
27 
28 #include "codegen/llvm-codegen.h"
29 #include "common/init.h"
30 #include "common/object-pool.h"
31 #include "exprs/expr-context.h"
33 #include "exprs/like-predicate.h"
34 #include "exprs/literal.h"
35 #include "exprs/null-literal.h"
36 #include "gen-cpp/Exprs_types.h"
37 #include "gen-cpp/hive_metastore_types.h"
38 #include "rpc/thrift-client.h"
39 #include "rpc/thrift-server.h"
40 #include "runtime/raw-value.h"
41 #include "runtime/string-value.h"
43 #include "service/fe-support.h"
44 #include "service/impala-server.h"
47 #include "util/debug-util.h"
48 #include "util/string-parser.h"
49 #include "util/test-info.h"
50 
51 #include "common/names.h"
52 
53 DECLARE_int32(be_port);
54 DECLARE_int32(beeswax_port);
55 DECLARE_string(impalad);
56 DECLARE_bool(abort_on_config_error);
57 DECLARE_bool(disable_optimization_passes);
58 DECLARE_bool(use_utc_for_unix_timestamp_conversions);
59 
60 namespace posix_time = boost::posix_time;
61 using boost::assign::list_of;
62 using boost::bad_lexical_cast;
63 using boost::date_time::c_local_adjustor;
64 using boost::posix_time::from_time_t;
65 using boost::posix_time::ptime;
66 using namespace Apache::Hadoop::Hive;
67 using namespace impala;
68 using namespace llvm;
69 
70 namespace impala {
73 
74 template <typename ORIGINAL_TYPE, typename VAL_TYPE>
75 string LiteralToString(VAL_TYPE val) {
76  return lexical_cast<string>(val);
77 }
78 
79 template<>
80 string LiteralToString<float, float>(float val) {
81  stringstream ss;
82  ss << "cast("
83  << lexical_cast<string>(val)
84  << " as float)";
85  return ss.str();
86 }
87 
88 template<>
89 string LiteralToString<float, double>(double val) {
90  stringstream ss;
91  ss << "cast("
92  << lexical_cast<string>(val)
93  << " as float)";
94  return ss.str();
95 }
96 
97 template<>
98 string LiteralToString<double, double>(double val) {
99  stringstream ss;
100  ss << "cast("
101  << lexical_cast<string>(val)
102  << " as double)";
103  return ss.str();
104 }
105 
106 // Override the time zone for the duration of the scope. The time zone is overridden
107 // using an environment variable there is no risk of making a permanent system change
108 // and no special permissions are needed. This is not thread-safe.
110  public:
111  ScopedTimeZoneOverride(string time_zone) {
112  original_time_zone_ = getenv("TZ");
113  setenv("TZ", time_zone.c_str(), /*overwrite*/ true);
114  tzset();
115  }
116 
118  if (original_time_zone_ == NULL) {
119  unsetenv("TZ");
120  } else {
121  setenv("TZ", original_time_zone_, /*overwrite*/ true);
122  }
123  tzset();
124  }
125 
126  private:
128 };
129 
130 // Enable FLAGS_use_local_tz_for_unix_timestamp_conversions for the duration of the scope.
132  public:
134  FLAGS_use_local_tz_for_unix_timestamp_conversions = true;
135  }
136 
138  FLAGS_use_local_tz_for_unix_timestamp_conversions = false;
139  }
140 };
141 
142 class ExprTest : public testing::Test {
143  protected:
144  // Maps from enum value of primitive integer type to the minimum value that is
145  // outside of the next smaller-resolution type. For example the value for type
146  // TYPE_SMALLINT is numeric_limits<int8_t>::max()+1. There is a GREATEST test in
147  // the MathFunctions tests that requires this to be an ordered map.
148  typedef map<int, int64_t> IntValMap;
150 
151  // Maps from primitive float type to smallest positive value that is larger
152  // than the largest value of the next smaller-resolution type.
153  typedef unordered_map<int, double> FloatValMap;
155 
156  // Maps from enum value of primitive type to a string representation of a default
157  // value for testing. For int and float types the strings represent the corresponding
158  // min values (in the maps above). For non-numeric types the default values are listed
159  // below.
160  unordered_map<int, string> default_type_strs_;
165  // Corresponding default values.
169 
170  // This is used to hold the return values from the query executor.
172 
173  virtual void SetUp() {
174  min_int_values_[TYPE_TINYINT] = 1;
175  min_int_values_[TYPE_SMALLINT] =
176  static_cast<int64_t>(numeric_limits<int8_t>::max()) + 1;
177  min_int_values_[TYPE_INT] = static_cast<int64_t>(numeric_limits<int16_t>::max()) + 1;
178  min_int_values_[TYPE_BIGINT] =
179  static_cast<int64_t>(numeric_limits<int32_t>::max()) + 1;
180 
181  min_float_values_[TYPE_FLOAT] = 1.1;
182  min_float_values_[TYPE_DOUBLE] =
183  static_cast<double>(numeric_limits<float>::max()) + 1.1;
184 
185  // Set up default test types, values, and strings.
186  default_bool_str_ = "false";
187  default_string_str_ = "'abc'";
188  default_timestamp_str_ = "cast('2011-01-01 09:01:01' as timestamp)";
189  default_decimal_str_ = "1.23";
190  default_bool_val_ = false;
191  default_string_val_ = "abc";
192  default_timestamp_val_ = TimestampValue(1293872461);
193  default_type_strs_[TYPE_TINYINT] =
194  lexical_cast<string>(min_int_values_[TYPE_TINYINT]);
195  default_type_strs_[TYPE_SMALLINT] =
196  lexical_cast<string>(min_int_values_[TYPE_SMALLINT]);
197  default_type_strs_[TYPE_INT] =
198  lexical_cast<string>(min_int_values_[TYPE_INT]);
199  default_type_strs_[TYPE_BIGINT] =
200  lexical_cast<string>(min_int_values_[TYPE_BIGINT]);
201  // Don't use lexical cast here because it results
202  // in a string 1.1000000000000001 that messes up the tests.
203  stringstream ss;
204  ss << "cast("
205  << lexical_cast<string>(min_float_values_[TYPE_FLOAT]) << " as float)";
206  default_type_strs_[TYPE_FLOAT] = ss.str();
207  ss.str("");
208  ss << "cast("
209  << lexical_cast<string>(min_float_values_[TYPE_FLOAT]) << " as double)";
210  default_type_strs_[TYPE_DOUBLE] = ss.str();
211  default_type_strs_[TYPE_BOOLEAN] = default_bool_str_;
212  default_type_strs_[TYPE_STRING] = default_string_str_;
213  default_type_strs_[TYPE_TIMESTAMP] = default_timestamp_str_;
214  default_type_strs_[TYPE_DECIMAL] = default_decimal_str_;
215  }
216 
217  void GetValue(const string& expr, const ColumnType& expr_type,
218  void** interpreted_value, bool expect_error = false) {
219  string stmt = "select " + expr;
220  vector<FieldSchema> result_types;
221  Status status = executor_->Exec(stmt, &result_types);
222  if (expect_error) {
223  ASSERT_FALSE(status.ok()) << "Expected error\nstmt: " << stmt;
224  return;
225  }
226  ASSERT_TRUE(status.ok()) << "stmt: " << stmt << "\nerror: " << status.GetDetail();
227  string result_row;
228  ASSERT_TRUE(executor_->FetchResult(&result_row).ok()) << expr;
229  EXPECT_EQ(TypeToOdbcString(expr_type.type), result_types[0].type) << expr;
230  *interpreted_value = ConvertValue(expr_type, result_row);
231  }
232 
233  void* ConvertValue(const ColumnType& type, const string& value) {
235  if (value.compare("NULL") == 0) return NULL;
236  switch (type.type) {
237  case TYPE_STRING:
238  case TYPE_CHAR:
239  // Float and double get conversion errors so leave them as strings.
240  // We convert the expected result to string.
241  case TYPE_FLOAT:
242  case TYPE_DOUBLE:
243  expr_value_.string_val = value;
244  return &expr_value_.string_val;
245  case TYPE_TINYINT:
246  expr_value_.tinyint_val =
247  StringParser::StringToInt<int8_t>(&value[0], value.size(), &result);
248  return &expr_value_.tinyint_val;
249  case TYPE_SMALLINT:
250  expr_value_.smallint_val =
251  StringParser::StringToInt<int16_t>(&value[0], value.size(), &result);
252  return &expr_value_.smallint_val;
253  case TYPE_INT:
254  expr_value_.int_val =
255  StringParser::StringToInt<int32_t>(&value[0], value.size(), &result);
256  return &expr_value_.int_val;
257  case TYPE_BIGINT:
258  expr_value_.bigint_val =
259  StringParser::StringToInt<int64_t>(&value[0], value.size(), &result);
260  return &expr_value_.bigint_val;
261  case TYPE_BOOLEAN:
262  expr_value_.bool_val = value.compare("false");
263  return &expr_value_.bool_val;
264  case TYPE_TIMESTAMP:
265  expr_value_.timestamp_val = TimestampValue(&value[0], value.size());
266  return &expr_value_.timestamp_val;
267  case TYPE_DECIMAL:
268  switch (type.GetByteSize()) {
269  case 4:
270  expr_value_.decimal4_val = StringParser::StringToDecimal<int32_t>(
271  &value[0], value.size(), type, &result);
272  return &expr_value_.decimal4_val;
273  case 8:
274  expr_value_.decimal8_val = StringParser::StringToDecimal<int64_t>(
275  &value[0], value.size(), type, &result);
276  return &expr_value_.decimal8_val;
277  case 16:
278  expr_value_.decimal16_val = StringParser::StringToDecimal<int128_t>(
279  &value[0], value.size(), type, &result);
280  return &expr_value_.decimal16_val;
281  default:
282  DCHECK(false) << type;
283  }
284  default:
285  DCHECK(false) << type;
286  }
287  return NULL;
288  }
289 
290  void TestStringValue(const string& expr, const string& expected_result) {
291  StringValue* result;
292  GetValue(expr, TYPE_STRING, reinterpret_cast<void**>(&result));
293  string tmp(result->ptr, result->len);
294  EXPECT_EQ(tmp, expected_result) << expr;
295  }
296 
297  void TestCharValue(const string& expr, const string& expected_result,
298  const ColumnType& type) {
299  StringValue* result;
300  GetValue(expr, type, reinterpret_cast<void**>(&result));
301  string tmp(result->ptr, result->len);
302  EXPECT_EQ(tmp, expected_result) << expr;
303  }
304 
305  // We can't put this into TestValue() because GTest can't resolve
306  // the ambiguity in TimestampValue::operator==, even with the appropriate casts.
307  void TestTimestampValue(const string& expr, const TimestampValue& expected_result,
308  const int64_t tolerance_in_seconds = 0) {
309  TimestampValue* result;
310  GetValue(expr, TYPE_TIMESTAMP, reinterpret_cast<void**>(&result));
311  if (tolerance_in_seconds == 0) {
312  EXPECT_EQ(*result, expected_result);
313  } else {
314  int64_t delta = abs(result->ToUnixTime() - expected_result.ToUnixTime());
315  EXPECT_LE(delta, tolerance_in_seconds);
316  }
317  }
318 
319  // Tests whether the returned TimestampValue is valid.
320  // We use this function for tests where the expected value is unknown, e.g., now().
321  void TestValidTimestampValue(const string& expr) {
322  TimestampValue* result;
323  GetValue(expr, TYPE_TIMESTAMP, reinterpret_cast<void**>(&result));
324  EXPECT_TRUE(result->HasDateOrTime());
325  }
326 
327  // Test conversions of Timestamps to and from string/int with values related to the
328  // Unix epoch. The caller should set the current time zone before calling.
329  // 'unix_time_at_local_epoch' should be the expected value of the Unix time when it
330  // was 1970-01-01 in the current time zone. 'local_time_at_unix_epoch' should be the
331  // local time at the Unix epoch (1970-01-01 UTC).
332  void TestTimestampUnixEpochConversions(int64_t unix_time_at_local_epoch,
333  string local_time_at_unix_epoch) {
334  TestValue("unix_timestamp(cast('" + local_time_at_unix_epoch + "' as timestamp))",
335  TYPE_BIGINT, 0);
336  TestValue("unix_timestamp('" + local_time_at_unix_epoch + "')", TYPE_BIGINT, 0);
337  TestValue("unix_timestamp('" + local_time_at_unix_epoch +
338  "', 'yyyy-MM-dd HH:mm:ss')", TYPE_BIGINT, 0);
339  TestValue("unix_timestamp('1970-01-01', 'yyyy-MM-dd')", TYPE_BIGINT,
340  unix_time_at_local_epoch);
341  TestValue("unix_timestamp('1970-01-01 10:10:10', 'yyyy-MM-dd')", TYPE_BIGINT,
342  unix_time_at_local_epoch);
343  TestValue("unix_timestamp('" + local_time_at_unix_epoch
344  + " extra text', 'yyyy-MM-dd HH:mm:ss')", TYPE_BIGINT, 0);
345  TestStringValue("cast(cast(0 as timestamp) as string)", local_time_at_unix_epoch);
346  TestStringValue("cast(cast(0 as timestamp) as string)", local_time_at_unix_epoch);
347  TestStringValue("from_unixtime(0)", local_time_at_unix_epoch);
348  TestStringValue("from_unixtime(cast(0 as bigint))", local_time_at_unix_epoch);
349  TestIsNull("from_unixtime(NULL)", TYPE_STRING);
350  TestStringValue("from_unixtime(0, 'yyyy-MM-dd HH:mm:ss')",
351  local_time_at_unix_epoch);
352  TestStringValue("from_unixtime(cast(0 as bigint), 'yyyy-MM-dd HH:mm:ss')",
353  local_time_at_unix_epoch);
354  TestStringValue("from_unixtime(" + lexical_cast<string>(unix_time_at_local_epoch)
355  + ", 'yyyy-MM-dd')", "1970-01-01");
356  TestStringValue("from_unixtime(cast(" + lexical_cast<string>(unix_time_at_local_epoch)
357  + " as bigint), 'yyyy-MM-dd')", "1970-01-01");
358  }
359 
360  // Decimals don't work with TestValue.
361  // TODO: figure out what operators need to be implemented to work with EXPECT_EQ
362  template <typename T>
363  void TestDecimalValue(const string& expr, const T& expected_result,
364  const ColumnType& expected_type) {
365  T* result = NULL;
366  GetValue(expr, expected_type, reinterpret_cast<void**>(&result));
367  EXPECT_EQ(result->value(), expected_result.value()) << expr
368  << ": values did not match. Expected: "
369  << expected_result.ToString(expected_type)
370  << " Actual: " << result->ToString(expected_type);
371  }
372 
373  template <class T> void TestValue(const string& expr, const ColumnType& expr_type,
374  const T& expected_result) {
375  void* result;
376  GetValue(expr, expr_type, &result);
377 
378  string expected_str;
379  float expected_float;
380  double expected_double;
381  switch (expr_type.type) {
382  case TYPE_BOOLEAN:
383  EXPECT_EQ(*reinterpret_cast<bool*>(result), expected_result) << expr;
384  break;
385  case TYPE_TINYINT:
386  EXPECT_EQ(*reinterpret_cast<int8_t*>(result), expected_result) << expr;
387  break;
388  case TYPE_SMALLINT:
389  EXPECT_EQ(*reinterpret_cast<int16_t*>(result), expected_result) << expr;
390  break;
391  case TYPE_INT:
392  EXPECT_EQ(*reinterpret_cast<int32_t*>(result), expected_result) << expr;
393  break;
394  case TYPE_BIGINT:
395  EXPECT_EQ(*reinterpret_cast<int64_t*>(result), expected_result) << expr;
396  break;
397  case TYPE_FLOAT:
398  // Converting the float back from a string is inaccurate so convert
399  // the expected result to a string.
400  // In case the expected_result was passed in as an int or double, convert it.
401  expected_float = static_cast<float>(expected_result);
402  RawValue::PrintValue(reinterpret_cast<const void*>(&expected_float),
403  TYPE_FLOAT, -1, &expected_str);
404  EXPECT_EQ(*reinterpret_cast<string*>(result), expected_str) << expr;
405  break;
406  case TYPE_DOUBLE:
407  expected_double = static_cast<double>(expected_result);
408  RawValue::PrintValue(reinterpret_cast<const void*>(&expected_double),
409  TYPE_DOUBLE, -1, &expected_str);
410  EXPECT_EQ(*reinterpret_cast<string*>(result), expected_str) << expr;
411  break;
412  default:
413  ASSERT_TRUE(false) << "invalid TestValue() type: " << expr_type;
414  }
415  }
416 
417  void TestIsNull(const string& expr, const ColumnType& expr_type) {
418  void* result;
419  GetValue(expr, expr_type, &result);
420  EXPECT_TRUE(result == NULL) << expr;
421  }
422 
423  void TestError(const string& expr) {
424  void* dummy_result;
425  GetValue(expr, INVALID_TYPE, &dummy_result, /* expect_error */ true);
426  }
427 
428  void TestNonOkStatus(const string& expr) {
429  string stmt = "select " + expr;
430  vector<FieldSchema> result_types;
431  Status status = executor_->Exec(stmt, &result_types);
432  ASSERT_FALSE(status.ok()) << "stmt: " << stmt << "\nunexpected Status::OK.";
433  }
434 
435  template <typename T> void TestFixedPointComparisons(bool test_boundaries) {
436  int64_t t_min = numeric_limits<T>::min();
437  int64_t t_max = numeric_limits<T>::max();
438  TestComparison(lexical_cast<string>(t_min), lexical_cast<string>(t_max), true);
439  TestBinaryPredicates(lexical_cast<string>(t_min), true);
440  TestBinaryPredicates(lexical_cast<string>(t_max), true);
441  if (test_boundaries) {
442  // this requires a cast of the second operand to a higher-resolution type
443  TestComparison(lexical_cast<string>(t_min - 1),
444  lexical_cast<string>(t_max), true);
445  // this requires a cast of the first operand to a higher-resolution type
446  TestComparison(lexical_cast<string>(t_min),
447  lexical_cast<string>(t_max + 1), true);
448  }
449  }
450 
451  template <typename T> void TestFloatingPointComparisons(bool test_boundaries) {
452  // t_min is the smallest positive value
453  T t_min = numeric_limits<T>::min();
454  T t_max = numeric_limits<T>::max();
455  TestComparison(lexical_cast<string>(t_min), lexical_cast<string>(t_max), true);
456  TestComparison(lexical_cast<string>(-1.0 * t_max), lexical_cast<string>(t_max), true);
457  TestBinaryPredicates(lexical_cast<string>(t_min), true);
458  TestBinaryPredicates(lexical_cast<string>(t_max), true);
459  if (test_boundaries) {
460  // this requires a cast of the second operand to a higher-resolution type
461  TestComparison(lexical_cast<string>(numeric_limits<T>::min() - 1),
462  lexical_cast<string>(numeric_limits<T>::max()), true);
463  // this requires a cast of the first operand to a higher-resolution type
464  TestComparison(lexical_cast<string>(numeric_limits<T>::min()),
465  lexical_cast<string>(numeric_limits<T>::max() + 1), true);
466  }
467 
468  // Compare nan: not equal to, larger than or smaller than anything, including itself
469  TestValue(lexical_cast<string>(t_min) + " < 0/0", TYPE_BOOLEAN, false);
470  TestValue(lexical_cast<string>(t_min) + " > 0/0", TYPE_BOOLEAN, false);
471  TestValue(lexical_cast<string>(t_min) + " = 0/0", TYPE_BOOLEAN, false);
472  TestValue(lexical_cast<string>(t_max) + " < 0/0", TYPE_BOOLEAN, false);
473  TestValue(lexical_cast<string>(t_max) + " > 0/0", TYPE_BOOLEAN, false);
474  TestValue(lexical_cast<string>(t_max) + " = 0/0", TYPE_BOOLEAN, false);
475  TestValue("0/0 < 0/0", TYPE_BOOLEAN, false);
476  TestValue("0/0 > 0/0", TYPE_BOOLEAN, false);
477  TestValue("0/0 = 0/0", TYPE_BOOLEAN, false);
478 
479  // Compare inf: larger than everything except nan (or smaller, for -inf)
480  TestValue(lexical_cast<string>(t_max) + " < 1/0", TYPE_BOOLEAN, true);
481  TestValue(lexical_cast<string>(t_min) + " > -1/0", TYPE_BOOLEAN, true);
482  TestValue("1/0 = 1/0", TYPE_BOOLEAN, true);
483  TestValue("1/0 < 0/0", TYPE_BOOLEAN, false);
484  TestValue("0/0 < 1/0", TYPE_BOOLEAN, false);
485  }
486 
488  TestValue<bool>("'abc' = 'abc'", TYPE_BOOLEAN, true);
489  TestValue<bool>("'abc' = 'abcd'", TYPE_BOOLEAN, false);
490  TestValue<bool>("'abc' != 'abcd'", TYPE_BOOLEAN, true);
491  TestValue<bool>("'abc' != 'abc'", TYPE_BOOLEAN, false);
492  TestValue<bool>("'abc' < 'abcd'", TYPE_BOOLEAN, true);
493  TestValue<bool>("'abcd' < 'abc'", TYPE_BOOLEAN, false);
494  TestValue<bool>("'abcd' < 'abcd'", TYPE_BOOLEAN, false);
495  TestValue<bool>("'abc' > 'abcd'", TYPE_BOOLEAN, false);
496  TestValue<bool>("'abcd' > 'abc'", TYPE_BOOLEAN, true);
497  TestValue<bool>("'abcd' > 'abcd'", TYPE_BOOLEAN, false);
498  TestValue<bool>("'abc' <= 'abcd'", TYPE_BOOLEAN, true);
499  TestValue<bool>("'abcd' <= 'abc'", TYPE_BOOLEAN, false);
500  TestValue<bool>("'abcd' <= 'abcd'", TYPE_BOOLEAN, true);
501  TestValue<bool>("'abc' >= 'abcd'", TYPE_BOOLEAN, false);
502  TestValue<bool>("'abcd' >= 'abc'", TYPE_BOOLEAN, true);
503  TestValue<bool>("'abcd' >= 'abcd'", TYPE_BOOLEAN, true);
504 
505  // Test some empty strings
506  TestValue<bool>("'abcd' >= ''", TYPE_BOOLEAN, true);
507  TestValue<bool>("'' > ''", TYPE_BOOLEAN, false);
508  TestValue<bool>("'' = ''", TYPE_BOOLEAN, true);
509  }
510 
512  TestValue("1.23 = 1.23", TYPE_BOOLEAN, true);
513  TestValue("1.23 = 1.230", TYPE_BOOLEAN, true);
514  TestValue("1.23 = 1.234", TYPE_BOOLEAN, false);
515  TestValue("1.23 != 1.234", TYPE_BOOLEAN, true);
516  TestValue("1.23 < 1.234", TYPE_BOOLEAN, true);
517  TestValue("1.23 > 1.234", TYPE_BOOLEAN, false);
518  TestValue("1.23 = 1.230000000000000000000", TYPE_BOOLEAN, true);
519  TestValue("1.2300 != 1.230000000000000000001", TYPE_BOOLEAN, true);
520  TestValue("cast(1 as decimal(38,0)) = cast(1 as decimal(38,37))", TYPE_BOOLEAN, true);
521  TestValue("cast(1 as decimal(38,0)) = cast(0.1 as decimal(38,38))",
522  TYPE_BOOLEAN, false);
523  TestValue("cast(1 as decimal(38,0)) > cast(0.1 as decimal(38,38))",
524  TYPE_BOOLEAN, true);
525  TestBinaryPredicates("cast(1 as decimal(8,0))", false);
526  TestBinaryPredicates("cast(1 as decimal(10,0))", false);
527  TestBinaryPredicates("cast(1 as decimal(38,0))", false);
528  }
529 
530  // Test comparison operators with a left or right NULL operand against op.
531  void TestNullComparison(const string& op) {
532  // NULL as right operand.
533  TestIsNull(op + " = NULL", TYPE_BOOLEAN);
534  TestIsNull(op + " != NULL", TYPE_BOOLEAN);
535  TestIsNull(op + " <> NULL", TYPE_BOOLEAN);
536  TestIsNull(op + " < NULL", TYPE_BOOLEAN);
537  TestIsNull(op + " > NULL", TYPE_BOOLEAN);
538  TestIsNull(op + " <= NULL", TYPE_BOOLEAN);
539  TestIsNull(op + " >= NULL", TYPE_BOOLEAN);
540  // NULL as left operand.
541  TestIsNull("NULL = " + op, TYPE_BOOLEAN);
542  TestIsNull("NULL != " + op, TYPE_BOOLEAN);
543  TestIsNull("NULL <> " + op, TYPE_BOOLEAN);
544  TestIsNull("NULL < " + op, TYPE_BOOLEAN);
545  TestIsNull("NULL > " + op, TYPE_BOOLEAN);
546  TestIsNull("NULL <= " + op, TYPE_BOOLEAN);
547  TestIsNull("NULL >= " + op, TYPE_BOOLEAN);
548  }
549 
550  // Test comparison operators with a left or right NULL operand on all types.
552  unordered_map<int, string>::iterator def_iter;
553  for(def_iter = default_type_strs_.begin(); def_iter != default_type_strs_.end();
554  ++def_iter) {
555  TestNullComparison(def_iter->second);
556  }
557  TestNullComparison("NULL");
558  }
559 
560  // Generate all possible tests for combinations of <smaller> <op> <larger>.
561  // Also test conversions from strings.
562  void TestComparison(const string& smaller, const string& larger, bool compare_strings) {
563  // disabled for now, because our implicit casts from strings are broken
564  // and might return analysis errors when they shouldn't
565  // TODO: fix and re-enable tests
566  compare_strings = false;
567  string eq_pred = smaller + " = " + larger;
568  TestValue(eq_pred, TYPE_BOOLEAN, false);
569  if (compare_strings) {
570  eq_pred = smaller + " = '" + larger + "'";
571  TestValue(eq_pred, TYPE_BOOLEAN, false);
572  }
573  string ne_pred = smaller + " != " + larger;
574  TestValue(ne_pred, TYPE_BOOLEAN, true);
575  if (compare_strings) {
576  ne_pred = smaller + " != '" + larger + "'";
577  TestValue(ne_pred, TYPE_BOOLEAN, true);
578  }
579  string ne2_pred = smaller + " <> " + larger;
580  TestValue(ne2_pred, TYPE_BOOLEAN, true);
581  if (compare_strings) {
582  ne2_pred = smaller + " <> '" + larger + "'";
583  TestValue(ne2_pred, TYPE_BOOLEAN, true);
584  }
585  string lt_pred = smaller + " < " + larger;
586  TestValue(lt_pred, TYPE_BOOLEAN, true);
587  if (compare_strings) {
588  lt_pred = smaller + " < '" + larger + "'";
589  TestValue(lt_pred, TYPE_BOOLEAN, true);
590  }
591  string le_pred = smaller + " <= " + larger;
592  TestValue(le_pred, TYPE_BOOLEAN, true);
593  if (compare_strings) {
594  le_pred = smaller + " <= '" + larger + "'";
595  TestValue(le_pred, TYPE_BOOLEAN, true);
596  }
597  string gt_pred = smaller + " > " + larger;
598  TestValue(gt_pred, TYPE_BOOLEAN, false);
599  if (compare_strings) {
600  gt_pred = smaller + " > '" + larger + "'";
601  TestValue(gt_pred, TYPE_BOOLEAN, false);
602  }
603  string ge_pred = smaller + " >= " + larger;
604  TestValue(ge_pred, TYPE_BOOLEAN, false);
605  if (compare_strings) {
606  ge_pred = smaller + " >= '" + larger + "'";
607  TestValue(ge_pred, TYPE_BOOLEAN, false);
608  }
609  }
610 
611  // Generate all possible tests for combinations of <value> <op> <value>
612  // Also test conversions from strings.
613  void TestBinaryPredicates(const string& value, bool compare_strings) {
614  // disabled for now, because our implicit casts from strings are broken
615  // and might return analysis errors when they shouldn't
616  // TODO: fix and re-enable tests
617  compare_strings = false;
618  string eq_pred = value + " = " + value;
619  TestValue(eq_pred, TYPE_BOOLEAN, true);
620  if (compare_strings) {
621  eq_pred = value + " = '" + value + "'";
622  TestValue(eq_pred, TYPE_BOOLEAN, true);
623  }
624  string ne_pred = value + " != " + value;
625  TestValue(ne_pred, TYPE_BOOLEAN, false);
626  if (compare_strings) {
627  ne_pred = value + " != '" + value + "'";
628  TestValue(ne_pred, TYPE_BOOLEAN, false);
629  }
630  string ne2_pred = value + " <> " + value;
631  TestValue(ne2_pred, TYPE_BOOLEAN, false);
632  if (compare_strings) {
633  ne2_pred = value + " <> '" + value + "'";
634  TestValue(ne2_pred, TYPE_BOOLEAN, false);
635  }
636  string lt_pred = value + " < " + value;
637  TestValue(lt_pred, TYPE_BOOLEAN, false);
638  if (compare_strings) {
639  lt_pred = value + " < '" + value + "'";
640  TestValue(lt_pred, TYPE_BOOLEAN, false);
641  }
642  string le_pred = value + " <= " + value;
643  TestValue(le_pred, TYPE_BOOLEAN, true);
644  if (compare_strings) {
645  le_pred = value + " <= '" + value + "'";
646  TestValue(le_pred, TYPE_BOOLEAN, true);
647  }
648  string gt_pred = value + " > " + value;
649  TestValue(gt_pred, TYPE_BOOLEAN, false);
650  if (compare_strings) {
651  gt_pred = value + " > '" + value + "'";
652  TestValue(gt_pred, TYPE_BOOLEAN, false);
653  }
654  string ge_pred = value + " >= " + value;
655  TestValue(ge_pred, TYPE_BOOLEAN, true);
656  if (compare_strings) {
657  ge_pred = value + " >= '" + value + "'";
658  TestValue(ge_pred, TYPE_BOOLEAN, true);
659  }
660  }
661 
662  template <typename T> void TestFixedPointLimits(const ColumnType& type) {
663  // cast to non-char type first, otherwise we might end up interpreting
664  // the min/max as an ascii value
665  int64_t t_min = numeric_limits<T>::min();
666  int64_t t_max = numeric_limits<T>::max();
667  TestValue(lexical_cast<string>(t_min), type, numeric_limits<T>::min());
668  TestValue(lexical_cast<string>(t_max), type, numeric_limits<T>::max());
669  }
670 
671  template <typename T> void TestFloatingPointLimits(const ColumnType& type) {
672  // numeric_limits<>::min() is the smallest positive value
673  TestValue(lexical_cast<string>(numeric_limits<T>::min()), type,
674  numeric_limits<T>::min());
675  TestValue(lexical_cast<string>(-1.0 * numeric_limits<T>::min()), type,
676  -1.0 * numeric_limits<T>::min());
677  TestValue(lexical_cast<string>(-1.0 * numeric_limits<T>::max()), type,
678  -1.0 * numeric_limits<T>::max());
679  TestValue(lexical_cast<string>(numeric_limits<T>::max() - 1.0), type,
680  numeric_limits<T>::max());
681  }
682 
683  // Test ops that that always promote to a fixed type (e.g., next higher
684  // resolution type): ADD, SUBTRACT, MULTIPLY, DIVIDE.
685  // Note that adding the " " when generating the expression is not just cosmetic.
686  // We have "--" as a comment element in our lexer,
687  // so subtraction of a negative value will be ignored without " ".
688  template <typename LeftOp, typename RightOp, typename Result>
689  void TestFixedResultTypeOps(LeftOp a, RightOp b, const ColumnType& expected_type) {
690  Result cast_a = static_cast<Result>(a);
691  Result cast_b = static_cast<Result>(b);
692  string a_str = LiteralToString<Result>(cast_a);
693  string b_str = LiteralToString<Result>(cast_b);
694  TestValue(a_str + " + " + b_str, expected_type,
695  static_cast<Result>(cast_a + cast_b));
696  TestValue(a_str + " - " + b_str, expected_type,
697  static_cast<Result>(cast_a - cast_b));
698  TestValue(a_str + " * " + b_str, expected_type,
699  static_cast<Result>(cast_a * cast_b));
700  TestValue(a_str + " / " + b_str, TYPE_DOUBLE,
701  static_cast<double>(a) / static_cast<double>(b));
702  }
703 
704  // Test int ops that promote to assignment compatible type: BITAND, BITOR, BITXOR,
705  // BITNOT, INT_DIVIDE, MOD.
706  // As a convention we use RightOp as the higher resolution type.
707  template <typename LeftOp, typename RightOp>
708  void TestVariableResultTypeIntOps(LeftOp a, RightOp b,
709  const ColumnType& expected_type) {
710  RightOp cast_a = static_cast<RightOp>(a);
711  RightOp cast_b = static_cast<RightOp>(b);
712  string a_str = lexical_cast<string>(static_cast<int64_t>(a));
713  string b_str = lexical_cast<string>(static_cast<int64_t>(b));
714  TestValue(a_str + " & " + b_str, expected_type, cast_a & cast_b);
715  TestValue(a_str + " | " + b_str, expected_type, cast_a | cast_b);
716  TestValue(a_str + " ^ " + b_str, expected_type, cast_a ^ cast_b);
717  // Exclusively use b of type RightOp for unary op BITNOT.
718  TestValue("~" + b_str, expected_type, ~cast_b);
719  TestValue(a_str + " DIV " + b_str, expected_type, cast_a / cast_b);
720  TestValue(a_str + " % " + b_str, expected_type, cast_a % cast_b);
721  }
722 
723  // Test ops that that always promote to a fixed type with NULL operands:
724  // ADD, SUBTRACT, MULTIPLY, DIVIDE.
725  // We need CastType to make lexical_cast work properly for low-resolution types.
726  template <typename NonNullOp, typename CastType>
727  void TestNullOperandFixedResultTypeOps(NonNullOp op, const ColumnType& expected_type) {
728  CastType cast_op = static_cast<CastType>(op);
729  string op_str = LiteralToString<CastType>(cast_op);
730  // NULL as right operand.
731  TestIsNull(op_str + " + NULL", expected_type);
732  TestIsNull(op_str + " - NULL", expected_type);
733  TestIsNull(op_str + " * NULL", expected_type);
734  TestIsNull(op_str + " / NULL", TYPE_DOUBLE);
735  // NULL as left operand.
736  TestIsNull("NULL + " + op_str, expected_type);
737  TestIsNull("NULL - " + op_str, expected_type);
738  TestIsNull("NULL * " + op_str, expected_type);
739  TestIsNull("NULL / " + op_str, TYPE_DOUBLE);
740  }
741 
742  // Test binary int ops with NULL as left or right operand:
743  // BITAND, BITOR, BITXOR, INT_DIVIDE, MOD.
744  template <typename NonNullOp>
746  const ColumnType& expected_type) {
747  string op_str = lexical_cast<string>(static_cast<int64_t>(op));
748  // NULL as right operand.
749  TestIsNull(op_str + " & NULL", expected_type);
750  TestIsNull(op_str + " | NULL", expected_type);
751  TestIsNull(op_str + " ^ NULL", expected_type);
752  TestIsNull(op_str + " DIV NULL", expected_type);
753  TestIsNull(op_str + " % NULL", expected_type);
754  // NULL as left operand.
755  TestIsNull("NULL & " + op_str, expected_type);
756  TestIsNull("NULL | " + op_str, expected_type);
757  TestIsNull("NULL ^ " + op_str, expected_type);
758  TestIsNull("NULL DIV " + op_str, expected_type);
759  TestIsNull("NULL % " + op_str, expected_type);
760  }
761 
762  // Test all binary ops with both operands being NULL.
763  // We expect such exprs to return TYPE_INT.
765  TestIsNull("NULL + NULL", TYPE_DOUBLE);
766  TestIsNull("NULL - NULL", TYPE_DOUBLE);
767  TestIsNull("NULL * NULL", TYPE_DOUBLE);
768  TestIsNull("NULL / NULL", TYPE_DOUBLE);
769  TestIsNull("NULL & NULL", TYPE_INT);
770  TestIsNull("NULL | NULL", TYPE_INT);
771  TestIsNull("NULL ^ NULL", TYPE_INT);
772  TestIsNull("NULL DIV NULL", TYPE_INT);
773  TestIsNull("NULL % NULL", TYPE_DOUBLE);
774  TestIsNull("~NULL", TYPE_INT);
775  }
776 
777  // Test casting stmt to all types. Expected result is val.
778  template<typename T>
779  void TestCast(const string& stmt, T val, bool timestamp_out_of_range = false) {
780  TestValue("cast(" + stmt + " as boolean)", TYPE_BOOLEAN, static_cast<bool>(val));
781  TestValue("cast(" + stmt + " as tinyint)", TYPE_TINYINT, static_cast<int8_t>(val));
782  TestValue("cast(" + stmt + " as smallint)", TYPE_SMALLINT, static_cast<int16_t>(val));
783  TestValue("cast(" + stmt + " as int)", TYPE_INT, static_cast<int32_t>(val));
784  TestValue("cast(" + stmt + " as integer)", TYPE_INT, static_cast<int32_t>(val));
785  TestValue("cast(" + stmt + " as bigint)", TYPE_BIGINT, static_cast<int64_t>(val));
786  TestValue("cast(" + stmt + " as float)", TYPE_FLOAT, static_cast<float>(val));
787  TestValue("cast(" + stmt + " as double)", TYPE_DOUBLE, static_cast<double>(val));
788  TestValue("cast(" + stmt + " as real)", TYPE_DOUBLE, static_cast<double>(val));
789  TestStringValue("cast(" + stmt + " as string)", lexical_cast<string>(val));
790  if (!timestamp_out_of_range) {
791  TestTimestampValue("cast(" + stmt + " as timestamp)", TimestampValue(val));
792  } else {
793  TestIsNull("cast(" + stmt + " as timestamp)", TYPE_TIMESTAMP);
794  }
795  }
796 };
797 
798 // Test casting 'stmt' to each of the native types. The result should be 'val'
799 // 'stmt' is a partial stmt that could be of any valid type.
800 template<>
801 void ExprTest::TestCast(const string& stmt, const char* val,
802  bool timestamp_out_of_range) {
803  try {
804  int8_t val8 = static_cast<int8_t>(lexical_cast<int16_t>(val));
805 #if 0
806  // Hive has weird semantics. What do we want to do?
807  TestValue(stmt + " as boolean)", TYPE_BOOLEAN, lexical_cast<bool>(val));
808 #endif
809  TestValue("cast(" + stmt + " as tinyint)", TYPE_TINYINT, val8);
810  TestValue("cast(" + stmt + " as smallint)", TYPE_SMALLINT, lexical_cast<int16_t>(val));
811  TestValue("cast(" + stmt + " as int)", TYPE_INT, lexical_cast<int32_t>(val));
812  TestValue("cast(" + stmt + " as bigint)", TYPE_BIGINT, lexical_cast<int64_t>(val));
813  TestValue("cast(" + stmt + " as float)", TYPE_FLOAT, lexical_cast<float>(val));
814  TestValue("cast(" + stmt + " as double)", TYPE_DOUBLE, lexical_cast<double>(val));
815  TestStringValue("cast(" + stmt + " as string)", lexical_cast<string>(val));
816  } catch (bad_lexical_cast& e) {
817  EXPECT_TRUE(false) << e.what();
818  }
819 }
820 
821 template <typename T> void TestSingleLiteralConstruction(
822  const ColumnType& type, const T& value, const string& string_val) {
824  RowDescriptor desc;
825  RuntimeState state(TPlanFragmentInstanceCtx(), "", NULL);
827 
828  Expr* expr = pool.Add(new Literal(type, value));
829  ExprContext ctx(expr);
830  ctx.Prepare(&state, desc, &tracker);
831  Status status = ctx.Open(&state);
832  EXPECT_TRUE(status.ok());
833  EXPECT_EQ(RawValue::Compare(ctx.GetValue(NULL), &value, type), 0)
834  << "type: " << type << ", value: " << value;
835  ctx.Close(&state);
836 }
837 
839  for (int type = TYPE_BOOLEAN; type != TYPE_DATE; ++type) {
840  NullLiteral expr(static_cast<PrimitiveType>(type));
841  ExprContext ctx(&expr);
842  RuntimeState state(TPlanFragmentInstanceCtx(), "", NULL);
844  Status status = ctx.Prepare(&state, RowDescriptor(), &tracker);
845  EXPECT_TRUE(status.ok());
846  status = ctx.Open(&state);
847  EXPECT_TRUE(status.ok());
848  EXPECT_TRUE(ctx.GetValue(NULL) == NULL);
849  ctx.Close(&state);
850  }
851 }
852 
853 TEST_F(ExprTest, LiteralConstruction) {
854  bool b_val = true;
855  int8_t c_val = 'f';
856  int16_t s_val = 123;
857  int32_t i_val = 234;
858  int64_t l_val = 1234;
859  float f_val = 3.14f;
860  double d_val_1 = 1.23;
861  double d_val_2 = 7e6;
862  double d_val_3 = 5.9e-3;
863  string str_input = "Hello";
864  StringValue str_val(const_cast<char*>(str_input.data()), str_input.length());
865 
870  TestSingleLiteralConstruction(TYPE_INT, i_val, "+234");
874  TestSingleLiteralConstruction(TYPE_FLOAT, f_val, "+3.14");
875  TestSingleLiteralConstruction(TYPE_DOUBLE, d_val_1, "1.23");
876  TestSingleLiteralConstruction(TYPE_DOUBLE, d_val_1, "+1.23");
878  TestSingleLiteralConstruction(TYPE_DOUBLE, d_val_2, "+7e6");
879  TestSingleLiteralConstruction(TYPE_DOUBLE, d_val_3, "5.9e-3");
880  TestSingleLiteralConstruction(TYPE_DOUBLE, d_val_3, "+5.9e-3");
881  TestSingleLiteralConstruction(TYPE_STRING, str_val, "Hello");
882 
883  // Min/Max Boundary value test for tiny/small/int/long
884  c_val = 127;
885  const char c_array_max[] = {(const char)127}; // avoid implicit casting
886  string c_input_max(c_array_max, 1);
887  s_val = 32767;
888  i_val = 2147483647;
889  l_val = 9223372036854775807l;
890  TestSingleLiteralConstruction(TYPE_TINYINT, c_val, c_input_max);
892  TestSingleLiteralConstruction(TYPE_INT, i_val, "2147483647");
893  TestSingleLiteralConstruction(TYPE_BIGINT, l_val, "9223372036854775807");
894 
895  const char c_array_min[] = {(const char)(-128)}; // avoid implicit casting
896  string c_input_min(c_array_min, 1);
897  c_val = -128;
898  s_val = -32768;
899  i_val = -2147483648;
900  l_val = -9223372036854775807l-1;
901  TestSingleLiteralConstruction(TYPE_TINYINT, c_val, c_input_min);
903  TestSingleLiteralConstruction(TYPE_INT, i_val, "-2147483648");
904  TestSingleLiteralConstruction(TYPE_BIGINT, l_val, "-9223372036854775808");
905 }
906 
907 
908 TEST_F(ExprTest, LiteralExprs) {
909  TestFixedPointLimits<int8_t>(TYPE_TINYINT);
910  TestFixedPointLimits<int16_t>(TYPE_SMALLINT);
911  TestFixedPointLimits<int32_t>(TYPE_INT);
912  TestFixedPointLimits<int64_t>(TYPE_BIGINT);
913  // The value is not an exact FLOAT so it gets compared as a DOUBLE
914  // and fails. This needs to be researched.
915  // TestFloatingPointLimits<float>(TYPE_FLOAT);
916  TestFloatingPointLimits<double>(TYPE_DOUBLE);
917 
918  TestValue("true", TYPE_BOOLEAN, true);
919  TestValue("false", TYPE_BOOLEAN, false);
920  TestStringValue("'test'", "test");
921  TestIsNull("null", TYPE_NULL);
922 }
923 
924 TEST_F(ExprTest, ArithmeticExprs) {
925  // Test float ops.
926  TestFixedResultTypeOps<float, float, double>(min_float_values_[TYPE_FLOAT],
927  min_float_values_[TYPE_FLOAT], TYPE_DOUBLE);
928  TestFixedResultTypeOps<float, double, double>(min_float_values_[TYPE_FLOAT],
929  min_float_values_[TYPE_DOUBLE], TYPE_DOUBLE);
930  TestFixedResultTypeOps<double, double, double>(min_float_values_[TYPE_DOUBLE],
931  min_float_values_[TYPE_DOUBLE], TYPE_DOUBLE);
932 
933  // Test behavior of float ops at max/min value boundaries.
934  // The tests with float type should trivially pass, since their results are double.
935  TestFixedResultTypeOps<float, float, double>(numeric_limits<float>::min(),
936  numeric_limits<float>::min(), TYPE_DOUBLE);
937  TestFixedResultTypeOps<float, float, double>(numeric_limits<float>::max(),
938  numeric_limits<float>::max(), TYPE_DOUBLE);
939  TestFixedResultTypeOps<float, float, double>(numeric_limits<float>::min(),
940  numeric_limits<float>::max(), TYPE_DOUBLE);
941  TestFixedResultTypeOps<float, float, double>(numeric_limits<float>::max(),
942  numeric_limits<float>::min(), TYPE_DOUBLE);
943  TestFixedResultTypeOps<double, double, double>(numeric_limits<double>::min(),
944  numeric_limits<double>::min(), TYPE_DOUBLE);
945  TestFixedResultTypeOps<double, double, double>(numeric_limits<double>::max(),
946  numeric_limits<double>::max(), TYPE_DOUBLE);
947  TestFixedResultTypeOps<double, double, double>(numeric_limits<double>::min(),
948  numeric_limits<double>::max(), TYPE_DOUBLE);
949  TestFixedResultTypeOps<double, double, double>(numeric_limits<double>::max(),
950  numeric_limits<double>::min(), TYPE_DOUBLE);
951 
952  // Test behavior with zero (especially for division by zero).
953  TestFixedResultTypeOps<float, float, double>(min_float_values_[TYPE_FLOAT],
954  0.0f, TYPE_DOUBLE);
955  TestFixedResultTypeOps<double, double, double>(min_float_values_[TYPE_DOUBLE],
956  0.0, TYPE_DOUBLE);
957 
958  // Test ops that always promote to fixed type (e.g., next higher resolution type).
959  TestFixedResultTypeOps<int8_t, int8_t, int16_t>(min_int_values_[TYPE_TINYINT],
960  min_int_values_[TYPE_TINYINT], TYPE_SMALLINT);
961  TestFixedResultTypeOps<int8_t, int16_t, int32_t>(min_int_values_[TYPE_TINYINT],
962  min_int_values_[TYPE_SMALLINT], TYPE_INT);
963  TestFixedResultTypeOps<int8_t, int32_t, int64_t>(min_int_values_[TYPE_TINYINT],
964  min_int_values_[TYPE_INT], TYPE_BIGINT);
965  TestFixedResultTypeOps<int8_t, int64_t, int64_t>(min_int_values_[TYPE_TINYINT],
966  min_int_values_[TYPE_BIGINT], TYPE_BIGINT);
967  TestFixedResultTypeOps<int16_t, int16_t, int32_t>(min_int_values_[TYPE_SMALLINT],
968  min_int_values_[TYPE_SMALLINT], TYPE_INT);
969  TestFixedResultTypeOps<int16_t, int32_t, int64_t>(min_int_values_[TYPE_SMALLINT],
970  min_int_values_[TYPE_INT], TYPE_BIGINT);
971  TestFixedResultTypeOps<int16_t, int64_t, int64_t>(min_int_values_[TYPE_SMALLINT],
972  min_int_values_[TYPE_BIGINT], TYPE_BIGINT);
973  TestFixedResultTypeOps<int32_t, int32_t, int64_t>(min_int_values_[TYPE_INT],
974  min_int_values_[TYPE_INT], TYPE_BIGINT);
975  TestFixedResultTypeOps<int32_t, int64_t, int64_t>(min_int_values_[TYPE_INT],
976  min_int_values_[TYPE_BIGINT], TYPE_BIGINT);
977  TestFixedResultTypeOps<int64_t, int64_t, int64_t>(min_int_values_[TYPE_BIGINT],
978  min_int_values_[TYPE_BIGINT], TYPE_BIGINT);
979 
980  // Test behavior on overflow/underflow.
981  TestFixedResultTypeOps<int64_t, int64_t, int64_t>(numeric_limits<int64_t>::min()+1,
982  numeric_limits<int64_t>::min()+1, TYPE_BIGINT);
983  TestFixedResultTypeOps<int64_t, int64_t, int64_t>(numeric_limits<int64_t>::max(),
984  numeric_limits<int64_t>::max(), TYPE_BIGINT);
985  TestFixedResultTypeOps<int64_t, int64_t, int64_t>(numeric_limits<int64_t>::min()+1,
986  numeric_limits<int64_t>::max(), TYPE_BIGINT);
987  TestFixedResultTypeOps<int64_t, int64_t, int64_t>(numeric_limits<int64_t>::max(),
988  numeric_limits<int64_t>::min()+1, TYPE_BIGINT);
989 
990  // Test behavior with NULLs.
991  TestNullOperandFixedResultTypeOps<float, double>(min_float_values_[TYPE_FLOAT],
992  TYPE_DOUBLE);
993  TestNullOperandFixedResultTypeOps<double, double>(min_float_values_[TYPE_DOUBLE],
994  TYPE_DOUBLE);
995  TestNullOperandFixedResultTypeOps<int8_t, int64_t>(min_int_values_[TYPE_TINYINT],
996  TYPE_SMALLINT);
997  TestNullOperandFixedResultTypeOps<int16_t, int64_t>(min_int_values_[TYPE_SMALLINT],
998  TYPE_INT);
999  TestNullOperandFixedResultTypeOps<int32_t, int64_t>(min_int_values_[TYPE_INT],
1000  TYPE_BIGINT);
1001  TestNullOperandFixedResultTypeOps<int64_t, int64_t>(min_int_values_[TYPE_BIGINT],
1002  TYPE_BIGINT);
1003 
1004  // Test int ops that promote to assignment compatible type.
1005  TestVariableResultTypeIntOps<int8_t, int8_t>(min_int_values_[TYPE_TINYINT],
1006  min_int_values_[TYPE_TINYINT], TYPE_TINYINT);
1007  TestVariableResultTypeIntOps<int8_t, int16_t>(min_int_values_[TYPE_TINYINT],
1008  min_int_values_[TYPE_SMALLINT], TYPE_SMALLINT);
1009  TestVariableResultTypeIntOps<int8_t, int32_t>(min_int_values_[TYPE_TINYINT],
1010  min_int_values_[TYPE_INT], TYPE_INT);
1011  TestVariableResultTypeIntOps<int8_t, int64_t>(min_int_values_[TYPE_TINYINT],
1012  min_int_values_[TYPE_BIGINT], TYPE_BIGINT);
1013  TestVariableResultTypeIntOps<int16_t, int16_t>(min_int_values_[TYPE_SMALLINT],
1014  min_int_values_[TYPE_SMALLINT], TYPE_SMALLINT);
1015  TestVariableResultTypeIntOps<int16_t, int32_t>(min_int_values_[TYPE_SMALLINT],
1016  min_int_values_[TYPE_INT],TYPE_INT);
1017  TestVariableResultTypeIntOps<int16_t, int64_t>(min_int_values_[TYPE_SMALLINT],
1018  min_int_values_[TYPE_BIGINT], TYPE_BIGINT);
1019  TestVariableResultTypeIntOps<int32_t, int32_t>(min_int_values_[TYPE_INT],
1020  min_int_values_[TYPE_INT], TYPE_INT);
1021  TestVariableResultTypeIntOps<int32_t, int64_t>(min_int_values_[TYPE_INT],
1022  min_int_values_[TYPE_BIGINT], TYPE_BIGINT);
1023  TestVariableResultTypeIntOps<int64_t, int64_t>(min_int_values_[TYPE_BIGINT],
1024  min_int_values_[TYPE_BIGINT], TYPE_BIGINT);
1025 
1026  // Test behavior of INT_DIVIDE and MOD with zero as second argument.
1027  IntValMap::iterator int_iter;
1028  for(int_iter = min_int_values_.begin(); int_iter != min_int_values_.end();
1029  ++int_iter) {
1030  string& val = default_type_strs_[int_iter->first];
1031  TestIsNull(val + " DIV 0", static_cast<PrimitiveType>(int_iter->first));
1032  TestIsNull(val + " % 0", static_cast<PrimitiveType>(int_iter->first));
1033  }
1034 
1035  // Test behavior with NULLs.
1036  TestNullOperandVariableResultTypeIntOps<int8_t>(min_int_values_[TYPE_TINYINT],
1037  TYPE_TINYINT);
1038  TestNullOperandVariableResultTypeIntOps<int16_t>(min_int_values_[TYPE_SMALLINT],
1039  TYPE_SMALLINT);
1040  TestNullOperandVariableResultTypeIntOps<int32_t>(min_int_values_[TYPE_INT],
1041  TYPE_INT);
1042  TestNullOperandVariableResultTypeIntOps<int64_t>(min_int_values_[TYPE_BIGINT],
1043  TYPE_BIGINT);
1044 
1045  // Tests for dealing with '-'.
1046  TestValue("-1", TYPE_TINYINT, -1);
1047  TestValue("1 - 1", TYPE_SMALLINT, 0);
1048  TestValue("1 - - 1", TYPE_SMALLINT, 2);
1049  TestValue("1 - - - 1", TYPE_SMALLINT, 0);
1050  TestValue("- 1 - 1", TYPE_SMALLINT, -2);
1051  TestValue("- 1 - - 1", TYPE_SMALLINT, 0);
1052  // The "--" indicates a comment to be ignored.
1053  // Therefore, the result should be -1.
1054  TestValue("- 1 --1", TYPE_TINYINT, -1);
1055 
1056  // Test all arithmetic exprs with only NULL operands.
1057  TestNullOperandsArithmeticOps();
1058 }
1059 
1060 TEST_F(ExprTest, DecimalArithmeticExprs) {
1061  TestDecimalValue("1.23 + cast(1 as decimal(4,3))",
1062  Decimal4Value(2230), ColumnType::CreateDecimalType(5,3));
1063  TestDecimalValue("1.23 - cast(0.23 as decimal(10,3))",
1064  Decimal4Value(1000), ColumnType::CreateDecimalType(11,3));
1065  TestDecimalValue("1.23 * cast(1 as decimal(20,3))",
1066  Decimal4Value(123000), ColumnType::CreateDecimalType(23,5));
1067  TestDecimalValue("cast(1.23 as decimal(8,2)) / cast(1 as decimal(4,3))",
1068  Decimal4Value(12300000), ColumnType::CreateDecimalType(16,7));
1069  TestDecimalValue("cast(1.23 as decimal(8,2)) % cast(1 as decimal(10,3))",
1070  Decimal4Value(230), ColumnType::CreateDecimalType(9,3));
1071  TestDecimalValue("cast(1.23 as decimal(8,2)) + cast(1 as decimal(20,3))",
1072  Decimal4Value(2230), ColumnType::CreateDecimalType(21, 3));
1073  TestDecimalValue("cast(1.23 as decimal(30,2)) - cast(1 as decimal(4,3))",
1074  Decimal4Value(230), ColumnType::CreateDecimalType(34,3));
1075  TestDecimalValue("cast(1.23 as decimal(30,2)) * cast(1 as decimal(10,3))",
1076  Decimal4Value(123000), ColumnType::CreateDecimalType(38,5));
1077  TestDecimalValue("cast(1.23 as decimal(30,2)) / cast(1 as decimal(20,3))",
1078  Decimal4Value(1230), ColumnType::CreateDecimalType(38, 3));
1079  TestDecimalValue("cast(1 as decimal(38,0)) + cast(.2 as decimal(38,1))",
1080  Decimal4Value(12), ColumnType::CreateDecimalType(38, 1));
1081  TestDecimalValue("cast(1 as decimal(38,0)) / cast(.2 as decimal(38,1))",
1082  Decimal4Value(50), ColumnType::CreateDecimalType(38, 1));
1083 
1084  // Test mod() UDF
1085  TestDecimalValue("mod(cast('1' as decimal(2,0)), cast('10' as decimal(2,0)))",
1086  Decimal4Value(1), ColumnType::CreateDecimalType(2, 0));
1087  TestDecimalValue("mod(cast('1.1' as decimal(2,1)), cast('1.0' as decimal(2,1)))",
1088  Decimal4Value(1), ColumnType::CreateDecimalType(2,1));
1089  TestDecimalValue("mod(cast('-1.23' as decimal(5,2)), cast('1.0' as decimal(5,2)))",
1090  Decimal4Value(-23), ColumnType::CreateDecimalType(5,2));
1091  TestDecimalValue("mod(cast('1' as decimal(12,0)), cast('10' as decimal(12,0)))",
1092  Decimal8Value(1), ColumnType::CreateDecimalType(12, 0));
1093  TestDecimalValue("mod(cast('1.1' as decimal(12,1)), cast('1.0' as decimal(12,1)))",
1094  Decimal8Value(1), ColumnType::CreateDecimalType(12,1));
1095  TestDecimalValue("mod(cast('-1.23' as decimal(12,2)), cast('1.0' as decimal(12,2)))",
1096  Decimal8Value(-23), ColumnType::CreateDecimalType(12,2));
1097  TestDecimalValue("mod(cast('1' as decimal(32,0)), cast('10' as decimal(32,0)))",
1098  Decimal16Value(1), ColumnType::CreateDecimalType(32, 0));
1099  TestDecimalValue("mod(cast('1.1' as decimal(32,1)), cast('1.0' as decimal(32,1)))",
1100  Decimal16Value(1), ColumnType::CreateDecimalType(32,1));
1101  TestDecimalValue("mod(cast('-1.23' as decimal(32,2)), cast('1.0' as decimal(32,2)))",
1102  Decimal16Value(-23), ColumnType::CreateDecimalType(32,2));
1103  TestIsNull("mod(cast(NULL as decimal(2,0)), cast('10' as decimal(2,0)))",
1104  ColumnType::CreateDecimalType(2,0));
1105  TestIsNull("mod(cast('10' as decimal(2,0)), cast(NULL as decimal(2,0)))",
1106  ColumnType::CreateDecimalType(2,0));
1107  TestIsNull("mod(cast('10' as decimal(2,0)), cast('0' as decimal(2,0)))",
1108  ColumnType::CreateDecimalType(2,0));
1109  TestIsNull("mod(cast('10' as decimal(2,0)), cast('0' as decimal(2,0)))",
1110  ColumnType::CreateDecimalType(2,0));
1111  TestIsNull("mod(cast(NULL as decimal(2,0)), NULL)",
1112  ColumnType::CreateDecimalType(2,0));
1113 }
1114 
1115 // There are two tests of ranges, the second of which requires a cast
1116 // of the second operand to a higher-resolution type.
1117 TEST_F(ExprTest, BinaryPredicates) {
1118  TestComparison("false", "true", false);
1119  TestBinaryPredicates("false", false);
1120  TestBinaryPredicates("true", false);
1121  TestFixedPointComparisons<int8_t>(true);
1122  TestFixedPointComparisons<int16_t>(true);
1123  TestFixedPointComparisons<int32_t>(true);
1124  TestFixedPointComparisons<int64_t>(false);
1125  TestFloatingPointComparisons<float>(true);
1126  TestFloatingPointComparisons<double>(false);
1127  TestStringComparisons();
1128  TestDecimalComparisons();
1129  TestNullComparisons();
1130 }
1131 
1132 // Test casting from all types to all other types
1133 TEST_F(ExprTest, CastExprs) {
1134  // From tinyint
1135  TestCast("cast(0 as tinyint)", 0);
1136  TestCast("cast(5 as tinyint)", 5);
1137  TestCast("cast(-5 as tinyint)", -5);
1138 
1139  // From smallint
1140  TestCast("cast(0 as smallint)", 0);
1141  TestCast("cast(5 as smallint)", 5);
1142  TestCast("cast(-5 as smallint)", -5);
1143 
1144  // From int
1145  TestCast("cast(0 as int)", 0);
1146  TestCast("cast(5 as int)", 5);
1147  TestCast("cast(-5 as int)", -5);
1148 
1149  // From bigint
1150  TestCast("cast(0 as bigint)", 0);
1151  TestCast("cast(5 as bigint)", 5);
1152  TestCast("cast(-5 as bigint)", -5);
1153 
1154  // From boolean
1155  TestCast("cast(0 as boolean)", 0);
1156  TestCast("cast(5 as boolean)", 1);
1157  TestCast("cast(-5 as boolean)", 1);
1158 
1159  // From Float
1160  TestCast("cast(0.0 as float)", 0.0f);
1161  TestCast("cast(5.0 as float)", 5.0f);
1162  TestCast("cast(-5.0 as float)", -5.0f);
1163  TestCast("cast(0.1234567890123 as float)", 0.1234567890123f);
1164  TestCast("cast(0.1234567890123 as float)", 0.123456791f); // same as above
1165  TestCast("cast(0.00000000001234567890123 as float)", 0.00000000001234567890123f);
1166  TestCast("cast(123456 as float)", 123456.0f);
1167 
1168  // From http://en.wikipedia.org/wiki/Single-precision_floating-point_format
1169  // Min positive normal value
1170  TestCast("cast(1.1754944e-38 as float)", 1.1754944e-38f);
1171  // Max representable value
1172  TestCast("cast(3.4028234e38 as float)", 3.4028234e38f, true);
1173 
1174 
1175  // From Double
1176  TestCast("cast(0.0 as double)", 0.0);
1177  TestCast("cast(5.0 as double)", 5.0);
1178  TestCast("cast(-5.0 as double)", -5.0);
1179  TestCast("cast(0.123e10 as double)", 0.123e10);
1180  TestCast("cast(123.123e10 as double)", 123.123e10, true);
1181  TestCast("cast(1.01234567890123456789 as double)", 1.01234567890123456789);
1182  TestCast("cast(1.01234567890123456789 as double)", 1.0123456789012346); // same as above
1183  TestCast("cast(0.01234567890123456789 as double)", 0.01234567890123456789);
1184  TestCast("cast(0.1234567890123456789 as double)", 0.1234567890123456789);
1185  TestCast("cast(-2.2250738585072020E-308 as double)", -2.2250738585072020e-308);
1186 
1187  // From http://en.wikipedia.org/wiki/Double-precision_floating-point_format
1188  // Min subnormal positive double
1189  TestCast("cast(4.9406564584124654e-324 as double)", 4.9406564584124654e-324);
1190  // Max subnormal double
1191  TestCast("cast(2.2250738585072009e-308 as double)", 2.2250738585072009e-308);
1192  // Min normal positive double
1193  TestCast("cast(2.2250738585072014e-308 as double)", 2.2250738585072014e-308);
1194  // Max Double
1195  TestCast("cast(1.7976931348623157e+308 as double)", 1.7976931348623157e308, true);
1196 
1197  // From String
1198  TestCast("'0'", "0");
1199  TestCast("'5'", "5");
1200  TestCast("'-5'", "-5");
1201  TestStringValue("cast(\"abc\" as string)", "abc");
1202 
1203  // From Timestamp to Timestamp
1204  TestStringValue("cast(cast(cast('2012-01-01 09:10:11.123456789' as timestamp) as"
1205  " timestamp) as string)", "2012-01-01 09:10:11.123456789");
1206 
1207 
1208  // Timestamp <--> Int
1209  TestIsNull("cast(cast('09:10:11.000000' as timestamp) as int)", TYPE_INT);
1210  TestValue("cast(cast('2000-01-01' as timestamp) as int)", TYPE_INT, 946684800);
1211  // Check that casting to a TINYINT gives the same result as if the Unix time were
1212  // cast instead.
1213  TestValue("cast(cast('2000-01-01' as timestamp) as tinyint)", TYPE_TINYINT, -128);
1214  TestValue("cast(946684800 as tinyint)", TYPE_TINYINT, -128);
1215  TestValue("cast(cast('2000-01-01 09:10:11.000000' as timestamp) as int)", TYPE_INT,
1216  946717811);
1217  TestTimestampValue("cast(946717811 as timestamp)",
1218  TimestampValue("2000-01-01 09:10:11", 19));
1219  TestValue("cast(cast('1400-01-01' as timestamp) as bigint)", TYPE_BIGINT, -17987443200);
1220  TestTimestampValue("cast(-17987443200 as timestamp)", TimestampValue("1400-01-01", 10));
1221  TestIsNull("cast(-17987443201 as timestamp)", TYPE_TIMESTAMP);
1222  // Timestamp <--> Float
1223  TestIsNull("cast(cast('09:10:11.000000' as timestamp) as float)", TYPE_FLOAT);
1224  TestValue("cast(cast('2000-01-01' as timestamp) as double)", TYPE_DOUBLE, 946684800);
1225  TestValue("cast(cast('2000-01-01' as timestamp) as float)", TYPE_FLOAT, 946684800);
1226  TestValue("cast(cast('2000-01-01 09:10:11.720000' as timestamp) as double)",
1227  TYPE_DOUBLE, 946717811.72);
1228  TestTimestampValue("cast(cast(946717811.033 as double) as timestamp)",
1229  TimestampValue("2000-01-01 09:10:11.032999992", 29));
1230  TestValue("cast(cast('1400-01-01' as timestamp) as double)", TYPE_DOUBLE,
1231  -17987443200);
1232  TestIsNull("cast(cast(-17987443201.03 as double) as timestamp)", TYPE_TIMESTAMP);
1233  // Use 4 digit years otherwise string parsing will fail.
1234  TestValue("cast(cast('9999-12-31 23:59:59' as timestamp) + interval 1 year as bigint)",
1235  TYPE_BIGINT, 253433923199);
1236  TestTimestampValue("cast(253433923199 as timestamp) - interval 1 year",
1237  TimestampValue("9999-12-31 23:59:59", 19));
1238  TestIsNull("cast(253433923200 as timestamp)", TYPE_TIMESTAMP);
1239  TestIsNull("cast(cast(null as bigint) as timestamp)", TYPE_TIMESTAMP);
1240  TestIsNull("cast(cast(null as timestamp) as bigint)", TYPE_BIGINT);
1241 
1242 #if 0
1243  // Test overflow. TODO: Hive casting rules are very weird here also. It seems for
1244  // types < TYPE_INT, it will just overflow and keep the low order bits. For TYPE_INT,
1245  // it caps it and int_max/int_min. Is this what we want to do?
1246  int val = 10000000;
1247  TestValue<int8_t>("cast(10000000 as tinyint)", TYPE_TINYINT, val & 0xff);
1248  TestValue<int8_t>("cast(-10000000 as tinyint)", TYPE_TINYINT, -val & 0xff);
1249  TestValue<int16_t>("cast(10000000 as smallint)", TYPE_SMALLINT, val & 0xffff);
1250  TestValue<int16_t>("cast(-10000000 as smallint)", TYPE_SMALLINT, -val & 0xffff);
1251 #endif
1252 }
1253 
1254 TEST_F(ExprTest, CompoundPredicates) {
1255  TestValue("TRUE AND TRUE", TYPE_BOOLEAN, true);
1256  TestValue("TRUE AND FALSE", TYPE_BOOLEAN, false);
1257  TestValue("FALSE AND TRUE", TYPE_BOOLEAN, false);
1258  TestValue("FALSE AND FALSE", TYPE_BOOLEAN, false);
1259  TestValue("TRUE && TRUE", TYPE_BOOLEAN, true);
1260  TestValue("TRUE && FALSE", TYPE_BOOLEAN, false);
1261  TestValue("FALSE && TRUE", TYPE_BOOLEAN, false);
1262  TestValue("FALSE && FALSE", TYPE_BOOLEAN, false);
1263  TestValue("TRUE OR TRUE", TYPE_BOOLEAN, true);
1264  TestValue("TRUE OR FALSE", TYPE_BOOLEAN, true);
1265  TestValue("FALSE OR TRUE", TYPE_BOOLEAN, true);
1266  TestValue("FALSE OR FALSE", TYPE_BOOLEAN, false);
1267  TestValue("TRUE || TRUE", TYPE_BOOLEAN, true);
1268  TestValue("TRUE || FALSE", TYPE_BOOLEAN, true);
1269  TestValue("FALSE || TRUE", TYPE_BOOLEAN, true);
1270  TestValue("FALSE || FALSE", TYPE_BOOLEAN, false);
1271  TestValue("NOT TRUE", TYPE_BOOLEAN, false);
1272  TestValue("NOT FALSE", TYPE_BOOLEAN, true);
1273  TestValue("!TRUE", TYPE_BOOLEAN, false);
1274  TestValue("!FALSE", TYPE_BOOLEAN, true);
1275  TestValue("TRUE AND (TRUE OR FALSE)", TYPE_BOOLEAN, true);
1276  TestValue("(TRUE AND TRUE) OR FALSE", TYPE_BOOLEAN, true);
1277  TestValue("(TRUE OR FALSE) AND TRUE", TYPE_BOOLEAN, true);
1278  TestValue("TRUE OR (FALSE AND TRUE)", TYPE_BOOLEAN, true);
1279  TestValue("TRUE AND TRUE OR FALSE", TYPE_BOOLEAN, true);
1280  TestValue("TRUE && (TRUE || FALSE)", TYPE_BOOLEAN, true);
1281  TestValue("(TRUE && TRUE) || FALSE", TYPE_BOOLEAN, true);
1282  TestValue("(TRUE || FALSE) && TRUE", TYPE_BOOLEAN, true);
1283  TestValue("TRUE || (FALSE && TRUE)", TYPE_BOOLEAN, true);
1284  TestValue("TRUE && TRUE || FALSE", TYPE_BOOLEAN, true);
1285  TestIsNull("TRUE AND NULL", TYPE_BOOLEAN);
1286  TestIsNull("NULL AND TRUE", TYPE_BOOLEAN);
1287  TestValue("FALSE AND NULL", TYPE_BOOLEAN, false);
1288  TestValue("NULL AND FALSE", TYPE_BOOLEAN, false);
1289  TestIsNull("NULL AND NULL", TYPE_BOOLEAN);
1290  TestValue("TRUE OR NULL", TYPE_BOOLEAN, true);
1291  TestValue("NULL OR TRUE", TYPE_BOOLEAN, true);
1292  TestIsNull("FALSE OR NULL", TYPE_BOOLEAN);
1293  TestIsNull("NULL OR FALSE", TYPE_BOOLEAN);
1294  TestIsNull("NULL OR NULL", TYPE_BOOLEAN);
1295  TestIsNull("NOT NULL", TYPE_BOOLEAN);
1296  TestIsNull("TRUE && NULL", TYPE_BOOLEAN);
1297  TestValue("FALSE && NULL", TYPE_BOOLEAN, false);
1298  TestIsNull("NULL && NULL", TYPE_BOOLEAN);
1299  TestValue("TRUE || NULL", TYPE_BOOLEAN, true);
1300  TestIsNull("FALSE || NULL", TYPE_BOOLEAN);
1301  TestIsNull("NULL || NULL", TYPE_BOOLEAN);
1302  TestIsNull("!NULL", TYPE_BOOLEAN);
1303 }
1304 
1306  TestValue("5 IS NULL", TYPE_BOOLEAN, false);
1307  TestValue("5 IS NOT NULL", TYPE_BOOLEAN, true);
1308  TestValue("NULL IS NULL", TYPE_BOOLEAN, true);
1309  TestValue("NULL IS NOT NULL", TYPE_BOOLEAN, false);
1310 }
1311 
1313  TestValue("'a' LIKE '%a%'", TYPE_BOOLEAN, true);
1314  TestValue("'a' LIKE '%abcde'", TYPE_BOOLEAN, false);
1315  TestValue("'a' LIKE 'abcde%'", TYPE_BOOLEAN, false);
1316  TestValue("'abcde' LIKE 'abcde%'", TYPE_BOOLEAN, true);
1317  TestValue("'abcde' LIKE '%abcde'", TYPE_BOOLEAN, true);
1318  TestValue("'abcde' LIKE '%abcde%'", TYPE_BOOLEAN, true);
1319  // Test multiple wildcard characters
1320  TestValue("'abcde' LIKE '%%bc%%'", TYPE_BOOLEAN, true);
1321  TestValue("'abcde' LIKE '%%cb%%'", TYPE_BOOLEAN, false);
1322  TestValue("'abcde' LIKE 'abc%%'", TYPE_BOOLEAN, true);
1323  TestValue("'abcde' LIKE 'cba%%'", TYPE_BOOLEAN, false);
1324  TestValue("'abcde' LIKE '%%bcde'", TYPE_BOOLEAN, true);
1325  TestValue("'abcde' LIKE '%%cbde'", TYPE_BOOLEAN, false);
1326  TestValue("'abcde' LIKE '%%bc%%'", TYPE_BOOLEAN, true);
1327  TestValue("'abcde' LIKE '%%cb%%'", TYPE_BOOLEAN, false);
1328  TestValue("'abcde' LIKE '%%abcde%%'", TYPE_BOOLEAN, true);
1329  TestValue("'abcde' LIKE '%%edcba%%'", TYPE_BOOLEAN, false);
1330  TestValue("'abcde' LIKE '%%'", TYPE_BOOLEAN, true);
1331  TestValue("'abcde' NOT LIKE '%%'", TYPE_BOOLEAN, false);
1332  TestValue("'abcde' LIKE '%%%'", TYPE_BOOLEAN, true);
1333  TestValue("'abcde' NOT LIKE '%%%'", TYPE_BOOLEAN, false);
1334  TestValue("'abcde' LIKE '%%cd%%'", TYPE_BOOLEAN, true);
1335  TestValue("'abcde' LIKE '%%dc%%'", TYPE_BOOLEAN, false);
1336  TestValue("'abcde' LIKE '%%_%%'", TYPE_BOOLEAN, true);
1337  // IMP-117
1338  TestValue("'ab' LIKE '%a'", TYPE_BOOLEAN, false);
1339  TestValue("'ab' NOT LIKE '%a'", TYPE_BOOLEAN, true);
1340  // IMP-117
1341  TestValue("'ba' LIKE 'a%'", TYPE_BOOLEAN, false);
1342  TestValue("'ab' LIKE 'a'", TYPE_BOOLEAN, false);
1343  TestValue("'a' LIKE '_'", TYPE_BOOLEAN, true);
1344  TestValue("'a' NOT LIKE '_'", TYPE_BOOLEAN, false);
1345  TestValue("'a' LIKE 'a'", TYPE_BOOLEAN, true);
1346  TestValue("'a' LIKE 'b'", TYPE_BOOLEAN, false);
1347  TestValue("'a' NOT LIKE 'a'", TYPE_BOOLEAN, false);
1348  TestValue("'a' NOT LIKE 'b'", TYPE_BOOLEAN, true);
1349  // IMP-117 -- initial part of pattern appears earlier in string.
1350  TestValue("'LARGE BRUSHED BRASS' LIKE '%BRASS'", TYPE_BOOLEAN, true);
1351  TestValue("'BRASS LARGE BRUSHED' LIKE '%BRASS'", TYPE_BOOLEAN, false);
1352  TestValue("'BRASS LARGE BRUSHED' LIKE 'BRUSHED%'", TYPE_BOOLEAN, false);
1353  TestValue("'BRASS LARGE BRUSHED' LIKE 'BRASS%'", TYPE_BOOLEAN, true);
1354  TestValue("'prefix1234' LIKE 'prefix%'", TYPE_BOOLEAN, true);
1355  TestValue("'1234suffix' LIKE '%suffix'", TYPE_BOOLEAN, true);
1356  TestValue("'1234substr5678' LIKE '%substr%'", TYPE_BOOLEAN, true);
1357  TestValue("'a%a' LIKE 'a\\%a'", TYPE_BOOLEAN, true);
1358  TestValue("'a123a' LIKE 'a\\%a'", TYPE_BOOLEAN, false);
1359  TestValue("'a_a' LIKE 'a\\_a'", TYPE_BOOLEAN, true);
1360  TestValue("'a1a' LIKE 'a\\_a'", TYPE_BOOLEAN, false);
1361  TestValue("'abla' LIKE 'a%a'", TYPE_BOOLEAN, true);
1362  TestValue("'ablb' LIKE 'a%a'", TYPE_BOOLEAN, false);
1363  TestValue("'abxcy1234a' LIKE 'a_x_y%a'", TYPE_BOOLEAN, true);
1364  TestValue("'axcy1234a' LIKE 'a_x_y%a'", TYPE_BOOLEAN, false);
1365  TestValue("'abxcy1234a' REGEXP 'a.x.y.*a'", TYPE_BOOLEAN, true);
1366  TestValue("'a.x.y.*a' REGEXP 'a\\\\.x\\\\.y\\\\.\\\\*a'", TYPE_BOOLEAN, true);
1367  TestValue("'abxcy1234a' REGEXP '\\a\\.x\\\\.y\\\\.\\\\*a'", TYPE_BOOLEAN, false);
1368  TestValue("'abxcy1234a' RLIKE 'a.x.y.*a'", TYPE_BOOLEAN, true);
1369  TestValue("'axcy1234a' REGEXP 'a.x.y.*a'", TYPE_BOOLEAN, false);
1370  TestValue("'axcy1234a' RLIKE 'a.x.y.*a'", TYPE_BOOLEAN, false);
1371  // Regex patterns with constants strings
1372  TestValue("'english' REGEXP 'en'", TYPE_BOOLEAN, true);
1373  TestValue("'english' REGEXP 'lis'", TYPE_BOOLEAN, true);
1374  TestValue("'english' REGEXP 'english'", TYPE_BOOLEAN, true);
1375  TestValue("'english' REGEXP 'engilsh'", TYPE_BOOLEAN, false);
1376  TestValue("'english' REGEXP '^english$'", TYPE_BOOLEAN, true);
1377  TestValue("'english' REGEXP '^lish$'", TYPE_BOOLEAN, false);
1378  TestValue("'english' REGEXP '^eng'", TYPE_BOOLEAN, true);
1379  TestValue("'english' REGEXP '^ng'", TYPE_BOOLEAN, false);
1380  TestValue("'english' REGEXP 'lish$'", TYPE_BOOLEAN, true);
1381  TestValue("'english' REGEXP 'lis$'", TYPE_BOOLEAN, false);
1382  // regex escape chars; insert special character in the middle to prevent
1383  // it from being matched as a substring
1384  TestValue("'.[]{}()x\\\\*+?|^$' LIKE '.[]{}()_\\\\\\\\*+?|^$'", TYPE_BOOLEAN, true);
1385  // escaped _ matches single _
1386  TestValue("'\\\\_' LIKE '\\\\_'", TYPE_BOOLEAN, false);
1387  TestValue("'_' LIKE '\\\\_'", TYPE_BOOLEAN, true);
1388  TestValue("'a' LIKE '\\\\_'", TYPE_BOOLEAN, false);
1389  // escaped escape char
1390  TestValue("'\\\\a' LIKE '\\\\\\_'", TYPE_BOOLEAN, true);
1391  TestValue("'_' LIKE '\\\\\\_'", TYPE_BOOLEAN, false);
1392  // make sure the 3rd \ counts toward the _
1393  TestValue("'\\\\_' LIKE '\\\\\\\\\\_'", TYPE_BOOLEAN, true);
1394  TestValue("'\\\\\\\\a' LIKE '\\\\\\\\\\_'", TYPE_BOOLEAN, false);
1395  // Test invalid patterns, unmatched parenthesis.
1396  TestNonOkStatus("'a' RLIKE '(./'");
1397  TestNonOkStatus("'a' REGEXP '(./'");
1398  // Pattern is converted for LIKE, and should not throw.
1399  TestValue("'a' LIKE '(./'", TYPE_BOOLEAN, false);
1400  // Test NULLs.
1401  TestIsNull("NULL LIKE 'a'", TYPE_BOOLEAN);
1402  TestIsNull("'a' LIKE NULL", TYPE_BOOLEAN);
1403  TestIsNull("NULL LIKE NULL", TYPE_BOOLEAN);
1404  TestIsNull("NULL RLIKE 'a'", TYPE_BOOLEAN);
1405  TestIsNull("'a' RLIKE NULL", TYPE_BOOLEAN);
1406  TestIsNull("NULL RLIKE NULL", TYPE_BOOLEAN);
1407  TestIsNull("NULL REGEXP 'a'", TYPE_BOOLEAN);
1408  TestIsNull("'a' REGEXP NULL", TYPE_BOOLEAN);
1409  TestIsNull("NULL REGEXP NULL", TYPE_BOOLEAN);
1410 }
1411 
1412 TEST_F(ExprTest, BetweenPredicate) {
1413  // Between is rewritten into a conjunctive compound predicate.
1414  // Compound predicates are also tested elsewere, so we just do basic testing here.
1415  TestValue("5 between 0 and 10", TYPE_BOOLEAN, true);
1416  TestValue("5 between 5 and 5", TYPE_BOOLEAN, true);
1417  TestValue("5 between 6 and 10", TYPE_BOOLEAN, false);
1418  TestValue("5 not between 0 and 10", TYPE_BOOLEAN, false);
1419  TestValue("5 not between 5 and 5", TYPE_BOOLEAN, false);
1420  TestValue("5 not between 6 and 10", TYPE_BOOLEAN, true);
1421  // Test operator precedence.
1422  TestValue("5+1 between 4 and 10", TYPE_BOOLEAN, true);
1423  TestValue("5+1 not between 4 and 10", TYPE_BOOLEAN, false);
1424  // Test TimestampValues.
1425  TestValue("cast('2011-10-22 09:10:11' as timestamp) between "
1426  "cast('2011-09-22 09:10:11' as timestamp) and "
1427  "cast('2011-12-22 09:10:11' as timestamp)", TYPE_BOOLEAN, true);
1428  TestValue("cast('2011-10-22 09:10:11' as timestamp) between "
1429  "cast('2011-11-22 09:10:11' as timestamp) and "
1430  "cast('2011-12-22 09:10:11' as timestamp)", TYPE_BOOLEAN, false);
1431  TestValue("cast('2011-10-22 09:10:11' as timestamp) not between "
1432  "cast('2011-09-22 09:10:11' as timestamp) and "
1433  "cast('2011-12-22 09:10:11' as timestamp)", TYPE_BOOLEAN, false);
1434  TestValue("cast('2011-10-22 09:10:11' as timestamp) not between "
1435  "cast('2011-11-22 09:10:11' as timestamp) and "
1436  "cast('2011-12-22 09:10:11' as timestamp)", TYPE_BOOLEAN, true);
1437  // Test strings.
1438  TestValue("'abc' between 'a' and 'z'", TYPE_BOOLEAN, true);
1439  TestValue("'abc' between 'aaa' and 'aab'", TYPE_BOOLEAN, false);
1440  TestValue("'abc' not between 'a' and 'z'", TYPE_BOOLEAN, false);
1441  TestValue("'abc' not between 'aaa' and 'aab'", TYPE_BOOLEAN, true);
1442  // Test NULLs.
1443  TestIsNull("NULL between 0 and 10", TYPE_BOOLEAN);
1444  TestIsNull("1 between NULL and 10", TYPE_BOOLEAN);
1445  TestIsNull("1 between 0 and NULL", TYPE_BOOLEAN);
1446  TestIsNull("1 between NULL and NULL", TYPE_BOOLEAN);
1447  TestIsNull("NULL between NULL and NULL", TYPE_BOOLEAN);
1448 }
1449 
1450 // Tests with NULLs are in the FE QueryTest.
1452  // Test integers.
1453  IntValMap::iterator int_iter;
1454  for(int_iter = min_int_values_.begin(); int_iter != min_int_values_.end();
1455  ++int_iter) {
1456  string& val = default_type_strs_[int_iter->first];
1457  TestValue(val + " in (2, 3, " + val + ")", TYPE_BOOLEAN, true);
1458  TestValue(val + " in (2, 3, 4)", TYPE_BOOLEAN, false);
1459  TestValue(val + " not in (2, 3, " + val + ")", TYPE_BOOLEAN, false);
1460  TestValue(val + " not in (2, 3, 4)", TYPE_BOOLEAN, true);
1461  }
1462 
1463  // Test floats.
1464  unordered_map<int, double>::iterator float_iter;
1465  for(float_iter = min_float_values_.begin(); float_iter != min_float_values_.end();
1466  ++float_iter) {
1467  string& val = default_type_strs_[float_iter->first];
1468  TestValue(val + " in (2, 3, " + val + ")", TYPE_BOOLEAN, true);
1469  TestValue(val + " in (2, 3, 4)", TYPE_BOOLEAN, false);
1470  TestValue(val + " not in (2, 3, " + val + ")", TYPE_BOOLEAN, false);
1471  TestValue(val + " not in (2, 3, 4)", TYPE_BOOLEAN, true);
1472  }
1473 
1474  // Test bools.
1475  TestValue("true in (true, false, false)", TYPE_BOOLEAN, true);
1476  TestValue("true in (false, false, false)", TYPE_BOOLEAN, false);
1477  TestValue("true not in (true, false, false)", TYPE_BOOLEAN, false);
1478  TestValue("true not in (false, false, false)", TYPE_BOOLEAN, true);
1479 
1480  // Test strings.
1481  TestValue("'ab' in ('ab', 'cd', 'efg')", TYPE_BOOLEAN, true);
1482  TestValue("'ab' in ('cd', 'efg', 'h')", TYPE_BOOLEAN, false);
1483  TestValue("'ab' not in ('ab', 'cd', 'efg')", TYPE_BOOLEAN, false);
1484  TestValue("'ab' not in ('cd', 'efg', 'h')", TYPE_BOOLEAN, true);
1485 
1486  // test chars
1487  TestValue("cast('ab' as char(2)) in (cast('ab' as char(2)), cast('cd' as char(2)))",
1488  TYPE_BOOLEAN, true);
1489 
1490  // Test timestamps.
1491  TestValue(default_timestamp_str_ + " "
1492  "in (cast('2011-11-23 09:10:11' as timestamp), "
1493  "cast('2011-11-24 09:11:12' as timestamp), " +
1494  default_timestamp_str_ + ")", TYPE_BOOLEAN, true);
1495  TestValue(default_timestamp_str_ + " "
1496  "in (cast('2011-11-22 09:10:11' as timestamp), "
1497  "cast('2011-11-23 09:11:12' as timestamp), "
1498  "cast('2011-11-24 09:12:13' as timestamp))", TYPE_BOOLEAN, false);
1499  TestValue(default_timestamp_str_ + " "
1500  "not in (cast('2011-11-22 09:10:11' as timestamp), "
1501  "cast('2011-11-23 09:11:12' as timestamp), " +
1502  default_timestamp_str_ + ")", TYPE_BOOLEAN, false);
1503  TestValue(default_timestamp_str_ + " "
1504  "not in (cast('2011-11-22 09:10:11' as timestamp), "
1505  "cast('2011-11-23 09:11:12' as timestamp), "
1506  "cast('2011-11-24 09:12:13' as timestamp))", TYPE_BOOLEAN, true);
1507 
1508  // Test decimals
1509  vector<string> dec_strs; // Decimal of every physical type
1510  dec_strs.push_back("cast(-1.23 as decimal(8,2))");
1511  dec_strs.push_back("cast(-1.23 as decimal(9,2))");
1512  dec_strs.push_back("cast(-1.23 as decimal(10,2))");
1513  dec_strs.push_back("cast(-1.23 as decimal(17,2))");
1514  dec_strs.push_back("cast(-1.23 as decimal(18,2))");
1515  dec_strs.push_back("cast(-1.23 as decimal(19,2))");
1516  dec_strs.push_back("cast(-1.23 as decimal(32,2))");
1517  BOOST_FOREACH(const string& dec_str, dec_strs) {
1518  TestValue(dec_str + "in (0)", TYPE_BOOLEAN, false);
1519  TestValue(dec_str + "in (-1.23)", TYPE_BOOLEAN, true);
1520  TestValue(dec_str + "in (-1.230)", TYPE_BOOLEAN, true);
1521  TestValue(dec_str + "in (-1.23, 1)", TYPE_BOOLEAN, true);
1522  TestValue(dec_str + "in (1, 1, 1, 1, 1, -1.23, 1, 1, 1, 1, 1, 1, -1.23)",
1523  TYPE_BOOLEAN, true);
1524  TestValue(dec_str + "in (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1.23)",
1525  TYPE_BOOLEAN, true);
1526  TestValue(dec_str + "in (1)", TYPE_BOOLEAN, false);
1527  TestValue(dec_str + "in (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)", TYPE_BOOLEAN, false);
1528  TestIsNull(dec_str + "in (NULL)", TYPE_BOOLEAN);
1529  TestIsNull("NULL in (-1.23)", TYPE_BOOLEAN);
1530  TestIsNull(dec_str + "in (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, NULL)", TYPE_BOOLEAN);
1531  TestValue(dec_str + "in (1, 1, 1, 1, 1, NULL, 1, 1, 1, 1, 1, 1, -1.23)",
1532  TYPE_BOOLEAN, true);
1533  }
1534 
1535  // Test operator precedence.
1536  TestValue("5+1 in (3, 6, 10)", TYPE_BOOLEAN, true);
1537  TestValue("5+1 not in (3, 6, 10)", TYPE_BOOLEAN, false);
1538 
1539  // Test NULLs
1540  TestIsNull("NULL in (1, 2, 3)", TYPE_BOOLEAN);
1541  TestIsNull("NULL in (NULL, 1)", TYPE_BOOLEAN);
1542  TestIsNull("3 in (NULL)", TYPE_BOOLEAN);
1543  TestIsNull("3 in (1, 2, NULL)", TYPE_BOOLEAN);
1544  TestValue("3 in (NULL, 3)", TYPE_BOOLEAN, true);
1545  TestIsNull("'hello' in (NULL)", TYPE_BOOLEAN);
1546  TestValue("'hello' in ('hello', NULL)", TYPE_BOOLEAN, true);
1547  TestIsNull("NULL in (NULL)", TYPE_BOOLEAN);
1548  TestIsNull("NULL in (NULL, NULL)", TYPE_BOOLEAN);
1549 }
1550 
1552  TestStringValue("substring('Hello', 1)", "Hello");
1553  TestStringValue("substring('Hello', -2)", "lo");
1554  TestStringValue("substring('Hello', cast(0 as bigint))", "");
1555  TestStringValue("substring('Hello', -5)", "Hello");
1556  TestStringValue("substring('Hello', cast(-6 as bigint))", "");
1557  TestStringValue("substring('Hello', 100)", "");
1558  TestStringValue("substring('Hello', -100)", "");
1559  TestIsNull("substring(NULL, 100)", TYPE_STRING);
1560  TestIsNull("substring('Hello', NULL)", TYPE_STRING);
1561  TestIsNull("substring(NULL, NULL)", TYPE_STRING);
1562 
1563  TestStringValue("substring('Hello', 1, 1)", "H");
1564  TestStringValue("substring('Hello', cast(2 as bigint), 100)", "ello");
1565  TestStringValue("substring('Hello', -3, cast(2 as bigint))", "ll");
1566  TestStringValue("substring('Hello', 1, 0)", "");
1567  TestStringValue("substring('Hello', cast(1 as bigint), cast(-1 as bigint))", "");
1568  TestIsNull("substring(NULL, 1, 100)", TYPE_STRING);
1569  TestIsNull("substring('Hello', NULL, 100)", TYPE_STRING);
1570  TestIsNull("substring('Hello', 1, NULL)", TYPE_STRING);
1571  TestIsNull("substring(NULL, NULL, NULL)", TYPE_STRING);
1572 
1573  TestStringValue("lower('')", "");
1574  TestStringValue("lower('HELLO')", "hello");
1575  TestStringValue("lower('Hello')", "hello");
1576  TestStringValue("lower('hello!')", "hello!");
1577  TestStringValue("lcase('HELLO')", "hello");
1578  TestIsNull("lower(NULL)", TYPE_STRING);
1579  TestIsNull("lcase(NULL)", TYPE_STRING);
1580 
1581  TestStringValue("upper('')", "");
1582  TestStringValue("upper('HELLO')", "HELLO");
1583  TestStringValue("upper('Hello')", "HELLO");
1584  TestStringValue("upper('hello!')", "HELLO!");
1585  TestStringValue("ucase('hello')", "HELLO");
1586  TestIsNull("upper(NULL)", TYPE_STRING);
1587  TestIsNull("ucase(NULL)", TYPE_STRING);
1588 
1589  TestStringValue("initcap('')", "");
1590  TestStringValue("initcap('a')", "A");
1591  TestStringValue("initcap('hello')", "Hello");
1592  TestStringValue("initcap('h e l l o')", "H E L L O");
1593  TestStringValue("initcap('hello this is a message')", "Hello This Is A Message");
1594  TestStringValue("initcap('Hello This Is A Message')", "Hello This Is A Message");
1595  TestStringValue("initcap(' hello tHis IS A _ MeSsAgE')", " Hello This "
1596  "Is A _ Message");
1597  TestStringValue("initcap('HELLO THIS IS A MESSAGE')", "Hello This Is A Message");
1598  TestStringValue("initcap(' hello\vthis\nis\ra\tlong\fmessage')", " Hello\vThis"
1599  "\nIs\rA\tLong\fMessage");
1600  TestIsNull("initcap(NULL)", TYPE_STRING);
1601 
1602  string length_aliases[] = {"length", "char_length", "character_length"};
1603  for (int i = 0; i < 3; i++) {
1604  TestValue(length_aliases[i] + "('')", TYPE_INT, 0);
1605  TestValue(length_aliases[i] + "('a')", TYPE_INT, 1);
1606  TestValue(length_aliases[i] + "('abcdefg')", TYPE_INT, 7);
1607  TestIsNull(length_aliases[i] + "(NULL)", TYPE_INT);
1608  }
1609 
1610  TestStringValue("reverse('abcdefg')", "gfedcba");
1611  TestStringValue("reverse('')", "");
1612  TestIsNull("reverse(NULL)", TYPE_STRING);
1613  TestStringValue("strleft('abcdefg', 0)", "");
1614  TestStringValue("strleft('abcdefg', 3)", "abc");
1615  TestStringValue("strleft('abcdefg', cast(10 as bigint))", "abcdefg");
1616  TestStringValue("strleft('abcdefg', -1)", "");
1617  TestStringValue("strleft('abcdefg', cast(-9 as bigint))", "");
1618  TestIsNull("strleft(NULL, 3)", TYPE_STRING);
1619  TestIsNull("strleft('abcdefg', NULL)", TYPE_STRING);
1620  TestIsNull("strleft(NULL, NULL)", TYPE_STRING);
1621  TestStringValue("strright('abcdefg', 0)", "");
1622  TestStringValue("strright('abcdefg', 3)", "efg");
1623  TestStringValue("strright('abcdefg', cast(10 as bigint))", "abcdefg");
1624  TestStringValue("strright('abcdefg', -1)", "");
1625  TestStringValue("strright('abcdefg', cast(-9 as bigint))", "");
1626  TestIsNull("strright(NULL, 3)", TYPE_STRING);
1627  TestIsNull("strright('abcdefg', NULL)", TYPE_STRING);
1628  TestIsNull("strright(NULL, NULL)", TYPE_STRING);
1629 
1630  TestStringValue("translate('', '', '')", "");
1631  TestStringValue("translate('abcd', '', '')", "abcd");
1632  TestStringValue("translate('abcd', 'xyz', '')", "abcd");
1633  TestStringValue("translate('abcd', 'a', '')", "bcd");
1634  TestStringValue("translate('abcd', 'aa', '')", "bcd");
1635  TestStringValue("translate('abcd', 'aba', '')", "cd");
1636  TestStringValue("translate('abcd', 'cd', '')", "ab");
1637  TestStringValue("translate('abcd', 'cd', 'xy')", "abxy");
1638  TestStringValue("translate('abcdabcd', 'cd', 'xy')", "abxyabxy");
1639  TestStringValue("translate('abcd', 'abc', 'xy')", "xyd");
1640  TestStringValue("translate('abcd', 'abc', 'wxyz')", "wxyd");
1641  TestStringValue("translate('x', 'xx', 'ab')", "a");
1642  TestIsNull("translate(NULL, '', '')", TYPE_STRING);
1643  TestIsNull("translate('', NULL, '')", TYPE_STRING);
1644  TestIsNull("translate('', '', NULL)", TYPE_STRING);
1645 
1646  TestStringValue("trim('')", "");
1647  TestStringValue("trim(' ')", "");
1648  TestStringValue("trim(' abcdefg ')", "abcdefg");
1649  TestStringValue("trim('abcdefg ')", "abcdefg");
1650  TestStringValue("trim(' abcdefg')", "abcdefg");
1651  TestStringValue("trim('abc defg')", "abc defg");
1652  TestIsNull("trim(NULL)", TYPE_STRING);
1653  TestStringValue("ltrim('')", "");
1654  TestStringValue("ltrim(' ')", "");
1655  TestStringValue("ltrim(' abcdefg ')", "abcdefg ");
1656  TestStringValue("ltrim('abcdefg ')", "abcdefg ");
1657  TestStringValue("ltrim(' abcdefg')", "abcdefg");
1658  TestStringValue("ltrim('abc defg')", "abc defg");
1659  TestIsNull("ltrim(NULL)", TYPE_STRING);
1660  TestStringValue("rtrim('')", "");
1661  TestStringValue("rtrim(' ')", "");
1662  TestStringValue("rtrim(' abcdefg ')", " abcdefg");
1663  TestStringValue("rtrim('abcdefg ')", "abcdefg");
1664  TestStringValue("rtrim(' abcdefg')", " abcdefg");
1665  TestStringValue("rtrim('abc defg')", "abc defg");
1666  TestIsNull("rtrim(NULL)", TYPE_STRING);
1667 
1668  TestStringValue("space(0)", "");
1669  TestStringValue("space(-1)", "");
1670  TestStringValue("space(cast(1 as bigint))", " ");
1671  TestStringValue("space(6)", " ");
1672  TestIsNull("space(NULL)", TYPE_STRING);
1673 
1674  TestStringValue("repeat('', 0)", "");
1675  TestStringValue("repeat('', cast(6 as bigint))", "");
1676  TestStringValue("repeat('ab', 0)", "");
1677  TestStringValue("repeat('ab', -1)", "");
1678  TestStringValue("repeat('ab', -100)", "");
1679  TestStringValue("repeat('ab', 1)", "ab");
1680  TestStringValue("repeat('ab', cast(6 as bigint))", "abababababab");
1681  TestIsNull("repeat(NULL, 6)", TYPE_STRING);
1682  TestIsNull("repeat('ab', NULL)", TYPE_STRING);
1683  TestIsNull("repeat(NULL, NULL)", TYPE_STRING);
1684 
1685  TestValue("ascii('')", TYPE_INT, 0);
1686  TestValue("ascii('abcde')", TYPE_INT, 'a');
1687  TestValue("ascii('Abcde')", TYPE_INT, 'A');
1688  TestValue("ascii('dddd')", TYPE_INT, 'd');
1689  TestValue("ascii(' ')", TYPE_INT, ' ');
1690  TestIsNull("ascii(NULL)", TYPE_INT);
1691 
1692  TestStringValue("lpad('', 0, '')", "");
1693  TestStringValue("lpad('abc', 0, '')", "");
1694  TestStringValue("lpad('abc', cast(3 as bigint), '')", "abc");
1695  TestStringValue("lpad('abc', 2, 'xyz')", "ab");
1696  TestStringValue("lpad('abc', 6, 'xyz')", "xyzabc");
1697  TestStringValue("lpad('abc', cast(5 as bigint), 'xyz')", "xyabc");
1698  TestStringValue("lpad('abc', 10, 'xyz')", "xyzxyzxabc");
1699  TestIsNull("lpad('abc', -10, 'xyz')", TYPE_STRING);
1700  TestIsNull("lpad(NULL, 10, 'xyz')", TYPE_STRING);
1701  TestIsNull("lpad('abc', NULL, 'xyz')", TYPE_STRING);
1702  TestIsNull("lpad('abc', 10, NULL)", TYPE_STRING);
1703  TestIsNull("lpad(NULL, NULL, NULL)", TYPE_STRING);
1704  TestStringValue("rpad('', 0, '')", "");
1705  TestStringValue("rpad('abc', 0, '')", "");
1706  TestStringValue("rpad('abc', cast(3 as bigint), '')", "abc");
1707  TestStringValue("rpad('abc', 2, 'xyz')", "ab");
1708  TestStringValue("rpad('abc', 6, 'xyz')", "abcxyz");
1709  TestStringValue("rpad('abc', cast(5 as bigint), 'xyz')", "abcxy");
1710  TestStringValue("rpad('abc', 10, 'xyz')", "abcxyzxyzx");
1711  TestIsNull("rpad('abc', -10, 'xyz')", TYPE_STRING);
1712  TestIsNull("rpad(NULL, 10, 'xyz')", TYPE_STRING);
1713  TestIsNull("rpad('abc', NULL, 'xyz')", TYPE_STRING);
1714  TestIsNull("rpad('abc', 10, NULL)", TYPE_STRING);
1715  TestIsNull("rpad(NULL, NULL, NULL)", TYPE_STRING);
1716 
1717  // Note that Hive returns positions starting from 1.
1718  // Hive returns 0 if substr was not found in str (or on other error coditions).
1719  TestValue("instr('', '')", TYPE_INT, 0);
1720  TestValue("instr('', 'abc')", TYPE_INT, 0);
1721  TestValue("instr('abc', '')", TYPE_INT, 0);
1722  TestValue("instr('abc', 'abc')", TYPE_INT, 1);
1723  TestValue("instr('xyzabc', 'abc')", TYPE_INT, 4);
1724  TestValue("instr('xyzabcxyz', 'bcx')", TYPE_INT, 5);
1725  TestIsNull("instr(NULL, 'bcx')", TYPE_INT);
1726  TestIsNull("instr('xyzabcxyz', NULL)", TYPE_INT);
1727  TestIsNull("instr(NULL, NULL)", TYPE_INT);
1728  TestValue("locate('', '')", TYPE_INT, 0);
1729  TestValue("locate('abc', '')", TYPE_INT, 0);
1730  TestValue("locate('', 'abc')", TYPE_INT, 0);
1731  TestValue("locate('abc', 'abc')", TYPE_INT, 1);
1732  TestValue("locate('abc', 'xyzabc')", TYPE_INT, 4);
1733  TestValue("locate('bcx', 'xyzabcxyz')", TYPE_INT, 5);
1734  TestIsNull("locate(NULL, 'xyzabcxyz')", TYPE_INT);
1735  TestIsNull("locate('bcx', NULL)", TYPE_INT);
1736  TestIsNull("locate(NULL, NULL)", TYPE_INT);
1737 
1738  // Test locate with starting pos param.
1739  // Note that Hive expects positions starting from 1 as input.
1740  TestValue("locate('', '', 0)", TYPE_INT, 0);
1741  TestValue("locate('abc', '', cast(0 as bigint))", TYPE_INT, 0);
1742  TestValue("locate('', 'abc', 0)", TYPE_INT, 0);
1743  TestValue("locate('', 'abc', -1)", TYPE_INT, 0);
1744  TestValue("locate('', '', 1)", TYPE_INT, 0);
1745  TestValue("locate('', 'abcde', cast(10 as bigint))", TYPE_INT, 0);
1746  TestValue("locate('abcde', 'abcde', -1)", TYPE_INT, 0);
1747  TestValue("locate('abcde', 'abcde', 10)", TYPE_INT, 0);
1748  TestValue("locate('abc', 'abcdef', 0)", TYPE_INT, 0);
1749  TestValue("locate('abc', 'abcdef', 1)", TYPE_INT, 1);
1750  TestValue("locate('abc', 'xyzabcdef', 3)", TYPE_INT, 4);
1751  TestValue("locate('abc', 'xyzabcdef', 4)", TYPE_INT, 4);
1752  TestValue("locate('abc', 'abcabcabc', cast(5 as bigint))", TYPE_INT, 7);
1753  TestIsNull("locate(NULL, 'abcabcabc', 5)", TYPE_INT);
1754  TestIsNull("locate('abc', NULL, 5)", TYPE_INT);
1755  TestIsNull("locate('abc', 'abcabcabc', NULL)", TYPE_INT);
1756  TestIsNull("locate(NULL, NULL, NULL)", TYPE_INT);
1757 
1758  TestStringValue("concat('a')", "a");
1759  TestStringValue("concat('a', 'b')", "ab");
1760  TestStringValue("concat('a', 'b', 'cde')", "abcde");
1761  TestStringValue("concat('a', 'b', 'cde', 'fg')", "abcdefg");
1762  TestStringValue("concat('a', 'b', 'cde', '', 'fg', '')", "abcdefg");
1763  TestIsNull("concat(NULL)", TYPE_STRING);
1764  TestIsNull("concat('a', NULL, 'b')", TYPE_STRING);
1765  TestIsNull("concat('a', 'b', NULL)", TYPE_STRING);
1766 
1767  TestStringValue("concat_ws(',', 'a')", "a");
1768  TestStringValue("concat_ws(',', 'a', 'b')", "a,b");
1769  TestStringValue("concat_ws(',', 'a', 'b', 'cde')", "a,b,cde");
1770  TestStringValue("concat_ws('', 'a', '', 'b', 'cde')", "abcde");
1771  TestStringValue("concat_ws('%%', 'a', 'b', 'cde', 'fg')", "a%%b%%cde%%fg");
1772  TestStringValue("concat_ws('|','a', 'b', 'cde', '', 'fg', '')", "a|b|cde||fg|");
1773  TestStringValue("concat_ws('', '', '', '')", "");
1774  TestIsNull("concat_ws(NULL, NULL)", TYPE_STRING);
1775  TestIsNull("concat_ws(',', NULL, 'b')", TYPE_STRING);
1776  TestIsNull("concat_ws(',', 'b', NULL)", TYPE_STRING);
1777 
1778  TestValue("find_in_set('ab', 'ab,ab,ab,ade,cde')", TYPE_INT, 1);
1779  TestValue("find_in_set('ab', 'abc,xyz,abc,ade,ab')", TYPE_INT, 5);
1780  TestValue("find_in_set('ab', 'abc,ad,ab,ade,cde')", TYPE_INT, 3);
1781  TestValue("find_in_set('xyz', 'abc,ad,ab,ade,cde')", TYPE_INT, 0);
1782  TestValue("find_in_set('ab', ',,,,ab,,,,')", TYPE_INT, 5);
1783  TestValue("find_in_set('', ',ad,ab,ade,cde')", TYPE_INT,1);
1784  TestValue("find_in_set('', 'abc,ad,ab,ade,,')", TYPE_INT, 5);
1785  TestValue("find_in_set('', 'abc,ad,,ade,cde,')", TYPE_INT,3);
1786  // First param contains comma.
1787  TestValue("find_in_set('abc,def', 'abc,ad,,ade,cde,')", TYPE_INT, 0);
1788  TestIsNull("find_in_set(NULL, 'abc,ad,,ade,cde')", TYPE_INT);
1789  TestIsNull("find_in_set('abc,def', NULL)", TYPE_INT);
1790  TestIsNull("find_in_set(NULL, NULL)", TYPE_INT);
1791 
1792  TestStringValue("cast('HELLO' as VARCHAR(3))", "HEL");
1793  TestStringValue("cast('HELLO' as VARCHAR(15))", "HELLO");
1794  TestStringValue("lower(cast('HELLO' as VARCHAR(3)))", "hel");
1795  TestStringValue("lower(cast(123456 as VARCHAR(3)))", "123");
1796  TestIsNull("cast(NULL as VARCHAR(3))", TYPE_STRING);
1797  TestCharValue("cast('12345' as CHAR(130))",
1798  "12345 "
1799  " ",
1800  ColumnType::CreateCharType(130));
1801 
1802  TestCharValue("cast(cast('HELLO' as VARCHAR(3)) as CHAR(3))", "HEL",
1803  ColumnType::CreateCharType(3));
1804  TestStringValue("cast(cast('HELLO' as CHAR(3)) as VARCHAR(3))", "HEL");
1805  TestCharValue("cast(cast('HELLO' as VARCHAR(7)) as CHAR(7))", "HELLO ",
1806  ColumnType::CreateCharType(7));
1807  TestCharValue("cast(cast('HELLO' as STRING) as CHAR(7))", "HELLO ",
1808  ColumnType::CreateCharType(7));
1809  TestStringValue("cast(cast('HELLO' as CHAR(7)) as VARCHAR(7))", "HELLO ");
1810  TestStringValue("cast(cast('HELLO' as CHAR(5)) as VARCHAR(3))", "HEL");
1811  TestCharValue("cast(cast('HELLO' as VARCHAR(7)) as CHAR(3))", "HEL",
1812  ColumnType::CreateCharType(3));
1813 
1814  TestCharValue("cast(5 as char(5))", "5 ", ColumnType::CreateCharType(5));
1815  TestCharValue("cast(5.1 as char(5))", "5.1 ", ColumnType::CreateCharType(5));
1816  TestCharValue("cast(cast(1 as decimal(2,1)) as char(5))", "1.0 ",
1817  ColumnType::CreateCharType(5));
1818  TestCharValue("cast(cast('2014-09-30 10:35:10.632995000' as TIMESTAMP) as char(35))",
1819  "2014-09-30 10:35:10.632995000 ",
1820  ColumnType::CreateCharType(35));
1821 
1822  TestCharValue("cast('HELLO' as CHAR(3))", "HEL",
1823  ColumnType::CreateCharType(3));
1824  TestCharValue("cast('HELLO' as CHAR(7))", "HELLO ",
1825  ColumnType::CreateCharType(7));
1826  TestCharValue("cast('HELLO' as CHAR(70))",
1827  "HELLO ",
1828  ColumnType::CreateCharType(70));
1829  TestValue("cast('HELLO' as CHAR(7)) = 'HELLO '", TYPE_BOOLEAN, true);
1830  TestValue("cast('HELLO' as CHAR(7)) = cast('HELLO' as CHAR(5))", TYPE_BOOLEAN, true);
1831  TestStringValue("lower(cast('HELLO' as CHAR(3)))", "hel");
1832  TestStringValue("lower(cast(123456 as CHAR(3)))", "123");
1833  TestStringValue("cast(cast(123456 as CHAR(3)) as VARCHAR(3))", "123");
1834  TestStringValue("cast(cast(123456 as CHAR(3)) as VARCHAR(65355))", "123");
1835  TestIsNull("cast(NULL as CHAR(3))", ColumnType::CreateCharType(3));
1836 
1837  TestCharValue("cast('HELLO' as CHAR(255))",
1838  "HELLO "
1839  " "
1840  " "
1841  " ", ColumnType::CreateCharType(255));
1842 
1843  TestStringValue("CASE cast('1.1' as char(3)) when cast('1.1' as char(3)) then "
1844  "cast('1' as char(1)) when cast('2.22' as char(4)) then "
1845  "cast('2' as char(1)) else cast('3' as char(1)) end", "1");
1846 
1847  // Test maximum VARCHAR value
1848  char query[ColumnType::MAX_VARCHAR_LENGTH + 1024];
1849  char big_str[ColumnType::MAX_VARCHAR_LENGTH+1];
1850  for (int i = 0 ; i < ColumnType::MAX_VARCHAR_LENGTH; i++) {
1851  big_str[i] = 'a';
1852  }
1853  big_str[ColumnType::MAX_VARCHAR_LENGTH] = '\0';
1854  sprintf(query, "cast('%sxxx' as VARCHAR(%d))", big_str, ColumnType::MAX_VARCHAR_LENGTH);
1855  TestStringValue(query, big_str);
1856 }
1857 
1858 TEST_F(ExprTest, StringRegexpFunctions) {
1859  // Single group.
1860  TestStringValue("regexp_extract('abxcy1234a', 'a.x', 0)", "abx");
1861  TestStringValue("regexp_extract('abxcy1234a', 'a.x.*a', 0)", "abxcy1234a");
1862  TestStringValue("regexp_extract('abxcy1234a', 'a.x.y.*a', 0)", "abxcy1234a");
1863  TestStringValue("regexp_extract('a.x.y.*a',"
1864  "'a\\\\.x\\\\.y\\\\.\\\\*a', 0)", "a.x.y.*a");
1865  TestStringValue("regexp_extract('abxcy1234a', 'abczy', cast(0 as bigint))", "");
1866  TestStringValue("regexp_extract('abxcy1234a', 'a\\\\.x\\\\.y\\\\.\\\\*a', 0)", "");
1867  TestStringValue("regexp_extract('axcy1234a', 'a.x.y.*a', 0)","");
1868  // Accessing non-existant group should return empty string.
1869  TestStringValue("regexp_extract('abxcy1234a', 'a.x', cast(2 as bigint))", "");
1870  TestStringValue("regexp_extract('abxcy1234a', 'a.x.*a', 1)", "");
1871  TestStringValue("regexp_extract('abxcy1234a', 'a.x.y.*a', 5)", "");
1872  TestStringValue("regexp_extract('abxcy1234a', 'a.x', -1)", "");
1873  // Multiple groups enclosed in ().
1874  TestStringValue("regexp_extract('abxcy1234a', '(a.x)(.y.*)(3.*a)', 0)", "abxcy1234a");
1875  TestStringValue("regexp_extract('abxcy1234a', '(a.x)(.y.*)(3.*a)', 1)", "abx");
1876  TestStringValue("regexp_extract('abxcy1234a', '(a.x)(.y.*)(3.*a)', 2)", "cy12");
1877  TestStringValue("regexp_extract('abxcy1234a', '(a.x)(.y.*)(3.*a)',"
1878  "cast(3 as bigint))", "34a");
1879  TestStringValue("regexp_extract('abxcy1234a', '(a.x)(.y.*)(3.*a)',"
1880  "cast(4 as bigint))", "");
1881  // Empty strings.
1882  TestStringValue("regexp_extract('', '', 0)", "");
1883  TestStringValue("regexp_extract('abxcy1234a', '', 0)", "");
1884  TestStringValue("regexp_extract('', 'abx', 0)", "");
1885  // Test finding of leftmost maximal match.
1886  TestStringValue("regexp_extract('I001=-200,I003=-210,I007=0', 'I001=-?[0-9]+', 0)",
1887  "I001=-200");
1888  // Invalid regex pattern, unmatched parenthesis.
1889  TestError("regexp_extract('abxcy1234a', '(/.', 0)");
1890  // NULL arguments.
1891  TestIsNull("regexp_extract(NULL, 'a.x', 2)", TYPE_STRING);
1892  TestIsNull("regexp_extract('abxcy1234a', NULL, 2)", TYPE_STRING);
1893  TestIsNull("regexp_extract('abxcy1234a', 'a.x', NULL)", TYPE_STRING);
1894  TestIsNull("regexp_extract(NULL, NULL, NULL)", TYPE_STRING);
1895  // Character classes.
1896  TestStringValue("regexp_extract('abxcy1234a', '[[:lower:]]*', 0)", "abxcy");
1897  TestStringValue("regexp_extract('abxcy1234a', '[[:digit:]]+', 0)", "1234");
1898  TestStringValue("regexp_extract('abxcy1234a', '[[:lower:]][[:digit:]]', 0)", "y1");
1899  TestStringValue("regexp_extract('aBcDeF', '[[:upper:]][[:lower:]]', 0)", "Bc");
1900  // "Single character" character classes.
1901  TestStringValue("regexp_extract('abxcy1234a', '\\\\w*', 0)", "abxcy1234a");
1902  TestStringValue("regexp_extract('abxcy1234a', '\\\\d+', 0)", "1234");
1903  TestStringValue("regexp_extract('abxcy1234a', '\\\\d\\\\D', 0)", "4a");
1904  // Leftmost longest match.
1905  TestStringValue("regexp_extract('abcabcd', '(a|ab|abc|abcd)', 0)", "abc");
1906 
1907  TestStringValue("regexp_replace('axcaycazc', 'a.c', 'a')", "aaa");
1908  TestStringValue("regexp_replace('axcaycazc', 'a.c', '')", "");
1909  TestStringValue("regexp_replace('axcaycazc', 'a.*', 'abcde')", "abcde");
1910  TestStringValue("regexp_replace('axcaycazc', 'a.*y.*z', 'xyz')", "xyzc");
1911  // No match for pattern.
1912  TestStringValue("regexp_replace('axcaycazc', 'a.z', 'xyz')", "axcaycazc");
1913  TestStringValue("regexp_replace('axcaycazc', 'a.*y.z', 'xyz')", "axcaycazc");
1914  // Empty strings.
1915  TestStringValue("regexp_replace('', '', '')", "");
1916  TestStringValue("regexp_replace('axcaycazc', '', '')", "axcaycazc");
1917  TestStringValue("regexp_replace('', 'err', '')", "");
1918  TestStringValue("regexp_replace('', '', 'abc')", "abc");
1919  TestStringValue("regexp_replace('axcaycazc', '', 'r')", "rarxrcraryrcrarzrcr");
1920  // Invalid regex pattern, unmatched parenthesis.
1921  TestError("regexp_replace('abxcy1234a', '(/.', 'x')");
1922  // NULL arguments.
1923  TestIsNull("regexp_replace(NULL, 'a.*', 'abcde')", TYPE_STRING);
1924  TestIsNull("regexp_replace('axcaycazc', NULL, 'abcde')", TYPE_STRING);
1925  TestIsNull("regexp_replace('axcaycazc', 'a.*', NULL)", TYPE_STRING);
1926  TestIsNull("regexp_replace(NULL, NULL, NULL)", TYPE_STRING);
1927 }
1928 
1929 TEST_F(ExprTest, StringParseUrlFunction) {
1930  // TODO: For now, our parse_url my not behave exactly like Hive
1931  // when given malformed URLs.
1932  // If necessary, we can closely follow Java's URL implementation
1933  // to behave exactly like Hive.
1934 
1935  // AUTHORITY part.
1936  TestStringValue("parse_url('http://user:pass@example.com:80/docs/books/tutorial/"
1937  "index.html?name=networking#DOWNLOADING', 'AUTHORITY')",
1938  "user:pass@example.com:80");
1939  TestStringValue("parse_url('http://user:pass@example.com/docs/books/tutorial/"
1940  "index.html?name=networking#DOWNLOADING', 'AUTHORITY')", "user:pass@example.com");
1941  // Without user and pass.
1942  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
1943  "index.html?name=networking#DOWNLOADING', 'AUTHORITY')", "example.com:80");
1944  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
1945  "index.html?name=networking#DOWNLOADING', 'AUTHORITY')", "example.com");
1946  // Exactly what Hive returns as well.
1947  TestStringValue("parse_url('http://example.com_xyzabc^&*', 'AUTHORITY')",
1948  "example.com_xyzabc^&*");
1949  // Missing protocol.
1950  TestIsNull("parse_url('example.com/docs/books/tutorial/"
1951  "index.html?name=networking#DOWNLOADING', 'HOST')", TYPE_STRING);
1952 
1953  // FILE part.
1954  TestStringValue("parse_url('http://user:pass@example.com:80/docs/books/tutorial/"
1955  "index.html?name=networking#DOWNLOADING', 'FILE')",
1956  "/docs/books/tutorial/index.html?name=networking");
1957  TestStringValue("parse_url('http://user:pass@example.com/docs/books/tutorial/"
1958  "index.html?name=networking#DOWNLOADING', 'FILE')",
1959  "/docs/books/tutorial/index.html?name=networking");
1960  // Without user and pass.
1961  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
1962  "index.html?name=networking#DOWNLOADING', 'FILE')",
1963  "/docs/books/tutorial/index.html?name=networking");
1964  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
1965  "index.html?name=networking#DOWNLOADING', 'FILE')",
1966  "/docs/books/tutorial/index.html?name=networking");
1967  // With trimming.
1968  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
1969  "index.html?name=networking ', 'FILE')",
1970  "/docs/books/tutorial/index.html?name=networking");
1971  // No question mark but a hash (consistent with Hive).
1972  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
1973  "index.html#something', 'FILE')",
1974  "/docs/books/tutorial/index.html");
1975  // No hash or question mark (consistent with Hive).
1976  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
1977  "index.htmlsomething', 'FILE')",
1978  "/docs/books/tutorial/index.htmlsomething");
1979  // Missing protocol.
1980  TestIsNull("parse_url('example.com/docs/books/tutorial/"
1981  "index.html?name=networking#DOWNLOADING', 'FILE')", TYPE_STRING);
1982 
1983  // HOST part.
1984  TestStringValue("parse_url('http://user:pass@example.com:80/docs/books/tutorial/"
1985  "index.html?name=networking#DOWNLOADING', 'HOST')", "example.com");
1986  TestStringValue("parse_url('http://user:pass@example.com/docs/books/tutorial/"
1987  "index.html?name=networking#DOWNLOADING', 'HOST')", "example.com");
1988  // Without user and pass.
1989  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
1990  "index.html?name=networking#DOWNLOADING', 'HOST')", "example.com");
1991  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
1992  "index.html?name=networking#DOWNLOADING', 'HOST')", "example.com");
1993  // Exactly what Hive returns as well.
1994  TestStringValue("parse_url('http://example.com_xyzabc^&*', 'HOST')",
1995  "example.com_xyzabc^&*");
1996  // Colon after host:port/ (with port)
1997  TestStringValue("parse_url('http://user:pass@example.com:80/docs/books/tutorial/"
1998  "index.html?name:networking#DOWNLOADING', 'HOST')", "example.com");
1999  // Colon after host/ (without port)
2000  TestStringValue("parse_url('http://user:pass@example.com/docs/books/tutorial/"
2001  "index.html?name:networking#DOWNLOADING', 'HOST')", "example.com");
2002  // Colon in query without '/' no port
2003  TestStringValue("parse_url('http://user:pass@example.com"
2004  "?name:networking#DOWNLOADING', 'HOST')", "example.com");
2005  // Colon in query without '/' with port
2006  TestStringValue("parse_url('http://user:pass@example.com:80"
2007  "?name:networking#DOWNLOADING', 'HOST')", "example.com");
2008  // '/' in query
2009  TestStringValue("parse_url('http://user:pass@example.com:80"
2010  "?name:networking/DOWNLOADING', 'HOST')", "example.com");
2011  // Missing protocol.
2012  TestIsNull("parse_url('example.com/docs/books/tutorial/"
2013  "index.html?name=networking#DOWNLOADING', 'HOST')", TYPE_STRING);
2014 
2015  // PATH part.
2016  TestStringValue("parse_url('http://user:pass@example.com:80/docs/books/tutorial/"
2017  "index.html?name=networking#DOWNLOADING', 'PATH')",
2018  "/docs/books/tutorial/index.html");
2019  TestStringValue("parse_url('http://user:pass@example.com/docs/books/tutorial/"
2020  "index.html?name=networking#DOWNLOADING', 'PATH')",
2021  "/docs/books/tutorial/index.html");
2022  // Without user and pass.
2023  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
2024  "index.html?name=networking#DOWNLOADING', 'PATH')",
2025  "/docs/books/tutorial/index.html");
2026  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
2027  "index.html?name=networking#DOWNLOADING', 'PATH')",
2028  "/docs/books/tutorial/index.html");
2029  // With trimming.
2030  TestStringValue("parse_url('http://user:pass@example.com/docs/books/tutorial/"
2031  "index.html ', 'PATH')",
2032  "/docs/books/tutorial/index.html");
2033  // No question mark but a hash (consistent with Hive).
2034  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
2035  "index.html#something', 'PATH')",
2036  "/docs/books/tutorial/index.html");
2037  // No hash or question mark (consistent with Hive).
2038  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
2039  "index.htmlsomething', 'PATH')",
2040  "/docs/books/tutorial/index.htmlsomething");
2041  // Missing protocol.
2042  TestIsNull("parse_url('example.com/docs/books/tutorial/"
2043  "index.html?name=networking#DOWNLOADING', 'PATH')", TYPE_STRING);
2044 
2045  // PROTOCOL part.
2046  TestStringValue("parse_url('http://user:pass@example.com:80/docs/books/tutorial/"
2047  "index.html?name=networking#DOWNLOADING', 'PROTOCOL')", "http");
2048  TestStringValue("parse_url('https://user:pass@example.com/docs/books/tutorial/"
2049  "index.html?name=networking#DOWNLOADING', 'PROTOCOL')", "https");
2050  // Without user and pass.
2051  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
2052  "index.html?name=networking#DOWNLOADING', 'PROTOCOL')", "http");
2053  TestStringValue("parse_url('https://example.com/docs/books/tutorial/"
2054  "index.html?name=networking#DOWNLOADING', 'PROTOCOL')", "https");
2055  // With trimming.
2056  TestStringValue("parse_url(' https://user:pass@example.com/docs/books/tutorial/"
2057  "index.html?name=networking#DOWNLOADING', 'PROTOCOL')", "https");
2058  // Missing protocol.
2059  TestIsNull("parse_url('user:pass@example.com/docs/books/tutorial/"
2060  "index.html?name=networking#DOWNLOADING', 'PROTOCOL')", TYPE_STRING);
2061 
2062  // QUERY part.
2063  TestStringValue("parse_url('http://user:pass@example.com:80/docs/books/tutorial/"
2064  "index.html?name=networking#DOWNLOADING', 'QUERY')", "name=networking");
2065  TestStringValue("parse_url('http://user:pass@example.com/docs/books/tutorial/"
2066  "index.html?name=networking#DOWNLOADING', 'QUERY')", "name=networking");
2067  // Without user and pass.
2068  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
2069  "index.html?name=networking#DOWNLOADING', 'QUERY')", "name=networking");
2070  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
2071  "index.html?name=networking#DOWNLOADING', 'QUERY')", "name=networking");
2072  // With trimming.
2073  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
2074  "index.html?name=networking ', 'QUERY')", "name=networking");
2075  // No '?'. Hive also returns NULL.
2076  TestIsNull("parse_url('http://example.com_xyzabc^&*', 'QUERY')", TYPE_STRING);
2077  // Missing protocol.
2078  TestIsNull("parse_url('example.com/docs/books/tutorial/"
2079  "index.html?name=networking#DOWNLOADING', 'QUERY')", TYPE_STRING);
2080 
2081  // PATH part.
2082  TestStringValue("parse_url('http://user:pass@example.com:80/docs/books/tutorial/"
2083  "index.html?name=networking#DOWNLOADING', 'PATH')",
2084  "/docs/books/tutorial/index.html");
2085  TestStringValue("parse_url('http://user:pass@example.com/docs/books/tutorial/"
2086  "index.html?name=networking#DOWNLOADING', 'PATH')",
2087  "/docs/books/tutorial/index.html");
2088  // Without user and pass.
2089  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
2090  "index.html?name=networking#DOWNLOADING', 'PATH')",
2091  "/docs/books/tutorial/index.html");
2092  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
2093  "index.html?name=networking#DOWNLOADING', 'PATH')",
2094  "/docs/books/tutorial/index.html");
2095  // With trimming.
2096  TestStringValue("parse_url('http://user:pass@example.com/docs/books/tutorial/"
2097  "index.html ', 'PATH')",
2098  "/docs/books/tutorial/index.html");
2099  // No question mark but a hash (consistent with Hive).
2100  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
2101  "index.html#something', 'PATH')",
2102  "/docs/books/tutorial/index.html");
2103  // No hash or question mark (consistent with Hive).
2104  TestStringValue("parse_url('http://example.com/docs/books/tutorial/"
2105  "index.htmlsomething', 'PATH')",
2106  "/docs/books/tutorial/index.htmlsomething");
2107  // Missing protocol.
2108  TestIsNull("parse_url('example.com/docs/books/tutorial/"
2109  "index.html?name=networking#DOWNLOADING', 'PATH')", TYPE_STRING);
2110 
2111  // USERINFO part.
2112  TestStringValue("parse_url('http://user:pass@example.com:80/docs/books/tutorial/"
2113  "index.html?name=networking#DOWNLOADING', 'USERINFO')", "user:pass");
2114  TestStringValue("parse_url('http://user:pass@example.com/docs/books/tutorial/"
2115  "index.html?name=networking#DOWNLOADING', 'USERINFO')", "user:pass");
2116  // Only user given.
2117  TestStringValue("parse_url('http://user@example.com/docs/books/tutorial/"
2118  "index.html?name=networking#DOWNLOADING', 'USERINFO')", "user");
2119  // No user or pass. Hive also returns NULL.
2120  TestIsNull("parse_url('http://example.com:80/docs/books/tutorial/"
2121  "index.html?name=networking#DOWNLOADING', 'USERINFO')", TYPE_STRING);
2122  // Missing protocol.
2123  TestIsNull("parse_url('example.com/docs/books/tutorial/"
2124  "index.html?name=networking#DOWNLOADING', 'USERINFO')", TYPE_STRING);
2125 
2126  // Invalid part parameters.
2127  // All characters in the part parameter must be uppercase (consistent with Hive).
2128  TestError("parse_url('http://example.com', 'authority')");
2129  TestError("parse_url('http://example.com', 'Authority')");
2130  TestError("parse_url('http://example.com', 'AUTHORITYXYZ')");
2131  TestError("parse_url('http://example.com', 'file')");
2132  TestError("parse_url('http://example.com', 'File')");
2133  TestError("parse_url('http://example.com', 'FILEXYZ')");
2134  TestError("parse_url('http://example.com', 'host')");
2135  TestError("parse_url('http://example.com', 'Host')");
2136  TestError("parse_url('http://example.com', 'HOSTXYZ')");
2137  TestError("parse_url('http://example.com', 'path')");
2138  TestError("parse_url('http://example.com', 'Path')");
2139  TestError("parse_url('http://example.com', 'PATHXYZ')");
2140  TestError("parse_url('http://example.com', 'protocol')");
2141  TestError("parse_url('http://example.com', 'Protocol')");
2142  TestError("parse_url('http://example.com', 'PROTOCOLXYZ')");
2143  TestError("parse_url('http://example.com', 'query')");
2144  TestError("parse_url('http://example.com', 'Query')");
2145  TestError("parse_url('http://example.com', 'QUERYXYZ')");
2146  TestError("parse_url('http://example.com', 'ref')");
2147  TestError("parse_url('http://example.com', 'Ref')");
2148  TestError("parse_url('http://example.com', 'REFXYZ')");
2149  TestError("parse_url('http://example.com', 'userinfo')");
2150  TestError("parse_url('http://example.com', 'Userinfo')");
2151  TestError("parse_url('http://example.com', 'USERINFOXYZ')");
2152 
2153  // NULL arguments.
2154  TestIsNull("parse_url(NULL, 'AUTHORITY')", TYPE_STRING);
2155  TestIsNull("parse_url('http://user:pass@example.com:80/docs/books/tutorial/"
2156  "index.html?name=networking#DOWNLOADING', NULL)", TYPE_STRING);
2157  TestIsNull("parse_url(NULL, NULL)", TYPE_STRING);
2158 
2159  // Key's value is terminated by '#'.
2160  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
2161  "index.html?name=networking#DOWNLOADING', 'QUERY', 'name')", "networking");
2162  // Key's value is terminated by end of string.
2163  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
2164  "index.html?name=networking', 'QUERY', 'name')", "networking");
2165  // Key's value is terminated by end of string, with trimming.
2166  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
2167  "index.html?name=networking ', 'QUERY', 'name')", "networking");
2168  // Key's value is terminated by '&'.
2169  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
2170  "index.html?name=networking&test=true', 'QUERY', 'name')", "networking");
2171  // Key's value is some query param in the middle.
2172  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
2173  "index.html?test=true&name=networking&op=true', 'QUERY', 'name')", "networking");
2174  // Key string appears in various parts of the url.
2175  TestStringValue("parse_url('http://name.name:80/name/books/tutorial/"
2176  "name.html?name_fake=true&name=networking&op=true#name', 'QUERY', 'name')",
2177  "networking");
2178  // We can still match this even though no '?' was given.
2179  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
2180  "index.htmltest=true&name=networking&op=true', 'QUERY', 'name')", "networking");
2181  // Requested key doesn't exist.
2182  TestIsNull("parse_url('http://example.com:80/docs/books/tutorial/"
2183  "index.html?test=true&name=networking&op=true', 'QUERY', 'error')", TYPE_STRING);
2184  // Requested key doesn't exist in query part.
2185  TestIsNull("parse_url('http://example.com:80/docs/books/tutorial/"
2186  "name.html?test=true&op=true', 'QUERY', 'name')", TYPE_STRING);
2187  // Requested key doesn't exist in query part, but matches at end of string.
2188  TestIsNull("parse_url('http://example.com:80/docs/books/tutorial/"
2189  "name.html?test=true&op=name', 'QUERY', 'name')", TYPE_STRING);
2190  // Malformed urls with incorrectly positioned '?' or '='.
2191  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
2192  "index.html?test=true&name=net?working&op=true', 'QUERY', 'name')", "net?working");
2193  TestStringValue("parse_url('http://example.com:80/docs/books/tutorial/"
2194  "index.html?test=true&name=net=working&op=true', 'QUERY', 'name')", "net=working");
2195  // Key paremeter given without QUERY part.
2196  TestIsNull("parse_url('http://example.com:80/docs/books/tutorial/"
2197  "index.html?test=true&name=networking&op=true', 'AUTHORITY', 'name')", TYPE_STRING);
2198  TestIsNull("parse_url('http://example.com:80/docs/books/tutorial/"
2199  "index.html?test=true&name=networking&op=true', 'FILE', 'name')", TYPE_STRING);
2200  TestIsNull("parse_url('http://example.com:80/docs/books/tutorial/"
2201  "index.html?test=true&name=networking&op=true', 'PATH', 'name')", TYPE_STRING);
2202  TestIsNull("parse_url('http://example.com:80/docs/books/tutorial/"
2203  "index.html?test=true&name=networking&op=true', 'PROTOCOL', 'name')", TYPE_STRING);
2204  TestIsNull("parse_url('http://example.com:80/docs/books/tutorial/"
2205  "index.html?test=true&name=networking&op=true', 'REF', 'name')", TYPE_STRING);
2206  TestError("parse_url('http://example.com:80/docs/books/tutorial/"
2207  "index.html?test=true&name=networking&op=true', 'XYZ', 'name')");
2208 }
2209 
2211  TestStringValue("current_database()", "default");
2212  TestStringValue("user()", "impala_test_user");
2213  TestStringValue("version()", GetVersionString());
2214  TestValue("sleep(100)", TYPE_BOOLEAN, true);
2215  TestIsNull("sleep(NULL)", TYPE_BOOLEAN);
2216 
2217  // Test typeOf
2218  TestStringValue("typeOf(!true)", "BOOLEAN");
2219  TestStringValue("typeOf(1)", "TINYINT");
2220  TestStringValue("typeOf(0)", "TINYINT");
2221  TestStringValue("typeOf(-1)", "TINYINT");
2222  TestStringValue("typeOf(128)", "SMALLINT");
2223  TestStringValue("typeOf(32768)", "INT");
2224  TestStringValue("typeOf(2147483648)", "BIGINT");
2225  TestStringValue("typeOf(4294967296)", "BIGINT");
2226  TestStringValue("typeOf(-4294967296)", "BIGINT");
2227  TestStringValue("typeOf(9223372036854775807)", "BIGINT");
2228  TestStringValue("typeOf(-9223372036854775808)", "BIGINT");
2229  TestStringValue("typeOf(cast(10 as FLOAT))", "FLOAT");
2230  TestStringValue("typeOf(cast(10 as DOUBLE))", "DOUBLE");
2231  TestStringValue("typeOf(current_database())", "STRING");
2232  TestStringValue("typeOf(now())", "TIMESTAMP");
2233  TestStringValue("typeOf(cast(10 as DECIMAL))", "DECIMAL(9,0)");
2234  TestStringValue("typeOf(0.0)", "DECIMAL(1,1)");
2235  TestStringValue("typeOf(3.14)", "DECIMAL(3,2)");
2236  TestStringValue("typeOf(-1.23)", "DECIMAL(3,2)");
2237  TestStringValue("typeOf(cast(NULL as STRING))", "STRING");
2238  TestStringValue("typeOf(\"\")", "STRING");
2239  TestStringValue("typeOf(NULL)", "BOOLEAN");
2240  TestStringValue("typeOf(34 < NULL)", "BOOLEAN");
2241  TestStringValue("typeOf(cast('a' as CHAR(2)))", "CHAR(2)");
2242  TestStringValue("typeOf(cast('abcdef' as CHAR(4)))", "CHAR(4)");
2243  TestStringValue("typeOf(cast('a' as VARCHAR(2)))", "VARCHAR(2)");
2244  TestStringValue("typeOf(cast('abcdef' as VARCHAR(4)))", "VARCHAR(4)");
2245  TestStringValue("typeOf(cast(NULL as CHAR(2)))", "CHAR(2)");
2246  TestStringValue("typeOf(cast(NULL as VARCHAR(4)))", "VARCHAR(4)");
2247 
2248  // Test fnv_hash
2249  string s("hello world");
2250  uint64_t expected = HashUtil::FnvHash64(s.data(), s.size(), HashUtil::FNV_SEED);
2251  TestValue("fnv_hash('hello world')", TYPE_BIGINT, expected);
2252  s = string("");
2253  expected = HashUtil::FnvHash64(s.data(), s.size(), HashUtil::FNV_SEED);
2254  TestValue("fnv_hash('')", TYPE_BIGINT, expected);
2255 
2256  IntValMap::iterator int_iter;
2257  for(int_iter = min_int_values_.begin(); int_iter != min_int_values_.end();
2258  ++int_iter) {
2259  ColumnType t = ColumnType(static_cast<PrimitiveType>(int_iter->first));
2260  expected = HashUtil::FnvHash64(
2261  &int_iter->second, t.GetByteSize(), HashUtil::FNV_SEED);
2262  string& val = default_type_strs_[int_iter->first];
2263  TestValue("fnv_hash(" + val + ")", TYPE_BIGINT, expected);
2264  }
2265 
2266  // Don't use min_float_values_ for testing floats and doubles due to improper float
2267  // and double literal handling, see IMPALA-669.
2268  float float_val = 42;
2269  expected = HashUtil::FnvHash64(&float_val, sizeof(float), HashUtil::FNV_SEED);
2270  TestValue("fnv_hash(CAST(42 as FLOAT))", TYPE_BIGINT, expected);
2271 
2272  double double_val = 42;
2273  expected = HashUtil::FnvHash64(&double_val, sizeof(double), HashUtil::FNV_SEED);
2274  TestValue("fnv_hash(CAST(42 as DOUBLE))", TYPE_BIGINT, expected);
2275 
2276  expected = HashUtil::FnvHash64(&default_timestamp_val_, 12, HashUtil::FNV_SEED);
2277  TestValue("fnv_hash(" + default_timestamp_str_ + ")", TYPE_BIGINT, expected);
2278 
2279  bool bool_val = false;
2280  expected = HashUtil::FnvHash64(&bool_val, 1, HashUtil::FNV_SEED);
2281  TestValue("fnv_hash(FALSE)", TYPE_BIGINT, expected);
2282 
2283  // Test NULL input returns NULL
2284  TestIsNull("fnv_hash(NULL)", TYPE_BIGINT);
2285 }
2286 
2287 TEST_F(ExprTest, NonFiniteFloats) {
2288  TestValue("is_inf(0.0)", TYPE_BOOLEAN, false);
2289  TestValue("is_inf(-1/0)", TYPE_BOOLEAN, true);
2290  TestValue("is_inf(1/0)", TYPE_BOOLEAN, true);
2291  TestValue("is_inf(0/0)", TYPE_BOOLEAN, false);
2292  TestValue("is_inf(NULL)", TYPE_BOOLEAN, false);
2293  TestValue("is_nan(NULL)", TYPE_BOOLEAN, false);
2294 
2295  TestValue("is_nan(0.0)", TYPE_BOOLEAN, false);
2296  TestValue("is_nan(1/0)", TYPE_BOOLEAN, false);
2297  TestValue("is_nan(0/0)", TYPE_BOOLEAN, true);
2298 
2299  TestValue("CAST(1/0 AS FLOAT)", TYPE_FLOAT, numeric_limits<float>::infinity());
2300  TestValue("CAST(1/0 AS DOUBLE)", TYPE_DOUBLE, numeric_limits<double>::infinity());
2301  TestValue("CAST(CAST(1/0 as FLOAT) as DOUBLE)", TYPE_DOUBLE,
2302  numeric_limits<double>::infinity());
2303  TestStringValue("CAST(1/0 AS STRING)", "inf");
2304  TestStringValue("CAST(CAST(1/0 AS FLOAT) AS STRING)", "inf");
2305 
2306  TestValue("CAST('inf' AS FLOAT)", TYPE_FLOAT, numeric_limits<float>::infinity());
2307  TestValue("CAST('inf' AS DOUBLE)", TYPE_DOUBLE, numeric_limits<double>::infinity());
2308  TestValue("CAST('Infinity' AS FLOAT)", TYPE_FLOAT, numeric_limits<float>::infinity());
2309  TestValue("CAST('-Infinity' AS DOUBLE)", TYPE_DOUBLE,
2310  -numeric_limits<double>::infinity());
2311 
2312  // NaN != NaN, so we have to wrap the value in a string
2313  TestStringValue("CAST(CAST('nan' AS FLOAT) AS STRING)", string("nan"));
2314  TestStringValue("CAST(CAST('nan' AS DOUBLE) AS STRING)", string("nan"));
2315  // 0/0 evalutes to -nan, test that we return "nan"
2316  TestStringValue("CAST(0/0 AS STRING)", string("nan"));
2317 }
2318 
2319 TEST_F(ExprTest, MathTrigonometricFunctions) {
2320  // It is important to calculate the expected values
2321  // using math functions, and not simply use constants.
2322  // Otherwise, floating point imprecisions may lead to failed tests.
2323  TestValue("sin(0.0)", TYPE_DOUBLE, sin(0.0));
2324  TestValue("sin(pi())", TYPE_DOUBLE, sin(M_PI));
2325  TestValue("sin(pi() / 2.0)", TYPE_DOUBLE, sin(M_PI / 2.0));
2326  TestValue("asin(-1.0)", TYPE_DOUBLE, asin(-1.0));
2327  TestValue("asin(1.0)", TYPE_DOUBLE, asin(1.0));
2328  TestValue("cos(0.0)", TYPE_DOUBLE, cos(0.0));
2329  TestValue("cos(pi())", TYPE_DOUBLE, cos(M_PI));
2330  TestValue("acos(-1.0)", TYPE_DOUBLE, acos(-1.0));
2331  TestValue("acos(1.0)", TYPE_DOUBLE, acos(1.0));
2332  TestValue("tan(pi() * -1.0)", TYPE_DOUBLE, tan(M_PI * -1.0));
2333  TestValue("tan(pi())", TYPE_DOUBLE, tan(M_PI));
2334  TestValue("atan(pi())", TYPE_DOUBLE, atan(M_PI));
2335  TestValue("atan(pi() * - 1.0)", TYPE_DOUBLE, atan(M_PI * -1.0));
2336  // this gets a very very small number rather than 0.
2337  // TestValue("radians(0)", TYPE_DOUBLE, 0);
2338  TestValue("radians(180.0)", TYPE_DOUBLE, M_PI);
2339  TestValue("degrees(0)", TYPE_DOUBLE, 0.0);
2340  TestValue("degrees(pi())", TYPE_DOUBLE, 180.0);
2341 
2342  // NULL arguments.
2343  TestIsNull("sin(NULL)", TYPE_DOUBLE);
2344  TestIsNull("asin(NULL)", TYPE_DOUBLE);
2345  TestIsNull("cos(NULL)", TYPE_DOUBLE);
2346  TestIsNull("acos(NULL)", TYPE_DOUBLE);
2347  TestIsNull("tan(NULL)", TYPE_DOUBLE);
2348  TestIsNull("atan(NULL)", TYPE_DOUBLE);
2349  TestIsNull("radians(NULL)", TYPE_DOUBLE);
2350  TestIsNull("degrees(NULL)", TYPE_DOUBLE);
2351 }
2352 
2353 TEST_F(ExprTest, MathConversionFunctions) {
2354  TestStringValue("bin(0)", "0");
2355  TestStringValue("bin(1)", "1");
2356  TestStringValue("bin(12)", "1100");
2357  TestStringValue("bin(1234567)", "100101101011010000111");
2358  TestStringValue("bin(" + lexical_cast<string>(numeric_limits<int64_t>::max()) + ")",
2359  "111111111111111111111111111111111111111111111111111111111111111");
2360  TestStringValue("bin(" + lexical_cast<string>(numeric_limits<int64_t>::min()+1) + ")",
2361  "1000000000000000000000000000000000000000000000000000000000000001");
2362 
2363  TestStringValue("hex(0)", "0");
2364  TestStringValue("hex(15)", "F");
2365  TestStringValue("hex(16)", "10");
2366  TestStringValue("hex(" + lexical_cast<string>(numeric_limits<int64_t>::max()) + ")",
2367  "7FFFFFFFFFFFFFFF");
2368  TestStringValue("hex(" + lexical_cast<string>(numeric_limits<int64_t>::min()+1) + ")",
2369  "8000000000000001");
2370  TestStringValue("hex('0')", "30");
2371  TestStringValue("hex('aAzZ')", "61417A5A");
2372  TestStringValue("hex('Impala')", "496D70616C61");
2373  TestStringValue("hex('impalA')", "696D70616C41");
2374  // Test non-ASCII characters
2375  TestStringValue("hex(unhex('D3'))", "D3");
2376  // Test width(2) and fill('0') for multiple characters < 16
2377  TestStringValue("hex(unhex('0303'))", "0303");
2378  TestStringValue("hex(unhex('D303D303'))", "D303D303");
2379 
2380  TestStringValue("unhex('30')", "0");
2381  TestStringValue("unhex('61417A5A')", "aAzZ");
2382  TestStringValue("unhex('496D70616C61')", "Impala");
2383  TestStringValue("unhex('696D70616C41')", "impalA");
2384  // Character not in hex alphabet results in empty string.
2385  TestStringValue("unhex('30GA')", "");
2386  // Uneven number of chars results in empty string.
2387  TestStringValue("unhex('30A')", "");
2388 
2389  // Run the test suite twice, once with a bigint parameter, and once with
2390  // string parameters.
2391  for (int i = 0; i < 2; ++i) {
2392  // First iteration is with bigint, second with string parameter.
2393  string q = (i == 0) ? "" : "'";
2394  // Invalid input: Base below -36 or above 36.
2395  TestIsNull("conv(" + q + "10" + q + ", 10, 37)", TYPE_STRING);
2396  TestIsNull("conv(" + q + "10" + q + ", 37, 10)", TYPE_STRING);
2397  TestIsNull("conv(" + q + "10" + q + ", 10, -37)", TYPE_STRING);
2398  TestIsNull("conv(" + q + "10" + q + ", -37, 10)", TYPE_STRING);
2399  // Invalid input: Base between -2 and 2.
2400  TestIsNull("conv(" + q + "10" + q + ", 10, 1)", TYPE_STRING);
2401  TestIsNull("conv(" + q + "10" + q + ", 1, 10)", TYPE_STRING);
2402  TestIsNull("conv(" + q + "10" + q + ", 10, -1)", TYPE_STRING);
2403  TestIsNull("conv(" + q + "10" + q + ", -1, 10)", TYPE_STRING);
2404  // Invalid input: Positive number but negative src base.
2405  TestIsNull("conv(" + q + "10" + q + ", -10, 10)", TYPE_STRING);
2406  // Test positive numbers.
2407  TestStringValue("conv(" + q + "10" + q + ", 10, 10)", "10");
2408  TestStringValue("conv(" + q + "10" + q + ", 2, 10)", "2");
2409  TestStringValue("conv(" + q + "11" + q + ", 36, 10)", "37");
2410  TestStringValue("conv(" + q + "11" + q + ", 36, 2)", "100101");
2411  TestStringValue("conv(" + q + "100101" + q + ", 2, 36)", "11");
2412  TestStringValue("conv(" + q + "0" + q + ", 10, 2)", "0");
2413  // Test negative numbers (tests from Hive).
2414  // If to_base is positive, the number should be handled as a 2's complement (64-bit).
2415  TestStringValue("conv(" + q + "-641" + q + ", 10, -10)", "-641");
2416  TestStringValue("conv(" + q + "1011" + q + ", 2, -16)", "B");
2417  TestStringValue("conv(" + q + "-1" + q + ", 10, 16)", "FFFFFFFFFFFFFFFF");
2418  TestStringValue("conv(" + q + "-15" + q + ", 10, 16)", "FFFFFFFFFFFFFFF1");
2419  // Test digits that are not available in srcbase. We expect those digits
2420  // from left-to-right that can be interpreted in srcbase to form the result
2421  // (i.e., the paring bails only when it encounters a digit not in srcbase).
2422  TestStringValue("conv(" + q + "17" + q + ", 7, 10)", "1");
2423  TestStringValue("conv(" + q + "371" + q + ", 7, 10)", "3");
2424  TestStringValue("conv(" + q + "371" + q + ", 7, 10)", "3");
2425  TestStringValue("conv(" + q + "445" + q + ", 5, 10)", "24");
2426  // Test overflow (tests from Hive).
2427  // If a number is two large, the result should be -1 (if signed),
2428  // or MAX_LONG (if unsigned).
2429  TestStringValue("conv(" + q + lexical_cast<string>(numeric_limits<int64_t>::max())
2430  + q + ", 36, 16)", "FFFFFFFFFFFFFFFF");
2431  TestStringValue("conv(" + q + lexical_cast<string>(numeric_limits<int64_t>::max())
2432  + q + ", 36, -16)", "-1");
2433  TestStringValue("conv(" + q + lexical_cast<string>(numeric_limits<int64_t>::min()+1)
2434  + q + ", 36, 16)", "FFFFFFFFFFFFFFFF");
2435  TestStringValue("conv(" + q + lexical_cast<string>(numeric_limits<int64_t>::min()+1)
2436  + q + ", 36, -16)", "-1");
2437  }
2438  // Test invalid input strings that start with an invalid digit.
2439  // Hive returns "0" in such cases.
2440  TestStringValue("conv('@', 16, 10)", "0");
2441  TestStringValue("conv('$123', 12, 2)", "0");
2442  TestStringValue("conv('*12g', 32, 5)", "0");
2443 
2444  // NULL arguments.
2445  TestIsNull("bin(NULL)", TYPE_STRING);
2446  TestIsNull("hex(NULL)", TYPE_STRING);
2447  TestIsNull("unhex(NULL)", TYPE_STRING);
2448  TestIsNull("conv(NULL, 10, 10)", TYPE_STRING);
2449  TestIsNull("conv(10, NULL, 10)", TYPE_STRING);
2450  TestIsNull("conv(10, 10, NULL)", TYPE_STRING);
2451  TestIsNull("conv(NULL, NULL, NULL)", TYPE_STRING);
2452 }
2453 
2455  TestValue("pi()", TYPE_DOUBLE, M_PI);
2456  TestValue("e()", TYPE_DOUBLE, M_E);
2457  TestValue("abs(cast(-1.0 as double))", TYPE_DOUBLE, 1.0);
2458  TestValue("abs(cast(1.0 as double))", TYPE_DOUBLE, 1.0);
2459  TestValue("sign(0.0)", TYPE_FLOAT, 0.0f);
2460  TestValue("sign(-0.0)", TYPE_FLOAT, 0.0f);
2461  TestValue("sign(+0.0)", TYPE_FLOAT, 0.0f);
2462  TestValue("sign(10.0)", TYPE_FLOAT, 1.0f);
2463  TestValue("sign(-10.0)", TYPE_FLOAT, -1.0f);
2464  TestIsNull("sign(NULL)", TYPE_FLOAT);
2465 
2466  // It is important to calculate the expected values
2467  // using math functions, and not simply use constants.
2468  // Otherwise, floating point imprecisions may lead to failed tests.
2469  TestValue("exp(2)", TYPE_DOUBLE, exp(2));
2470  TestValue("exp(e())", TYPE_DOUBLE, exp(M_E));
2471  TestValue("ln(e())", TYPE_DOUBLE, 1.0);
2472  TestValue("ln(255.0)", TYPE_DOUBLE, log(255.0));
2473  TestValue("log10(1000.0)", TYPE_DOUBLE, 3.0);
2474  TestValue("log10(50.0)", TYPE_DOUBLE, log10(50.0));
2475  TestValue("log2(64.0)", TYPE_DOUBLE, 6.0);
2476  TestValue("log2(678.0)", TYPE_DOUBLE, log(678.0) / log(2.0));
2477  TestValue("log(10.0, 1000.0)", TYPE_DOUBLE, log(1000.0) / log(10.0));
2478  TestValue("log(2.0, 64.0)", TYPE_DOUBLE, 6.0);
2479  TestValue("pow(2.0, 10.0)", TYPE_DOUBLE, pow(2.0, 10.0));
2480  TestValue("pow(e(), 2.0)", TYPE_DOUBLE, M_E * M_E);
2481  TestValue("power(2.0, 10.0)", TYPE_DOUBLE, pow(2.0, 10.0));
2482  TestValue("power(e(), 2.0)", TYPE_DOUBLE, M_E * M_E);
2483  TestValue("sqrt(121.0)", TYPE_DOUBLE, 11.0);
2484  TestValue("sqrt(2.0)", TYPE_DOUBLE, sqrt(2.0));
2485 
2486  // Run twice to test deterministic behavior.
2487  uint32_t seed = 0;
2488  double expected = static_cast<double>(rand_r(&seed)) / static_cast<double>(RAND_MAX);
2489  TestValue("rand()", TYPE_DOUBLE, expected);
2490  TestValue("rand()", TYPE_DOUBLE, expected);
2491  seed = 1234;
2492  expected = static_cast<double>(rand_r(&seed)) / static_cast<double>(RAND_MAX);
2493  TestValue("rand(1234)", TYPE_DOUBLE, expected);
2494  TestValue("rand(1234)", TYPE_DOUBLE, expected);
2495 
2496  // Test bigint param.
2497  TestValue("pmod(10, 3)", TYPE_BIGINT, 1);
2498  TestValue("pmod(-10, 3)", TYPE_BIGINT, 2);
2499  TestValue("pmod(10, -3)", TYPE_BIGINT, -2);
2500  TestValue("pmod(-10, -3)", TYPE_BIGINT, -1);
2501  TestValue("pmod(1234567890, 13)", TYPE_BIGINT, 10);
2502  TestValue("pmod(-1234567890, 13)", TYPE_BIGINT, 3);
2503  TestValue("pmod(1234567890, -13)", TYPE_BIGINT, -3);
2504  TestValue("pmod(-1234567890, -13)", TYPE_BIGINT, -10);
2505  // Test double param.
2506  TestValue("pmod(12.3, 4.0)", TYPE_DOUBLE, fmod(fmod(12.3, 4.0) + 4.0, 4.0));
2507  TestValue("pmod(-12.3, 4.0)", TYPE_DOUBLE, fmod(fmod(-12.3, 4.0) + 4.0, 4.0));
2508  TestValue("pmod(12.3, -4.0)", TYPE_DOUBLE, fmod(fmod(12.3, -4.0) - 4.0, -4.0));
2509  TestValue("pmod(-12.3, -4.0)", TYPE_DOUBLE, fmod(fmod(-12.3, -4.0) - 4.0, -4.0));
2510  TestValue("pmod(123456.789, 13.456)", TYPE_DOUBLE,
2511  fmod(fmod(123456.789, 13.456) + 13.456, 13.456));
2512  TestValue("pmod(-123456.789, 13.456)", TYPE_DOUBLE,
2513  fmod(fmod(-123456.789, 13.456) + 13.456, 13.456));
2514  TestValue("pmod(123456.789, -13.456)", TYPE_DOUBLE,
2515  fmod(fmod(123456.789, -13.456) - 13.456, -13.456));
2516  TestValue("pmod(-123456.789, -13.456)", TYPE_DOUBLE,
2517  fmod(fmod(-123456.789, -13.456) - 13.456, -13.456));
2518 
2519  // Test floating-point modulo function.
2520  TestValue("fmod(cast(12345.345 as float), cast(7 as float))",
2521  TYPE_FLOAT, fmodf(12345.345f, 7.0f));
2522  TestValue("fmod(cast(-12345.345 as float), cast(7 as float))",
2523  TYPE_FLOAT, fmodf(-12345.345f, 7.0f));
2524  TestValue("fmod(cast(-12345.345 as float), cast(-7 as float))",
2525  TYPE_FLOAT, fmodf(-12345.345f, -7.0f));
2526  TestValue("fmod(cast(12345.345 as double), 7)", TYPE_DOUBLE, fmod(12345.345, 7.0));
2527  TestValue("fmod(-12345.345, cast(7 as double))", TYPE_DOUBLE, fmod(-12345.345, 7.0));
2528  TestValue("fmod(cast(-12345.345 as double), -7)", TYPE_DOUBLE, fmod(-12345.345, -7.0));
2529  // Test floating-point modulo operator.
2530  TestValue("cast(12345.345 as float) % cast(7 as float)",
2531  TYPE_FLOAT, fmodf(12345.345f, 7.0f));
2532  TestValue("cast(-12345.345 as float) % cast(7 as float)",
2533  TYPE_FLOAT, fmodf(-12345.345f, 7.0f));
2534  TestValue("cast(-12345.345 as float) % cast(-7 as float)",
2535  TYPE_FLOAT, fmodf(-12345.345f, -7.0f));
2536  TestValue("cast(12345.345 as double) % 7", TYPE_DOUBLE, fmod(12345.345, 7.0));
2537  TestValue("-12345.345 % cast(7 as double)", TYPE_DOUBLE, fmod(-12345.345, 7.0));
2538  TestValue("cast(-12345.345 as double) % -7", TYPE_DOUBLE, fmod(-12345.345, -7.0));
2539  // Test floating-point modulo by zero.
2540  TestIsNull("fmod(cast(-12345.345 as float), cast(0 as float))", TYPE_FLOAT);
2541  TestIsNull("fmod(cast(-12345.345 as double), 0)", TYPE_DOUBLE);
2542  TestIsNull("cast(-12345.345 as float) % cast(0 as float)", TYPE_FLOAT);
2543  TestIsNull("cast(-12345.345 as double) % 0", TYPE_DOUBLE);
2544 
2545  // Test int param.
2546  TestValue("mod(cast(10 as tinyint), cast(3 as tinyint))", TYPE_TINYINT, 10 % 3);
2547  TestValue("mod(cast(10 as smallint), cast(3 as smallint))", TYPE_SMALLINT, 10 % 3);
2548  TestValue("mod(cast(10 as int), cast(3 as int))", TYPE_INT, 10 % 3);
2549  TestValue("mod(cast(10 as bigint), cast(3 as bigint))", TYPE_BIGINT, 10 % 3);
2550  TestIsNull("mod(cast(123 as tinyint), 0)", TYPE_TINYINT);
2551  TestIsNull("mod(cast(123 as smallint), 0)", TYPE_SMALLINT);
2552  TestIsNull("mod(cast(123 as int), 0)", TYPE_INT);
2553  TestIsNull("mod(cast(123 as bigint), 0)", TYPE_BIGINT);
2554  TestIsNull("mod(cast(123 as tinyint), NULL)", TYPE_TINYINT);
2555  TestIsNull("mod(cast(123 as smallint), NULL)", TYPE_SMALLINT);
2556  TestIsNull("mod(cast(123 as int), NULL)", TYPE_INT);
2557  TestIsNull("mod(cast(123 as bigint), NULL)", TYPE_BIGINT);
2558  TestIsNull("mod(cast(NULL as int), NULL)", TYPE_INT);
2559  // Test numeric param.
2560  TestValue("mod(cast(12.3 as float), cast(4.0 as float))", TYPE_FLOAT, fmodf(12.3f, 4.0f));
2561  TestValue("mod(cast(12.3 as double), cast(4.0 as double))", TYPE_DOUBLE, fmod(12.3, 4.0));
2562  TestIsNull("mod(cast(12345.345 as float), cast(0 as float))", TYPE_FLOAT);
2563  TestIsNull("mod(cast(12345.345 as double), cast(0 as double))", TYPE_DOUBLE);
2564  TestIsNull("mod(cast(12345.345 as float), NULL)", TYPE_FLOAT);
2565  TestIsNull("mod(cast(12345.345 as double), NULL)", TYPE_DOUBLE);
2566  TestIsNull("mod(cast(NULL as float), NULL)", TYPE_FLOAT);
2567 
2568  // Test positive().
2569  TestValue("positive(cast(123 as tinyint))", TYPE_TINYINT, 123);
2570  TestValue("positive(cast(123 as smallint))", TYPE_SMALLINT, 123);
2571  TestValue("positive(cast(123 as int))", TYPE_INT, 123);
2572  TestValue("positive(cast(123 as bigint))", TYPE_BIGINT, 123);
2573  TestValue("positive(cast(3.1415 as float))", TYPE_FLOAT, 3.1415f);
2574  TestValue("positive(cast(3.1415 as double))", TYPE_DOUBLE, 3.1415);
2575  TestValue("positive(cast(-123 as tinyint))", TYPE_TINYINT, -123);
2576  TestValue("positive(cast(-123 as smallint))", TYPE_SMALLINT, -123);
2577  TestValue("positive(cast(-123 as int))", TYPE_INT, -123);
2578  TestValue("positive(cast(-123 as bigint))", TYPE_BIGINT, -123);
2579  TestValue("positive(cast(-3.1415 as float))", TYPE_FLOAT, -3.1415f);
2580  TestValue("positive(cast(-3.1415 as double))", TYPE_DOUBLE, -3.1415);
2581  // Test negative().
2582  TestValue("negative(cast(123 as tinyint))", TYPE_TINYINT, -123);
2583  TestValue("negative(cast(123 as smallint))", TYPE_SMALLINT, -123);
2584  TestValue("negative(cast(123 as int))", TYPE_INT, -123);
2585  TestValue("negative(cast(123 as bigint))", TYPE_BIGINT, -123);
2586  TestValue("negative(cast(3.1415 as float))", TYPE_FLOAT, -3.1415f);
2587  TestValue("negative(cast(3.1415 as double))", TYPE_DOUBLE, -3.1415);
2588  TestValue("negative(cast(-123 as tinyint))", TYPE_TINYINT, 123);
2589  TestValue("negative(cast(-123 as smallint))", TYPE_SMALLINT, 123);
2590  TestValue("negative(cast(-123 as int))", TYPE_INT, 123);
2591  TestValue("negative(cast(-123 as bigint))", TYPE_BIGINT, 123);
2592  TestValue("negative(cast(-3.1415 as float))", TYPE_FLOAT, 3.1415f);
2593  TestValue("negative(cast(-3.1415 as double))", TYPE_DOUBLE, 3.1415);
2594 
2595  // Test bigint param.
2596  TestValue("quotient(12, 6)", TYPE_BIGINT, 2);
2597  TestValue("quotient(-12, 6)", TYPE_BIGINT, -2);
2598  TestIsNull("quotient(-12, 0)", TYPE_BIGINT);
2599  // Test double param.
2600  TestValue("quotient(30.5, 2.5)", TYPE_BIGINT, 15);
2601  TestValue("quotient(-30.5, 2.5)", TYPE_BIGINT, -15);
2602  TestIsNull("quotient(-30.5, 0.000999)", TYPE_BIGINT);
2603 
2604  // Tests to verify logic of least(). All types but STRING use the same
2605  // templated function, so there is no need to run all tests with all types.
2606  // Test single value.
2607  TestValue("least(1)", TYPE_TINYINT, 1);
2608  TestValue<float>("least(cast(1.25 as float))", TYPE_FLOAT, 1.25f);
2609  // Test ordering
2610  TestValue("least(10, 20)", TYPE_TINYINT, 10);
2611  TestValue("least(20, 10)", TYPE_TINYINT, 10);
2612  TestValue<float>("least(cast(500.25 as float), 300.25)", TYPE_FLOAT, 300.25f);
2613  TestValue<float>("least(cast(300.25 as float), 500.25)", TYPE_FLOAT, 300.25f);
2614  // Test to make sure least value is found in a mixed order set
2615  TestValue("least(1, 3, 4, 0, 6)", TYPE_TINYINT, 0);
2616  TestValue<float>("least(cast(1.25 as float), 3.25, 4.25, 0.25, 6.25)",
2617  TYPE_FLOAT, 0.25f);
2618  // Test to make sure the least value is found from a list of duplicates
2619  TestValue("least(1, 1, 1, 1)", TYPE_TINYINT, 1);
2620  TestValue<float>("least(cast(1.0 as float), 1.0, 1.0, 1.0)", TYPE_FLOAT, 1.0f);
2621  // Test repeating groups and ordering
2622  TestValue("least(2, 2, 1, 1)", TYPE_TINYINT, 1);
2623  TestValue("least(0, -2, 1)", TYPE_TINYINT, -2);
2624  TestValue<float>("least(cast(2.0 as float), 2.0, 1.0, 1.0)", TYPE_FLOAT, 1.0f);
2625  TestValue<float>("least(cast(0.0 as float), -2.0, 1.0)", TYPE_FLOAT, -2.0f);
2626  // Test all int types.
2627  string val_list;
2628  val_list = "0";
2629  BOOST_FOREACH(IntValMap::value_type& entry, min_int_values_) {
2630  string val_str = lexical_cast<string>(entry.second);
2631  val_list.append(", " + val_str);
2632  PrimitiveType t = static_cast<PrimitiveType>(entry.first);
2633  TestValue<int64_t>("least(" + val_list + ")", t, 0);
2634  }
2635  // Test double type.
2636  TestValue<double>("least(0.0, cast(-2.0 as double), 1.0)", TYPE_DOUBLE, -2.0f);
2637  // Test timestamp param
2638  TestStringValue("cast(least(cast('2014-09-26 12:00:00' as timestamp), "
2639  "cast('2013-09-26 12:00:00' as timestamp)) as string)", "2013-09-26 12:00:00");
2640  // Test string param.
2641  TestStringValue("least('2', '5', '12', '3')", "12");
2642  TestStringValue("least('apples', 'oranges', 'bananas')", "apples");
2643  TestStringValue("least('apples', 'applis', 'applas')", "applas");
2644  TestStringValue("least('apples', '!applis', 'applas')", "!applis");
2645  TestStringValue("least('apples', 'apples', 'apples')", "apples");
2646  TestStringValue("least('apples')", "apples");
2647  TestStringValue("least('A')", "A");
2648  TestStringValue("least('A', 'a')", "A");
2649  // Test ordering
2650  TestStringValue("least('a', 'A')", "A");
2651  TestStringValue("least('APPLES', 'APPLES')", "APPLES");
2652  TestStringValue("least('apples', 'APPLES')", "APPLES");
2653  TestStringValue("least('apples', 'app\nles')", "app\nles");
2654  TestStringValue("least('apples', 'app les')", "app les");
2655  TestStringValue("least('apples', 'app\nles')", "app\nles");
2656  TestStringValue("least('apples', 'app\tles')", "app\tles");
2657  TestStringValue("least('apples', 'app\fles')", "app\fles");
2658  TestStringValue("least('apples', 'app\vles')", "app\vles");
2659  TestStringValue("least('apples', 'app\rles')", "app\rles");
2660 
2661  // Tests to verify logic of greatest(). All types but STRING use the same
2662  // templated function, so there is no need to run all tests with all types.
2663  TestValue("greatest(1)", TYPE_TINYINT, 1);
2664  TestValue<float>("greatest(cast(1.25 as float))", TYPE_FLOAT, 1.25f);
2665  // Test ordering
2666  TestValue("greatest(10, 20)", TYPE_TINYINT, 20);
2667  TestValue("greatest(20, 10)", TYPE_TINYINT, 20);
2668  TestValue<float>("greatest(cast(500.25 as float), 300.25)", TYPE_FLOAT, 500.25f);
2669  TestValue<float>("greatest(cast(300.25 as float), 500.25)", TYPE_FLOAT, 500.25f);
2670  // Test to make sure least value is found in a mixed order set
2671  TestValue("greatest(1, 3, 4, 0, 6)", TYPE_TINYINT, 6);
2672  TestValue<float>("greatest(cast(1.25 as float), 3.25, 4.25, 0.25, 6.25)",
2673  TYPE_FLOAT, 6.25f);
2674  // Test to make sure the least value is found from a list of duplicates
2675  TestValue("greatest(1, 1, 1, 1)", TYPE_TINYINT, 1);
2676  TestValue<float>("greatest(cast(1.0 as float), 1.0, 1.0, 1.0)", TYPE_FLOAT, 1.0f);
2677  // Test repeating groups and ordering
2678  TestValue("greatest(2, 2, 1, 1)", TYPE_TINYINT, 2);
2679  TestValue("greatest(0, -2, 1)", TYPE_TINYINT, 1);
2680  TestValue<float>("greatest(cast(2.0 as float), 2.0, 1.0, 1.0)", TYPE_FLOAT, 2.0f);
2681  TestValue<float>("greatest(cast(0.0 as float), -2.0, 1.0)", TYPE_FLOAT, 1.0f);
2682  // Test all int types. A list of values will be built, each iteration adds a bigger
2683  // value. This requires min_int_values_ to be an ordered map.
2684  val_list = "0";
2685  BOOST_FOREACH(IntValMap::value_type& entry, min_int_values_) {
2686  string val_str = lexical_cast<string>(entry.second);
2687  val_list.append(", " + val_str);
2688  PrimitiveType t = static_cast<PrimitiveType>(entry.first);
2689  TestValue<int64_t>("greatest(" + val_list + ")", t, entry.second);
2690  }
2691  // Test double type.
2692  TestValue<double>("greatest(cast(0.0 as float), cast(-2.0 as double), 1.0)",
2693  TYPE_DOUBLE, 1.0);
2694  // Test timestamp param
2695  TestStringValue("cast(greatest(cast('2014-09-26 12:00:00' as timestamp), "
2696  "cast('2013-09-26 12:00:00' as timestamp)) as string)", "2014-09-26 12:00:00");
2697  // Test string param
2698  TestStringValue("greatest('2', '5', '12', '3')", "5");
2699  TestStringValue("greatest('apples', 'oranges', 'bananas')", "oranges");
2700  TestStringValue("greatest('apples', 'applis', 'applas')", "applis");
2701  TestStringValue("greatest('apples', '!applis', 'applas')", "apples");
2702  TestStringValue("greatest('apples', 'apples', 'apples')", "apples");
2703  TestStringValue("greatest('apples')", "apples");
2704  TestStringValue("greatest('A')", "A");
2705  TestStringValue("greatest('A', 'a')", "a");
2706  // Test ordering
2707  TestStringValue("greatest('a', 'A')", "a");
2708  TestStringValue("greatest('APPLES', 'APPLES')", "APPLES");
2709  TestStringValue("greatest('apples', 'APPLES')", "apples");
2710  TestStringValue("greatest('apples', 'app\nles')", "apples");
2711  TestStringValue("greatest('apples', 'app les')", "apples");
2712  TestStringValue("greatest('apples', 'app\nles')", "apples");
2713  TestStringValue("greatest('apples', 'app\tles')", "apples");
2714  TestStringValue("greatest('apples', 'app\fles')", "apples");
2715  TestStringValue("greatest('apples', 'app\vles')", "apples");
2716  TestStringValue("greatest('apples', 'app\rles')", "apples");
2717 
2718  // NULL arguments.
2719  TestIsNull("abs(NULL)", TYPE_BIGINT);
2720  TestIsNull("sign(NULL)", TYPE_FLOAT);
2721  TestIsNull("exp(NULL)", TYPE_DOUBLE);
2722  TestIsNull("ln(NULL)", TYPE_DOUBLE);
2723  TestIsNull("log10(NULL)", TYPE_DOUBLE);
2724  TestIsNull("log2(NULL)", TYPE_DOUBLE);
2725  TestIsNull("log(NULL, 64.0)", TYPE_DOUBLE);
2726  TestIsNull("log(2.0, NULL)", TYPE_DOUBLE);
2727  TestIsNull("log(NULL, NULL)", TYPE_DOUBLE);
2728  TestIsNull("pow(NULL, 10.0)", TYPE_DOUBLE);
2729  TestIsNull("pow(2.0, NULL)", TYPE_DOUBLE);
2730  TestIsNull("pow(NULL, NULL)", TYPE_DOUBLE);
2731  TestIsNull("power(NULL, 10.0)", TYPE_DOUBLE);
2732  TestIsNull("power(2.0, NULL)", TYPE_DOUBLE);
2733  TestIsNull("power(NULL, NULL)", TYPE_DOUBLE);
2734  TestIsNull("sqrt(NULL)", TYPE_DOUBLE);
2735  TestIsNull("rand(NULL)", TYPE_DOUBLE);
2736  TestIsNull("pmod(NULL, 3)", TYPE_BIGINT);
2737  TestIsNull("pmod(10, NULL)", TYPE_BIGINT);
2738  TestIsNull("pmod(NULL, NULL)", TYPE_BIGINT);
2739  TestIsNull("fmod(NULL, cast(3.2 as float))", TYPE_FLOAT);
2740  TestIsNull("fmod(cast(10.3 as float), NULL)", TYPE_FLOAT);
2741  TestIsNull("fmod(NULL, cast(3.2 as double))", TYPE_DOUBLE);
2742  TestIsNull("fmod(cast(10.3 as double), NULL)", TYPE_DOUBLE);
2743  TestIsNull("NULL % cast(3.2 as float)", TYPE_FLOAT);
2744  TestIsNull("cast(10.3 as float) % NULL", TYPE_FLOAT);
2745  TestIsNull("NULL % cast(3.2 as double)", TYPE_DOUBLE);
2746  TestIsNull("cast(10.3 as double) % NULL", TYPE_DOUBLE);
2747  TestIsNull("fmod(NULL, NULL)", TYPE_FLOAT);
2748  TestIsNull("NULL % NULL", TYPE_DOUBLE);
2749  TestIsNull("positive(NULL)", TYPE_TINYINT);
2750  TestIsNull("negative(NULL)", TYPE_TINYINT);
2751  TestIsNull("quotient(NULL, 1.0)", TYPE_BIGINT);
2752  TestIsNull("quotient(1.0, NULL)", TYPE_BIGINT);
2753  TestIsNull("quotient(NULL, NULL)", TYPE_BIGINT);
2754  TestIsNull("least(NULL)", TYPE_TINYINT);
2755  TestIsNull("least(cast(NULL as tinyint))", TYPE_TINYINT);
2756  TestIsNull("least(cast(NULL as smallint))", TYPE_SMALLINT);
2757  TestIsNull("least(cast(NULL as int))", TYPE_INT);
2758  TestIsNull("least(cast(NULL as bigint))", TYPE_BIGINT);
2759  TestIsNull("least(cast(NULL as float))", TYPE_FLOAT);
2760  TestIsNull("least(cast(NULL as double))", TYPE_DOUBLE);
2761  TestIsNull("least(cast(NULL as timestamp))", TYPE_TIMESTAMP);
2762  TestIsNull("greatest(NULL)", TYPE_TINYINT);
2763  TestIsNull("greatest(cast(NULL as tinyint))", TYPE_TINYINT);
2764  TestIsNull("greatest(cast(NULL as smallint))", TYPE_SMALLINT);
2765  TestIsNull("greatest(cast(NULL as int))", TYPE_INT);
2766  TestIsNull("greatest(cast(NULL as bigint))", TYPE_BIGINT);
2767  TestIsNull("greatest(cast(NULL as float))", TYPE_FLOAT);
2768  TestIsNull("greatest(cast(NULL as double))", TYPE_DOUBLE);
2769  TestIsNull("greatest(cast(NULL as timestamp))", TYPE_TIMESTAMP);
2770 }
2771 
2772 TEST_F(ExprTest, MathRoundingFunctions) {
2773  TestValue("ceil(cast(0.1 as double))", TYPE_BIGINT, 1);
2774  TestValue("ceil(cast(-10.05 as double))", TYPE_BIGINT, -10);
2775  TestValue("ceiling(cast(0.1 as double))", TYPE_BIGINT, 1);
2776  TestValue("ceiling(cast(-10.05 as double))", TYPE_BIGINT, -10);
2777  TestValue("floor(cast(0.1 as double))", TYPE_BIGINT, 0);
2778  TestValue("floor(cast(-10.007 as double))", TYPE_BIGINT, -11);
2779 
2780  TestValue("round(cast(1.499999 as double))", TYPE_BIGINT, 1);
2781  TestValue("round(cast(1.5 as double))", TYPE_BIGINT, 2);
2782  TestValue("round(cast(1.500001 as double))", TYPE_BIGINT, 2);
2783  TestValue("round(cast(-1.499999 as double))", TYPE_BIGINT, -1);
2784  TestValue("round(cast(-1.5 as double))", TYPE_BIGINT, -2);
2785  TestValue("round(cast(-1.500001 as double))", TYPE_BIGINT, -2);
2786 
2787  TestValue("round(cast(3.14159265 as double), 0)", TYPE_DOUBLE, 3.0);
2788  TestValue("round(cast(3.14159265 as double), 1)", TYPE_DOUBLE, 3.1);
2789  TestValue("round(cast(3.14159265 as double), 2)", TYPE_DOUBLE, 3.14);
2790  TestValue("round(cast(3.14159265 as double), 3)", TYPE_DOUBLE, 3.142);
2791  TestValue("round(cast(3.14159265 as double), 4)", TYPE_DOUBLE, 3.1416);
2792  TestValue("round(cast(3.14159265 as double), 5)", TYPE_DOUBLE, 3.14159);
2793  TestValue("round(cast(-3.14159265 as double), 0)", TYPE_DOUBLE, -3.0);
2794  TestValue("round(cast(-3.14159265 as double), 1)", TYPE_DOUBLE, -3.1);
2795  TestValue("round(cast(-3.14159265 as double), 2)", TYPE_DOUBLE, -3.14);
2796  TestValue("round(cast(-3.14159265 as double), 3)", TYPE_DOUBLE, -3.142);
2797  TestValue("round(cast(-3.14159265 as double), 4)", TYPE_DOUBLE, -3.1416);
2798  TestValue("round(cast(-3.14159265 as double), 5)", TYPE_DOUBLE, -3.14159);
2799 
2800  // NULL arguments.
2801  TestIsNull("ceil(cast(NULL as double))", TYPE_BIGINT);
2802  TestIsNull("ceiling(cast(NULL as double))", TYPE_BIGINT);
2803  TestIsNull("floor(cast(NULL as double))", TYPE_BIGINT);
2804  TestIsNull("round(cast(NULL as double))", TYPE_BIGINT);
2805  TestIsNull("round(cast(NULL as double), 1)", TYPE_DOUBLE);
2806  TestIsNull("round(cast(3.14159265 as double), NULL)", TYPE_DOUBLE);
2807  TestIsNull("round(cast(NULL as double), NULL)", TYPE_DOUBLE);
2808 }
2809 
2810 TEST_F(ExprTest, UnaryOperators) {
2811  TestValue("+1", TYPE_TINYINT, 1);
2812  TestValue("-1", TYPE_TINYINT, -1);
2813  TestValue("- -1", TYPE_TINYINT, 1);
2814  TestValue("+-1", TYPE_TINYINT, -1);
2815  TestValue("++1", TYPE_TINYINT, 1);
2816 
2817  TestValue("+cast(1. as float)", TYPE_FLOAT, 1.0f);
2818  TestValue("+cast(1.0 as float)", TYPE_FLOAT, 1.0f);
2819  TestValue("-cast(1.0 as float)", TYPE_DOUBLE, -1.0);
2820 
2821  TestValue("1 - - - 1", TYPE_SMALLINT, 0);
2822 }
2823 
2824 // TODO: I think a lot of these casts are not necessary and we should fix this
2826  // Regression test for CDH-19918
2827  TestStringValue("cast(from_utc_timestamp(cast(1301180400 as timestamp),"
2828  "'Europe/Moscow') as string)", "2011-03-27 03:00:00");
2829  TestStringValue("cast(from_utc_timestamp(cast(1301180399 as timestamp),"
2830  "'Europe/Moscow') as string)", "2011-03-27 01:59:59");
2831  TestStringValue("cast(from_utc_timestamp(cast(1288404000 as timestamp),"
2832  "'Europe/Moscow') as string)", "2010-10-30 06:00:00");
2833  TestStringValue("cast(from_utc_timestamp(cast(1288584000 as timestamp),"
2834  "'Europe/Moscow') as string)", "2010-11-01 07:00:00");
2835  TestStringValue("cast(from_utc_timestamp(cast(1301104740 as timestamp),"
2836  "'Europe/Moscow') as string)", "2011-03-26 04:59:00");
2837  TestStringValue("cast(from_utc_timestamp(cast(1301277600 as timestamp),"
2838  "'Europe/Moscow') as string)", "2011-03-28 06:00:00");
2839  TestStringValue("cast(from_utc_timestamp(cast(1324947600 as timestamp),"
2840  "'Europe/Moscow') as string)", "2011-12-27 05:00:00");
2841  TestStringValue("cast(from_utc_timestamp(cast(1325725200 as timestamp),"
2842  "'Europe/Moscow') as string)", "2012-01-05 05:00:00");
2843  TestStringValue("cast(from_utc_timestamp(cast(1333594800 as timestamp),"
2844  "'Europe/Moscow') as string)", "2012-04-05 07:00:00");
2845 
2846  // Regression for IMPALA-1105
2847  TestIsNull("cast(cast('NOTATIMESTAMP' as timestamp) as string)", TYPE_STRING);
2848 
2849  TestStringValue("cast(cast('2012-01-01 09:10:11.123456789' as timestamp) as string)",
2850  "2012-01-01 09:10:11.123456789");
2851  // Add/sub years.
2852  TestStringValue("cast(date_add(cast('2012-01-01 09:10:11.123456789' "
2853  "as timestamp), interval 10 years) as string)",
2854  "2022-01-01 09:10:11.123456789");
2855  TestStringValue("cast(date_sub(cast('2012-01-01 09:10:11.123456789' "
2856  "as timestamp), interval 10 years) as string)",
2857  "2002-01-01 09:10:11.123456789");
2858  TestStringValue("cast(date_add(cast('2012-01-01 09:10:11.123456789' "
2859  "as timestamp), interval cast(10 as bigint) years) as string)",
2860  "2022-01-01 09:10:11.123456789");
2861  TestStringValue("cast(date_sub(cast('2012-01-01 09:10:11.123456789' "
2862  "as timestamp), interval cast(10 as bigint) years) as string)",
2863  "2002-01-01 09:10:11.123456789");
2864  // These return NULL because year is out of range (IMPALA-1493). If very large
2865  // intervals are used the results will be incorrect due to using boost (IMPALA-1675
2866  // still unresolved).
2867  TestIsNull(
2868  "CAST('2005-10-11 00:00:00' AS TIMESTAMP) - INTERVAL 718 YEAR", TYPE_TIMESTAMP);
2869  TestIsNull(
2870  "CAST('2005-10-11 00:00:00' AS TIMESTAMP) + INTERVAL -718 YEAR", TYPE_TIMESTAMP);
2871  TestIsNull(
2872  "CAST('2005-10-11 00:00:00' AS TIMESTAMP) + INTERVAL 9718 YEAR", TYPE_TIMESTAMP);
2873  TestIsNull(
2874  "CAST('2005-10-11 00:00:00' AS TIMESTAMP) - INTERVAL -9718 YEAR", TYPE_TIMESTAMP);
2875  // Add/sub months.
2876  TestStringValue("cast(date_add(cast('2012-01-01 09:10:11.123456789' "
2877  "as timestamp), interval 13 months) as string)",
2878  "2013-02-01 09:10:11.123456789");
2879  TestStringValue("cast(date_sub(cast('2013-02-01 09:10:11.123456789' "
2880  "as timestamp), interval 13 months) as string)",
2881  "2012-01-01 09:10:11.123456789");
2882  TestStringValue("cast(date_add(cast('2012-01-31 09:10:11.123456789' "
2883  "as timestamp), interval cast(1 as bigint) month) as string)",
2884  "2012-02-29 09:10:11.123456789");
2885  TestStringValue("cast(date_sub(cast('2012-02-29 09:10:11.123456789' "
2886  "as timestamp), interval cast(1 as bigint) month) as string)",
2887  "2012-01-31 09:10:11.123456789");
2888  // Add/sub weeks.
2889  TestStringValue("cast(date_add(cast('2012-01-01 09:10:11.123456789' "
2890  "as timestamp), interval 2 weeks) as string)",
2891  "2012-01-15 09:10:11.123456789");
2892  TestStringValue("cast(date_sub(cast('2012-01-15 09:10:11.123456789' "
2893  "as timestamp), interval 2 weeks) as string)",
2894  "2012-01-01 09:10:11.123456789");
2895  TestStringValue("cast(date_add(cast('2012-01-01 09:10:11.123456789' "
2896  "as timestamp), interval cast(53 as bigint) weeks) as string)",
2897  "2013-01-06 09:10:11.123456789");
2898  TestStringValue("cast(date_sub(cast('2013-01-06 09:10:11.123456789' "
2899  "as timestamp), interval cast(53 as bigint) weeks) as string)",
2900  "2012-01-01 09:10:11.123456789");
2901  // Add/sub days.
2902  TestStringValue("cast(date_add(cast('2012-01-01 09:10:11.123456789' "
2903  "as timestamp), interval 10 days) as string)",
2904  "2012-01-11 09:10:11.123456789");
2905  TestStringValue("cast(date_sub(cast('2012-01-01 09:10:11.123456789' "
2906  "as timestamp), interval 10 days) as string)",
2907  "2011-12-22 09:10:11.123456789");
2908  TestStringValue("cast(date_add(cast('2011-12-22 09:10:11.12345678' "
2909  "as timestamp), interval cast(10 as bigint) days) as string)",
2910  "2012-01-01 09:10:11.123456780");
2911  TestStringValue("cast(date_sub(cast('2011-12-22 09:10:11.12345678' "
2912  "as timestamp), interval cast(365 as bigint) days) as string)",
2913  "2010-12-22 09:10:11.123456780");
2914  // Add/sub days (HIVE's date_add/sub variant).
2915  TestStringValue("cast(date_add(cast('2012-01-01 09:10:11.123456789' "
2916  "as timestamp), 10) as string)",
2917  "2012-01-11 09:10:11.123456789");
2918  TestStringValue(
2919  "cast(date_sub(cast('2012-01-01 09:10:11.123456789' as timestamp), 10) as string)",
2920  "2011-12-22 09:10:11.123456789");
2921  TestStringValue(
2922  "cast(date_add(cast('2011-12-22 09:10:11.12345678' as timestamp),"
2923  "cast(10 as bigint)) as string)", "2012-01-01 09:10:11.123456780");
2924  TestStringValue(
2925  "cast(date_sub(cast('2011-12-22 09:10:11.12345678' as timestamp),"
2926  "cast(365 as bigint)) as string)", "2010-12-22 09:10:11.123456780");
2927  // Add/sub hours.
2928  TestStringValue("cast(date_add(cast('2012-01-01 00:00:00.123456789' "
2929  "as timestamp), interval 25 hours) as string)",
2930  "2012-01-02 01:00:00.123456789");
2931  TestStringValue("cast(date_sub(cast('2012-01-02 01:00:00.123456789' "
2932  "as timestamp), interval 25 hours) as string)",
2933  "2012-01-01 00:00:00.123456789");
2934  TestStringValue("cast(date_add(cast('2012-01-01 00:00:00.123456789' "
2935  "as timestamp), interval cast(25 as bigint) hours) as string)",
2936  "2012-01-02 01:00:00.123456789");
2937  TestStringValue("cast(date_sub(cast('2012-01-02 01:00:00.123456789' "
2938  "as timestamp), interval cast(25 as bigint) hours) as string)",
2939  "2012-01-01 00:00:00.123456789");
2940  // Add/sub minutes.
2941  TestStringValue("cast(date_add(cast('2012-01-01 00:00:00.123456789' "
2942  "as timestamp), interval 1533 minutes) as string)",
2943  "2012-01-02 01:33:00.123456789");
2944  TestStringValue("cast(date_sub(cast('2012-01-02 01:33:00.123456789' "
2945  "as timestamp), interval 1533 minutes) as string)",
2946  "2012-01-01 00:00:00.123456789");
2947  TestStringValue("cast(date_add(cast('2012-01-01 00:00:00.123456789' "
2948  "as timestamp), interval cast(1533 as bigint) minutes) as string)",
2949  "2012-01-02 01:33:00.123456789");
2950  TestStringValue("cast(date_sub(cast('2012-01-02 01:33:00.123456789' "
2951  "as timestamp), interval cast(1533 as bigint) minutes) as string)",
2952  "2012-01-01 00:00:00.123456789");
2953  // Add/sub seconds.
2954  TestStringValue("cast(date_add(cast('2012-01-01 00:00:00.123456789' "
2955  "as timestamp), interval 90033 seconds) as string)",
2956  "2012-01-02 01:00:33.123456789");
2957  TestStringValue("cast(date_sub(cast('2012-01-02 01:00:33.123456789' "
2958  "as timestamp), interval 90033 seconds) as string)",
2959  "2012-01-01 00:00:00.123456789");
2960  TestStringValue("cast(date_add(cast('2012-01-01 00:00:00.123456789' "
2961  "as timestamp), interval cast(90033 as bigint) seconds) as string)",
2962  "2012-01-02 01:00:33.123456789");
2963  TestStringValue("cast(date_sub(cast('2012-01-02 01:00:33.123456789' "
2964  "as timestamp), interval cast(90033 as bigint) seconds) as string)",
2965  "2012-01-01 00:00:00.123456789");
2966  // Add/sub milliseconds.
2967  TestStringValue("cast(date_add(cast('2012-01-01 00:00:00.000000001' "
2968  "as timestamp), interval 90000033 milliseconds) as string)",
2969  "2012-01-02 01:00:00.033000001");
2970  TestStringValue("cast(date_sub(cast('2012-01-02 01:00:00.033000001' "
2971  "as timestamp), interval 90000033 milliseconds) as string)",
2972  "2012-01-01 00:00:00.000000001");
2973  TestStringValue("cast(date_add(cast('2012-01-01 00:00:00.000000001' "
2974  "as timestamp), interval cast(90000033 as bigint) milliseconds) as string)",
2975  "2012-01-02 01:00:00.033000001");
2976  TestStringValue("cast(date_sub(cast('2012-01-02 01:00:00.033000001' "
2977  "as timestamp), interval cast(90000033 as bigint) milliseconds) as string)",
2978  "2012-01-01 00:00:00.000000001");
2979  // Add/sub microseconds.
2980  TestStringValue("cast(date_add(cast('2012-01-01 00:00:00.000000001' "
2981  "as timestamp), interval 1033 microseconds) as string)",
2982  "2012-01-01 00:00:00.001033001");
2983  TestStringValue("cast(date_sub(cast('2012-01-01 00:00:00.001033001' "
2984  "as timestamp), interval 1033 microseconds) as string)",
2985  "2012-01-01 00:00:00.000000001");
2986  TestStringValue("cast(date_add(cast('2012-01-01 00:00:00.000000001' "
2987  "as timestamp), interval cast(1033 as bigint) microseconds) as string)",
2988  "2012-01-01 00:00:00.001033001");
2989  TestStringValue("cast(date_sub(cast('2012-01-01 00:00:00.001033001' "
2990  "as timestamp), interval cast(1033 as bigint) microseconds) as string)",
2991  "2012-01-01 00:00:00.000000001");
2992  // Add/sub nanoseconds.
2993  TestStringValue("cast(date_add(cast('2012-01-01 00:00:00.000000001' "
2994  "as timestamp), interval 1033 nanoseconds) as string)",
2995  "2012-01-01 00:00:00.000001034");
2996  TestStringValue("cast(date_sub(cast('2012-01-01 00:00:00.000001034' "
2997  "as timestamp), interval 1033 nanoseconds) as string)",
2998  "2012-01-01 00:00:00.000000001");
2999  TestStringValue("cast(date_add(cast('2012-01-01 00:00:00.000000001' "
3000  "as timestamp), interval cast(1033 as bigint) nanoseconds) as string)",
3001  "2012-01-01 00:00:00.000001034");
3002  TestStringValue("cast(date_sub(cast('2012-01-01 00:00:00.000001034' "
3003  "as timestamp), interval cast(1033 as bigint) nanoseconds) as string)",
3004  "2012-01-01 00:00:00.000000001");
3005 
3006  // NULL arguments.
3007  TestIsNull("date_add(NULL, interval 10 years)", TYPE_TIMESTAMP);
3008  TestIsNull("date_add(cast('2012-01-01 09:10:11.123456789' as timestamp),"
3009  "interval NULL years)", TYPE_TIMESTAMP);
3010  TestIsNull("date_add(NULL, interval NULL years)", TYPE_TIMESTAMP);
3011  TestIsNull("date_sub(NULL, interval 10 years)", TYPE_TIMESTAMP);
3012  TestIsNull("date_sub(cast('2012-01-01 09:10:11.123456789' as timestamp),"
3013  "interval NULL years)", TYPE_TIMESTAMP);
3014  TestIsNull("date_sub(NULL, interval NULL years)", TYPE_TIMESTAMP);
3015 
3016  // Test add/sub behavior with very large time values.
3017  string max_int = lexical_cast<string>(numeric_limits<int32_t>::max());
3018  string max_long = lexical_cast<string>(numeric_limits<int32_t>::max());
3019  TestStringValue(
3020  "cast(years_add(cast('2000-01-01 00:00:00' "
3021  "as timestamp), " + max_int + ") as string)",
3022  "1999-01-01 00:00:00");
3023  TestStringValue(
3024  "cast(years_sub(cast('2000-01-01 00:00:00' "
3025  "as timestamp), " + max_long + ") as string)",
3026  "2001-01-01 00:00:00");
3027  TestStringValue(
3028  "cast(years_add(cast('2000-01-01 00:00:00' "
3029  "as timestamp), " + max_int + ") as string)",
3030  "1999-01-01 00:00:00");
3031  TestStringValue(
3032  "cast(years_sub(cast('2000-01-01 00:00:00' "
3033  "as timestamp), " + max_long + ") as string)",
3034  "2001-01-01 00:00:00");
3035 
3036  // Test Unix epoch conversions.
3037  TestTimestampUnixEpochConversions(0, "1970-01-01 00:00:00");
3038 
3039  // Regression tests for IMPALA-1579, Unix times should be BIGINTs instead of INTs.
3040  TestValue("unix_timestamp('2038-01-19 03:14:07')", TYPE_BIGINT, 2147483647);
3041  TestValue("unix_timestamp('2038-01-19 03:14:08')", TYPE_BIGINT, 2147483648);
3042  TestValue("unix_timestamp('2038/01/19 03:14:08', 'yyyy/MM/dd HH:mm:ss')", TYPE_BIGINT,
3043  2147483648);
3044  TestValue("unix_timestamp(cast('2038-01-19 03:14:08' as timestamp))", TYPE_BIGINT,
3045  2147483648);
3046 
3047 
3048  // Test Unix epoch conversions again but now converting into local timestamp values.
3049  {
3051  // Determine what the local time would have been when it was 1970-01-01 GMT
3052  ptime local_time_at_epoch = c_local_adjustor<ptime>::utc_to_local(from_time_t(0));
3053  // ... and as an Impala compatible string.
3054  string local_time_at_epoch_as_str = to_iso_extended_string(local_time_at_epoch.date())
3055  + " " + to_simple_string(local_time_at_epoch.time_of_day());
3056  // Determine what the Unix timestamp would have been when it was 1970-01-01 in the
3057  // local time zone.
3058  int64_t unix_time_at_local_epoch =
3059  (from_time_t(0) - local_time_at_epoch).total_seconds();
3060  TestTimestampUnixEpochConversions(unix_time_at_local_epoch,
3061  local_time_at_epoch_as_str);
3062 
3063  // Check that daylight savings calculation is done.
3064  {
3065  ScopedTimeZoneOverride time_zone("PST8PDT");
3066  TestValue("unix_timestamp('2015-01-01')", TYPE_BIGINT, 1420099200); // PST applies
3067  TestValue("unix_timestamp('2015-07-01')", TYPE_BIGINT, 1435734000); // PDT applies
3068  }
3069  {
3070  ScopedTimeZoneOverride time_zone("EST5EDT");
3071  TestValue("unix_timestamp('2015-01-01')", TYPE_BIGINT, 1420088400); // EST applies
3072  TestValue("unix_timestamp('2015-07-01')", TYPE_BIGINT, 1435723200); // EDT applies
3073  }
3074  }
3075 
3076  TestIsNull("from_unixtime(NULL, 'yyyy-MM-dd')", TYPE_STRING);
3077  TestStringValue("from_unixtime(unix_timestamp('1999-01-01 10:10:10'), \
3078  'yyyy-MM-dd')", "1999-01-01");
3079  TestStringValue("from_unixtime(unix_timestamp('1999-01-01 10:10:10'), \
3080  'yyyy-MM-dd HH:mm:ss')", "1999-01-01 10:10:10");
3081  TestStringValue("from_unixtime(unix_timestamp('1999-01-01 10:10:10') + (60*60*24), \
3082  'yyyy-MM-dd')", "1999-01-02");
3083  TestStringValue("from_unixtime(unix_timestamp('1999-01-01 10:10:10') + 10, \
3084  'yyyy-MM-dd HH:mm:ss')", "1999-01-01 10:10:20");
3085  TestValue("cast('2011-12-22 09:10:11.123456789' as timestamp) > \
3086  cast('2011-12-22 09:10:11.12345678' as timestamp)", TYPE_BOOLEAN, true);
3087  TestValue("cast('2011-12-22 08:10:11.123456789' as timestamp) > \
3088  cast('2011-12-22 09:10:11.12345678' as timestamp)", TYPE_BOOLEAN, false);
3089  TestValue("cast('2011-12-22 09:10:11.000000' as timestamp) = \
3090  cast('2011-12-22 09:10:11' as timestamp)", TYPE_BOOLEAN, true);
3091  TestValue("year(cast('2011-12-22 09:10:11.000000' as timestamp))", TYPE_INT, 2011);
3092  TestValue("month(cast('2011-12-22 09:10:11.000000' as timestamp))", TYPE_INT, 12);
3093  TestValue("dayofmonth(cast('2011-12-22 09:10:11.000000' as timestamp))", TYPE_INT, 22);
3094  TestValue("day(cast('2011-12-22 09:10:11.000000' as timestamp))", TYPE_INT, 22);
3095  TestValue("dayofyear(cast('2011-12-22 09:10:11.000000' as timestamp))", TYPE_INT, 356);
3096  TestValue("weekofyear(cast('2011-12-22 09:10:11.000000' as timestamp))", TYPE_INT, 51);
3097  TestValue("dayofweek(cast('2011-12-18 09:10:11.000000' as timestamp))", TYPE_INT, 1);
3098  TestValue("dayofweek(cast('2011-12-22 09:10:11.000000' as timestamp))", TYPE_INT, 5);
3099  TestValue("dayofweek(cast('2011-12-24 09:10:11.000000' as timestamp))", TYPE_INT, 7);
3100  TestValue("hour(cast('2011-12-22 09:10:11.000000' as timestamp))", TYPE_INT, 9);
3101  TestValue("minute(cast('2011-12-22 09:10:11.000000' as timestamp))", TYPE_INT, 10);
3102  TestValue("second(cast('2011-12-22 09:10:11.000000' as timestamp))", TYPE_INT, 11);
3103  TestValue("year(cast('2011-12-22' as timestamp))", TYPE_INT, 2011);
3104  TestValue("month(cast('2011-12-22' as timestamp))", TYPE_INT, 12);
3105  TestValue("dayofmonth(cast('2011-12-22' as timestamp))", TYPE_INT, 22);
3106  TestValue("day(cast('2011-12-22' as timestamp))", TYPE_INT, 22);
3107  TestValue("dayofyear(cast('2011-12-22' as timestamp))", TYPE_INT, 356);
3108  TestValue("weekofyear(cast('2011-12-22' as timestamp))", TYPE_INT, 51);
3109  TestValue("dayofweek(cast('2011-12-18' as timestamp))", TYPE_INT, 1);
3110  TestValue("dayofweek(cast('2011-12-22' as timestamp))", TYPE_INT, 5);
3111  TestValue("dayofweek(cast('2011-12-24' as timestamp))", TYPE_INT, 7);
3112  TestValue("hour(cast('09:10:11.000000' as timestamp))", TYPE_INT, 9);
3113  TestValue("minute(cast('09:10:11.000000' as timestamp))", TYPE_INT, 10);
3114  TestValue("second(cast('09:10:11.000000' as timestamp))", TYPE_INT, 11);
3115  TestStringValue(
3116  "to_date(cast('2011-12-22 09:10:11.12345678' as timestamp))", "2011-12-22");
3117 
3118  TestValue("datediff(cast('2011-12-22 09:10:11.12345678' as timestamp), \
3119  cast('2012-12-22' as timestamp))", TYPE_INT, -366);
3120  TestValue("datediff(cast('2012-12-22' as timestamp), \
3121  cast('2011-12-22 09:10:11.12345678' as timestamp))", TYPE_INT, 366);
3122 
3123  TestIsNull("cast('24:59:59' as timestamp)", TYPE_TIMESTAMP);
3124  TestIsNull("cast('10000-12-31' as timestamp)", TYPE_TIMESTAMP);
3125  TestIsNull("cast('10000-12-31 23:59:59' as timestamp)", TYPE_TIMESTAMP);
3126  TestIsNull("cast('2000-12-31 24:59:59' as timestamp)", TYPE_TIMESTAMP);
3127 
3128  TestIsNull("year(cast('09:10:11.000000' as timestamp))", TYPE_INT);
3129  TestIsNull("month(cast('09:10:11.000000' as timestamp))", TYPE_INT);
3130  TestIsNull("dayofmonth(cast('09:10:11.000000' as timestamp))", TYPE_INT);
3131  TestIsNull("day(cast('09:10:11.000000' as timestamp))", TYPE_INT);
3132  TestIsNull("dayofyear(cast('09:10:11.000000' as timestamp))", TYPE_INT);
3133  TestIsNull("dayofweek(cast('09:10:11.000000' as timestamp))", TYPE_INT);
3134  TestIsNull("weekofyear(cast('09:10:11.000000' as timestamp))", TYPE_INT);
3135  TestIsNull("datediff(cast('09:10:11.12345678' as timestamp), "
3136  "cast('2012-12-22' as timestamp))", TYPE_INT);
3137 
3138  TestIsNull("year(NULL)", TYPE_INT);
3139  TestIsNull("month(NULL)", TYPE_INT);
3140  TestIsNull("dayofmonth(NULL)", TYPE_INT);
3141  TestIsNull("day(NULL)", TYPE_INT);
3142  TestIsNull("dayofweek(NULL)", TYPE_INT);
3143  TestIsNull("dayofyear(NULL)", TYPE_INT);
3144  TestIsNull("weekofyear(NULL)", TYPE_INT);
3145  TestIsNull("datediff(NULL, cast('2011-12-22 09:10:11.12345678' as timestamp))",
3146  TYPE_INT);
3147  TestIsNull("datediff(cast('2012-12-22' as timestamp), NULL)", TYPE_INT);
3148  TestIsNull("datediff(NULL, NULL)", TYPE_INT);
3149 
3150  TestStringValue("dayname(cast('2011-12-18 09:10:11.000000' as timestamp))", "Sunday");
3151  TestStringValue("dayname(cast('2011-12-19 09:10:11.000000' as timestamp))", "Monday");
3152  TestStringValue("dayname(cast('2011-12-20 09:10:11.000000' as timestamp))", "Tuesday");
3153  TestStringValue("dayname(cast('2011-12-21 09:10:11.000000' as timestamp))",
3154  "Wednesday");
3155  TestStringValue("dayname(cast('2011-12-22 09:10:11.000000' as timestamp))",
3156  "Thursday");
3157  TestStringValue("dayname(cast('2011-12-23 09:10:11.000000' as timestamp))", "Friday");
3158  TestStringValue("dayname(cast('2011-12-24 09:10:11.000000' as timestamp))",
3159  "Saturday");
3160  TestStringValue("dayname(cast('2011-12-25 09:10:11.000000' as timestamp))", "Sunday");
3161  TestIsNull("dayname(NULL)", TYPE_STRING);
3162 
3163  // Tests from Hive
3164  // The hive documentation states that timestamps are timezoneless, but the tests
3165  // show that they treat them as being in the current timezone so these tests
3166  // use the utc conversion to correct for that and get the same answers as
3167  // are in the hive test output.
3168  TestValue("cast(to_utc_timestamp(cast('2011-01-01 01:01:01' as timestamp), 'PST') "
3169  "as boolean)", TYPE_BOOLEAN, true);
3170  TestValue("cast(to_utc_timestamp(cast('2011-01-01 01:01:01' as timestamp), 'PST') "
3171  "as tinyint)", TYPE_TINYINT, 77);
3172  TestValue("cast(to_utc_timestamp(cast('2011-01-01 01:01:01' as timestamp), 'PST') "
3173  "as smallint)", TYPE_SMALLINT, -4787);
3174  TestValue("cast(to_utc_timestamp(cast('2011-01-01 01:01:01' as timestamp), 'PST') "
3175  "as int)", TYPE_INT, 1293872461);
3176  TestValue("cast(to_utc_timestamp(cast('2011-01-01 01:01:01' as timestamp), 'PST') "
3177  "as bigint)", TYPE_BIGINT, 1293872461);
3178  // We have some rounding errors going backend to front, so do it as a string.
3179  TestStringValue("cast(cast (to_utc_timestamp(cast('2011-01-01 01:01:01' "
3180  "as timestamp), 'PST') as float) as string)", "1.29387251e+09");
3181  TestValue("cast(to_utc_timestamp(cast('2011-01-01 01:01:01' as timestamp), 'PST') "
3182  "as double)", TYPE_DOUBLE, 1.293872461E9);
3183  TestValue("cast(to_utc_timestamp(cast('2011-01-01 01:01:01.1' as timestamp), 'PST') "
3184  "as double)", TYPE_DOUBLE, 1.2938724611E9);
3185  TestValue("cast(to_utc_timestamp(cast('2011-01-01 01:01:01.0001' as timestamp), 'PST') "
3186  "as double)", TYPE_DOUBLE, 1.2938724610001E9);
3187  // We get some decimal-binary skew here
3188  TestStringValue("cast(from_utc_timestamp(cast(1.3041352164485E9 as timestamp), 'PST') "
3189  "as string)", "2011-04-29 20:46:56.448499917");
3190  // NULL arguments.
3191  TestIsNull("from_utc_timestamp(NULL, 'PST')", TYPE_TIMESTAMP);
3192  TestIsNull("from_utc_timestamp(cast('2011-01-01 01:01:01.1' as timestamp), NULL)",
3193  TYPE_TIMESTAMP);
3194  TestIsNull("from_utc_timestamp(NULL, NULL)", TYPE_TIMESTAMP);
3195 
3196  // Tests from Hive. When casting from timestamp to numeric, timestamps are considered
3197  // to be local values.
3198  {
3200  ScopedTimeZoneOverride time_zone("PST8PDT");
3201  TestValue("cast(cast('2011-01-01 01:01:01' as timestamp) as boolean)", TYPE_BOOLEAN,
3202  true);
3203  TestValue("cast(cast('2011-01-01 01:01:01' as timestamp) as tinyint)", TYPE_TINYINT,
3204  77);
3205  TestValue("cast(cast('2011-01-01 01:01:01' as timestamp) as smallint)", TYPE_SMALLINT,
3206  -4787);
3207  TestValue("cast(cast('2011-01-01 01:01:01' as timestamp) as int)", TYPE_INT,
3208  1293872461);
3209  TestValue("cast(cast('2011-01-01 01:01:01' as timestamp) as bigint)", TYPE_BIGINT,
3210  1293872461);
3211  // We have some rounding errors going backend to front, so do it as a string.
3212  TestStringValue("cast(cast(cast('2011-01-01 01:01:01' as timestamp) as float)"
3213  " as string)", "1.29387251e+09");
3214  TestValue("cast(cast('2011-01-01 01:01:01' as timestamp) as double)", TYPE_DOUBLE,
3215  1.293872461E9);
3216  TestValue("cast(cast('2011-01-01 01:01:01.1' as timestamp) as double)", TYPE_DOUBLE,
3217  1.2938724611E9);
3218  TestValue("cast(cast('2011-01-01 01:01:01.0001' as timestamp) as double)",
3219  TYPE_DOUBLE, 1.2938724610001E9);
3220  // We get some decimal-binary skew here
3221  TestStringValue("cast(cast(1.3041352164485E9 as timestamp) as string)",
3222  "2011-04-29 20:46:56.448499917");
3223  // NULL arguments.
3224  TestIsNull("from_utc_timestamp(NULL, 'PST')", TYPE_TIMESTAMP);
3225  TestIsNull("from_utc_timestamp(cast('2011-01-01 01:01:01.1' as timestamp), NULL)",
3226  TYPE_TIMESTAMP);
3227  TestIsNull("from_utc_timestamp(NULL, NULL)", TYPE_TIMESTAMP);
3228  }
3229 
3230  // Hive silently ignores bad timezones. We log a problem.
3231  TestStringValue(
3232  "cast(from_utc_timestamp("
3233  "cast('1970-01-01 00:00:00' as timestamp), 'FOOBAR') as string)",
3234  "1970-01-01 00:00:00");
3235 
3236  // With support of date strings this generates a date and 0 time.
3237  TestStringValue(
3238  "cast(cast('1999-01-10' as timestamp) as string)", "1999-01-10 00:00:00");
3239 
3240  // Test functions with unknown expected value.
3241  TestValidTimestampValue("now()");
3242  TestValidTimestampValue("current_timestamp()");
3243  TestValidTimestampValue("cast(unix_timestamp() as timestamp)");
3244 
3245  // Test that the epoch is reasonable. Allow a few seconds to compensate for execution
3246  // time.
3247  int tolerance_in_seconds = 5;
3248  time_t unix_time = (posix_time::microsec_clock::local_time() - from_time_t(0))
3249  .total_seconds();
3250  stringstream expr_sql;
3251  expr_sql << "unix_timestamp() between " << unix_time - tolerance_in_seconds
3252  << " and " << unix_time + tolerance_in_seconds;
3253  TestValue(expr_sql.str(), TYPE_BOOLEAN, true);
3254  {
3256  unix_time = time(NULL);
3257  expr_sql.str("");
3258  expr_sql << "unix_timestamp() between " << unix_time - tolerance_in_seconds
3259  << " and " << unix_time + tolerance_in_seconds;
3260  TestValue(expr_sql.str(), TYPE_BOOLEAN, true);
3261  }
3262  // Test that the other current time functions are also reasonable.
3263  ptime local_time = c_local_adjustor<ptime>::utc_to_local(from_time_t(unix_time));
3264  TestTimestampValue("now()", TimestampValue(local_time), tolerance_in_seconds);
3265  TestTimestampValue("current_timestamp()", TimestampValue(local_time),
3266  tolerance_in_seconds);
3267  TestTimestampValue("cast(unix_timestamp() as timestamp)", TimestampValue(local_time),
3268  tolerance_in_seconds);
3269 
3270  // Test alias
3271  TestValue("now() = current_timestamp()", TYPE_BOOLEAN, true);
3272 
3273  // Test custom formats
3274  TestValue("unix_timestamp('1970|01|01 00|00|00', 'yyyy|MM|dd HH|mm|ss')", TYPE_BIGINT,
3275  0);
3276  TestValue("unix_timestamp('01,Jan,1970,00,00,00', 'dd,MMM,yyyy,HH,mm,ss')", TYPE_BIGINT,
3277  0);
3278  // This time format is misleading because a trailing Z means UTC but a timestamp can
3279  // have no time zone association. unix_timestamp() expects inputs to be in local time.
3280  TestValue<int64_t>("unix_timestamp('1983-08-05T05:00:00.000Z', "
3281  "'yyyy-MM-ddTHH:mm:ss.SSSZ')", TYPE_BIGINT, 428907600);
3282 
3283  TestStringValue("from_unixtime(0, 'yyyy|MM|dd HH|mm|ss')", "1970|01|01 00|00|00");
3284  TestStringValue("from_unixtime(0, 'dd,MMM,yyyy,HH,mm,ss')", "01,Jan,1970,00,00,00");
3285 
3286  // Test invalid formats returns error
3287  TestError("unix_timestamp('1970-01-01 00:00:00', 'yyyy-MM-dd hh:mm:ss')");
3288  TestError("unix_timestamp('1970-01-01 10:10:10', NULL)");
3289  TestError("unix_timestamp(NULL, NULL)");
3290  TestError("from_unixtime(0, NULL)");
3291  TestError("from_unixtime(cast(0 as bigint), NULL)");
3292  TestError("from_unixtime(NULL, NULL)");
3293  TestError("unix_timestamp('1970-01-01 00:00:00', ' ')");
3294  TestError("unix_timestamp('1970-01-01 00:00:00', ' -===-')");
3295  TestError("unix_timestamp('1970-01-01', '\"foo\"')");
3296  TestError("from_unixtime(0, 'YY-MM-dd HH:mm:dd')");
3297  TestError("from_unixtime(0, 'yyyy-MM-dd hh::dd')");
3298  TestError("from_unixtime(cast(0 as bigint), 'YY-MM-dd HH:mm:dd')");
3299  TestError("from_unixtime(cast(0 as bigint), 'yyyy-MM-dd hh::dd')");
3300  TestError("from_unixtime(0, '')");
3301  TestError("from_unixtime(0, NULL)");
3302  TestError("from_unixtime(0, ' ')");
3303  TestError("from_unixtime(0, ' -=++=- ')");
3304 
3305  // Valid format string, but invalid Timestamp, should return null;
3306  TestIsNull("unix_timestamp('1970-01-01', 'yyyy-MM-dd HH:mm:ss')", TYPE_BIGINT);
3307  TestIsNull("unix_timestamp('1970', 'yyyy-MM-dd')", TYPE_BIGINT);
3308  TestIsNull("unix_timestamp('', 'yyyy-MM-dd')", TYPE_BIGINT);
3309  TestIsNull("unix_timestamp('|1|1 00|00|00', 'yyyy|M|d HH|MM|ss')", TYPE_BIGINT);
3310 
3311  TestIsNull("unix_timestamp('1970-01', 'yyyy-MM-dd')", TYPE_BIGINT);
3312  TestIsNull("unix_timestamp('1970-20-01', 'yyyy-MM-dd')", TYPE_BIGINT);
3313 
3314 
3315  // regression test for IMPALA-1105
3316  TestIsNull("cast(trunc('2014-07-22 01:34:55 +0100', 'year') as STRING)", TYPE_STRING);
3317  TestStringValue("cast(trunc(cast('2014-04-01' as timestamp), 'SYYYY') as string)",
3318  "2014-01-01 00:00:00");
3319  TestIsNull("cast(trunc('01:34:55', 'year') as STRING)", TYPE_STRING);
3320  TestStringValue("cast(trunc(cast('07:02:03' as timestamp), 'MI') as string)",
3321  "07:02:00");
3322  // note: no time value in string defaults to 00:00:00
3323  TestStringValue("cast(trunc(cast('2014-01-01' as timestamp), 'MI') as string)",
3324  "2014-01-01 00:00:00");
3325 
3326  TestStringValue(
3327  "cast(trunc(cast('2014-04-01 01:01:01' as timestamp), 'SYYYY') as string)",
3328  "2014-01-01 00:00:00");
3329  TestStringValue(
3330  "cast(trunc(cast('2014-04-01 01:01:01' as timestamp), 'YYYY') as string)",
3331  "2014-01-01 00:00:00");
3332  TestStringValue(
3333  "cast(trunc(cast('2014-04-01 01:01:01' as timestamp), 'YEAR') as string)",
3334  "2014-01-01 00:00:00");
3335  TestStringValue(
3336  "cast(trunc(cast('2014-04-01 01:01:01' as timestamp), 'SYEAR') as string)",
3337  "2014-01-01 00:00:00");
3338  TestStringValue(
3339  "cast(trunc(cast('2014-04-01 01:01:01' as timestamp), 'YYY') as string)",
3340  "2014-01-01 00:00:00");
3341  TestStringValue(
3342  "cast(trunc(cast('2014-04-01 01:01:01' as timestamp), 'YY') as string)",
3343  "2014-01-01 00:00:00");
3344  TestStringValue(
3345  "cast(trunc(cast('2014-04-01 01:01:01' as timestamp), 'Y') as string)",
3346  "2014-01-01 00:00:00");
3347  TestStringValue(
3348  "cast(trunc(cast('2000-01-01 00:00:00' as timestamp), 'Y') as string)",
3349  "2000-01-01 00:00:00");
3350  TestStringValue(
3351  "cast(trunc(cast('2000-01-01 00:00:00' as timestamp), 'Q') as string)",
3352  "2000-01-01 00:00:00");
3353  TestStringValue(
3354  "cast(trunc(cast('2000-01-01 01:00:00' as timestamp), 'Q') as string)",
3355  "2000-01-01 00:00:00");
3356  TestStringValue(
3357  "cast(trunc(cast('2000-02-01 01:00:00' as timestamp), 'Q') as string)",
3358  "2000-01-01 00:00:00");
3359  TestStringValue(
3360  "cast(trunc(cast('2000-03-01 01:00:00' as timestamp), 'Q') as string)",
3361  "2000-01-01 00:00:00");
3362  TestStringValue(
3363  "cast(trunc(cast('2000-04-01 01:00:00' as timestamp), 'Q') as string)",
3364  "2000-04-01 00:00:00");
3365  TestStringValue(
3366  "cast(trunc(cast('2000-05-01 01:00:00' as timestamp), 'Q') as string)",
3367  "2000-04-01 00:00:00");
3368  TestStringValue(
3369  "cast(trunc(cast('2000-06-01 01:00:00' as timestamp), 'Q') as string)",
3370  "2000-04-01 00:00:00");
3371  TestStringValue(
3372  "cast(trunc(cast('2000-07-01 01:00:00' as timestamp), 'Q') as string)",
3373  "2000-07-01 00:00:00");
3374  TestStringValue(
3375  "cast(trunc(cast('2000-08-01 01:00:00' as timestamp), 'Q') as string)",
3376  "2000-07-01 00:00:00");
3377  TestStringValue(
3378  "cast(trunc(cast('2000-09-01 01:00:00' as timestamp), 'Q') as string)",
3379  "2000-07-01 00:00:00");
3380  TestStringValue(
3381  "cast(trunc(cast('2000-10-01 01:00:00' as timestamp), 'Q') as string)",
3382  "2000-10-01 00:00:00");
3383  TestStringValue(
3384  "cast(trunc(cast('2000-11-01 01:00:00' as timestamp), 'Q') as string)",
3385  "2000-10-01 00:00:00");
3386  TestStringValue(
3387  "cast(trunc(cast('2000-12-01 01:00:00' as timestamp), 'Q') as string)",
3388  "2000-10-01 00:00:00");
3389  TestStringValue(
3390  "cast(trunc(cast('2001-02-05 01:01:01' as timestamp), 'MONTH') as string)",
3391  "2001-02-01 00:00:00");
3392  TestStringValue(
3393  "cast(trunc(cast('2001-02-05 01:01:01' as timestamp), 'MON') as string)",
3394  "2001-02-01 00:00:00");
3395  TestStringValue(
3396  "cast(trunc(cast('2001-02-05 01:01:01' as timestamp), 'MM') as string)",
3397  "2001-02-01 00:00:00");
3398  TestStringValue(
3399  "cast(trunc(cast('2001-02-05 01:01:01' as timestamp), 'RM') as string)",
3400  "2001-02-01 00:00:00");
3401  TestStringValue(
3402  "cast(trunc(cast('2001-01-01 00:00:00' as timestamp), 'MM') as string)",
3403  "2001-01-01 00:00:00");
3404  TestStringValue(
3405  "cast(trunc(cast('2001-12-29 00:00:00' as timestamp), 'MM') as string)",
3406  "2001-12-01 00:00:00");
3407  TestStringValue(
3408  "cast(trunc(cast('2014-01-08 01:02:03' as timestamp), 'WW') as string)",
3409  "2014-01-08 00:00:00");
3410  TestStringValue(
3411  "cast(trunc(cast('2014-01-07 00:00:00' as timestamp), 'WW') as string)",
3412  "2014-01-01 00:00:00");
3413  TestStringValue(
3414  "cast(trunc(cast('2014-01-08 00:00:00' as timestamp), 'WW') as string)",
3415  "2014-01-08 00:00:00");
3416  TestStringValue(
3417  "cast(trunc(cast('2014-01-09 00:00:00' as timestamp), 'WW') as string)",
3418  "2014-01-08 00:00:00");
3419  TestStringValue(
3420  "cast(trunc(cast('2014-01-14 00:00:00' as timestamp), 'WW') as string)",
3421  "2014-01-08 00:00:00");
3422  TestStringValue(
3423  "cast(trunc(cast('2014-01-08 01:02:03' as timestamp), 'W') as string)",
3424  "2014-01-08 00:00:00");
3425  TestStringValue(
3426  "cast(trunc(cast('2014-01-07 00:00:00' as timestamp), 'W') as string)",
3427  "2014-01-01 00:00:00");
3428  TestStringValue(
3429  "cast(trunc(cast('2014-01-08 00:00:00' as timestamp), 'W') as string)",
3430  "2014-01-08 00:00:00");
3431  TestStringValue(
3432  "cast(trunc(cast('2014-01-09 00:00:00' as timestamp), 'W') as string)",
3433  "2014-01-08 00:00:00");
3434  TestStringValue(
3435  "cast(trunc(cast('2014-01-14 00:00:00' as timestamp), 'W') as string)",
3436  "2014-01-08 00:00:00");
3437  TestStringValue(
3438  "cast(trunc(cast('2014-02-01 01:02:03' as timestamp), 'W') as string)",
3439  "2014-02-01 00:00:00");
3440  TestStringValue(
3441  "cast(trunc(cast('2014-02-02 00:00:00' as timestamp), 'W') as string)",
3442  "2014-02-01 00:00:00");
3443  TestStringValue(
3444  "cast(trunc(cast('2014-02-03 00:00:00' as timestamp), 'W') as string)",
3445  "2014-02-01 00:00:00");
3446  TestStringValue(
3447  "cast(trunc(cast('2014-02-07 00:00:00' as timestamp), 'W') as string)",
3448  "2014-02-01 00:00:00");
3449  TestStringValue(
3450  "cast(trunc(cast('2014-02-08 00:00:00' as timestamp), 'W') as string)",
3451  "2014-02-08 00:00:00");
3452  TestStringValue(
3453  "cast(trunc(cast('2014-02-24 00:00:00' as timestamp), 'W') as string)",
3454  "2014-02-22 00:00:00");
3455  TestStringValue(
3456  "cast(trunc(cast('2014-01-08 01:02:03' as timestamp), 'DDD') as string)",
3457  "2014-01-08 00:00:00");
3458  TestStringValue(
3459  "cast(trunc(cast('2014-01-08 01:02:03' as timestamp), 'DD') as string)",
3460  "2014-01-08 00:00:00");
3461  TestStringValue(
3462  "cast(trunc(cast('2014-02-08 01:02:03' as timestamp), 'J') as string)",
3463  "2014-02-08 00:00:00");
3464  TestStringValue(
3465  "cast(trunc(cast('2014-02-08 00:00:00' as timestamp), 'J') as string)",
3466  "2014-02-08 00:00:00");
3467  TestStringValue(
3468  "cast(trunc(cast('2014-02-19 00:00:00' as timestamp), 'J') as string)",
3469  "2014-02-19 00:00:00");
3470  TestStringValue(
3471  "cast(trunc(cast('2012-09-10 01:02:03' as timestamp), 'DAY') as string)",
3472  "2012-09-10 00:00:00");
3473  TestStringValue(
3474  "cast(trunc(cast('2012-09-10 01:02:03' as timestamp), 'DY') as string)",
3475  "2012-09-10 00:00:00");
3476  TestStringValue(
3477  "cast(trunc(cast('2012-09-10 01:02:03' as timestamp), 'D') as string)",
3478  "2012-09-10 00:00:00");
3479  TestStringValue(
3480  "cast(trunc(cast('2012-09-11 01:02:03' as timestamp), 'D') as string)",
3481  "2012-09-10 00:00:00");
3482  TestStringValue(
3483  "cast(trunc(cast('2012-09-12 01:02:03' as timestamp), 'D') as string)",
3484  "2012-09-10 00:00:00");
3485  TestStringValue(
3486  "cast(trunc(cast('2012-09-16 01:02:03' as timestamp), 'D') as string)",
3487  "2012-09-10 00:00:00");
3488  TestStringValue(
3489  "cast(trunc(cast('2012-09-10 07:02:03' as timestamp), 'HH') as string)",
3490  "2012-09-10 07:00:00");
3491  TestStringValue(
3492  "cast(trunc(cast('2012-09-10 07:02:03' as timestamp), 'HH12') as string)",
3493  "2012-09-10 07:00:00");
3494  TestStringValue(
3495  "cast(trunc(cast('2012-09-10 07:02:03' as timestamp), 'HH24') as string)",
3496  "2012-09-10 07:00:00");
3497  TestStringValue(
3498  "cast(trunc(cast('2012-09-10 00:02:03' as timestamp), 'HH') as string)",
3499  "2012-09-10 00:00:00");
3500  TestStringValue(
3501  "cast(trunc(cast('2012-09-10 23:02:03' as timestamp), 'HH') as string)",
3502  "2012-09-10 23:00:00");
3503  TestStringValue(
3504  "cast(trunc(cast('2012-09-10 23:59:59' as timestamp), 'HH') as string)",
3505  "2012-09-10 23:00:00");
3506  TestStringValue(
3507  "cast(trunc(cast('2012-09-10 07:02:03' as timestamp), 'MI') as string)",
3508  "2012-09-10 07:02:00");
3509  TestStringValue(
3510  "cast(trunc(cast('2012-09-10 07:00:03' as timestamp), 'MI') as string)",
3511  "2012-09-10 07:00:00");
3512  TestStringValue(
3513  "cast(trunc(cast('2012-09-10 07:00:00' as timestamp), 'MI') as string)",
3514  "2012-09-10 07:00:00");
3515  TestStringValue(
3516  "cast(trunc(cast('2012-09-10 07:59:03' as timestamp), 'MI') as string)",
3517  "2012-09-10 07:59:00");
3518  TestStringValue(
3519  "cast(trunc(cast('2012-09-10 07:59:59' as timestamp), 'MI') as string)",
3520  "2012-09-10 07:59:00");
3521  TestNonOkStatus(
3522  "cast(trunc(cast('2012-09-10 07:59:59' as timestamp), 'MIN') as string)");
3523  TestNonOkStatus(
3524  "cast(trunc(cast('2012-09-10 07:59:59' as timestamp), 'XXYYZZ') as string)");
3525 
3526  // Extract as a regular function
3527  TestValue("extract(cast('2006-05-12 18:27:28.12345' as timestamp), 'YEAR')",
3528  TYPE_INT, 2006);
3529  TestValue("extract('2006-05-12 18:27:28.12345', 'YEAR')", TYPE_INT, 2006);
3530  TestValue("extract(cast('2006-05-12 18:27:28.12345' as timestamp), 'MoNTH')",
3531  TYPE_INT, 5);
3532  TestValue("extract(cast('2006-05-12 18:27:28.12345' as timestamp), 'DaY')",
3533  TYPE_INT, 12);
3534  TestValue("extract(cast('2006-05-12 06:27:28.12345' as timestamp), 'hour')",
3535  TYPE_INT, 6);
3536  TestValue("extract(cast('2006-05-12 18:27:28.12345' as timestamp), 'MINUTE')",
3537  TYPE_INT, 27);
3538  TestValue("extract(cast('2006-05-12 18:27:28.12345' as timestamp), 'SECOND')",
3539  TYPE_INT, 28);
3540  TestValue("extract(cast('2006-05-12 18:27:28.12345' as timestamp), 'MILLISECOND')",
3541  TYPE_INT, 123);
3542  TestValue("extract(cast('2006-05-13 01:27:28.12345' as timestamp), 'EPOCH')",
3543  TYPE_INT, 1147483648);
3544  TestNonOkStatus("extract(cast('2006-05-13 01:27:28.12345' as timestamp), 'foo')");
3545  TestNonOkStatus("extract(cast('2006-05-13 01:27:28.12345' as timestamp), NULL)");
3546  TestIsNull("extract(NULL, 'EPOCH')", TYPE_INT);
3547  TestNonOkStatus("extract(NULL, NULL)");
3548 
3549  // Extract using FROM keyword
3550  TestValue("extract(YEAR from cast('2006-05-12 18:27:28.12345' as timestamp))",
3551  TYPE_INT, 2006);
3552  TestValue("extract(MoNTH from cast('2006-05-12 18:27:28.12345' as timestamp))",
3553  TYPE_INT, 5);
3554  TestValue("extract(DaY from cast('2006-05-12 18:27:28.12345' as timestamp))",
3555  TYPE_INT, 12);
3556  TestValue("extract(hour from cast('2006-05-12 06:27:28.12345' as timestamp))",
3557  TYPE_INT, 6);
3558  TestValue("extract(MINUTE from cast('2006-05-12 18:27:28.12345' as timestamp))",
3559  TYPE_INT, 27);
3560  TestValue("extract(SECOND from cast('2006-05-12 18:27:28.12345' as timestamp))",
3561  TYPE_INT, 28);
3562  TestValue("extract(MILLISECOND from cast('2006-05-12 18:27:28.12345' as timestamp))",
3563  TYPE_INT, 123);
3564  TestValue("extract(EPOCH from cast('2006-05-13 01:27:28.12345' as timestamp))",
3565  TYPE_INT, 1147483648);
3566  TestNonOkStatus("extract(foo from cast('2006-05-13 01:27:28.12345' as timestamp))");
3567  TestNonOkStatus("extract(NULL from cast('2006-05-13 01:27:28.12345' as timestamp))");
3568  TestIsNull("extract(EPOCH from NULL)", TYPE_INT);
3569 
3570  // Date_part, same as extract function but with arguments swapped
3571  TestValue("date_part('YEAR', cast('2006-05-12 18:27:28.12345' as timestamp))",
3572  TYPE_INT, 2006);
3573  TestValue("date_part('MoNTH', cast('2006-05-12 18:27:28.12345' as timestamp))",
3574  TYPE_INT, 5);
3575  TestValue("date_part('DaY', cast('2006-05-12 18:27:28.12345' as timestamp))",
3576  TYPE_INT, 12);
3577  TestValue("date_part('hour', cast('2006-05-12 06:27:28.12345' as timestamp))",
3578  TYPE_INT, 6);
3579  TestValue("date_part('MINUTE', cast('2006-05-12 18:27:28.12345' as timestamp))",
3580  TYPE_INT, 27);
3581  TestValue("date_part('SECOND', cast('2006-05-12 18:27:28.12345' as timestamp))",
3582  TYPE_INT, 28);
3583  TestValue("date_part('MILLISECOND', cast('2006-05-12 18:27:28.12345' as timestamp))",
3584  TYPE_INT, 123);
3585  TestValue("date_part('EPOCH', cast('2006-05-13 01:27:28.12345' as timestamp))",
3586  TYPE_INT, 1147483648);
3587  TestNonOkStatus("date_part('foo', cast('2006-05-13 01:27:28.12345' as timestamp))");
3588  TestNonOkStatus("date_part(NULL, cast('2006-05-13 01:27:28.12345' as timestamp))");
3589  TestIsNull("date_part('EPOCH', NULL)", TYPE_INT);
3590  TestNonOkStatus("date_part(NULL, NULL)");
3591 }
3592 
3594  // If first param evaluates to true, should return second parameter,
3595  // false or NULL should return the third.
3596  TestValue("if(TRUE, FALSE, TRUE)", TYPE_BOOLEAN, false);
3597  TestValue("if(FALSE, FALSE, TRUE)", TYPE_BOOLEAN, true);
3598  TestValue("if(TRUE, 10, 20)", TYPE_TINYINT, 10);
3599  TestValue("if(FALSE, 10, 20)", TYPE_TINYINT, 20);
3600  TestValue("if(TRUE, cast(5.5 as double), cast(8.8 as double))", TYPE_DOUBLE, 5.5);
3601  TestValue("if(FALSE, cast(5.5 as double), cast(8.8 as double))", TYPE_DOUBLE, 8.8);
3602  TestStringValue("if(TRUE, 'abc', 'defgh')", "abc");
3603  TestStringValue("if(FALSE, 'abc', 'defgh')", "defgh");
3604  TimestampValue then_val(1293872461);
3605  TimestampValue else_val(929387245);
3606  TestTimestampValue("if(TRUE, cast('2011-01-01 09:01:01' as timestamp), "
3607  "cast('1999-06-14 19:07:25' as timestamp))", then_val);
3608  TestTimestampValue("if(FALSE, cast('2011-01-01 09:01:01' as timestamp), "
3609  "cast('1999-06-14 19:07:25' as timestamp))", else_val);
3610 
3611  // Test nullif(). Return NULL if lhs equals rhs, lhs otherwise.
3612  TestIsNull("nullif(NULL, NULL)", TYPE_BOOLEAN);
3613  TestIsNull("nullif(TRUE, TRUE)", TYPE_BOOLEAN);
3614  TestValue("nullif(TRUE, FALSE)", TYPE_BOOLEAN, true);
3615  TestIsNull("nullif(NULL, TRUE)", TYPE_BOOLEAN);
3616  TestValue("nullif(TRUE, NULL)", TYPE_BOOLEAN, true);
3617  TestIsNull("nullif(NULL, 10)", TYPE_TINYINT);
3618  TestValue("nullif(10, NULL)", TYPE_TINYINT, 10);
3619  TestIsNull("nullif(10, 10)", TYPE_TINYINT);
3620  TestValue("nullif(10, 20)", TYPE_TINYINT, 10);
3621  TestIsNull("nullif(cast(10.10 as double), cast(10.10 as double))", TYPE_DOUBLE);
3622  TestValue("nullif(cast(10.10 as double), cast(20.20 as double))", TYPE_DOUBLE, 10.10);
3623  TestIsNull("nullif(cast(NULL as double), 10.10)", TYPE_DOUBLE);
3624  TestValue("nullif(cast(10.10 as double), NULL)", TYPE_DOUBLE, 10.10);
3625  TestIsNull("nullif('abc', 'abc')", TYPE_STRING);
3626  TestStringValue("nullif('abc', 'def')", "abc");
3627  TestIsNull("nullif(NULL, 'abc')", TYPE_STRING);
3628  TestStringValue("nullif('abc', NULL)", "abc");
3629  TestIsNull("nullif(cast('2011-01-01 09:01:01' as timestamp), "
3630  "cast('2011-01-01 09:01:01' as timestamp))", TYPE_TIMESTAMP);
3631  TimestampValue testlhs(1293872461);
3632  TestTimestampValue("nullif(cast('2011-01-01 09:01:01' as timestamp), "
3633  "cast('1999-06-14 19:07:25' as timestamp))", testlhs);
3634  TestIsNull("nullif(NULL, "
3635  "cast('2011-01-01 09:01:01' as timestamp))", TYPE_TIMESTAMP);
3636  TestTimestampValue("nullif(cast('2011-01-01 09:01:01' as timestamp), "
3637  "NULL)", testlhs);
3638 
3639  // Test IsNull() function and its aliases on all applicable types and NULL.
3640  string isnull_aliases[] = {"IsNull", "IfNull", "Nvl"};
3641  for (int i = 0; i < 3; ++i) {
3642  string& f = isnull_aliases[i];
3643  TestValue(f + "(true, NULL)", TYPE_BOOLEAN, true);
3644  TestValue(f + "(1, NULL)", TYPE_TINYINT, 1);
3645  TestValue(f + "(cast(1 as smallint), NULL)", TYPE_SMALLINT, 1);
3646  TestValue(f + "(cast(1 as int), NULL)", TYPE_INT, 1);
3647  TestValue(f + "(cast(1 as bigint), NULL)", TYPE_BIGINT, 1);
3648  TestValue(f + "(cast(10.0 as float), NULL)", TYPE_FLOAT, 10.0f);
3649  TestValue(f + "(cast(10.0 as double), NULL)", TYPE_DOUBLE, 10.0);
3650  TestStringValue(f + "('abc', NULL)", "abc");
3651  TestTimestampValue(f + "(" + default_timestamp_str_ + ", NULL)",
3652  default_timestamp_val_);
3653  // Test first argument is NULL.
3654  TestValue(f + "(NULL, true)", TYPE_BOOLEAN, true);
3655  TestValue(f + "(NULL, 1)", TYPE_TINYINT, 1);
3656  TestValue(f + "(NULL, cast(1 as smallint))", TYPE_SMALLINT, 1);
3657  TestValue(f + "(NULL, cast(1 as int))", TYPE_INT, 1);
3658  TestValue(f + "(NULL, cast(1 as bigint))", TYPE_BIGINT, 1);
3659  TestValue(f + "(NULL, cast(10.0 as float))", TYPE_FLOAT, 10.0f);
3660  TestValue(f + "(NULL, cast(10.0 as double))", TYPE_DOUBLE, 10.0);
3661  TestStringValue(f + "(NULL, 'abc')", "abc");
3662  TestTimestampValue(f + "(NULL, " + default_timestamp_str_ + ")",
3663  default_timestamp_val_);
3664  // Test NULL. The return type is boolean to avoid a special NULL function signature.
3665  TestIsNull(f + "(NULL, NULL)", TYPE_BOOLEAN);
3666  }
3667 
3668  TestIsNull("coalesce(NULL)", TYPE_BOOLEAN);
3669  TestIsNull("coalesce(NULL, NULL)", TYPE_BOOLEAN);
3670  TestValue("coalesce(TRUE)", TYPE_BOOLEAN, true);
3671  TestValue("coalesce(NULL, TRUE, NULL)", TYPE_BOOLEAN, true);
3672  TestValue("coalesce(FALSE, NULL, TRUE, NULL)", TYPE_BOOLEAN, false);
3673  TestValue("coalesce(NULL, NULL, NULL, TRUE, NULL, NULL)", TYPE_BOOLEAN, true);
3674  TestValue("coalesce(10)", TYPE_TINYINT, 10);
3675  TestValue("coalesce(NULL, 10)", TYPE_TINYINT, 10);
3676  TestValue("coalesce(10, NULL)", TYPE_TINYINT, 10);
3677  TestValue("coalesce(NULL, 10, NULL)", TYPE_TINYINT, 10);
3678  TestValue("coalesce(20, NULL, 10, NULL)", TYPE_TINYINT, 20);
3679  TestValue("coalesce(20, NULL, cast(10 as smallint), NULL)", TYPE_SMALLINT, 20);
3680  TestValue("coalesce(20, NULL, cast(10 as int), NULL)", TYPE_INT, 20);
3681  TestValue("coalesce(20, NULL, cast(10 as bigint), NULL)", TYPE_BIGINT, 20);
3682  TestValue("coalesce(cast(5.5 as float))", TYPE_FLOAT, 5.5f);
3683  TestValue("coalesce(NULL, cast(5.5 as float))", TYPE_FLOAT, 5.5f);
3684  TestValue("coalesce(cast(5.5 as float), NULL)", TYPE_FLOAT, 5.5f);
3685  TestValue("coalesce(NULL, cast(5.5 as float), NULL)", TYPE_FLOAT, 5.5f);
3686  TestValue("coalesce(cast(9.8 as float), NULL, cast(5.5 as float), NULL)",
3687  TYPE_FLOAT, 9.8f);
3688  TestValue("coalesce(cast(9.8 as double), NULL, cast(5.5 as double), NULL)",
3689  TYPE_DOUBLE, 9.8);
3690  TestStringValue("coalesce('abc')", "abc");
3691  TestStringValue("coalesce(NULL, 'abc', NULL)", "abc");
3692  TestStringValue("coalesce('defgh', NULL, 'abc', NULL)", "defgh");
3693  TestStringValue("coalesce(NULL, NULL, NULL, 'abc', NULL, NULL)", "abc");
3694  TimestampValue ats(1293872461);
3695  TimestampValue bts(929387245);
3696  TestTimestampValue("coalesce(cast('2011-01-01 09:01:01' as timestamp))", ats);
3697  TestTimestampValue("coalesce(NULL, cast('2011-01-01 09:01:01' as timestamp),"
3698  "NULL)", ats);
3699  TestTimestampValue("coalesce(cast('1999-06-14 19:07:25' as timestamp), NULL,"
3700  "cast('2011-01-01 09:01:01' as timestamp), NULL)", bts);
3701  TestTimestampValue("coalesce(NULL, NULL, NULL,"
3702  "cast('2011-01-01 09:01:01' as timestamp), NULL, NULL)", ats);
3703 
3704  // Test logic of case expr using int types.
3705  // The different types and casting are tested below.
3706  TestValue("case when true then 1 end", TYPE_TINYINT, 1);
3707  TestValue("case when false then 1 when true then 2 end", TYPE_TINYINT, 2);
3708  TestValue("case when false then 1 when false then 2 when true then 3 end",
3709  TYPE_TINYINT, 3);
3710  // Test else expr.
3711  TestValue("case when false then 1 else 10 end", TYPE_TINYINT, 10);
3712  TestValue("case when false then 1 when false then 2 else 10 end", TYPE_TINYINT, 10);
3713  TestValue("case when false then 1 when false then 2 when false then 3 else 10 end",
3714  TYPE_TINYINT, 10);
3715  TestIsNull("case when false then 1 end", TYPE_TINYINT);
3716  // Test with case expr.
3717  TestValue("case 21 when 21 then 1 end", TYPE_TINYINT, 1);
3718  TestValue("case 21 when 20 then 1 when 21 then 2 end", TYPE_TINYINT, 2);
3719  TestValue("case 21 when 20 then 1 when 19 then 2 when 21 then 3 end", TYPE_TINYINT, 3);
3720  // Should skip when-exprs that are NULL
3721  TestIsNull("case when NULL then 1 end", TYPE_TINYINT);
3722  TestIsNull("case when NULL then 1 else NULL end", TYPE_TINYINT);
3723  TestValue("case when NULL then 1 else 2 end", TYPE_TINYINT, 2);
3724  TestValue("case when NULL then 1 when true then 2 else 3 end", TYPE_TINYINT, 2);
3725  // Should return else expr, if case-expr is NULL.
3726  TestIsNull("case NULL when 1 then 1 end", TYPE_TINYINT);
3727  TestIsNull("case NULL when 1 then 1 else NULL end", TYPE_TINYINT);
3728  TestValue("case NULL when 1 then 1 else 2 end", TYPE_TINYINT, 2);
3729  TestValue("case 10 when NULL then 1 else 2 end", TYPE_TINYINT, 2);
3730  TestValue("case 10 when NULL then 1 when 10 then 2 else 3 end", TYPE_TINYINT, 2);
3731  TestValue("case 'abc' when NULL then 1 when NULL then 2 else 3 end", TYPE_TINYINT, 3);
3732  // Not statically known that it will return NULL.
3733  TestIsNull("case NULL when NULL then true end", TYPE_BOOLEAN);
3734  TestIsNull("case NULL when NULL then true else NULL end", TYPE_BOOLEAN);
3735  // Statically known that it will return NULL.
3736  TestIsNull("case NULL when NULL then NULL end", TYPE_BOOLEAN);
3737  TestIsNull("case NULL when NULL then NULL else NULL end", TYPE_BOOLEAN);
3738 
3739  // Test all types in case/when exprs, without casts.
3740  unordered_map<int, string>::iterator def_iter;
3741  for(def_iter = default_type_strs_.begin(); def_iter != default_type_strs_.end();
3742  ++def_iter) {
3743  TestValue("case " + def_iter->second + " when " + def_iter->second +
3744  " then true else true end", TYPE_BOOLEAN, true);
3745  }
3746 
3747  // Test all int types in then and else exprs.
3748  // Also tests implicit casting in all exprs.
3749  IntValMap::iterator int_iter;
3750  for (int_iter = min_int_values_.begin(); int_iter != min_int_values_.end();
3751  ++int_iter) {
3752  PrimitiveType t = static_cast<PrimitiveType>(int_iter->first);
3753  string& s = default_type_strs_[t];
3754  TestValue("case when true then " + s + " end", t, int_iter->second);
3755  TestValue("case when false then 1 else " + s + " end", t, int_iter->second);
3756  TestValue("case when true then 1 else " + s + " end", t, 1);
3757  TestValue("case 0 when " + s + " then true else false end", TYPE_BOOLEAN, false);
3758  }
3759 
3760  // Test for zeroifnull
3761  // zeroifnull(NULL) returns 0, zeroifnull(non-null) returns the argument
3762  TestValue("zeroifnull(NULL)", TYPE_TINYINT, 0);
3763  TestValue("zeroifnull(cast (NULL as TINYINT))", TYPE_TINYINT, 0);
3764  TestValue("zeroifnull(cast (5 as TINYINT))", TYPE_TINYINT, 5);
3765  TestValue("zeroifnull(cast (NULL as SMALLINT))", TYPE_SMALLINT, 0);
3766  TestValue("zeroifnull(cast (5 as SMALLINT))", TYPE_SMALLINT, 5);
3767  TestValue("zeroifnull(cast (NULL as INT))", TYPE_INT, 0);
3768  TestValue("zeroifnull(cast (5 as INT))", TYPE_INT, 5);
3769  TestValue("zeroifnull(cast (NULL as BIGINT))", TYPE_BIGINT, 0);
3770  TestValue("zeroifnull(cast (5 as BIGINT))", TYPE_BIGINT, 5);
3771  TestValue<float>("zeroifnull(cast (NULL as FLOAT))", TYPE_FLOAT, 0.0f);
3772  TestValue<float>("zeroifnull(cast (5 as FLOAT))", TYPE_FLOAT, 5.0f);
3773  TestValue<double>("zeroifnull(cast (NULL as DOUBLE))", TYPE_DOUBLE, 0.0);
3774  TestValue<double>("zeroifnull(cast (5 as DOUBLE))", TYPE_DOUBLE, 5.0);
3775 
3776  // Test for NullIfZero
3777  // Test that 0 converts to NULL and NULL remains NULL
3778  TestIsNull("nullifzero(cast (0 as TINYINT))", TYPE_TINYINT);
3779  TestIsNull("nullifzero(cast (NULL as TINYINT))", TYPE_TINYINT);
3780  TestIsNull("nullifzero(cast (0 as SMALLINT))", TYPE_SMALLINT);
3781  TestIsNull("nullifzero(cast (NULL as SMALLINT))", TYPE_SMALLINT);
3782  TestIsNull("nullifzero(cast (0 as INT))", TYPE_INT);
3783  TestIsNull("nullifzero(cast (NULL as INT))", TYPE_INT);
3784  TestIsNull("nullifzero(cast (0 as BIGINT))", TYPE_BIGINT);
3785  TestIsNull("nullifzero(cast (NULL as BIGINT))", TYPE_BIGINT);
3786  TestIsNull("nullifzero(cast (0 as FLOAT))", TYPE_FLOAT);
3787  TestIsNull("nullifzero(cast (NULL as FLOAT))", TYPE_FLOAT);
3788  TestIsNull("nullifzero(cast (0 as DOUBLE))", TYPE_DOUBLE);
3789  TestIsNull("nullifzero(cast (NULL as DOUBLE))", TYPE_DOUBLE);
3790 
3791  // test that non-zero args are returned unchanged.
3792  TestValue("nullifzero(cast (5 as TINYINT))", TYPE_TINYINT, 5);
3793  TestValue("nullifzero(cast (5 as SMALLINT))", TYPE_SMALLINT, 5);
3794  TestValue("nullifzero(cast (5 as INT))", TYPE_INT, 5);
3795  TestValue("nullifzero(cast (5 as BIGINT))", TYPE_BIGINT, 5);
3796  TestValue<float>("nullifzero(cast (5 as FLOAT))", TYPE_FLOAT, 5.0f);
3797  TestValue<double>("nullifzero(cast (5 as DOUBLE))", TYPE_DOUBLE, 5.0);
3798 
3799  // Test all float types in then and else exprs.
3800  // Also tests implicit casting in all exprs.
3801  // TODO: Something with our float literals is broken:
3802  // 1.1 gets recognized as a DOUBLE, but numeric_limits<float>::max()) + 1.1 as a FLOAT.
3803 #if 0
3804  unordered_map<int, double>::iterator float_iter;
3805  for (float_iter = min_float_values_.begin(); float_iter != min_float_values_.end();
3806  ++float_iter) {
3807  PrimitiveType t = static_cast<PrimitiveType>(float_iter->first);
3808  string& s = default_type_strs_[t];
3809  TestValue("case when true then " + s + " end", t, float_iter->second);
3810  TestValue("case when false then 1 else " + s + " end", t, float_iter->second);
3811  TestValue("case when true then 1 else " + s + " end", t, 1.0);
3812  TestValue("case 0 when " + s + " then true else false end", TYPE_BOOLEAN, false);
3813  }
3814 #endif
3815 
3816  // Test all other types.
3817  // We don't tests casts because these types don't allow casting up to them.
3818  TestValue("case when true then " + default_bool_str_ + " end", TYPE_BOOLEAN,
3819  default_bool_val_);
3820  TestValue("case when false then true else " + default_bool_str_ + " end", TYPE_BOOLEAN,
3821  default_bool_val_);
3822  // String type.
3823  TestStringValue("case when true then " + default_string_str_ + " end",
3824  default_string_val_);
3825  TestStringValue("case when false then '1' else " + default_string_str_ + " end",
3826  default_string_val_);
3827  // Timestamp type.
3828  TestTimestampValue("case when true then " + default_timestamp_str_ + " end",
3829  default_timestamp_val_);
3830  TestTimestampValue("case when false then cast('1999-06-14 19:07:25' as timestamp) "
3831  "else " + default_timestamp_str_ + " end", default_timestamp_val_);
3832 
3833  // Test Decode. This function is internalized as a CaseExpr so no
3834  // extra testing should be needed. To be safe, a sanity test will be done.
3835  TestValue("decode(1, 2, 3, 4)", TYPE_TINYINT, 4);
3836  // In Decode NULLs are equal
3837  TestValue("decode(NULL + 1, NULL + 2, 3)", TYPE_TINYINT, 3);
3838  TestValue("decode(NULL, NULL, 2)", TYPE_TINYINT, 2);
3839  TestIsNull("decode(1, NULL, 2)", TYPE_TINYINT);
3840  TestIsNull("decode(NULL, 1, 2)", TYPE_TINYINT);
3841 }
3842 
3843 TEST_F(ExprTest, ConditionalFunctionIsTrue) {
3844  TestValue("istrue(cast(false as boolean))", TYPE_BOOLEAN, false);
3845  TestValue("istrue(cast(true as boolean))", TYPE_BOOLEAN, true);
3846  TestValue("istrue(cast(NULL as boolean))", TYPE_BOOLEAN, false);
3847  TestValue("istrue(cast(0 as boolean))", TYPE_BOOLEAN, false);
3848  TestValue("istrue(cast(5 as boolean))", TYPE_BOOLEAN, true);
3849  TestValue("istrue(cast(-5 as boolean))", TYPE_BOOLEAN, true);
3850  TestValue("istrue(cast(0.0 as boolean))", TYPE_BOOLEAN, false);
3851  TestValue("istrue(cast(5.0 as boolean))", TYPE_BOOLEAN, true);
3852  TestValue("istrue(cast(-5.0 as boolean))", TYPE_BOOLEAN, true);
3853 
3854  TestError("istrue(0)");
3855  TestError("istrue(5)");
3856  TestError("istrue(-5)");
3857  TestError("istrue(0.0)");
3858  TestError("istrue(5.0)");
3859  TestError("istrue(-5.0)");
3860  TestError("istrue(\"\")");
3861  TestError("istrue(\"abc\")");
3862  TestError("istrue(cast('2012-01-01 09:10:11.123456789' as timestamp))");
3863 
3864  TestError("istrue(999999999999999999999999999999999999999)");
3865  TestError("istrue(-99999999999999999999999999999999999999)");
3866  TestError("istrue(99999999999999999999999999999999999999.9)");
3867  TestError("istrue(-9999999999999999999999999999999999999.9)");
3868 }
3869 
3870 TEST_F(ExprTest, ConditionalFunctionIsFalse) {
3871  TestValue("isfalse(cast(false as boolean))", TYPE_BOOLEAN, true);
3872  TestValue("isfalse(cast(true as boolean))", TYPE_BOOLEAN, false);
3873  TestValue("isfalse(cast(NULL as boolean))", TYPE_BOOLEAN, false);
3874  // The output of cast(0 as boolean) is false.
3875  TestValue("isfalse(cast(0 as boolean))", TYPE_BOOLEAN, true);
3876  TestValue("isfalse(cast(5 as boolean))", TYPE_BOOLEAN, false);
3877  TestValue("isfalse(cast(-5 as boolean))", TYPE_BOOLEAN, false);
3878  // The output of cast(0.0 as boolean) is false.
3879  TestValue("isfalse(cast(0.0 as boolean))", TYPE_BOOLEAN, true);
3880  TestValue("isfalse(cast(5.0 as boolean))", TYPE_BOOLEAN, false);
3881  TestValue("isfalse(cast(-5.0 as boolean))", TYPE_BOOLEAN, false);
3882 
3883  TestError("isfalse(0)");
3884  TestError("isfalse(5)");
3885  TestError("isfalse(-5)");
3886  TestError("isfalse(0.0)");
3887  TestError("isfalse(5.0)");
3888  TestError("isfalse(-5.0)");
3889  TestError("isfalse(\"\")");
3890  TestError("isfalse(\"abc\")");
3891  TestError("isfalse(cast('2012-01-01 09:10:11.123456789' as timestamp))");
3892 
3893  TestError("isfalse(999999999999999999999999999999999999999)");
3894  TestError("isfalse(-99999999999999999999999999999999999999)");
3895  TestError("isfalse(99999999999999999999999999999999999999.9)");
3896  TestError("isfalse(-9999999999999999999999999999999999999.9)");
3897 }
3898 
3899 TEST_F(ExprTest, ConditionalFunctionIsNotTrue) {
3900  TestValue("isnottrue(cast(false as boolean))", TYPE_BOOLEAN, true);
3901  TestValue("isnottrue(cast(true as boolean))", TYPE_BOOLEAN, false);
3902  TestValue("isnottrue(cast(NULL as boolean))", TYPE_BOOLEAN, true);
3903  // The output of cast(0 as boolean) is false.
3904  TestValue("isnottrue(cast(0 as boolean))", TYPE_BOOLEAN, true);
3905  TestValue("isnottrue(cast(5 as boolean))", TYPE_BOOLEAN, false);
3906  TestValue("isnottrue(cast(-5 as boolean))", TYPE_BOOLEAN, false);
3907  // The output of cast(0.0 as boolean) is false.
3908  TestValue("isnottrue(cast(0.0 as boolean))", TYPE_BOOLEAN, true);
3909  TestValue("isnottrue(cast(5.0 as boolean))", TYPE_BOOLEAN, false);
3910  TestValue("isnottrue(cast(-5.0 as boolean))", TYPE_BOOLEAN, false);
3911 
3912  TestError("isnottrue(0)");
3913  TestError("isnottrue(5)");
3914  TestError("isnottrue(-5)");
3915  TestError("isnottrue(0.0)");
3916  TestError("isnottrue(5.0)");
3917  TestError("isnottrue(-5.0)");
3918  TestError("isnottrue(\"\")");
3919  TestError("isnottrue(\"abc\")");
3920  TestError("isnottrue(cast('2012-01-01 09:10:11.123456789' as timestamp))");
3921 
3922  TestError("isnottrue(999999999999999999999999999999999999999)");
3923  TestError("isnottrue(-99999999999999999999999999999999999999)");
3924  TestError("isnottrue(99999999999999999999999999999999999999.9)");
3925  TestError("isnottrue(-9999999999999999999999999999999999999.9)");
3926 }
3927 
3928 TEST_F(ExprTest, ConditionalFunctionIsNotFalse) {
3929  TestValue("isnotfalse(cast(false as boolean))", TYPE_BOOLEAN, false);
3930  TestValue("isnotfalse(cast(true as boolean))", TYPE_BOOLEAN, true);
3931  TestValue("isnotfalse(cast(NULL as boolean))", TYPE_BOOLEAN, true);
3932  TestValue("isnotfalse(cast(0 as boolean))", TYPE_BOOLEAN, false);
3933  TestValue("isnotfalse(cast(5 as boolean))", TYPE_BOOLEAN, true);
3934  TestValue("isnotfalse(cast(-5 as boolean))", TYPE_BOOLEAN, true);
3935  TestValue("isnotfalse(cast(0.0 as boolean))", TYPE_BOOLEAN, false);
3936  TestValue("isnotfalse(cast(5.0 as boolean))", TYPE_BOOLEAN, true);
3937  TestValue("isnotfalse(cast(-5.0 as boolean))", TYPE_BOOLEAN, true);
3938 
3939  TestError("isnotfalse(0)");
3940  TestError("isnotfalse(5)");
3941  TestError("isnotfalse(-5)");
3942  TestError("isnotfalse(0.0)");
3943  TestError("isnotfalse(5.0)");
3944  TestError("isnotfalse(-5.0)");
3945  TestError("isnotfalse(\"\")");
3946  TestError("isnotfalse(\"abc\")");
3947  TestError("isnotfalse(cast('2012-01-01 09:10:11.123456789' as timestamp))");
3948 
3949  TestError("isnotfalse(999999999999999999999999999999999999999)");
3950  TestError("isnotfalse(-99999999999999999999999999999999999999)");
3951  TestError("isnotfalse(99999999999999999999999999999999999999.9)");
3952  TestError("isnotfalse(-9999999999999999999999999999999999999.9)");
3953 }
3954 
3955 // Validates that Expr::ComputeResultsLayout() for 'exprs' is correct.
3956 // - expected_byte_size: total byte size to store all results for exprs
3957 // - expected_var_begin: byte offset where variable length types begin
3958 // - expected_offsets: mapping of byte sizes to a set valid offsets
3959 // exprs that have the same byte size can end up in a number of locations
3960 void ValidateLayout(const vector<Expr*>& exprs, int expected_byte_size,
3961  int expected_var_begin, const map<int, set<int> >& expected_offsets) {
3962 
3963  vector<int> offsets;
3964  set<int> offsets_found;
3965 
3966  int var_begin;
3967  int byte_size = Expr::ComputeResultsLayout(exprs, &offsets, &var_begin);
3968 
3969  EXPECT_EQ(byte_size, expected_byte_size);
3970  EXPECT_EQ(var_begin, expected_var_begin);
3971 
3972  // Walk the computed offsets and make sure the resulting sets match expected_offsets
3973  for (int i = 0; i < exprs.size(); ++i) {
3974  int expr_byte_size = exprs[i]->type().GetByteSize();
3975  map<int, set<int> >::const_iterator iter = expected_offsets.find(expr_byte_size);
3976  EXPECT_TRUE(iter != expected_offsets.end());
3977 
3978  const set<int>& possible_offsets = iter->second;
3979  int computed_offset = offsets[i];
3980  // The computed offset has to be one of the possible. Exprs types with the
3981  // same size are not ordered wrt each other.
3982  EXPECT_TRUE(possible_offsets.find(computed_offset) != possible_offsets.end());
3983  // The offset should not have been found before
3984  EXPECT_TRUE(offsets_found.find(computed_offset) == offsets_found.end());
3985  offsets_found.insert(computed_offset);
3986  }
3987 }
3988 
3989 TEST_F(ExprTest, ResultsLayoutTest) {
3990  ObjectPool pool;
3991 
3992  vector<Expr*> exprs;
3993  map<int, set<int> > expected_offsets;
3994 
3995  // Test empty exprs
3996  ValidateLayout(exprs, 0, -1, expected_offsets);
3997 
3998  // Test single Expr case
3999  expected_offsets.clear();
4000  for (int type = TYPE_BOOLEAN; type <= TYPE_STRING; ++type) {
4001  ColumnType t = ColumnType(static_cast<PrimitiveType>(type));
4002  exprs.clear();
4003  expected_offsets.clear();
4004  // With one expr, all offsets should be 0.
4005  expected_offsets[t.GetByteSize()] = list_of(0);
4006  exprs.push_back(pool.Add(Literal::CreateLiteral(t, "0")));
4007  if (t.type == TYPE_STRING) {
4008  ValidateLayout(exprs, 16, 0, expected_offsets);
4009  } else {
4010  ValidateLayout(exprs, t.GetByteSize(), -1, expected_offsets);
4011  }
4012  }
4013 
4014  int expected_byte_size = 0;
4015  int expected_var_begin = 0;
4016  expected_offsets.clear();
4017  exprs.clear();
4018 
4019  // Test layout adding a bunch of exprs. This is designed to trigger padding.
4020  // The expected result is computed along the way
4021  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_BOOLEAN, "0")));
4022  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_TINYINT, "0")));
4023  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_TINYINT, "0")));
4024  expected_offsets[1].insert(expected_byte_size);
4025  expected_offsets[1].insert(expected_byte_size + 1);
4026  expected_offsets[1].insert(expected_byte_size + 2);
4027  expected_byte_size += 3 * 1 + 1; // 1 byte of padding
4028 
4029  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_SMALLINT, "0")));
4030  expected_offsets[2].insert(expected_byte_size);
4031  expected_byte_size += 1 * 2 + 2; // 2 bytes of padding
4032 
4033  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_INT, "0")));
4034  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_FLOAT, "0")));
4035  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_FLOAT, "0")));
4036  expected_offsets[4].insert(expected_byte_size);
4037  expected_offsets[4].insert(expected_byte_size + 4);
4038  expected_offsets[4].insert(expected_byte_size + 8);
4039  expected_byte_size += 3 * 4 + 4; // 4 bytes of padding
4040 
4041  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_BIGINT, "0")));
4042  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_BIGINT, "0")));
4043  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_BIGINT, "0")));
4044  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_DOUBLE, "0")));
4045  expected_offsets[8].insert(expected_byte_size);
4046  expected_offsets[8].insert(expected_byte_size + 8);
4047  expected_offsets[8].insert(expected_byte_size + 16);
4048  expected_offsets[8].insert(expected_byte_size + 24);
4049  expected_byte_size += 4 * 8; // No more padding
4050 
4051  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_TIMESTAMP, "0")));
4052  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_TIMESTAMP, "0")));
4053  expected_offsets[16].insert(expected_byte_size);
4054  expected_offsets[16].insert(expected_byte_size + 16);
4055  expected_byte_size += 2 * 16;
4056 
4057  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_STRING, "0")));
4058  exprs.push_back(pool.Add(Literal::CreateLiteral(TYPE_STRING, "0")));
4059  expected_offsets[0].insert(expected_byte_size);
4060  expected_offsets[0].insert(expected_byte_size + 16);
4061  expected_var_begin = expected_byte_size;
4062  expected_byte_size += 2 * 16;
4063 
4064  // Validate computed layout
4065  ValidateLayout(exprs, expected_byte_size, expected_var_begin, expected_offsets);
4066 
4067  // Randomize the expr order and validate again. This is implemented by a
4068  // sort when the layout is computed so it shouldn't be very sensitive to
4069  // a particular order.
4070 
4071  srand(0); // Seed rand to get repeatable results
4072  for (int i = 0; i < 10; ++i) {
4073  std::random_shuffle(exprs.begin(), exprs.end());
4074  ValidateLayout(exprs, expected_byte_size, expected_var_begin, expected_offsets);
4075  }
4076 }
4077 
4078 // TODO: is there an easy way to templatize/parametrize these tests?
4080  TestValue("precision(cast (1 as decimal(10,2)))", TYPE_INT, 10);
4081  TestValue("scale(cast(1 as decimal(10,2)))", TYPE_INT, 2);
4082 
4083  TestValue("precision(1)", TYPE_INT, 3);
4084  TestValue("precision(cast(1 as smallint))", TYPE_INT, 5);
4085  TestValue("precision(cast(123 as bigint))", TYPE_INT, 19);
4086  TestValue("precision(123.45)", TYPE_INT, 5);
4087  TestValue("scale(123.45)", TYPE_INT, 2);
4088  TestValue("precision(1 + 1)", TYPE_INT, 5);
4089  TestValue("scale(1 + 1)", TYPE_INT, 0);
4090  TestValue("precision(1 + 1)", TYPE_INT, 5);
4091 
4092  TestValue("scale(cast(NULL as decimal(10, 2)))", TYPE_INT, 2);
4093 
4094  // Test result scale/precision from round()/truncate()
4095  TestValue("scale(round(123.456, 3))", TYPE_INT, 3);
4096  TestValue("precision(round(cast(\"123.456\" as decimal(6, 3)), 3))", TYPE_INT, 6);
4097 
4098  TestValue("scale(truncate(123.456, 1))", TYPE_INT, 1);
4099  TestValue("precision(truncate(123.456, 1))", TYPE_INT, 4);
4100 
4101  TestValue("scale(round(cast(\"123.456\" as decimal(6, 3)), -2))", TYPE_INT, 0);
4102  TestValue("precision(round(123.456, -2))", TYPE_INT, 4);
4103 
4104  TestValue("scale(truncate(123.456, 10))", TYPE_INT, 10);
4105  TestValue("precision(truncate(cast(\"123.456\" as decimal(6, 3)), 10))", TYPE_INT, 13);
4106 
4107  TestValue("scale(round(123.456, -10))", TYPE_INT, 0);
4108  TestValue("precision(round(cast(\"123.456\" as decimal(6, 3)), -10))", TYPE_INT, 4);
4109 
4110  // Abs()
4111  TestDecimalValue("abs(cast('0' as decimal(2,0)))", Decimal4Value(0),
4112  ColumnType::CreateDecimalType(2, 0));
4113  TestDecimalValue("abs(cast('1.1' as decimal(2,1)))", Decimal4Value(11),
4114  ColumnType::CreateDecimalType(2,1));
4115  TestDecimalValue("abs(cast('-1.23' as decimal(5,2)))", Decimal4Value(123),
4116  ColumnType::CreateDecimalType(5,2));
4117  TestDecimalValue("abs(cast('0' as decimal(12,0)))", Decimal8Value(0),
4118  ColumnType::CreateDecimalType(12, 0));
4119  TestDecimalValue("abs(cast('1.1' as decimal(12,1)))", Decimal8Value(11),
4120  ColumnType::CreateDecimalType(12,1));
4121  TestDecimalValue("abs(cast('-1.23' as decimal(12,2)))", Decimal8Value(123),
4122  ColumnType::CreateDecimalType(12,2));
4123  TestDecimalValue("abs(cast('0' as decimal(32,0)))", Decimal16Value(0),
4124  ColumnType::CreateDecimalType(32, 0));
4125  TestDecimalValue("abs(cast('1.1' as decimal(32,1)))", Decimal8Value(11),
4126  ColumnType::CreateDecimalType(32,1));
4127  TestDecimalValue("abs(cast('-1.23' as decimal(32,2)))", Decimal8Value(123),
4128  ColumnType::CreateDecimalType(32,2));
4129  TestIsNull("abs(cast(NULL as decimal(2,0)))", ColumnType::CreateDecimalType(2,0));
4130 
4131  // Tests take a while and at this point we've cycled through each type. No reason
4132  // to keep testing the codegen path.
4133  if (!disable_codegen_) return;
4134 
4135  // IsNull()
4136  TestDecimalValue("isnull(cast('0' as decimal(2,0)), NULL)", Decimal4Value(0),
4137  ColumnType::CreateDecimalType(2, 0));
4138  TestDecimalValue("isnull(cast('1.1' as decimal(18,1)), NULL)", Decimal8Value(11),
4139  ColumnType::CreateDecimalType(18,1));
4140  TestDecimalValue("isnull(cast('-1.23' as decimal(32,2)), NULL)", Decimal8Value(-123),
4141  ColumnType::CreateDecimalType(32,2));
4142  TestDecimalValue("isnull(NULL, cast('0' as decimal(2,0)))", Decimal4Value(0),
4143  ColumnType::CreateDecimalType(2, 0));
4144  TestDecimalValue("isnull(NULL, cast('1.1' as decimal(18,1)))", Decimal8Value(11),
4145  ColumnType::CreateDecimalType(18,1));
4146  TestDecimalValue("isnull(NULL, cast('-1.23' as decimal(32,2)))", Decimal8Value(-123),
4147  ColumnType::CreateDecimalType(32,2));
4148  TestIsNull("isnull(cast(NULL as decimal(2,0)), NULL)",
4149  ColumnType::CreateDecimalType(2,0));
4150 
4151  // NullIf()
4152  TestDecimalValue("isnull(cast('0' as decimal(2,0)), NULL)", Decimal4Value(0),
4153  ColumnType::CreateDecimalType(2, 0));
4154  TestDecimalValue("isnull(cast('1.1' as decimal(18,1)), NULL)", Decimal8Value(11),
4155  ColumnType::CreateDecimalType(18,1));
4156  TestDecimalValue("isnull(cast('-1.23' as decimal(32,2)), NULL)", Decimal8Value(-123),
4157  ColumnType::CreateDecimalType(32,2));
4158  TestDecimalValue("isnull(NULL, cast('0' as decimal(2,0)))", Decimal4Value(0),
4159  ColumnType::CreateDecimalType(2, 0));
4160  TestDecimalValue("isnull(NULL, cast('1.1' as decimal(18,1)))", Decimal8Value(11),
4161  ColumnType::CreateDecimalType(18,1));
4162  TestDecimalValue("isnull(NULL, cast('-1.23' as decimal(32,2)))", Decimal8Value(-123),
4163  ColumnType::CreateDecimalType(32,2));
4164  TestIsNull("isnull(cast(NULL as decimal(2,0)), NULL)",
4165  ColumnType::CreateDecimalType(2,0));
4166 
4167  // NullIfZero()
4168  TestDecimalValue("nullifzero(cast('10' as decimal(2,0)))", Decimal4Value(10),
4169  ColumnType::CreateDecimalType(2, 0));
4170  TestDecimalValue("nullifzero(cast('1.1' as decimal(18,1)))", Decimal8Value(11),
4171  ColumnType::CreateDecimalType(18,1));
4172  TestDecimalValue("nullifzero(cast('-1.23' as decimal(32,2)))", Decimal8Value(-123),
4173  ColumnType::CreateDecimalType(32,2));
4174  TestIsNull("nullifzero(cast('0' as decimal(2,0)))",
4175  ColumnType::CreateDecimalType(2, 0));
4176  TestIsNull("nullifzero(cast('0' as decimal(18,1)))",
4177  ColumnType::CreateDecimalType(18,1));
4178  TestIsNull("nullifzero(cast('0' as decimal(32,2)))",
4179  ColumnType::CreateDecimalType(32,2));
4180 
4181  // IfFn()
4182  TestDecimalValue("if(TRUE, cast('0' as decimal(2,0)), NULL)", Decimal4Value(0),
4183  ColumnType::CreateDecimalType(2, 0));
4184  TestDecimalValue("if(TRUE, cast('1.1' as decimal(18,1)), NULL)", Decimal8Value(11),
4185  ColumnType::CreateDecimalType(18,1));
4186  TestDecimalValue("if(TRUE, cast('-1.23' as decimal(32,2)), NULL)", Decimal8Value(-123),
4187  ColumnType::CreateDecimalType(32,2));
4188  TestDecimalValue("if(FALSE, NULL, cast('0' as decimal(2,0)))", Decimal4Value(0),
4189  ColumnType::CreateDecimalType(2, 0));
4190  TestDecimalValue("if(FALSE, NULL, cast('1.1' as decimal(18,1)))", Decimal8Value(11),
4191  ColumnType::CreateDecimalType(18,1));
4192  TestDecimalValue("if(FALSE, NULL, cast('-1.23' as decimal(32,2)))", Decimal8Value(-123),
4193  ColumnType::CreateDecimalType(32,2));
4194  TestIsNull("if(TRUE, cast(NULL as decimal(32,2)), NULL)",
4195  ColumnType::CreateDecimalType(32,2));
4196  TestIsNull("if(FALSE, cast('-1.23' as decimal(32,2)), NULL)",
4197  ColumnType::CreateDecimalType(32,2));
4198 
4199  // ZeroIfNull()
4200  TestDecimalValue("zeroifnull(cast('10' as decimal(2,0)))", Decimal4Value(10),
4201  ColumnType::CreateDecimalType(2, 0));
4202  TestDecimalValue("zeroifnull(cast('1.1' as decimal(18,1)))", Decimal8Value(11),
4203  ColumnType::CreateDecimalType(18,1));
4204  TestDecimalValue("zeroifnull(cast('-1.23' as decimal(32,2)))", Decimal8Value(-123),
4205  ColumnType::CreateDecimalType(32,2));
4206  TestDecimalValue("zeroifnull(cast(NULL as decimal(2,0)))", Decimal4Value(0),
4207  ColumnType::CreateDecimalType(2, 0));
4208  TestDecimalValue("zeroifnull(cast(NULL as decimal(18,1)))", Decimal8Value(0),
4209  ColumnType::CreateDecimalType(18,1));
4210  TestDecimalValue("zeroifnull(cast(NULL as decimal(32,2)))", Decimal16Value(0),
4211  ColumnType::CreateDecimalType(32,2));
4212 
4213  // Coalesce()
4214  TestDecimalValue("coalesce(NULL, cast('0' as decimal(2,0)))", Decimal4Value(0),
4215  ColumnType::CreateDecimalType(2, 0));
4216  TestDecimalValue("coalesce(NULL, cast('0' as decimal(18,0)))", Decimal8Value(0),
4217  ColumnType::CreateDecimalType(18, 0));
4218  TestDecimalValue("coalesce(NULL, cast('1.1' as decimal(18,1)))", Decimal8Value(11),
4219  ColumnType::CreateDecimalType(18,1));
4220  TestDecimalValue("coalesce(NULL, cast('-1.23' as decimal(18,2)))", Decimal8Value(-123),
4221  ColumnType::CreateDecimalType(18,2));
4222  TestDecimalValue("coalesce(NULL, cast('0' as decimal(32,0)))", Decimal16Value(0),
4223  ColumnType::CreateDecimalType(32, 0));
4224  TestDecimalValue("coalesce(NULL, cast('1.1' as decimal(32,1)))", Decimal8Value(11),
4225  ColumnType::CreateDecimalType(32,1));
4226  TestDecimalValue("coalesce(NULL, cast('-1.23' as decimal(32,2)))", Decimal8Value(-123),
4227  ColumnType::CreateDecimalType(32,2));
4228  TestIsNull("coalesce(cast(NULL as decimal(2,0)), NULL)",
4229  ColumnType::CreateDecimalType(2,0));
4230 
4231  // Case
4232  TestDecimalValue("CASE when true then cast('10' as decimal(2,0)) end",
4233  Decimal4Value(10), ColumnType::CreateDecimalType(2, 0));
4234  TestDecimalValue("CASE when true then cast('1.1' as decimal(18,1)) end",
4235  Decimal8Value(11), ColumnType::CreateDecimalType(18,1));
4236  TestDecimalValue("CASE when true then cast('-1.23' as decimal(32,2)) end",
4237  Decimal8Value(-123), ColumnType::CreateDecimalType(32,2));
4238  TestDecimalValue("CASE when false then NULL else cast('10' as decimal(2,0)) end",
4239  Decimal4Value(10), ColumnType::CreateDecimalType(2, 0));
4240  TestDecimalValue("CASE when false then NULL else cast('1.1' as decimal(18,1)) end",
4241  Decimal8Value(11), ColumnType::CreateDecimalType(18,1));
4242  TestDecimalValue("CASE when false then NULL else cast('-1.23' as decimal(32,2)) end",
4243  Decimal8Value(-123), ColumnType::CreateDecimalType(32,2));
4244 
4245  TestValue("CASE 1.1 when 1.1 then 1 when 2.22 then 2 else 3 end", TYPE_TINYINT, 1);
4246  TestValue("CASE 2.22 when 1.1 then 1 when 2.22 then 2 else 3 end", TYPE_TINYINT, 2);
4247  TestValue("CASE 2.21 when 1.1 then 1 when 2.22 then 2 else 3 end", TYPE_TINYINT, 3);
4248  TestValue("CASE NULL when 1.1 then 1 when 2.22 then 2 else 3 end", TYPE_TINYINT, 3);
4249 
4250  TestDecimalValue("CASE 2.21 when 1.1 then 1.1 when 2.21 then 2.2 else 3.3 end",
4251  Decimal4Value(22), ColumnType::CreateDecimalType(2, 1));
4252 
4253  // Positive()
4254  TestDecimalValue("positive(cast('10' as decimal(2,0)))",
4255  Decimal4Value(10), ColumnType::CreateDecimalType(2, 0));
4256  TestDecimalValue("positive(cast('1.1' as decimal(18,1)))",
4257  Decimal8Value(11), ColumnType::CreateDecimalType(18,1));
4258  TestDecimalValue("positive(cast('-1.23' as decimal(32,2)))",
4259  Decimal8Value(-123), ColumnType::CreateDecimalType(32,2));
4260  TestIsNull("positive(cast(NULL as decimal(32,2)))",
4261  ColumnType::CreateDecimalType(32,2));
4262 
4263  // Negative()
4264  TestDecimalValue("negative(cast('10' as decimal(2,0)))",
4265  Decimal4Value(-10), ColumnType::CreateDecimalType(2, 0));
4266  TestDecimalValue("negative(cast('1.1' as decimal(18,1)))",
4267  Decimal8Value(-11), ColumnType::CreateDecimalType(18,1));
4268  TestDecimalValue("negative(cast('-1.23' as decimal(32,2)))",
4269  Decimal8Value(123), ColumnType::CreateDecimalType(32,2));
4270  TestIsNull("negative(cast(NULL as decimal(32,2)))",
4271  ColumnType::CreateDecimalType(32,2));
4272 
4273  // TODO: Disabled due to IMPALA-1111.
4274  // Least()
4275  TestDecimalValue("least(cast('10' as decimal(2,0)), cast('-10' as decimal(2,0)))",
4276  Decimal4Value(-10), ColumnType::CreateDecimalType(2, 0));
4277  TestDecimalValue("least(cast('1.1' as decimal(18,1)), cast('-1.1' as decimal(18,1)))",
4278  Decimal8Value(-11), ColumnType::CreateDecimalType(18,1));
4279  TestDecimalValue("least(cast('-1.23' as decimal(32,2)), cast('1.23' as decimal(32,2)))",
4280  Decimal8Value(-123), ColumnType::CreateDecimalType(32,2));
4281  TestIsNull("least(cast(NULL as decimal(32,2)), cast('1.23' as decimal(32,2)))",
4282  ColumnType::CreateDecimalType(32,2));
4283  TestIsNull("least(cast('1.23' as decimal(32,2)), NULL)",
4284  ColumnType::CreateDecimalType(32,2));
4285  TestIsNull("least(cast(NULl as decimal(32,2)), NULL)",
4286  ColumnType::CreateDecimalType(32,2));
4287 
4288  // Greatest()
4289  TestDecimalValue("greatest(cast('10' as decimal(2,0)), cast('-10' as decimal(2,0)))",
4290  Decimal4Value(10), ColumnType::CreateDecimalType(2, 0));
4291  TestDecimalValue(
4292  "greatest(cast('1.1' as decimal(18,1)), cast('-1.1' as decimal(18,1)))",
4293  Decimal8Value(11), ColumnType::CreateDecimalType(18,1));
4294  TestDecimalValue(
4295  "greatest(cast('-1.23' as decimal(32,2)), cast('1.23' as decimal(32,2)))",
4296  Decimal8Value(123), ColumnType::CreateDecimalType(32,2));
4297  TestIsNull("greatest(cast(NULL as decimal(32,2)), cast('1.23' as decimal(32,2)))",
4298  ColumnType::CreateDecimalType(32,2));
4299  TestIsNull("greatest(cast('1.23' as decimal(32,2)), NULL)",
4300  ColumnType::CreateDecimalType(32,2));
4301  TestIsNull("greatest(cast(NULl as decimal(32,2)), NULL)",
4302  ColumnType::CreateDecimalType(32,2));
4303 
4304  Decimal4Value dec4(10);
4305  // DecimalFunctions::FnvHash hashes both the unscaled value and scale
4306  uint64_t expected = HashUtil::FnvHash64(&dec4, 4, HashUtil::FNV_SEED);
4307  TestValue("fnv_hash(cast('10' as decimal(2,0)))", TYPE_BIGINT, expected);
4308 
4309  Decimal8Value dec8 = Decimal8Value(11);
4310  expected = HashUtil::FnvHash64(&dec8, 8, HashUtil::FNV_SEED);
4311  TestValue("fnv_hash(cast('1.1' as decimal(18,1)))", TYPE_BIGINT, expected);
4312 
4313  Decimal16Value dec16 = Decimal16Value(-123);
4314  expected = HashUtil::FnvHash64(&dec16, 16, HashUtil::FNV_SEED);
4315  TestValue("fnv_hash(cast('-1.23' as decimal(32,2)))", TYPE_BIGINT, expected);
4316 
4317 
4318  // In these tests we iterate through the underlying decimal types and alternate
4319  // between positive and negative values.
4320 
4321  // Ceil()
4322  TestDecimalValue("ceil(cast('0' as decimal(6,5)))", Decimal4Value(0),
4323  ColumnType::CreateDecimalType(6, 0));
4324  TestDecimalValue("ceil(cast('3.14159' as decimal(6,5)))", Decimal4Value(4),
4325  ColumnType::CreateDecimalType(6, 0));
4326  TestDecimalValue("ceil(cast('-3.14159' as decimal(6,5)))", Decimal4Value(-3),
4327  ColumnType::CreateDecimalType(6, 0));
4328  TestDecimalValue("ceil(cast('3' as decimal(6,5)))", Decimal4Value(3),
4329  ColumnType::CreateDecimalType(6, 0));
4330  TestDecimalValue("ceil(cast('3.14159' as decimal(13,5)))", Decimal8Value(4),
4331  ColumnType::CreateDecimalType(13, 0));
4332  TestDecimalValue("ceil(cast('-3.14159' as decimal(13,5)))", Decimal8Value(-3),
4333  ColumnType::CreateDecimalType(13, 0));
4334  TestDecimalValue("ceil(cast('3' as decimal(13,5)))", Decimal8Value(3),
4335  ColumnType::CreateDecimalType(13, 0));
4336  TestDecimalValue("ceil(cast('3.14159' as decimal(33,5)))", Decimal16Value(4),
4337  ColumnType::CreateDecimalType(33, 0));
4338  TestDecimalValue("ceil(cast('-3.14159' as decimal(33,5)))", Decimal16Value(-3),
4339  ColumnType::CreateDecimalType(33, 0));
4340  TestDecimalValue("ceil(cast('3' as decimal(33,5)))", Decimal16Value(3),
4341  ColumnType::CreateDecimalType(33, 0));
4342  TestDecimalValue("ceil(cast('9.14159' as decimal(6,5)))", Decimal4Value(10),
4343  ColumnType::CreateDecimalType(2, 0));
4344  TestIsNull("ceil(cast(NULL as decimal(2,0)))", ColumnType::CreateDecimalType(2,0));
4345 
4346  // Floor()
4347  TestDecimalValue("floor(cast('3.14159' as decimal(6,5)))", Decimal4Value(3),
4348  ColumnType::CreateDecimalType(6, 0));
4349  TestDecimalValue("floor(cast('-3.14159' as decimal(6,5)))", Decimal4Value(-4),
4350  ColumnType::CreateDecimalType(6, 0));
4351  TestDecimalValue("floor(cast('3' as decimal(6,5)))", Decimal4Value(3),
4352  ColumnType::CreateDecimalType(6, 0));
4353  TestDecimalValue("floor(cast('3.14159' as decimal(13,5)))", Decimal8Value(3),
4354  ColumnType::CreateDecimalType(13, 0));
4355  TestDecimalValue("floor(cast('-3.14159' as decimal(13,5)))", Decimal8Value(-4),
4356  ColumnType::CreateDecimalType(13, 0));
4357  TestDecimalValue("floor(cast('3' as decimal(13,5)))", Decimal8Value(3),
4358  ColumnType::CreateDecimalType(13, 0));
4359  TestDecimalValue("floor(cast('3.14159' as decimal(33,5)))", Decimal16Value(3),
4360  ColumnType::CreateDecimalType(33, 0));
4361  TestDecimalValue("floor(cast('-3.14159' as decimal(33,5)))", Decimal16Value(-4),
4362  ColumnType::CreateDecimalType(33, 0));
4363  TestDecimalValue("floor(cast('3' as decimal(33,5)))", Decimal16Value(3),
4364  ColumnType::CreateDecimalType(33, 0));
4365  TestDecimalValue("floor(cast('-9.14159' as decimal(6,5)))", Decimal4Value(-10),
4366  ColumnType::CreateDecimalType(2, 0));
4367  TestIsNull("floor(cast(NULL as decimal(2,0)))", ColumnType::CreateDecimalType(2,0));
4368 
4369  // Round()
4370  TestDecimalValue("round(cast('3.14159' as decimal(6,5)))", Decimal4Value(3),
4371  ColumnType::CreateDecimalType(6, 0));
4372  TestDecimalValue("round(cast('-3.14159' as decimal(6,5)))", Decimal4Value(-3),
4373  ColumnType::CreateDecimalType(6, 0));
4374  TestDecimalValue("round(cast('3' as decimal(6,5)))", Decimal4Value(3),
4375  ColumnType::CreateDecimalType(6, 0));
4376  TestDecimalValue("round(cast('3.14159' as decimal(13,5)))", Decimal8Value(3),
4377  ColumnType::CreateDecimalType(13, 0));
4378  TestDecimalValue("round(cast('-3.14159' as decimal(13,5)))", Decimal8Value(-3),
4379  ColumnType::CreateDecimalType(13, 0));
4380  TestDecimalValue("round(cast('3' as decimal(13,5)))", Decimal8Value(3),
4381  ColumnType::CreateDecimalType(13, 0));
4382  TestDecimalValue("round(cast('3.14159' as decimal(33,5)))", Decimal16Value(3),
4383  ColumnType::CreateDecimalType(33, 0));
4384  TestDecimalValue("round(cast('-3.14159' as decimal(33,5)))", Decimal16Value(-3),
4385  ColumnType::CreateDecimalType(33, 0));
4386  TestDecimalValue("round(cast('3' as decimal(33,5)))", Decimal16Value(3),
4387  ColumnType::CreateDecimalType(33, 0));
4388  TestDecimalValue("round(cast('9.54159' as decimal(6,5)))", Decimal4Value(10),
4389  ColumnType::CreateDecimalType(2, 0));
4390  TestDecimalValue("round(cast('-9.54159' as decimal(6,5)))", Decimal4Value(-10),
4391  ColumnType::CreateDecimalType(2, 0));
4392  TestIsNull("round(cast(NULL as decimal(2,0)))", ColumnType::CreateDecimalType(2,0));
4393 
4394  // Truncate()
4395  TestDecimalValue("truncate(cast('3.54159' as decimal(6,5)))", Decimal4Value(3),
4396  ColumnType::CreateDecimalType(6, 0));
4397  TestDecimalValue("truncate(cast('-3.54159' as decimal(6,5)))", Decimal4Value(-3),
4398  ColumnType::CreateDecimalType(6, 0));
4399  TestDecimalValue("truncate(cast('3' as decimal(6,5)))", Decimal4Value(3),
4400  ColumnType::CreateDecimalType(6, 0));
4401  TestDecimalValue("truncate(cast('3.54159' as decimal(13,5)))", Decimal8Value(3),
4402  ColumnType::CreateDecimalType(13, 0));
4403  TestDecimalValue("truncate(cast('-3.54159' as decimal(13,5)))", Decimal8Value(-3),
4404  ColumnType::CreateDecimalType(13, 0));
4405  TestDecimalValue("truncate(cast('3' as decimal(13,5)))", Decimal8Value(3),
4406  ColumnType::CreateDecimalType(13, 0));
4407  TestDecimalValue("truncate(cast('3.54159' as decimal(33,5)))", Decimal16Value(3),
4408  ColumnType::CreateDecimalType(33, 0));
4409  TestDecimalValue("truncate(cast('-3.54159' as decimal(33,5)))", Decimal16Value(-3),
4410  ColumnType::CreateDecimalType(33, 0));
4411  TestDecimalValue("truncate(cast('3' as decimal(33,5)))", Decimal16Value(3),
4412  ColumnType::CreateDecimalType(33, 0));
4413  TestDecimalValue("truncate(cast('9.54159' as decimal(6,5)))", Decimal4Value(9),
4414  ColumnType::CreateDecimalType(1, 0));
4415  TestIsNull("truncate(cast(NULL as decimal(2,0)))", ColumnType::CreateDecimalType(2,0));
4416 
4417  // RoundTo()
4418  TestIsNull("round(cast(NULL as decimal(2,0)), 1)", ColumnType::CreateDecimalType(2,0));
4419 
4420  TestDecimalValue("round(cast('3.1615' as decimal(6,4)), 0)", Decimal4Value(3),
4421  ColumnType::CreateDecimalType(2, 0));
4422  TestDecimalValue("round(cast('-3.1615' as decimal(6,4)), 1)", Decimal4Value(-32),
4423  ColumnType::CreateDecimalType(3, 1));
4424  TestDecimalValue("round(cast('3.1615' as decimal(6,4)), 2)", Decimal4Value(316),
4425  ColumnType::CreateDecimalType(4, 2));
4426  TestDecimalValue("round(cast('3.1615' as decimal(6,4)), 3)", Decimal4Value(3162),
4427  ColumnType::CreateDecimalType(5, 3));
4428  TestDecimalValue("round(cast('-3.1615' as decimal(6,4)), 3)", Decimal4Value(-3162),
4429  ColumnType::CreateDecimalType(5, 3));
4430  TestDecimalValue("round(cast('3.1615' as decimal(6,4)), 4)", Decimal4Value(31615),
4431  ColumnType::CreateDecimalType(6, 4));
4432  TestDecimalValue("round(cast('-3.1615' as decimal(6,4)), 5)", Decimal4Value(-316150),
4433  ColumnType::CreateDecimalType(7, 5));
4434  TestDecimalValue("round(cast('175.0' as decimal(6,1)), 0)", Decimal4Value(175),
4435  ColumnType::CreateDecimalType(6, 0));
4436  TestDecimalValue("round(cast('-175.0' as decimal(6,1)), -1)", Decimal4Value(-180),
4437  ColumnType::CreateDecimalType(6, 0));
4438  TestDecimalValue("round(cast('175.0' as decimal(6,1)), -2)", Decimal4Value(200),
4439  ColumnType::CreateDecimalType(6, 0));
4440  TestDecimalValue("round(cast('-175.0' as decimal(6,1)), -3)", Decimal4Value(0),
4441  ColumnType::CreateDecimalType(6, 0));
4442  TestDecimalValue("round(cast('175.0' as decimal(6,1)), -4)", Decimal4Value(0),
4443  ColumnType::CreateDecimalType(6, 0));
4444  TestDecimalValue("round(cast('999.951' as decimal(6,3)), 1)", Decimal4Value(10000),
4445  ColumnType::CreateDecimalType(5, 1));
4446 
4447  TestDecimalValue("round(cast('-3.1615' as decimal(16,4)), 0)", Decimal8Value(-3),
4448  ColumnType::CreateDecimalType(12, 0));
4449  TestDecimalValue("round(cast('3.1615' as decimal(16,4)), 1)", Decimal8Value(32),
4450  ColumnType::CreateDecimalType(13, 1));
4451  TestDecimalValue("round(cast('-3.1615' as decimal(16,4)), 2)", Decimal8Value(-316),
4452  ColumnType::CreateDecimalType(14, 2));
4453  TestDecimalValue("round(cast('3.1615' as decimal(16,4)), 3)", Decimal8Value(3162),
4454  ColumnType::CreateDecimalType(15, 3));
4455  TestDecimalValue("round(cast('-3.1615' as decimal(16,4)), 3)", Decimal8Value(-3162),
4456  ColumnType::CreateDecimalType(15, 3));
4457  TestDecimalValue("round(cast('-3.1615' as decimal(16,4)), 4)", Decimal8Value(-31615),
4458  ColumnType::CreateDecimalType(16, 4));
4459  TestDecimalValue("round(cast('3.1615' as decimal(16,4)), 5)", Decimal8Value(316150),
4460  ColumnType::CreateDecimalType(17, 5));
4461  TestDecimalValue("round(cast('-999.951' as decimal(16,3)), 1)", Decimal8Value(-10000),
4462  ColumnType::CreateDecimalType(17, 1));
4463 
4464  TestDecimalValue("round(cast('-175.0' as decimal(15,1)), 0)", Decimal8Value(-175),
4465  ColumnType::CreateDecimalType(15, 0));
4466  TestDecimalValue("round(cast('175.0' as decimal(15,1)), -1)", Decimal8Value(180),
4467  ColumnType::CreateDecimalType(15, 0));
4468  TestDecimalValue("round(cast('-175.0' as decimal(15,1)), -2)", Decimal8Value(-200),
4469  ColumnType::CreateDecimalType(15, 0));
4470  TestDecimalValue("round(cast('175.0' as decimal(15,1)), -3)", Decimal8Value(0),
4471  ColumnType::CreateDecimalType(15, 0));
4472  TestDecimalValue("round(cast('-175.0' as decimal(15,1)), -4)", Decimal8Value(0),
4473  ColumnType::CreateDecimalType(15, 0));
4474 
4475  TestDecimalValue("round(cast('3.1615' as decimal(32,4)), 0)", Decimal16Value(3),
4476  ColumnType::CreateDecimalType(32, 0));
4477  TestDecimalValue("round(cast('-3.1615' as decimal(32,4)), 1)", Decimal16Value(-32),
4478  ColumnType::CreateDecimalType(33, 1));
4479  TestDecimalValue("round(cast('3.1615' as decimal(32,4)), 2)", Decimal16Value(316),
4480  ColumnType::CreateDecimalType(34, 2));
4481  TestDecimalValue("round(cast('3.1615' as decimal(32,4)), 3)", Decimal16Value(3162),
4482  ColumnType::CreateDecimalType(35, 3));
4483  TestDecimalValue("round(cast('-3.1615' as decimal(32,4)), 3)", Decimal16Value(-3162),
4484  ColumnType::CreateDecimalType(36, 3));
4485  TestDecimalValue("round(cast('3.1615' as decimal(32,4)), 4)", Decimal16Value(31615),
4486  ColumnType::CreateDecimalType(37, 4));
4487  TestDecimalValue("round(cast('-3.1615' as decimal(32,5)), 5)", Decimal16Value(-316150),
4488  ColumnType::CreateDecimalType(38, 5));
4489  TestDecimalValue("round(cast('-175.0' as decimal(35,1)), 0)", Decimal16Value(-175),
4490  ColumnType::CreateDecimalType(35, 0));
4491  TestDecimalValue("round(cast('175.0' as decimal(35,1)), -1)", Decimal16Value(180),
4492  ColumnType::CreateDecimalType(35, 0));
4493  TestDecimalValue("round(cast('-175.0' as decimal(35,1)), -2)", Decimal16Value(-200),
4494  ColumnType::CreateDecimalType(35, 0));
4495  TestDecimalValue("round(cast('175.0' as decimal(35,1)), -3)", Decimal16Value(0),
4496  ColumnType::CreateDecimalType(35, 0));
4497  TestDecimalValue("round(cast('-175.0' as decimal(35,1)), -4)", Decimal16Value(0),
4498  ColumnType::CreateDecimalType(35, 0));
4499  TestDecimalValue("round(cast('99999.9951' as decimal(35,4)), 2)",
4500  Decimal16Value(10000000), ColumnType::CreateDecimalType(36, 2));
4501 
4502  // TruncateTo()
4503  TestIsNull("truncate(cast(NULL as decimal(2,0)), 1)",
4504  ColumnType::CreateDecimalType(2,0));
4505 
4506  TestDecimalValue("truncate(cast('-3.1615' as decimal(6,4)), 0)", Decimal4Value(-3),
4507  ColumnType::CreateDecimalType(6, 0));
4508  TestDecimalValue("truncate(cast('3.1615' as decimal(6,4)), 1)", Decimal4Value(31),
4509  ColumnType::CreateDecimalType(6, 1));
4510  TestDecimalValue("truncate(cast('-3.1615' as decimal(6,4)), 2)", Decimal4Value(-316),
4511  ColumnType::CreateDecimalType(6, 2));
4512  TestDecimalValue("truncate(cast('3.1615' as decimal(6,4)), 3)", Decimal4Value(3161),
4513  ColumnType::CreateDecimalType(6, 3));
4514  TestDecimalValue("truncate(cast('-3.1615' as decimal(6,4)), 4)", Decimal4Value(-31615),
4515  ColumnType::CreateDecimalType(6, 4));
4516  TestDecimalValue("truncate(cast('3.1615' as decimal(6,4)), 5)", Decimal4Value(316150),
4517  ColumnType::CreateDecimalType(7, 5));
4518  TestDecimalValue("truncate(cast('175.0' as decimal(6,1)), 0)", Decimal4Value(175),
4519  ColumnType::CreateDecimalType(6, 0));
4520  TestDecimalValue("truncate(cast('-175.0' as decimal(6,1)), -1)", Decimal4Value(-170),
4521  ColumnType::CreateDecimalType(6, 0));
4522  TestDecimalValue("truncate(cast('175.0' as decimal(6,1)), -2)", Decimal4Value(100),
4523  ColumnType::CreateDecimalType(6, 0));
4524  TestDecimalValue("truncate(cast('-175.0' as decimal(6,1)), -3)", Decimal4Value(0),
4525  ColumnType::CreateDecimalType(6, 0));
4526  TestDecimalValue("truncate(cast('175.0' as decimal(6,1)), -4)", Decimal4Value(0),
4527  ColumnType::CreateDecimalType(6, 0));
4528 
4529  TestDecimalValue("truncate(cast('-3.1615' as decimal(16,4)), 0)", Decimal8Value(-3),
4530  ColumnType::CreateDecimalType(12, 0));
4531  TestDecimalValue("truncate(cast('3.1615' as decimal(16,4)), 1)", Decimal8Value(31),
4532  ColumnType::CreateDecimalType(13, 1));
4533  TestDecimalValue("truncate(cast('-3.1615' as decimal(16,4)), 2)", Decimal8Value(-316),
4534  ColumnType::CreateDecimalType(14, 2));
4535  TestDecimalValue("truncate(cast('3.1615' as decimal(16,4)), 3)", Decimal8Value(3161),
4536  ColumnType::CreateDecimalType(15, 3));
4537  TestDecimalValue("truncate(cast('3.1615' as decimal(16,4)), 4)",
4538  Decimal8Value(31615), ColumnType::CreateDecimalType(16, 4));
4539  TestDecimalValue("truncate(cast('-3.1615' as decimal(16,4)), 5)",
4540  Decimal8Value(-316150), ColumnType::CreateDecimalType(17, 5));
4541  TestDecimalValue("truncate(cast('-175.0' as decimal(15,1)), 0)", Decimal8Value(-175),
4542  ColumnType::CreateDecimalType(15, 0));
4543  TestDecimalValue("truncate(cast('175.0' as decimal(15,1)), -1)", Decimal8Value(170),
4544  ColumnType::CreateDecimalType(15, 0));
4545  TestDecimalValue("truncate(cast('-175.0' as decimal(15,1)), -2)", Decimal8Value(-100),
4546  ColumnType::CreateDecimalType(15, 0));
4547  TestDecimalValue("truncate(cast('175.0' as decimal(15,1)), -3)", Decimal8Value(0),
4548  ColumnType::CreateDecimalType(15, 0));
4549  TestDecimalValue("truncate(cast('-175.0' as decimal(15,1)), -4)", Decimal8Value(0),
4550  ColumnType::CreateDecimalType(15, 0));
4551 
4552  TestDecimalValue("truncate(cast('-3.1615' as decimal(32,4)), 0)",
4553  Decimal16Value(-3), ColumnType::CreateDecimalType(28, 0));
4554  TestDecimalValue("truncate(cast('3.1615' as decimal(32,4)), 1)",
4555  Decimal16Value(31), ColumnType::CreateDecimalType(29, 1));
4556  TestDecimalValue("truncate(cast('-3.1615' as decimal(32,4)), 2)",
4557  Decimal16Value(-316), ColumnType::CreateDecimalType(30, 2));
4558  TestDecimalValue("truncate(cast('3.1615' as decimal(32,4)), 3)",
4559  Decimal16Value(3161), ColumnType::CreateDecimalType(31, 3));
4560  TestDecimalValue("truncate(cast('-3.1615' as decimal(32,4)), 4)",
4561  Decimal16Value(-31615), ColumnType::CreateDecimalType(32, 4));
4562  TestDecimalValue("truncate(cast('3.1615' as decimal(32,4)), 5)",
4563  Decimal16Value(316150), ColumnType::CreateDecimalType(33, 5));
4564  TestDecimalValue("truncate(cast('-175.0' as decimal(35,1)), 0)",
4565  Decimal16Value(-175), ColumnType::CreateDecimalType(35, 0));
4566  TestDecimalValue("truncate(cast('175.0' as decimal(35,1)), -1)",
4567  Decimal16Value(170), ColumnType::CreateDecimalType(35, 0));
4568  TestDecimalValue("truncate(cast('-175.0' as decimal(35,1)), -2)",
4569  Decimal16Value(-100), ColumnType::CreateDecimalType(35, 0));
4570  TestDecimalValue("truncate(cast('175.0' as decimal(35,1)), -3)",
4571  Decimal16Value(0), ColumnType::CreateDecimalType(35, 0));
4572  TestDecimalValue("truncate(cast('-175.0' as decimal(35,1)), -4)",
4573  Decimal16Value(0), ColumnType::CreateDecimalType(35, 0));
4574 
4575  // Overflow on Round()/etc. This can only happen when the input is has enough
4576  // leading 9's.
4577  // Rounding this value requires a precision of 39 so it overflows.
4578  TestIsNull("round(99999999999999999999999999999999999999., -1)",
4579  ColumnType::CreateDecimalType(38, 0));
4580  TestIsNull("round(-99999999999999999999999999999999000000., -7)",
4581  ColumnType::CreateDecimalType(38, 0));
4582 }
4583 
4584 // Sanity check some overflow casting. We have a random test framework that covers
4585 // this more thoroughly.
4586 TEST_F(ExprTest, DecimalOverflowCasts) {
4587  TestDecimalValue("cast(123.456 as decimal(6,3))",
4588  Decimal4Value(123456), ColumnType::CreateDecimalType(6, 3));
4589  TestDecimalValue("cast(-123.456 as decimal(6,1))",
4590  Decimal4Value(-1234), ColumnType::CreateDecimalType(6, 1));
4591  TestDecimalValue("cast(123.456 as decimal(6,0))",
4592  Decimal4Value(123), ColumnType::CreateDecimalType(6, 0));
4593  TestDecimalValue("cast(-123.456 as decimal(3,0))",
4594  Decimal4Value(-123), ColumnType::CreateDecimalType(3, 0));
4595 
4596  TestDecimalValue("cast(123.4567890 as decimal(10,7))",
4597  Decimal8Value(1234567890L), ColumnType::CreateDecimalType(10, 7));
4598 
4599  TestDecimalValue("cast(cast(\"123.01234567890123456789\" as decimal(23,20))\
4600  as decimal(12,9))", Decimal8Value(123012345678L),
4601  ColumnType::CreateDecimalType(12, 9));
4602  TestDecimalValue("cast(cast(\"123.01234567890123456789\" as decimal(23,20))\
4603  as decimal(4,1))", Decimal4Value(1230), ColumnType::CreateDecimalType(4, 1));
4604 
4605  TestDecimalValue("cast(cast(\"123.0123456789\" as decimal(13,10))\
4606  as decimal(5,2))", Decimal4Value(12301), ColumnType::CreateDecimalType(5, 2));
4607 
4608  // Overflow
4609  TestIsNull("cast(123.456 as decimal(2,0))", ColumnType::CreateDecimalType(2, 0));
4610  TestIsNull("cast(123.456 as decimal(2,1))", ColumnType::CreateDecimalType(2, 2));
4611  TestIsNull("cast(123.456 as decimal(2,2))", ColumnType::CreateDecimalType(2, 2));
4612  TestIsNull("cast(99.99 as decimal(2,2))", ColumnType::CreateDecimalType(2, 2));
4613  TestDecimalValue("cast(99.99 as decimal(2,0))",
4614  Decimal4Value(99), ColumnType::CreateDecimalType(2, 0));
4615  TestIsNull("cast(-99.99 as decimal(2,2))", ColumnType::CreateDecimalType(2, 2));
4616  TestDecimalValue("cast(-99.99 as decimal(3,1))",
4617  Decimal4Value(-999), ColumnType::CreateDecimalType(3, 1));
4618 
4619  TestDecimalValue("cast(999.99 as decimal(6,3))",
4620  Decimal4Value(999990), ColumnType::CreateDecimalType(6, 3));
4621  TestDecimalValue("cast(-999.99 as decimal(7,4))",
4622  Decimal4Value(-9999900), ColumnType::CreateDecimalType(7, 4));
4623  TestIsNull("cast(9990.99 as decimal(6,3))", ColumnType::CreateDecimalType(6, 3));
4624  TestIsNull("cast(-9990.99 as decimal(7,4))", ColumnType::CreateDecimalType(7, 4));
4625 
4626  TestDecimalValue("cast(123.4567890 as decimal(4, 1))",
4627  Decimal4Value(1234), ColumnType::CreateDecimalType(4, 1));
4628  TestDecimalValue("cast(-123.4567890 as decimal(5, 2))",
4629  Decimal4Value(-12345), ColumnType::CreateDecimalType(5, 2));
4630  TestIsNull("cast(123.4567890 as decimal(2, 1))", ColumnType::CreateDecimalType(2, 1));
4631  TestIsNull("cast(123.4567890 as decimal(6, 5))", ColumnType::CreateDecimalType(6, 5));
4632 
4633  TestDecimalValue("cast(pi() as decimal(1, 0))",
4634  Decimal4Value(3), ColumnType::CreateDecimalType(1,0));
4635  TestDecimalValue("cast(pi() as decimal(4, 1))",
4636  Decimal4Value(31), ColumnType::CreateDecimalType(4,1));
4637  TestDecimalValue("cast(pi() as decimal(30, 1))",
4638  Decimal8Value(31), ColumnType::CreateDecimalType(30,1));
4639  TestIsNull("cast(pi() as decimal(4, 4))", ColumnType::CreateDecimalType(4, 4));
4640  TestIsNull("cast(pi() as decimal(11, 11))", ColumnType::CreateDecimalType(11, 11));
4641  TestIsNull("cast(pi() as decimal(31, 31))", ColumnType::CreateDecimalType(31, 31));
4642 
4643  TestIsNull("cast(140573315541874605.4665184383287 as decimal(17, 13))",
4644  ColumnType::CreateDecimalType(17, 13));
4645  TestIsNull("cast(140573315541874605.4665184383287 as decimal(9, 3))",
4646  ColumnType::CreateDecimalType(17, 13));
4647 
4648  // value has 30 digits before the decimal, casting to 29 is an overflow.
4649  TestIsNull("cast(99999999999999999999999999999.9 as decimal(29, 1))",
4650  ColumnType::CreateDecimalType(29, 1));
4651 
4652  // Tests converting a non-trivial empty string to a decimal (IMPALA-1566).
4653  TestIsNull("cast(regexp_replace('','a','b') as decimal(15,2))",
4654  ColumnType::CreateDecimalType(15,2));
4655 }
4656 
4657 TEST_F(ExprTest, NullValueFunction) {
4658  TestValue("nullvalue(cast(NULL as boolean))", TYPE_BOOLEAN, true);
4659  TestValue("nullvalue(cast(0 as boolean))", TYPE_BOOLEAN, false);
4660  TestValue("nullvalue(cast(5 as boolean))", TYPE_BOOLEAN, false);
4661  TestValue("nullvalue(cast(-5 as boolean))", TYPE_BOOLEAN, false);
4662  TestValue("nullvalue(cast(NULL as int))", TYPE_BOOLEAN, true);
4663 
4664  TestValue("nullvalue(cast(0 as int))", TYPE_BOOLEAN, false);
4665  TestValue("nullvalue(cast(5 as int))", TYPE_BOOLEAN, false);
4666  TestValue("nullvalue(cast(-5 as int))", TYPE_BOOLEAN, false);
4667 
4668  TestValue("nullvalue(cast(NULL as tinyint))", TYPE_BOOLEAN, true);
4669  TestValue("nullvalue(cast(0 as tinyint))", TYPE_BOOLEAN, false);
4670  TestValue("nullvalue(cast(5 as tinyint))", TYPE_BOOLEAN, false);
4671  TestValue("nullvalue(cast(-5 as tinyint))", TYPE_BOOLEAN, false);
4672 
4673  TestValue("nullvalue(cast(NULL as smallint))", TYPE_BOOLEAN, true);
4674  TestValue("nullvalue(cast(0 as smallint))", TYPE_BOOLEAN, false);
4675  TestValue("nullvalue(cast(5 as smallint))", TYPE_BOOLEAN, false);
4676  TestValue("nullvalue(cast(-5 as smallint))", TYPE_BOOLEAN, false);
4677 
4678  TestValue("nullvalue(cast(NULL as bigint))", TYPE_BOOLEAN, true);
4679  TestValue("nullvalue(cast(0 as bigint))", TYPE_BOOLEAN, false);
4680  TestValue("nullvalue(cast(5 as bigint))", TYPE_BOOLEAN, false);
4681  TestValue("nullvalue(cast(-5 as bigint))", TYPE_BOOLEAN, false);
4682 
4683  TestValue("nullvalue(cast(NULL as float))", TYPE_BOOLEAN, true);
4684  TestValue("nullvalue(cast(0 as float))", TYPE_BOOLEAN, false);
4685  TestValue("nullvalue(cast(5.0 as float))", TYPE_BOOLEAN, false);
4686  TestValue("nullvalue(cast(-5.0 as float))", TYPE_BOOLEAN, false);
4687 
4688  TestValue("nullvalue(cast(NULL as double))", TYPE_BOOLEAN, true);
4689  TestValue("nullvalue(cast(0.0 as double))", TYPE_BOOLEAN, false);
4690  TestValue("nullvalue(cast(5.0 as double))", TYPE_BOOLEAN, false);
4691  TestValue("nullvalue(cast(-5.0 as double))", TYPE_BOOLEAN, false);
4692 
4693  TestValue("nullvalue(cast(NULL as decimal(38,0)))", TYPE_BOOLEAN, true);
4694  TestValue("nullvalue(cast(0 as decimal(38,0)))", TYPE_BOOLEAN, false);
4695  TestValue("nullvalue(cast(5 as decimal(38,0)))", TYPE_BOOLEAN, false);
4696  TestValue("nullvalue(cast(-5 as decimal(38,0)))", TYPE_BOOLEAN, false);
4697  TestValue("nullvalue(cast(0.0 as decimal(38,38)))", TYPE_BOOLEAN, false);
4698  TestValue("nullvalue(cast(0.1 as decimal(38,38)))", TYPE_BOOLEAN, false);
4699  TestValue("nullvalue(cast(-0.1 as decimal(38,38)))", TYPE_BOOLEAN, false);
4700 
4701  TestValue("nullvalue(cast(NULL as string))", TYPE_BOOLEAN, true);
4702  TestValue("nullvalue(cast('0' as string))", TYPE_BOOLEAN, false);
4703  TestValue("nullvalue(cast('5' as string))", TYPE_BOOLEAN, false);
4704  TestValue("nullvalue(cast('-5' as string))", TYPE_BOOLEAN, false);
4705  TestValue("nullvalue(cast(\"abc\" as string))", TYPE_BOOLEAN, false);
4706  TestValue("nullvalue(cast(\"\" as string))", TYPE_BOOLEAN, false);
4707 
4708  TestValue("nullvalue(cast(NULL as timestamp))", TYPE_BOOLEAN, true);
4709  TestValue("nullvalue(cast('2012-01-01 09:10:11.123456789' as timestamp))",
4710  TYPE_BOOLEAN, false);
4711 
4712  TestValue("nullvalue(0)", TYPE_BOOLEAN, false);
4713  TestValue("nullvalue(-5)", TYPE_BOOLEAN, false);
4714  TestValue("nullvalue(5)", TYPE_BOOLEAN, false);
4715  TestValue("nullvalue(0.0)", TYPE_BOOLEAN, false);
4716  TestValue("nullvalue(-1.2345)", TYPE_BOOLEAN, false);
4717  TestValue("nullvalue(1.2345)", TYPE_BOOLEAN, false);
4718  TestValue("nullvalue(\"\")", TYPE_BOOLEAN, false);
4719  TestValue("nullvalue(\"abc\")", TYPE_BOOLEAN, false);
4720 
4721  TestValue("nullvalue(99999999999999999999999999999999999)", TYPE_BOOLEAN, false);
4722  TestValue("nullvalue(-99999999999999999999999999999999999)", TYPE_BOOLEAN, false);
4723  TestValue("nullvalue(99999999999999999999999999999999999.9)", TYPE_BOOLEAN, false);
4724  TestValue("nullvalue(-99999999999999999999999999999999999.9)", TYPE_BOOLEAN, false);
4725 }
4726 
4727 TEST_F(ExprTest, NonNullValueFunction) {
4728  TestValue("nonnullvalue(cast(NULL as boolean))", TYPE_BOOLEAN, false);
4729  TestValue("nonnullvalue(cast(0 as boolean))", TYPE_BOOLEAN, true);
4730  TestValue("nonnullvalue(cast(5 as boolean))", TYPE_BOOLEAN, true);
4731  TestValue("nonnullvalue(cast(-5 as boolean))", TYPE_BOOLEAN, true);
4732 
4733  TestValue("nonnullvalue(cast(NULL as int))", TYPE_BOOLEAN, false);
4734  TestValue("nonnullvalue(cast(0 as int))", TYPE_BOOLEAN, true);
4735  TestValue("nonnullvalue(cast(5 as int))", TYPE_BOOLEAN, true);
4736  TestValue("nonnullvalue(cast(-5 as int))", TYPE_BOOLEAN, true);
4737 
4738  TestValue("nonnullvalue(cast(NULL as tinyint))", TYPE_BOOLEAN, false);
4739  TestValue("nonnullvalue(cast(0 as tinyint))", TYPE_BOOLEAN, true);
4740  TestValue("nonnullvalue(cast(5 as tinyint))", TYPE_BOOLEAN, true);
4741  TestValue("nonnullvalue(cast(-5 as tinyint))", TYPE_BOOLEAN, true);
4742 
4743  TestValue("nonnullvalue(cast(NULL as smallint))", TYPE_BOOLEAN, false);
4744  TestValue("nonnullvalue(cast(0 as smallint))", TYPE_BOOLEAN, true);
4745  TestValue("nonnullvalue(cast(5 as smallint))", TYPE_BOOLEAN, true);
4746  TestValue("nonnullvalue(cast(-5 as smallint))", TYPE_BOOLEAN, true);
4747 
4748  TestValue("nonnullvalue(cast(NULL as bigint))", TYPE_BOOLEAN, false);
4749  TestValue("nonnullvalue(cast(0 as bigint))", TYPE_BOOLEAN, true);
4750  TestValue("nonnullvalue(cast(5 as bigint))", TYPE_BOOLEAN, true);
4751  TestValue("nonnullvalue(cast(-5 as bigint))", TYPE_BOOLEAN, true);
4752 
4753  TestValue("nonnullvalue(cast(NULL as float))", TYPE_BOOLEAN, false);
4754  TestValue("nonnullvalue(cast(0 as float))", TYPE_BOOLEAN, true);
4755  TestValue("nonnullvalue(cast(5.0 as float))", TYPE_BOOLEAN, true);
4756  TestValue("nonnullvalue(cast(-5.0 as float))", TYPE_BOOLEAN, true);
4757 
4758  TestValue("nonnullvalue(cast(NULL as double))", TYPE_BOOLEAN, false);
4759  TestValue("nonnullvalue(cast(0.0 as double))", TYPE_BOOLEAN, true);
4760  TestValue("nonnullvalue(cast(5.0 as double))", TYPE_BOOLEAN, true);
4761  TestValue("nonnullvalue(cast(-5.0 as double))", TYPE_BOOLEAN, true);
4762 
4763  TestValue("nonnullvalue(cast(NULL as decimal(38,0)))", TYPE_BOOLEAN, false);
4764  TestValue("nonnullvalue(cast(0 as decimal(38,0)))", TYPE_BOOLEAN, true);
4765  TestValue("nonnullvalue(cast(5 as decimal(38,0)))", TYPE_BOOLEAN, true);
4766  TestValue("nonnullvalue(cast(-5 as decimal(38,0)))", TYPE_BOOLEAN, true);
4767  TestValue("nonnullvalue(cast(0.0 as decimal(38,38)))", TYPE_BOOLEAN, true);
4768  TestValue("nonnullvalue(cast(0.1 as decimal(38,38)))", TYPE_BOOLEAN, true);
4769  TestValue("nonnullvalue(cast(-0.1 as decimal(38,38)))", TYPE_BOOLEAN, true);
4770 
4771  TestValue("nonnullvalue(cast(NULL as string))", TYPE_BOOLEAN, false);
4772  TestValue("nonnullvalue(cast('0' as string))", TYPE_BOOLEAN, true);
4773  TestValue("nonnullvalue(cast('5' as string))", TYPE_BOOLEAN, true);
4774  TestValue("nonnullvalue(cast('-5' as string))", TYPE_BOOLEAN, true);
4775  TestValue("nonnullvalue(cast(\"abc\" as string))", TYPE_BOOLEAN, true);
4776  TestValue("nonnullvalue(cast(\"\" as string))", TYPE_BOOLEAN, true);
4777 
4778  TestValue("nonnullvalue(cast(NULL as timestamp))", TYPE_BOOLEAN, false);
4779  TestValue("nonnullvalue(cast('2012-01-01 09:10:11.123456789' as timestamp))",
4780  TYPE_BOOLEAN, true);
4781 
4782  TestValue("nonnullvalue(0)", TYPE_BOOLEAN, true);
4783  TestValue("nonnullvalue(-5)", TYPE_BOOLEAN, true);
4784  TestValue("nonnullvalue(5)", TYPE_BOOLEAN, true);
4785  TestValue("nonnullvalue(0.0)", TYPE_BOOLEAN, true);
4786  TestValue("nonnullvalue(-1.2345)", TYPE_BOOLEAN, true);
4787  TestValue("nonnullvalue(1.2345)", TYPE_BOOLEAN, true);
4788  TestValue("nonnullvalue(\"\")", TYPE_BOOLEAN, true);
4789  TestValue("nonnullvalue(\"abc\")", TYPE_BOOLEAN, true);
4790 
4791  TestValue("nonnullvalue(99999999999999999999999999999999999)", TYPE_BOOLEAN, true);
4792  TestValue("nonnullvalue(-99999999999999999999999999999999999)", TYPE_BOOLEAN, true);
4793  TestValue("nonnullvalue(99999999999999999999999999999999999.9)", TYPE_BOOLEAN, true);
4794  TestValue("nonnullvalue(-99999999999999999999999999999999999.9)", TYPE_BOOLEAN, true);
4795 }
4796 
4797 TEST_F(ExprTest, UdfInterfaceBuiltins) {
4798  TestValue("udf_pi()", TYPE_DOUBLE, M_PI);
4799  TestValue("udf_abs(-1)", TYPE_DOUBLE, 1.0);
4800  TestStringValue("udf_lower('Hello_WORLD')", "hello_world");
4801 
4802  TestValue("max_tinyint()", TYPE_TINYINT, numeric_limits<int8_t>::max());
4803  TestValue("max_smallint()", TYPE_SMALLINT, numeric_limits<int16_t>::max());
4804  TestValue("max_int()", TYPE_INT, numeric_limits<int32_t>::max());
4805  TestValue("max_bigint()", TYPE_BIGINT, numeric_limits<int64_t>::max());
4806 
4807  TestValue("min_tinyint()", TYPE_TINYINT, numeric_limits<int8_t>::min());
4808  TestValue("min_smallint()", TYPE_SMALLINT, numeric_limits<int16_t>::min());
4809  TestValue("min_int()", TYPE_INT, numeric_limits<int32_t>::min());
4810  TestValue("min_bigint()", TYPE_BIGINT, numeric_limits<int64_t>::min());
4811 }
4812 
4813 TEST_F(ExprTest, MADlib) {
4814  TestStringValue("madlib_encode_vector(madlib_vector(1.0, 2.0, 3.0))",
4815  "aaaaaipdaaaaaaaeaaaaaeae");
4816  TestStringValue("madlib_print_vector(madlib_vector(1, 2, 3))", "<1, 2, 3>");
4817  TestStringValue(
4818  "madlib_encode_vector(madlib_decode_vector(madlib_encode_vector("
4819  "madlib_vector(1.0, 2.0, 3.0))))",
4820  "aaaaaipdaaaaaaaeaaaaaeae");
4821  TestValue("madlib_vector_get(0, madlib_vector(1.0, 2.0, 3.0))", TYPE_DOUBLE, 1.0);
4822  TestValue("madlib_vector_get(1, madlib_vector(1.0, 2.0, 3.0))", TYPE_DOUBLE, 2.0);
4823  TestValue("madlib_vector_get(2, madlib_vector(1.0, 2.0, 3.0))", TYPE_DOUBLE, 3.0);
4824  TestIsNull("madlib_vector_get(3, madlib_vector(1.0, 2.0, 3.0))", TYPE_DOUBLE);
4825  TestIsNull("madlib_vector_get(-1, madlib_vector(1.0, 2.0, 3.0))", TYPE_DOUBLE);
4826  TestValue(
4827  "madlib_vector_get(2, madlib_decode_vector(madlib_encode_vector("
4828  "madlib_vector(1.0, 2.0, 3.0))))",
4829  TYPE_DOUBLE, 3.0);
4830 }
4831 
4832 } // namespace impala
4833 
4834 int main(int argc, char **argv) {
4835  ::testing::InitGoogleTest(&argc, argv);
4836  InitCommonRuntime(argc, argv, true, TestInfo::BE_TEST);
4837  InitFeSupport();
4839 
4840  // Disable llvm optimization passes if the env var is no set to true. Running without
4841  // the optimizations makes the tests run much faster.
4842  char* optimizations = getenv("EXPR_TEST_ENABLE_OPTIMIZATIONS");
4843  if (optimizations != NULL && strcmp(optimizations, "true") == 0) {
4844  cout << "Running with optimization passes." << endl;
4845  FLAGS_disable_optimization_passes = false;
4846  } else {
4847  cout << "Running without optimization passes." << endl;
4848  FLAGS_disable_optimization_passes = true;
4849  }
4850 
4851  // Create an in-process Impala server and in-process backends for test environment
4852  // without any startup validation check
4853  FLAGS_impalad = "localhost:21000";
4854  FLAGS_abort_on_config_error = false;
4855  VLOG_CONNECTION << "creating test env";
4856  VLOG_CONNECTION << "starting backends";
4857  InProcessImpalaServer* impala_server =
4858  new InProcessImpalaServer("localhost", FLAGS_be_port, 0, 0, "", 0);
4859  EXIT_IF_ERROR(
4860  impala_server->StartWithClientServers(FLAGS_beeswax_port, FLAGS_beeswax_port + 1,
4861  false));
4862  impala_server->SetCatalogInitialized();
4865 
4866  vector<string> options;
4867  options.push_back("DISABLE_CODEGEN=1");
4868  disable_codegen_ = true;
4869  executor_->setExecOptions(options);
4870 
4871  cout << "Running without codegen" << endl;
4872  int ret = RUN_ALL_TESTS();
4873  if (ret != 0) return ret;
4874 
4875  options.push_back("DISABLE_CODEGEN=0");
4876  disable_codegen_ = false;
4877  executor_->setExecOptions(options);
4878  cout << endl << "Running with codegen" << endl;
4879  return RUN_ALL_TESTS();
4880 }
Status Exec(const std::string &query_string, std::vector< Apache::Hadoop::Hive::FieldSchema > *col_types)
void TestTimestampUnixEpochConversions(int64_t unix_time_at_local_epoch, string local_time_at_unix_epoch)
Definition: expr-test.cc:332
string default_timestamp_str_
Definition: expr-test.cc:163
void TestVariableResultTypeIntOps(LeftOp a, RightOp b, const ColumnType &expected_type)
Definition: expr-test.cc:708
void TestNullOperandFixedResultTypeOps(NonNullOp op, const ColumnType &expected_type)
Definition: expr-test.cc:727
const std::string GetDetail() const
Definition: status.cc:184
bool disable_codegen_
Definition: expr-test.cc:72
void InitFeSupport()
Definition: fe-support.cc:346
void TestValidTimestampValue(const string &expr)
Definition: expr-test.cc:321
MemTracker tracker
string default_decimal_str_
Definition: expr-test.cc:164
This class handles the Like, Regexp, and Rlike predicates and uses the udf interface.
int128_t abs(const int128_t &x)
void GetValue(const string &expr, const ColumnType &expr_type, void **interpreted_value, bool expect_error=false)
Definition: expr-test.cc:217
void TestFixedPointComparisons(bool test_boundaries)
Definition: expr-test.cc:435
TEST_F(ExprTest, MADlib)
Definition: expr-test.cc:4813
DECLARE_int32(be_port)
DecimalValue< int128_t > Decimal16Value
string GetVersionString(bool compact)
Returns "<program short name> version <GetBuildVersion(compact)>".
Definition: debug-util.cc:239
void TestNullComparison(const string &op)
Definition: expr-test.cc:531
void InitCommonRuntime(int argc, char **argv, bool init_jvm, TestInfo::Mode m=TestInfo::NON_TEST)
Definition: init.cc:122
void TestTimestampValue(const string &expr, const TimestampValue &expected_result, const int64_t tolerance_in_seconds=0)
Definition: expr-test.cc:307
TimestampValue default_timestamp_val_
Definition: expr-test.cc:168
The materialized value returned by ExprContext::GetValue().
Definition: expr-value.h:25
void * GetValue(TupleRow *row)
bool HasDateOrTime() const
void TestCharValue(const string &expr, const string &expected_result, const ColumnType &type)
Definition: expr-test.cc:297
ExprValue expr_value_
Definition: expr-test.cc:171
void TestNullOperandsArithmeticOps()
Definition: expr-test.cc:764
int main(int argc, char **argv)
Definition: expr-test.cc:4834
string TypeToOdbcString(PrimitiveType t)
Definition: types.cc:96
void TestNonOkStatus(const string &expr)
Definition: expr-test.cc:428
string LiteralToString< float, double >(double val)
Definition: expr-test.cc:89
unordered_map< int, double > FloatValMap
Definition: expr-test.cc:153
void TestNullComparisons()
Definition: expr-test.cc:551
bool default_bool_val_
Definition: expr-test.cc:166
void TestStringValue(const string &expr, const string &expected_result)
Definition: expr-test.cc:290
Status Prepare(RuntimeState *state, const RowDescriptor &row_desc, MemTracker *tracker)
Definition: expr-context.cc:47
void TestComparison(const string &smaller, const string &larger, bool compare_strings)
Definition: expr-test.cc:562
string LiteralToString(VAL_TYPE val)
Definition: expr-test.cc:75
DecimalValue< int64_t > Decimal8Value
map< int, int64_t > IntValMap
Definition: expr-test.cc:148
Conditional functions that can be expressed as UDFs.
void TestDecimalValue(const string &expr, const T &expected_result, const ColumnType &expected_type)
Definition: expr-test.cc:363
PrimitiveType type
Definition: types.h:60
FloatValMap min_float_values_
Definition: expr-test.cc:154
void ValidateLayout(const vector< Expr * > &exprs, int expected_byte_size, int expected_var_begin, const map< int, set< int > > &expected_offsets)
Definition: expr-test.cc:3960
#define VLOG_CONNECTION
Definition: logging.h:55
string default_string_val_
Definition: expr-test.cc:167
void TestDecimalComparisons()
Definition: expr-test.cc:511
void TestFixedPointLimits(const ColumnType &type)
Definition: expr-test.cc:662
PrimitiveType
Definition: types.h:27
ObjectPool pool
DecimalValue< int32_t > Decimal4Value
void TestBinaryPredicates(const string &value, bool compare_strings)
Definition: expr-test.cc:613
IntValMap min_int_values_
Definition: expr-test.cc:149
int GetByteSize() const
Returns the byte size of this type. Returns 0 for variable length types.
Definition: types.h:178
string default_string_str_
Definition: expr-test.cc:162
#define EXIT_IF_ERROR(stmt)
Definition: status.h:248
This is the superclass of all expr evaluation nodes.
Definition: expr.h:116
This class is thread-safe.
Definition: mem-tracker.h:61
void TestSingleLiteralConstruction(const ColumnType &type, const T &value, const string &string_val)
Definition: expr-test.cc:821
void * ConvertValue(const ColumnType &type, const string &value)
Definition: expr-test.cc:233
void TestFixedResultTypeOps(LeftOp a, RightOp b, const ColumnType &expected_type)
Definition: expr-test.cc:689
string LiteralToString< float, float >(float val)
Definition: expr-test.cc:80
BooleanVal TestError(FunctionContext *context)
Definition: test-udfs.cc:141
void TestError(const string &expr)
Definition: expr-test.cc:423
TODO: Reconsider whether this class needs to exist.
uint64_t Test(T *ht, const ProbeTuple *input, uint64_t num_tuples)
void TestCast(const string &stmt, T val, bool timestamp_out_of_range=false)
Definition: expr-test.cc:779
Query execution against running impalad process.
DECLARE_bool(abort_on_config_error)
void TestFloatingPointLimits(const ColumnType &type)
Definition: expr-test.cc:671
void TestFloatingPointComparisons(bool test_boundaries)
Definition: expr-test.cc:451
static void InitializeLlvm(bool load_backend=false)
Definition: llvm-codegen.cc:78
void TestStringComparisons()
Definition: expr-test.cc:487
void setExecOptions(const std::vector< std::string > &exec_options)
Status StartWithClientServers(int beeswax_port, int hs2_port, bool use_statestore)
void TestNullOperandVariableResultTypeIntOps(NonNullOp op, const ColumnType &expected_type)
Definition: expr-test.cc:745
virtual void SetUp()
Definition: expr-test.cc:173
unordered_map< int, string > default_type_strs_
Definition: expr-test.cc:160
ScopedTimeZoneOverride(string time_zone)
Definition: expr-test.cc:111
ImpaladQueryExecutor * executor_
execution state of coordinator fragment
Definition: expr-test.cc:71
string LiteralToString< double, double >(double val)
Definition: expr-test.cc:98
DECLARE_string(impalad)
void TestValue(const string &expr, const ColumnType &expr_type, const T &expected_result)
Definition: expr-test.cc:373
Status Open(RuntimeState *state)
Must be called after calling Prepare(). Should not be called on clones.
Definition: expr-context.cc:56
bool ok() const
Definition: status.h:172
Predicate for evaluating expressions of the form "val [NOT] IN (x1, x2, x3...)".
Definition: in-predicate.h:42
time_t ToUnixTime() const
void Close(RuntimeState *state)
Closes all FunctionContexts. Must be called on every ExprContext, including clones.
Definition: expr-context.cc:67
Status FetchResult(RowBatch **batch)
string default_bool_str_
Definition: expr-test.cc:161
void TestIsNull(const string &expr, const ColumnType &expr_type)
Definition: expr-test.cc:417