Skip to content

Commit

Permalink
[Opt](date_add/sub) Throw exception when result of date_add/sub out o…
Browse files Browse the repository at this point in the history
…f range (#26475)
  • Loading branch information
zhiqiang-hhhh authored and zclllyybb committed May 22, 2024
1 parent d53c38c commit a7308f5
Show file tree
Hide file tree
Showing 3 changed files with 400 additions and 6 deletions.
39 changes: 33 additions & 6 deletions be/src/vec/functions/function_date_or_datetime_computation.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <type_traits>
#include <utility>

#include "common/compiler_util.h"
#include "common/exception.h"
#include "common/logging.h"
#include "common/status.h"
#include "fmt/format.h"
Expand Down Expand Up @@ -413,7 +415,11 @@ struct DateTimeOp {
// otherwise it will be implicitly converted to bool, causing the rvalue to fail to match the lvalue.
// the same goes for the following.
vec_to[i] = Transform::execute(vec_from0[i], vec_from1[i], invalid);
DCHECK(!invalid);

if (UNLIKELY(invalid)) {
throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} {} {} out of range",
Transform::name, vec_from0[i], vec_from1[i]);
}
}
}

Expand All @@ -438,7 +444,11 @@ struct DateTimeOp {
bool invalid = true;
for (size_t i = 0; i < size; ++i) {
vec_to[i] = Transform::execute(vec_from0[i], vec_from1[i], invalid);
DCHECK(!invalid);

if (UNLIKELY(invalid)) {
throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} {} {} out of range",
Transform::name, vec_from0[i], vec_from1[i]);
}
}
}

Expand All @@ -462,7 +472,11 @@ struct DateTimeOp {
bool invalid = true;
for (size_t i = 0; i < size; ++i) {
vec_to[i] = Transform::execute(vec_from[i], delta, invalid);
DCHECK(!invalid);

if (UNLIKELY(invalid)) {
throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} {} {} out of range",
Transform::name, vec_from[i], delta);
}
}
}

Expand All @@ -486,7 +500,11 @@ struct DateTimeOp {

for (size_t i = 0; i < size; ++i) {
vec_to[i] = Transform::execute(vec_from[i], delta, invalid);
DCHECK(!invalid);

if (UNLIKELY(invalid)) {
throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} {} {} out of range",
Transform::name, vec_from[i], delta);
}
}
}

Expand All @@ -510,7 +528,11 @@ struct DateTimeOp {

for (size_t i = 0; i < size; ++i) {
vec_to[i] = Transform::execute(from, delta.get_int(i), invalid);
DCHECK(!invalid);

if (UNLIKELY(invalid)) {
throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} {} {} out of range",
Transform::name, from, delta.get_int(i));
}
}
}

Expand All @@ -524,6 +546,7 @@ struct DateTimeOp {
vec_to[i] = Transform::execute(from, delta[i], reinterpret_cast<bool&>(null_map[i]));
}
}

static void constant_vector(const FromType1& from, PaddedPODArray<ToType>& vec_to,
const PaddedPODArray<FromType2>& delta) {
size_t size = delta.size();
Expand All @@ -532,7 +555,11 @@ struct DateTimeOp {

for (size_t i = 0; i < size; ++i) {
vec_to[i] = Transform::execute(from, delta[i], invalid);
DCHECK(!invalid);

if (UNLIKELY(invalid)) {
throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} {} {} out of range",
Transform::name, from, delta[i]);
}
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select_nullable_1 --
\N \N \N
\N \N \N
9998-12-31 9998-12-31 9998-12-31T23:59:59
\N \N \N

-- !select_nullable_2 --
\N \N \N
\N \N \N
9999-11-30 9999-11-30 9999-11-30T23:59:59
\N \N \N

-- !select_nullable_3 --
\N \N \N
\N \N \N
9999-12-24 9999-12-24 9999-12-24T23:59:59
\N \N \N

-- !select_nullable_4 --
\N \N \N
\N \N \N
9999-12-30 9999-12-30 9999-12-30T23:59:59
\N \N \N

-- !select_nullable_5 --
\N \N \N
\N \N \N
9999-12-30T23:00 9999-12-30T23:00 9999-12-31T22:59:59
\N \N \N

-- !select_nullable_6 --
\N \N \N
\N \N \N
9999-12-30T23:59 9999-12-30T23:59 9999-12-31T23:58:59
\N \N \N

-- !select_nullable_7 --
\N \N \N
\N \N \N
9999-12-30T23:59:59 9999-12-30T23:59:59 9999-12-31T23:59:58
\N \N \N

-- !select_nullable_8 --
0001-01-01 0001-01-01 0001-01-01T00:00
\N \N \N
\N \N \N
\N \N \N

-- !select_nullable_9 --
0000-02-01 0000-02-01 0000-02-01T00:00
\N \N \N
\N \N \N
\N \N \N

-- !select_nullable_10 --
0000-01-08 0000-01-08 0000-01-08T00:00
\N \N \N
\N \N \N
\N \N \N

-- !select_nullable_11 --
0000-01-02 0000-01-02 0000-01-02T00:00
\N \N \N
\N \N \N
\N \N \N

-- !select_nullable_12 --
0000-01-01T01:00 0000-01-01T01:00 0000-01-01T01:00
\N \N \N
9999-12-31T01:00 9999-12-31T01:00 \N
\N \N \N

-- !select_nullable_13 --
0000-01-01T00:01 0000-01-01T00:01 0000-01-01T00:01
\N \N \N
9999-12-31T00:01 9999-12-31T00:01 \N
\N \N \N

-- !select_nullable_14 --
0000-01-01T00:00:01 0000-01-01T00:00:01 0000-01-01T00:00:01
\N \N \N
9999-12-31T00:00:01 9999-12-31T00:00:01 \N
\N \N \N

-- !select_nullable_15 --
0000-01-02T00:00 0000-01-02T00:00 0000-01-02T00:00
\N \N \N
\N \N \N
\N \N \N

Loading

0 comments on commit a7308f5

Please sign in to comment.