Skip to content

Commit

Permalink
ddl: fix TypeBit default value (#9467)
Browse files Browse the repository at this point in the history
close #9461

ddl: fix TypeBit default value

Signed-off-by: Lloyd-Pottiger <[email protected]>
  • Loading branch information
Lloyd-Pottiger authored Sep 26, 2024
1 parent d91bcde commit 1ce2f11
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 74 deletions.
18 changes: 9 additions & 9 deletions dbms/src/TiDB/Schema/TiDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,7 @@ Field ColumnInfo::defaultValueToField() const
});
case TypeBit:
{
// TODO: We shall use something like `orig_default_bit`, which will never change once created,
// rather than `default_bit`, which could be altered.
// See https://github.com/pingcap/tidb/issues/17641 and https://github.com/pingcap/tidb/issues/17642
const auto & bit_value = default_bit_value;
// TODO: There might be cases that `orig_default` is not null but `default_bit` is null,
// i.e. bit column added with an default value but later modified to another.
// For these cases, neither `orig_default` (may get corrupted) nor `default_bit` (modified) is correct.
// This is a bug anyway, we choose to make it simple, i.e. use `default_bit`.
const auto & bit_value = origin_default_bit_value;
if (bit_value.isEmpty())
{
if (hasNotNullFlag())
Expand Down Expand Up @@ -382,6 +375,7 @@ try
json->set("origin_default", origin_default_value);
json->set("default", default_value);
json->set("default_bit", default_bit_value);
json->set("origin_default_bit", origin_default_bit_value);
{
// "type" field
Poco::JSON::Object::Ptr tp_json = new Poco::JSON::Object();
Expand Down Expand Up @@ -433,6 +427,8 @@ try
default_value = json->get("default");
if (!json->isNull("default_bit"))
default_bit_value = json->get("default_bit");
if (!json->isNull("origin_default_bit"))
origin_default_bit_value = json->get("origin_default_bit");
{
// type
auto type_json = json->getObject("type");
Expand Down Expand Up @@ -1286,7 +1282,11 @@ ColumnInfo toTiDBColumnInfo(const tipb::ColumnInfo & tipb_column_info)
// TiFlash get default value from origin_default_value, check `Field ColumnInfo::defaultValueToField() const`
// So we need to set origin_default_value to tipb_column_info.default_val()
// Related logic in tidb, https://github.com/pingcap/tidb/blob/45318da24d8e4c0c6aab836d291a33f949dd18bf/pkg/table/tables/tables.go#L2303-L2329
tidb_column_info.origin_default_value = tipb_column_info.default_val();
// For TypeBit, we need to set origin_default_bit_value to tipb_column_info.default_val().
if (tidb_column_info.tp == TypeBit)
tidb_column_info.origin_default_bit_value = tipb_column_info.default_val();
else
tidb_column_info.origin_default_value = tipb_column_info.default_val();
tidb_column_info.collate = tipb_column_info.collation();
for (int i = 0; i < tipb_column_info.elems_size(); ++i)
tidb_column_info.elems.emplace_back(tipb_column_info.elems(i), i + 1);
Expand Down
1 change: 1 addition & 0 deletions dbms/src/TiDB/Schema/TiDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ struct ColumnInfo
Int32 offset = -1;
Poco::Dynamic::Var origin_default_value;
Poco::Dynamic::Var default_value;
Poco::Dynamic::Var origin_default_bit_value;
Poco::Dynamic::Var default_bit_value;
TP tp = TypeDecimal; // TypeDecimal is not used by TiDB.
UInt32 flag = 0;
Expand Down
Loading

0 comments on commit 1ce2f11

Please sign in to comment.