From 72ca080f646c768d5a32ab0d843a30d687be468a Mon Sep 17 00:00:00 2001 From: Alexey Arno Date: Sun, 14 Dec 2014 22:43:37 +0300 Subject: [PATCH] Further simplify the type compatibility checking code --- .../DB/DataTypes/DataTypeFixedString.h | 2 + dbms/include/DB/DataTypes/DataTypeString.h | 2 + dbms/include/DB/DataTypes/IDataType.h | 3 + .../include/DB/DataTypes/typesAreCompatible.h | 12 --- dbms/src/Core/Block.cpp | 19 ++++- dbms/src/DataTypes/typesAreCompatible.cpp | 79 ------------------- 6 files changed, 25 insertions(+), 92 deletions(-) delete mode 100644 dbms/include/DB/DataTypes/typesAreCompatible.h delete mode 100644 dbms/src/DataTypes/typesAreCompatible.cpp diff --git a/dbms/include/DB/DataTypes/DataTypeFixedString.h b/dbms/include/DB/DataTypes/DataTypeFixedString.h index 924746eabc2..bcc5e36d233 100644 --- a/dbms/include/DB/DataTypes/DataTypeFixedString.h +++ b/dbms/include/DB/DataTypes/DataTypeFixedString.h @@ -30,6 +30,8 @@ public: return "FixedString(" + toString(n) + ")"; } + bool behavesAsString() const { return true; } + DataTypePtr clone() const { return new DataTypeFixedString(n); diff --git a/dbms/include/DB/DataTypes/DataTypeString.h b/dbms/include/DB/DataTypes/DataTypeString.h index 73ddbd86b70..0a12c954c5b 100644 --- a/dbms/include/DB/DataTypes/DataTypeString.h +++ b/dbms/include/DB/DataTypes/DataTypeString.h @@ -23,6 +23,8 @@ public: return "String"; } + bool behavesAsString() const { return true; } + DataTypePtr clone() const { return new DataTypeString; diff --git a/dbms/include/DB/DataTypes/IDataType.h b/dbms/include/DB/DataTypes/IDataType.h index 61e96706b39..13f3c6fb96e 100644 --- a/dbms/include/DB/DataTypes/IDataType.h +++ b/dbms/include/DB/DataTypes/IDataType.h @@ -30,6 +30,9 @@ public: /// Если тип числовой, уместны ли с ним все арифметические операции и приведение типов. /// true для чисел, false для даты и даты-с-временем. virtual bool behavesAsNumber() const { return false; } + + /// Является ли тип строковым. + virtual bool behavesAsString() const { return false; } /// Клонировать virtual SharedPtr clone() const = 0; diff --git a/dbms/include/DB/DataTypes/typesAreCompatible.h b/dbms/include/DB/DataTypes/typesAreCompatible.h deleted file mode 100644 index 181fad08b41..00000000000 --- a/dbms/include/DB/DataTypes/typesAreCompatible.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -namespace DB -{ - -/** Проверить, что типы совместимые. - */ -bool typesAreCompatible(const IDataType& lhs, const IDataType& rhs); - -} diff --git a/dbms/src/Core/Block.cpp b/dbms/src/Core/Block.cpp index a8c523ecbb4..e02822806f5 100644 --- a/dbms/src/Core/Block.cpp +++ b/dbms/src/Core/Block.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include @@ -17,6 +16,24 @@ #include +namespace +{ + +bool typesAreCompatible(const DB::IDataType & lhs, const DB::IDataType & rhs) +{ + if (lhs.behavesAsNumber() && rhs.behavesAsNumber()) + return true; + + if (lhs.behavesAsString() && rhs.behavesAsString()) + return true; + + if (lhs.getName() == rhs.getName()) + return true; + + return false; +} + +} namespace DB { diff --git a/dbms/src/DataTypes/typesAreCompatible.cpp b/dbms/src/DataTypes/typesAreCompatible.cpp deleted file mode 100644 index 5aa91f60f77..00000000000 --- a/dbms/src/DataTypes/typesAreCompatible.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include -#include -#include -#include - -#include -#include -#include -#include - -namespace -{ - -typedef std::map > TypesCompatibilityList; - -void addTypes(TypesCompatibilityList& tcl, const DB::IDataType & lhs, const DB::IDataType& rhs) -{ - tcl[lhs.getName()].push_back(rhs.getName()); - tcl[rhs.getName()].push_back(lhs.getName()); -} - -TypesCompatibilityList init() -{ - TypesCompatibilityList tcl; - - addTypes(tcl, DB::DataTypeString(), DB::DataTypeFixedString(1)); - - addTypes(tcl, DB::DataTypeFloat32(), DB::DataTypeFloat64()); - - addTypes(tcl, DB::DataTypeInt8(), DB::DataTypeUInt8()); - addTypes(tcl, DB::DataTypeInt8(), DB::DataTypeInt16()); - addTypes(tcl, DB::DataTypeInt8(), DB::DataTypeUInt16()); - addTypes(tcl, DB::DataTypeInt8(), DB::DataTypeInt32()); - addTypes(tcl, DB::DataTypeInt8(), DB::DataTypeUInt32()); - addTypes(tcl, DB::DataTypeInt8(), DB::DataTypeInt64()); - addTypes(tcl, DB::DataTypeInt8(), DB::DataTypeUInt64()); - - addTypes(tcl, DB::DataTypeInt16(), DB::DataTypeUInt16()), - addTypes(tcl, DB::DataTypeInt16(), DB::DataTypeInt32()), - addTypes(tcl, DB::DataTypeInt16(), DB::DataTypeUInt32()), - addTypes(tcl, DB::DataTypeInt16(), DB::DataTypeInt64()), - addTypes(tcl, DB::DataTypeInt16(), DB::DataTypeUInt64()), - - addTypes(tcl, DB::DataTypeInt32(), DB::DataTypeUInt32()), - addTypes(tcl, DB::DataTypeInt32(), DB::DataTypeInt64()); - addTypes(tcl, DB::DataTypeInt32(), DB::DataTypeUInt64()); - - addTypes(tcl, DB::DataTypeInt64(), DB::DataTypeUInt64()); - - for (auto & it : tcl) - { - auto & types_list = it.second; - std::sort(types_list.begin(), types_list.end()); - } - - return tcl; -} - -} - -namespace DB -{ - -bool typesAreCompatible(const IDataType & lhs, const IDataType & rhs) -{ - static const auto types_compatibility_list = init(); - - if (lhs.getName() == rhs.getName()) - return true; - - auto it = types_compatibility_list.find(lhs.getName()); - if (it == types_compatibility_list.end()) - return false; - - const auto & types_list = it->second; - return std::binary_search(types_list.begin(), types_list.end(), rhs.getName()); -} - -}