diff --git a/dbms/src/Functions/FunctionsJson.h b/dbms/src/Functions/FunctionsJson.h index 587bb933b15..c8b7c80e586 100644 --- a/dbms/src/Functions/FunctionsJson.h +++ b/dbms/src/Functions/FunctionsJson.h @@ -787,11 +787,12 @@ class FunctionCastDecimalAsJson : public IFunction JsonBinary::JsonBinaryWriteBuffer write_buffer(data_to, reserve_size); for (size_t i = 0; i < column_from->size(); ++i) { - const auto & field = (*column_from)[i].template safeGet>(); // same as https://github.com/pingcap/tidb/blob/90628349860718bb84c94fe7dc1e1f9bd9da4348/pkg/expression/builtin_cast.go#L854-L865 // https://github.com/pingcap/tidb/issues/48796 // TODO `select json_type(cast(1111.11 as json))` should return `DECIMAL`, we return `DOUBLE` now. - JsonBinary::appendNumber(write_buffer, static_cast(field)); + JsonBinary::appendNumber( + write_buffer, + static_cast((*column_from)[i].template safeGet>())); writeChar(0, write_buffer); offsets_to[i] = write_buffer.count(); } diff --git a/dbms/src/Functions/tests/gtest_cast_as_json.cpp b/dbms/src/Functions/tests/gtest_cast_as_json.cpp index 93cefdccedc..3e8149aa6e6 100644 --- a/dbms/src/Functions/tests/gtest_cast_as_json.cpp +++ b/dbms/src/Functions/tests/gtest_cast_as_json.cpp @@ -197,28 +197,28 @@ try using DecimalField256 = DecimalField; /// Decimal32 - executeAndAssert(func_name, DecimalField32(1011, 1), "101.1"); - executeAndAssert(func_name, DecimalField32(-1011, 1), "-101.1"); - executeAndAssert(func_name, DecimalField32(9999, 1), "999.9"); - executeAndAssert(func_name, DecimalField32(-9999, 1), "-999.9"); + executeAndAssert(func_name, DecimalField32(1011, 1), "101.1"); + executeAndAssert(func_name, DecimalField32(-1011, 1), "-101.1"); + executeAndAssert(func_name, DecimalField32(9999, 1), "999.9"); + executeAndAssert(func_name, DecimalField32(-9999, 1), "-999.9"); /// Decimal64 - executeAndAssert(func_name, DecimalField64(1011, 1), "101.1"); - executeAndAssert(func_name, DecimalField64(-1011, 1), "-101.1"); - executeAndAssert(func_name, DecimalField64(9999, 1), "999.9"); - executeAndAssert(func_name, DecimalField64(-9999, 1), "-999.9"); + executeAndAssert(func_name, DecimalField64(1011, 1), "101.1"); + executeAndAssert(func_name, DecimalField64(-1011, 1), "-101.1"); + executeAndAssert(func_name, DecimalField64(9999, 1), "999.9"); + executeAndAssert(func_name, DecimalField64(-9999, 1), "-999.9"); /// Decimal128 - executeAndAssert(func_name, DecimalField128(1011, 1), "101.1"); - executeAndAssert(func_name, DecimalField128(-1011, 1), "-101.1"); - executeAndAssert(func_name, DecimalField128(9999, 1), "999.9"); - executeAndAssert(func_name, DecimalField128(-9999, 1), "-999.9"); + executeAndAssert(func_name, DecimalField128(1011, 1), "101.1"); + executeAndAssert(func_name, DecimalField128(-1011, 1), "-101.1"); + executeAndAssert(func_name, DecimalField128(9999, 1), "999.9"); + executeAndAssert(func_name, DecimalField128(-9999, 1), "-999.9"); /// Decimal256 - executeAndAssert(func_name, DecimalField256(static_cast(1011), 1), "101.1"); - executeAndAssert(func_name, DecimalField256(static_cast(-1011), 1), "-101.1"); - executeAndAssert(func_name, DecimalField256(static_cast(9999), 1), "999.9"); - executeAndAssert(func_name, DecimalField256(static_cast(-9999), 1), "-999.9"); + executeAndAssert(func_name, DecimalField256(static_cast(1011), 1), "101.1"); + executeAndAssert(func_name, DecimalField256(static_cast(-1011), 1), "-101.1"); + executeAndAssert(func_name, DecimalField256(static_cast(9999), 1), "999.9"); + executeAndAssert(func_name, DecimalField256(static_cast(-9999), 1), "-999.9"); } CATCH @@ -322,20 +322,20 @@ try static auto const date_type_ptr = std::make_shared(); // DataTypeMyDateTime + // Only raw function test is tested, so input_tidb_tp is always nullptr and only the case of TiDB::TypeTimestamp is tested here. { auto data_col_ptr = createColumn( {MyDateTime(2023, 1, 2, 3, 4, 5, 6).toPackedUInt(), MyDateTime(0, 0, 0, 0, 0, 0, 0).toPackedUInt()}) .column; ColumnWithTypeAndName input(data_col_ptr, datetime_type_ptr, ""); - auto res = executeFunctionWithCast("cast_time_as_json", {input}); + auto res = executeFunctionWithCast("cast_time_as_json", {input}); auto expect = createColumn>({"\"2023-01-02 03:04:05.000006\"", "\"0000-00-00 00:00:00.000000\""}); ASSERT_COLUMN_EQ(expect, res); } // DataTypeMyDate - // Only raw function test is tested, so input_tidb_tp is always nullptr and only the case of TiDB::TypeTimestamp is tested here. { auto data_col_ptr = createColumn( {MyDate(2023, 12, 31).toPackedUInt(), MyDate(0, 0, 0).toPackedUInt()})