Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync default value from tidb to flash #113

Merged
merged 1 commit into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions dbms/src/Storages/Transaction/RegionBlockReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ std::tuple<Block, bool> readRegionBlock(const TiDB::TableInfo & table_info,

row.emplace_back(Field(column.id));
// Fill `zero` value if NOT NULL specified or else NULL.
// TODO: Need to consider default value.
row.push_back(column.hasNotNullFlag() ? GenDecodeRow(column.getCodecFlag()) : Field());
row.push_back(column.defaultValueToField());
}

// Remove values of non-existing columns, which could be data inserted (but not flushed) before DDLs that drop some columns.
Expand Down
10 changes: 6 additions & 4 deletions dbms/src/Storages/Transaction/SchemaBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Parsers/ParserCreateQuery.h>
#include <Parsers/ParserDropQuery.h>
#include <Parsers/parseQuery.h>
#include <Parsers/ASTLiteral.h>
#include <Storages/MutableSupport.h>
#include <Storages/Transaction/SchemaBuilder.h>
#include <Storages/Transaction/TMTContext.h>
Expand All @@ -15,10 +16,7 @@
namespace DB
{

using TableInfo = TiDB::TableInfo;
using DBInfo = TiDB::DBInfo;
using TableInfoPtr = TiDB::TableInfoPtr;
using DBInfoPtr = TiDB::DBInfoPtr;
using namespace TiDB;

inline AlterCommands detectSchemaChanges(Logger * log, const TiDB::TableInfo & table_info, const TiDB::TableInfo & orig_table_info)
{
Expand All @@ -39,6 +37,10 @@ inline AlterCommands detectSchemaChanges(Logger * log, const TiDB::TableInfo & t
command.type = AlterCommand::ADD_COLUMN;
command.column_name = column_info.name;
command.data_type = getDataTypeByColumnInfo(column_info);
if (!column_info.origin_default_value.isEmpty()) {
LOG_DEBUG(log, "add default value for column: " + column_info.name);
command.default_expression = ASTPtr(new ASTLiteral(column_info.defaultValueToField()));
}
// TODO: support after column.
LOG_DEBUG(log, "detect add column.");
}
Expand Down
42 changes: 42 additions & 0 deletions dbms/src/Storages/Transaction/TiDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,51 @@ namespace TiDB
using DB::ReadBufferFromString;
using DB::WriteBuffer;
using DB::WriteBufferFromOwnString;
using DB::Field;

ColumnInfo::ColumnInfo(Poco::JSON::Object::Ptr json) { deserialize(json); }

// TODO:: Refine Decimal Default Value !!
// TODO:: Refine Enum Default Value !!
// TODO:: Refine Date/Datatime/TimeStamp Defalut Value !!
Field ColumnInfo::defaultValueToField() const {
auto & value = origin_default_value;
switch (tp) {
// Integer Type.
case TypeTiny:
case TypeShort:
case TypeLong:
case TypeLongLong:
case TypeInt24:
return value.convert<Int64>();
// Floating type.
case TypeFloat:
case TypeDouble:
return value.convert<double>();
case TypeTimestamp:
// FIXME: may be string
return value.convert<Int64>();
case TypeDate:
case TypeDatetime:
case TypeVarchar:
case TypeTinyBlob:
case TypeMediumBlob:
case TypeLongBlob:
case TypeBlob:
case TypeVarString:
case TypeString:
return value.convert<String>();
case TypeEnum:
// FIXME: may be int or string
return value.convert<String>();
case TypeNull:
return Field();
default:
throw Exception("Have not proccessed type: " + std::to_string(tp));
}
return Field();
}

Poco::JSON::Object::Ptr ColumnInfo::getJSONObject() const try
{
Poco::JSON::Object::Ptr json = new Poco::JSON::Object();
Expand Down
1 change: 1 addition & 0 deletions dbms/src/Storages/Transaction/TiDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ struct ColumnInfo
#undef M

CodecFlag getCodecFlag() const;
DB::Field defaultValueToField() const;
};

enum PartitionType
Expand Down