Skip to content

Commit

Permalink
remove OVERFLOW_AS_WARNING flag (#9862)
Browse files Browse the repository at this point in the history
close #9752

Signed-off-by: guo-shaoge <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
guo-shaoge and ti-chi-bot[bot] authored Feb 11, 2025
1 parent 7694684 commit 607d850
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 46 deletions.
5 changes: 2 additions & 3 deletions dbms/src/Debug/dbgQueryCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ struct QueryFragment
tipb::DAGRequest & dag_request = *dag_request_ptr;
dag_request.set_time_zone_name(properties.tz_name);
dag_request.set_time_zone_offset(properties.tz_offset);
dag_request.set_flags(
dag_request.flags() | (1u << 1u /* TRUNCATE_AS_WARNING */) | (1u << 6u /* OVERFLOW_AS_WARNING */));
dag_request.set_flags(dag_request.flags() | (1u << 1u /* TRUNCATE_AS_WARNING */));
if (is_top_fragment)
{
if (properties.encode_type == "chunk")
Expand Down Expand Up @@ -179,4 +178,4 @@ QueryTasks queryPlanToQueryTasks(
const Context & context);

const ASTTablesInSelectQueryElement * getJoin(ASTSelectQuery & ast_query);
} // namespace DB
} // namespace DB
18 changes: 10 additions & 8 deletions dbms/src/Flash/Coprocessor/DAGContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,22 +358,24 @@ std::unordered_map<String, BlockInputStreams> & DAGContext::getInBoundIOInputStr
return inbound_io_input_streams_map;
}

void DAGContext::handleTruncateError(const String & msg)
void DAGContext::handleTruncateErrorInternal(const String & msg)
{
if (!(flags & TiDBSQLFlags::IGNORE_TRUNCATE || flags & TiDBSQLFlags::TRUNCATE_AS_WARNING))
{
throw TiFlashException("Truncate error " + msg, Errors::Types::Truncated);
throw TiFlashException(msg, Errors::Types::Truncated);
}
appendWarning(msg);
}

void DAGContext::handleOverflowError(const String & msg, const TiFlashError & error)
void DAGContext::handleTruncateError(const String & msg)
{
if (!(flags & TiDBSQLFlags::OVERFLOW_AS_WARNING))
{
throw TiFlashException("Overflow error: " + msg, error);
}
appendWarning("Overflow error: " + msg);
handleTruncateErrorInternal("Truncate error " + msg);
}

void DAGContext::handleOverflowError(const String & msg)
{
// TiDB removed OverflowAsWarning flag in tidb/pull/49122.
handleTruncateErrorInternal("Overflow error " + msg);
}

void DAGContext::handleDivisionByZero()
Expand Down
6 changes: 4 additions & 2 deletions dbms/src/Flash/Coprocessor/DAGContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ constexpr UInt64 PAD_CHAR_TO_FULL_LENGTH = 1u << 2u;
constexpr UInt64 IN_INSERT_STMT = 1u << 3u;
constexpr UInt64 IN_UPDATE_OR_DELETE_STMT = 1u << 4u;
constexpr UInt64 IN_SELECT_STMT = 1u << 5u;
constexpr UInt64 OVERFLOW_AS_WARNING = 1u << 6u;
// TiDB removed OverflowAsWarning flag in tidb/pull/49122.
// constexpr UInt64 OVERFLOW_AS_WARNING = 1u << 6u;
constexpr UInt64 IGNORE_ZERO_IN_DATE = 1u << 7u;
constexpr UInt64 DIVIDED_BY_ZERO_AS_WARNING = 1u << 8u;
constexpr UInt64 IN_LOAD_DATA_STMT = 1u << 10u;
Expand Down Expand Up @@ -210,7 +211,7 @@ class DAGContext
bool is_append = false);

void handleTruncateError(const String & msg);
void handleOverflowError(const String & msg, const TiFlashError & error);
void handleOverflowError(const String & msg);
void handleDivisionByZero();
void handleInvalidTime(const String & msg, const TiFlashError & error);
void appendWarning(const String & msg, int32_t code = 0);
Expand Down Expand Up @@ -408,6 +409,7 @@ class DAGContext
void initExecutorIdToJoinIdMap();
void initOutputInfo();
tipb::EncodeType analyzeDAGEncodeType() const;
void handleTruncateErrorInternal(const String & msg);

private:
std::shared_ptr<ProcessListEntry> process_list_entry;
Expand Down
8 changes: 4 additions & 4 deletions dbms/src/Flash/Coprocessor/tests/gtest_dag_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ TEST(DAGContextTest, FlagsTest)
context.setFlags(f);
ASSERT_EQ(context.getFlags(), f);

UInt64 f1 = f | TiDBSQLFlags::OVERFLOW_AS_WARNING;
context.addFlag(TiDBSQLFlags::OVERFLOW_AS_WARNING);
UInt64 f1 = f | TiDBSQLFlags::TRUNCATE_AS_WARNING;
context.addFlag(TiDBSQLFlags::TRUNCATE_AS_WARNING);
ASSERT_EQ(context.getFlags(), f1);

context.delFlag(TiDBSQLFlags::OVERFLOW_AS_WARNING);
ASSERT_EQ(context.getFlags(), f);
context.delFlag(TiDBSQLFlags::TRUNCATE_AS_WARNING);
ASSERT_EQ(context.getFlags(), TiDBSQLFlags::IN_LOAD_DATA_STMT);
}

} // namespace tests
Expand Down
46 changes: 22 additions & 24 deletions dbms/src/Functions/FunctionsTiDBConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,20 +295,20 @@ struct TiDBConvertToInteger
T rounded_value = std::round(value);
if (rounded_value < 0)
{
context.getDAGContext()->handleOverflowError("Cast real as integer", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("Cast real as integer");
if (context.getDAGContext()->shouldClipToZero())
return static_cast<ToFieldType>(0);
return static_cast<ToFieldType>(rounded_value);
}
auto field_max = static_cast<T>(std::numeric_limits<ToFieldType>::max());
if (rounded_value > field_max)
{
context.getDAGContext()->handleOverflowError("Cast real as integer", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("Cast real as integer");
return std::numeric_limits<ToFieldType>::max();
}
else if (rounded_value == field_max)
{
context.getDAGContext()->handleOverflowError("cast real as int", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast real as int");
return std::numeric_limits<ToFieldType>::max();
}
else
Expand All @@ -323,12 +323,12 @@ struct TiDBConvertToInteger
auto field_max = static_cast<T>(std::numeric_limits<ToFieldType>::max());
if (rounded_value < field_min)
{
context.getDAGContext()->handleOverflowError("cast real as int", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast real as int");
return std::numeric_limits<ToFieldType>::min();
}
if (rounded_value >= field_max)
{
context.getDAGContext()->handleOverflowError("cast real as int", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast real as int");
return std::numeric_limits<ToFieldType>::max();
}
return static_cast<ToFieldType>(rounded_value);
Expand All @@ -340,7 +340,7 @@ struct TiDBConvertToInteger
auto v = value.getValue().value;
if (v < 0)
{
context.getDAGContext()->handleOverflowError("cast decimal as int", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast decimal as int");
return static_cast<ToFieldType>(0);
}
ScaleType scale = value.getScale();
Expand All @@ -352,7 +352,7 @@ struct TiDBConvertToInteger
Int128 max_value = std::numeric_limits<ToFieldType>::max();
if (v > max_value)
{
context.getDAGContext()->handleOverflowError("cast decimal as int", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast decimal as int");
return max_value;
}
return static_cast<ToFieldType>(v);
Expand All @@ -369,7 +369,7 @@ struct TiDBConvertToInteger
}
if (v > std::numeric_limits<ToFieldType>::max() || v < std::numeric_limits<ToFieldType>::min())
{
context.getDAGContext()->handleOverflowError("cast decimal as int", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast decimal as int");
if (v > 0)
return std::numeric_limits<ToFieldType>::max();
return std::numeric_limits<ToFieldType>::min();
Expand Down Expand Up @@ -476,15 +476,15 @@ struct TiDBConvertToInteger
{
auto [value, err] = toUInt<T>(int_string);
if (err == OVERFLOW_ERR)
context.getDAGContext()->handleOverflowError("cast str as int", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast str as int");
return static_cast<T>(value);
}
else
{
/// TODO: append warning CastAsSignedOverflow if try to cast negative value to unsigned
auto [value, err] = toInt<T>(int_string);
if (err == OVERFLOW_ERR)
context.getDAGContext()->handleOverflowError("cast str as int", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast str as int");
return static_cast<T>(value);
}
}
Expand Down Expand Up @@ -632,20 +632,20 @@ struct TiDBConvertToFloat
value = std::round(value) / shift;
if (value > max_f)
{
context.getDAGContext()->handleOverflowError("cast as real", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast as real");
value = max_f;
}
if (value < -max_f)
{
context.getDAGContext()->handleOverflowError("cast as real", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast as real");
value = -max_f;
}
}
if constexpr (to_unsigned)
{
if (value < 0)
{
context.getDAGContext()->handleOverflowError("cast as real", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast as real");
value = 0;
}
}
Expand Down Expand Up @@ -743,12 +743,12 @@ struct TiDBConvertToFloat
Float64 f = strtod(float_string.data, nullptr);
if (f == std::numeric_limits<Float64>::infinity())
{
context.getDAGContext()->handleOverflowError("Truncated incorrect DOUBLE value", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("Truncated incorrect DOUBLE value");
return std::numeric_limits<Float64>::max();
}
if (f == -std::numeric_limits<double>::infinity())
{
context.getDAGContext()->handleOverflowError("Truncated incorrect DOUBLE value", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("Truncated incorrect DOUBLE value");
return -std::numeric_limits<Float64>::max();
}
return produceTargetFloat64(f, need_truncate, shift, max_f, context);
Expand Down Expand Up @@ -981,7 +981,7 @@ struct TiDBConvertToDecimal
auto max_value = DecimalMaxValue::get(prec);
if (value > static_cast<Float64>(max_value))
{
context.getDAGContext()->handleOverflowError("cast real to decimal", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast real to decimal");
if (!neg)
return static_cast<UType>(max_value);
else
Expand Down Expand Up @@ -1116,7 +1116,7 @@ struct TiDBConvertToDecimal
if (err == OVERFLOW_ERR || frac_offset_by_exponent > std::numeric_limits<Int32>::max() / 2
|| frac_offset_by_exponent < std::numeric_limits<Int32>::min() / 2)
{
context.getDAGContext()->handleOverflowError("cast string as decimal", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast string as decimal");
if (decimal_parts.exp_part.data[0] == '-')
return static_cast<UType>(0);
else
Expand Down Expand Up @@ -1146,7 +1146,7 @@ struct TiDBConvertToDecimal
v = v * 10 + decimal_parts.int_part.data[pos] - '0';
if (v > max_value)
{
context.getDAGContext()->handleOverflowError("cast string as decimal", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast string as decimal");
return static_cast<UType>(is_negative ? -max_value : max_value);
}
current_scale++;
Expand Down Expand Up @@ -1178,7 +1178,7 @@ struct TiDBConvertToDecimal
v = v * 10 + decimal_parts.frac_part.data[pos] - '0';
if (v > max_value)
{
context.getDAGContext()->handleOverflowError("cast string as decimal", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast string as decimal");
return static_cast<UType>(is_negative ? -max_value : max_value);
}
current_scale++;
Expand All @@ -1197,9 +1197,7 @@ struct TiDBConvertToDecimal
v *= 10;
if (v > max_value)
{
context.getDAGContext()->handleOverflowError(
"cast string as decimal",
Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast string as decimal");
return static_cast<UType>(is_negative ? -max_value : max_value);
}
current_scale++;
Expand All @@ -1209,7 +1207,7 @@ struct TiDBConvertToDecimal

if (v > max_value)
{
context.getDAGContext()->handleOverflowError("cast string as decimal", Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError("cast string as decimal");
return static_cast<UType>(is_negative ? -max_value : max_value);
}
return static_cast<UType>(is_negative ? -v : v);
Expand Down Expand Up @@ -1361,7 +1359,7 @@ struct TiDBConvertToDecimal
{
if (to_value > max_value || to_value < -max_value)
{
context.getDAGContext()->handleOverflowError(msg, Errors::Types::Truncated);
context.getDAGContext()->handleOverflowError(msg);
if (to_value > 0)
return static_cast<ReturnType>(max_value);
else
Expand Down
1 change: 0 additions & 1 deletion dbms/src/Functions/tests/bench_function_cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ class CastToDecimalBench : public benchmark::Fixture
auto context = DB::tests::TiFlashTestEnv::getContext(); \
auto dag_context_ptr = std::make_unique<DAGContext>(1024); \
UInt64 ori_flags = dag_context_ptr->getFlags(); \
dag_context_ptr->addFlag(TiDBSQLFlags::OVERFLOW_AS_WARNING); \
dag_context_ptr->addFlag(TiDBSQLFlags::TRUNCATE_AS_WARNING); \
dag_context_ptr->clearWarnings(); \
context->setDAGContext(dag_context_ptr.get()); \
Expand Down
4 changes: 2 additions & 2 deletions dbms/src/Functions/tests/gtest_tidb_conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ try

auto & dag_context = getDAGContext();
UInt64 ori_flags = dag_context.getFlags();
dag_context.addFlag(TiDBSQLFlags::OVERFLOW_AS_WARNING);
dag_context.addFlag(TiDBSQLFlags::TRUNCATE_AS_WARNING);
dag_context.clearWarnings();

ASSERT_COLUMN_EQ(
Expand Down Expand Up @@ -2258,7 +2258,7 @@ try
// from_prec(3) + to_scale(7) > Int32::real_prec(10) - 1, so CastInternalType should be **Int64**.
auto & dag_context = getDAGContext();
UInt64 ori_flags = dag_context.getFlags();
dag_context.addFlag(TiDBSQLFlags::OVERFLOW_AS_WARNING);
dag_context.addFlag(TiDBSQLFlags::TRUNCATE_AS_WARNING);
dag_context.clearWarnings();
ASSERT_COLUMN_EQ(
createColumn<Nullable<Decimal32>>(
Expand Down
3 changes: 1 addition & 2 deletions dbms/src/TestUtils/mockExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ void DAGRequestBuilder::initDAGRequest(tipb::DAGRequest & dag_request)
{
dag_request.set_time_zone_name(properties.tz_name);
dag_request.set_time_zone_offset(properties.tz_offset);
dag_request.set_flags(
dag_request.flags() | (1u << 1u /* TRUNCATE_AS_WARNING */) | (1u << 6u /* OVERFLOW_AS_WARNING */));
dag_request.set_flags(dag_request.flags() | (1u << 1u /* TRUNCATE_AS_WARNING */));

if (properties.encode_type == "chunk")
dag_request.set_encode_type(tipb::EncodeType::TypeChunk);
Expand Down

0 comments on commit 607d850

Please sign in to comment.