Skip to content

Commit

Permalink
Merge branch 'master' into cop
Browse files Browse the repository at this point in the history
  • Loading branch information
zanmato1984 authored Aug 27, 2019
2 parents 61cdc8f + da0cac9 commit 53dcd1f
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 91 deletions.
16 changes: 12 additions & 4 deletions dbms/src/Common/FieldVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ static inline String formatQuoted(T x)
return wb.str();
}

template <typename T>
static inline void writeQuoted(const DecimalField<T> & x, WriteBuffer & buf)
{
writeChar('\'', buf);
writeText(x.getValue(), x.getScale(), buf);
writeChar('\'', buf);
}

template <typename T>
static inline String formatQuotedWithPrefix(T x, const char * prefix)
{
Expand Down Expand Up @@ -108,10 +116,10 @@ String FieldVisitorToString::operator() (const UInt64 & x) const { return format
String FieldVisitorToString::operator() (const Int64 & x) const { return formatQuoted(x); }
String FieldVisitorToString::operator() (const Float64 & x) const { return formatFloat(x); }
String FieldVisitorToString::operator() (const String & x) const { return formatQuoted(x); }
String FieldVisitorToString::operator() (const DecimalField<Decimal32> & x) const { return "Decimal32_" + x.toString(); }
String FieldVisitorToString::operator() (const DecimalField<Decimal64> & x) const { return "Decimal64_" + x.toString(); }
String FieldVisitorToString::operator() (const DecimalField<Decimal128> & x) const { return "Decimal128_" + x.toString(); }
String FieldVisitorToString::operator() (const DecimalField<Decimal256> & x) const { return "Decimal256_" + x.toString(); }
String FieldVisitorToString::operator() (const DecimalField<Decimal32> & x) const { return formatQuoted(x); }
String FieldVisitorToString::operator() (const DecimalField<Decimal64> & x) const { return formatQuoted(x); }
String FieldVisitorToString::operator() (const DecimalField<Decimal128> & x) const { return formatQuoted(x); }
String FieldVisitorToString::operator() (const DecimalField<Decimal256> & x) const { return formatQuoted(x); }


String FieldVisitorToString::operator() (const Array & x) const
Expand Down
26 changes: 18 additions & 8 deletions dbms/src/Debug/MockTiDB.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Debug/MockTiDB.h>

#include <Common/FieldVisitors.h>
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeDateTime.h>
#include <DataTypes/DataTypeDecimal.h>
Expand Down Expand Up @@ -128,7 +129,7 @@ bool tryGetDecimalType(const IDataType * nested_type, ColumnInfo & column_info)
return false;
}

ColumnInfo getColumnInfoFromColumn(const NameAndTypePair & column, ColumnID id)
ColumnInfo getColumnInfoFromColumn(const NameAndTypePair & column, ColumnID id, const Field & default_value)
{
ColumnInfo column_info;
column_info.id = id;
Expand Down Expand Up @@ -189,9 +190,17 @@ ColumnInfo getColumnInfoFromColumn(const NameAndTypePair & column, ColumnID id)
if (checkDataType<DataTypeUInt64>(nested_type))
column_info.tp = TiDB::TypeLongLong;

// Default value.
// TODO: Parse default value and set flag properly.
column_info.setNoDefaultValueFlag();
// Default value, currently we only support int.
if (!default_value.isNull())
{
// convert any type to string , this is TiDB's style.

column_info.origin_default_value = applyVisitor(FieldVisitorToString(), default_value);
}
else
{
column_info.setNoDefaultValueFlag();
}

return column_info;
}
Expand Down Expand Up @@ -240,7 +249,7 @@ TableID MockTiDB::newTable(const String & database_name, const String & table_na
int i = 1;
for (auto & column : columns.getAllPhysical())
{
table_info.columns.emplace_back(getColumnInfoFromColumn(column, i++));
table_info.columns.emplace_back(getColumnInfoFromColumn(column, i++, Field()));
}

table_info.pk_is_handle = false;
Expand Down Expand Up @@ -323,7 +332,8 @@ void MockTiDB::dropPartition(const String & database_name, const String & table_
version_diff[version] = diff;
}

void MockTiDB::addColumnToTable(const String & database_name, const String & table_name, const NameAndTypePair & column)
void MockTiDB::addColumnToTable(
const String & database_name, const String & table_name, const NameAndTypePair & column, const Field & default_value)
{
std::lock_guard lock(tables_mutex);

Expand All @@ -334,7 +344,7 @@ void MockTiDB::addColumnToTable(const String & database_name, const String & tab
!= columns.end())
throw Exception("Column " + column.name + " already exists in TiDB table " + qualified_name, ErrorCodes::LOGICAL_ERROR);

ColumnInfo column_info = getColumnInfoFromColumn(column, table->allocColumnID());
ColumnInfo column_info = getColumnInfoFromColumn(column, table->allocColumnID(), default_value);
columns.emplace_back(column_info);

version++;
Expand Down Expand Up @@ -381,7 +391,7 @@ void MockTiDB::modifyColumnInTable(const String & database_name, const String &
if (it == columns.end())
throw Exception("Column " + column.name + " does not exist in TiDB table " + qualified_name, ErrorCodes::LOGICAL_ERROR);

ColumnInfo column_info = getColumnInfoFromColumn(column, 0);
ColumnInfo column_info = getColumnInfoFromColumn(column, 0, Field());
if (it->hasUnsignedFlag() != column_info.hasUnsignedFlag())
throw Exception("Modify column " + column.name + " UNSIGNED flag is not allowed", ErrorCodes::LOGICAL_ERROR);
if (it->hasNotNullFlag() != column_info.hasNotNullFlag())
Expand Down
3 changes: 2 additions & 1 deletion dbms/src/Debug/MockTiDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class MockTiDB : public ext::singleton<MockTiDB>

void dropDB(Context & context, const String & database_name, bool drop_regions);

void addColumnToTable(const String & database_name, const String & table_name, const NameAndTypePair & column);
void addColumnToTable(
const String & database_name, const String & table_name, const NameAndTypePair & column, const Field & default_value);

void dropColumnFromTable(const String & database_name, const String & table_name, const String & column_name);

Expand Down
18 changes: 11 additions & 7 deletions dbms/src/Debug/dbgFuncMockTiDBTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Interpreters/InterpreterCreateQuery.h>
#include <Interpreters/InterpreterRenameQuery.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ParserCreateQuery.h>
Expand Down Expand Up @@ -171,10 +172,17 @@ void MockTiDBTable::dbgFuncAddColumnToTiDBTable(Context & context, const ASTs &
if (cols.getAllPhysical().size() > 1)
throw Exception("Not support multiple columns", ErrorCodes::LOGICAL_ERROR);

// TODO: Support partition table.

NameAndTypePair column = cols.getAllPhysical().front();
MockTiDB::instance().addColumnToTable(database_name, table_name, column);
auto it = cols.defaults.find(column.name);
Field default_value;
if (it != cols.defaults.end())
{
const auto * func = typeid_cast<const ASTFunction *>(it->second.expression.get());
const auto * value_ptr
= typeid_cast<const ASTLiteral *>(typeid_cast<const ASTExpressionList *>(func->arguments.get())->children[0].get());
default_value = value_ptr->value;
}
MockTiDB::instance().addColumnToTable(database_name, table_name, column, default_value);

std::stringstream ss;
ss << "added column " << column.name << " " << column.type->getName();
Expand All @@ -192,8 +200,6 @@ void MockTiDBTable::dbgFuncDropColumnFromTiDBTable(Context & /*context*/, const

MockTiDB::TablePtr table = MockTiDB::instance().getTableByName(database_name, table_name);

// TODO: Support partition table.

MockTiDB::instance().dropColumnFromTable(database_name, table_name, column_name);

std::stringstream ss;
Expand Down Expand Up @@ -221,8 +227,6 @@ void MockTiDBTable::dbgFuncModifyColumnInTiDBTable(DB::Context & context, const
if (cols.getAllPhysical().size() > 1)
throw Exception("Not support multiple columns", ErrorCodes::LOGICAL_ERROR);

// TODO: Support partition table.

NameAndTypePair column = cols.getAllPhysical().front();
MockTiDB::instance().modifyColumnInTable(database_name, table_name, column);

Expand Down
9 changes: 6 additions & 3 deletions dbms/src/Storages/StorageMergeTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ void StorageMergeTree::alterInternal(

auto table_hard_lock = lockStructureForAlter(__PRETTY_FUNCTION__);

IDatabase::ASTModifier storage_modifier = [primary_key_is_modified, new_primary_key_ast, this] (IAST & ast)
IDatabase::ASTModifier storage_modifier = [primary_key_is_modified, new_primary_key_ast, table_info] (IAST & ast)
{
auto & storage_ast = typeid_cast<ASTStorage &>(ast);

Expand All @@ -392,8 +392,11 @@ void StorageMergeTree::alterInternal(
typeid_cast<ASTExpressionList &>(*storage_ast.engine->arguments).children.at(1) = tuple;
}

auto literal = std::make_shared<ASTLiteral>(Field(data.table_info->serialize(true)));
typeid_cast<ASTExpressionList &>(*storage_ast.engine->arguments).children.back() = literal;
if (table_info)
{
auto literal = std::make_shared<ASTLiteral>(Field(table_info->get().serialize(true)));
typeid_cast<ASTExpressionList &>(*storage_ast.engine->arguments).children.back() = literal;
}
};

context.getDatabase(database_name)->alterTable(context, table_name, new_columns, storage_modifier);
Expand Down
Loading

0 comments on commit 53dcd1f

Please sign in to comment.