Skip to content

Commit

Permalink
Fix json ddl default value (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
solotzg authored Dec 28, 2019
1 parent 9ee682a commit b85321f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 32 deletions.
52 changes: 27 additions & 25 deletions dbms/src/Storages/Transaction/JSONCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ extern const int LOGICAL_ERROR;

using JsonVar = Poco::Dynamic::Var;

constexpr UInt8 TYPE_CODE_OBJECT = 0x01; // TypeCodeObject indicates the JSON is an object.
constexpr UInt8 TYPE_CODE_ARRAY = 0x03; // TypeCodeArray indicates the JSON is an array.
constexpr UInt8 TYPE_CODE_LITERAL = 0x04; // TypeCodeLiteral indicates the JSON is a literal.
constexpr UInt8 TYPE_CODE_INT64 = 0x09; // TypeCodeInt64 indicates the JSON is a signed integer.
constexpr UInt8 TYPE_CODE_UINT64 = 0x0a; // TypeCodeUint64 indicates the JSON is a unsigned integer.
constexpr UInt8 TYPE_CODE_FLOAT64 = 0x0b; // TypeCodeFloat64 indicates the JSON is a double float number.
constexpr UInt8 TYPE_CODE_STRING = 0x0c; // TypeCodeString indicates the JSON is a string.
constexpr UInt8 LITERAL_NIL = 0x00; // LiteralNil represents JSON null.
constexpr UInt8 LITERAL_TRUE = 0x01; // LiteralTrue represents JSON true.
constexpr UInt8 LITERAL_FALSE = 0x02; // LiteralFalse represents JSON false.
extern const UInt8 TYPE_CODE_OBJECT = 0x01; // TypeCodeObject indicates the JSON is an object.
extern const UInt8 TYPE_CODE_ARRAY = 0x03; // TypeCodeArray indicates the JSON is an array.
extern const UInt8 TYPE_CODE_LITERAL = 0x04; // TypeCodeLiteral indicates the JSON is a literal.
extern const UInt8 TYPE_CODE_INT64 = 0x09; // TypeCodeInt64 indicates the JSON is a signed integer.
extern const UInt8 TYPE_CODE_UINT64 = 0x0a; // TypeCodeUint64 indicates the JSON is a unsigned integer.
extern const UInt8 TYPE_CODE_FLOAT64 = 0x0b; // TypeCodeFloat64 indicates the JSON is a double float number.
extern const UInt8 TYPE_CODE_STRING = 0x0c; // TypeCodeString indicates the JSON is a string.
extern const UInt8 LITERAL_NIL = 0x00; // LiteralNil represents JSON null.
extern const UInt8 LITERAL_TRUE = 0x01; // LiteralTrue represents JSON true.
extern const UInt8 LITERAL_FALSE = 0x02; // LiteralFalse represents JSON false.

constexpr size_t VALUE_ENTRY_SIZE = 5;
constexpr size_t KEY_ENTRY_LENGTH = 6;
Expand Down Expand Up @@ -212,15 +212,23 @@ String DecodeJsonAsString(size_t & cursor, const String & raw_value)
return decodeValue(type, cursor, raw_value);
}

template<bool doDecode>
struct need_decode{};
template <bool doDecode>
struct need_decode
{
};


template<>
struct need_decode<true>{ typedef String type; };
template <>
struct need_decode<true>
{
typedef String type;
};

template<>
struct need_decode<false>{ typedef void type; };
template <>
struct need_decode<false>
{
typedef void type;
};

template <bool doDecode>
typename need_decode<doDecode>::type DecodeJson(size_t & cursor, const String & raw_value)
Expand Down Expand Up @@ -263,14 +271,8 @@ typename need_decode<doDecode>::type DecodeJson(size_t & cursor, const String &
return static_cast<typename need_decode<doDecode>::type>(raw_value.substr(base, size));
}

void SkipJson(size_t & cursor, const String & raw_value)
{
DecodeJson<false>(cursor, raw_value);
}
void SkipJson(size_t & cursor, const String & raw_value) { DecodeJson<false>(cursor, raw_value); }

String DecodeJsonAsBinary(size_t & cursor, const String & raw_value)
{
return DecodeJson<true>(cursor, raw_value);
}
String DecodeJsonAsBinary(size_t & cursor, const String & raw_value) { return DecodeJson<true>(cursor, raw_value); }

} // namespace DB
} // namespace DB
2 changes: 1 addition & 1 deletion dbms/src/Storages/Transaction/RegionBlockReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static Field GenDecodeRow(const ColumnInfo & col_info)
case TiDB::CodecFlagVarUInt:
return Field(UInt64(0));
case TiDB::CodecFlagJson:
return Field(String());
return TiDB::genJsonNull();
case TiDB::CodecFlagDuration:
return Field(Int64(0));
default:
Expand Down
19 changes: 17 additions & 2 deletions dbms/src/Storages/Transaction/TiDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include <Storages/MutableSupport.h>
#include <Storages/Transaction/TiDB.h>

namespace DB
{
extern const UInt8 TYPE_CODE_LITERAL;
extern const UInt8 LITERAL_NIL;
} // namespace DB

namespace TiDB
{
using DB::Decimal128;
Expand Down Expand Up @@ -49,6 +55,9 @@ Field ColumnInfo::defaultValueToField() const
case TypeVarString:
case TypeString:
return value.convert<String>();
case TypeJSON:
// JSON can't have a default value
return genJsonNull();
case TypeEnum:
return getEnumIndex(value.convert<String>());
case TypeNull:
Expand Down Expand Up @@ -282,8 +291,7 @@ catch (const Poco::Exception & e)

TableInfo::TableInfo(const String & table_info_json) { deserialize(table_info_json); }

String TableInfo::serialize() const
try
String TableInfo::serialize() const try
{
std::stringstream buf;

Expand Down Expand Up @@ -507,4 +515,11 @@ TableInfo TableInfo::producePartitionTableInfo(TableID table_or_partition_id) co

String TableInfo::getPartitionTableName(TableID part_id) const { return name + "_" + std::to_string(part_id); }

String genJsonNull()
{
// null
const static String null({char(DB::TYPE_CODE_LITERAL), char(DB::LITERAL_NIL)});
return null;
}

} // namespace TiDB
8 changes: 5 additions & 3 deletions dbms/src/Storages/Transaction/TiDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ using DB::Timestamp;
M(Float, 4, Float, Float32, false) \
M(Double, 5, Float, Float64, false) \
M(Null, 6, Nil, Nothing, false) \
M(Timestamp, 7, UInt, MyDateTime, false) \
M(Timestamp, 7, UInt, MyDateTime, false) \
M(LongLong, 8, Int, Int64, false) \
M(Int24, 9, VarInt, Int32, true) \
M(Date, 10, UInt, MyDate, false) \
M(Date, 10, UInt, MyDate, false) \
M(Time, 11, Duration, Int64, false) \
M(Datetime, 12, UInt, MyDateTime, false) \
M(Datetime, 12, UInt, MyDateTime, false) \
M(Year, 13, Int, Int16, false) \
M(NewDate, 14, Int, MyDate, false) \
M(Varchar, 15, CompactBytes, String, false) \
Expand Down Expand Up @@ -288,4 +288,6 @@ struct TableInfo

using DBInfoPtr = std::shared_ptr<DBInfo>;

String genJsonNull();

} // namespace TiDB
2 changes: 1 addition & 1 deletion dbms/src/Storages/Transaction/TiKVDecodedValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct ValueExtraInfo

void atomicUpdate(DecodedRow *& data) const
{
static void * expected = nullptr;
void * expected = nullptr;
if (!decoded.compare_exchange_strong(expected, (void *)data))
delete data;
data = nullptr;
Expand Down

0 comments on commit b85321f

Please sign in to comment.