Add text type
Project: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/commit/3a3772d9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/tree/3a3772d9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/diff/3a3772d9 Branch: refs/heads/refactor-type Commit: 3a3772d91bd94d269b2f2fa49895f53385d46381 Parents: bef66ad Author: Jianqiao Zhu <[email protected]> Authored: Tue Oct 3 17:03:20 2017 -0500 Committer: Jianqiao Zhu <[email protected]> Committed: Wed Oct 11 13:37:54 2017 -0500 ---------------------------------------------------------------------- types/ArrayType.cpp | 29 ++++ types/ArrayType.hpp | 25 ++- types/CMakeLists.txt | 39 ++++- types/MetaType.cpp | 46 +++++ types/MetaTypeLite.cpp | 11 +- types/MetaTypeLite.hpp | 4 + types/TextType.cpp | 50 ++++++ types/TextType.hpp | 73 ++++++++ types/TypeFactory.cpp | 136 --------------- types/TypeFactory.hpp | 110 +----------- types/TypeFactoryLite.cpp | 170 +++++++++++++++++++ types/TypeFactoryLite.hpp | 141 +++++++++++++++ types/TypeID.cpp | 1 + types/TypeID.hpp | 1 + types/TypeRegistrar.hpp | 3 + types/TypeSynthesizer.hpp | 20 +-- types/TypeUtil.hpp | 11 +- .../unary_operations/CastOperation.hpp | 5 - 18 files changed, 602 insertions(+), 273 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/ArrayType.cpp ---------------------------------------------------------------------- diff --git a/types/ArrayType.cpp b/types/ArrayType.cpp index df2b9de..30cc4a5 100644 --- a/types/ArrayType.cpp +++ b/types/ArrayType.cpp @@ -29,6 +29,35 @@ namespace quickstep { +std::string ArrayType::getName() const { + std::string name("Array("); + name.append(element_type_.getName()); + name.push_back(')'); + if (nullable_) { + name.append(" NULL"); + } + return name; +} + +bool ArrayType::checkValuesEqual(const UntypedLiteral *lhs, + const UntypedLiteral *rhs, + const Type &rhs_type) const { + if (!equals(rhs_type)) { + return false; + } + const ArrayLiteral &lhs_array = castValueToLiteral(lhs); + const ArrayLiteral &rhs_array = castValueToLiteral(rhs); + if (lhs_array.size() != rhs_array.size()) { + return false; + } + for (std::size_t i = 0; i < lhs_array.size(); ++i) { + if (!element_type_.checkValuesEqual(lhs_array.at(i), rhs_array.at(i))) { + return false; + } + } + return true; +} + TypedValue ArrayType::marshallValue(const UntypedLiteral *value) const { const ArrayLiteral &array = *static_cast<const ArrayLiteral*>(value); serialization::ArrayLiteral proto; http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/ArrayType.hpp ---------------------------------------------------------------------- diff --git a/types/ArrayType.hpp b/types/ArrayType.hpp index a48cfa3..f7e2212 100644 --- a/types/ArrayType.hpp +++ b/types/ArrayType.hpp @@ -25,6 +25,7 @@ #include "types/Type.hpp" #include "types/TypeID.hpp" +#include "types/TypeRegistrar.hpp" #include "types/TypeSynthesizer.hpp" #include "utility/Macros.hpp" @@ -41,9 +42,15 @@ class TypedValue; class ArrayType : public TypeSynthesizer<kArray> { public: int getPrintWidth() const override { - return 16; + return 32; } + std::string getName() const override; + + bool checkValuesEqual(const UntypedLiteral *lhs, + const UntypedLiteral *rhs, + const Type &rhs_type) const override; + TypedValue marshallValue(const UntypedLiteral *value) const override; UntypedLiteral* unmarshallValue(const void *data, @@ -76,6 +83,22 @@ class ArrayType : public TypeSynthesizer<kArray> { QUICKSTEP_SYNTHESIZE_TYPE(ArrayType); }; + +template <TypeID type_id, TypeID element_type_id> +class FirstOrderArrayType { + public: + + + private: + + QUICKSTEP_SYNTHESIZE_TYPE(FirstOrderArrayType); +}; + + + + + + /** @} */ } // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt index 69ac906..d4bc26e 100644 --- a/types/CMakeLists.txt +++ b/types/CMakeLists.txt @@ -56,9 +56,11 @@ add_library(quickstep_types_NullType ../empty_src.cpp NullType.hpp) add_library(quickstep_types_NumericSuperType ../empty_src.cpp NumericSuperType.hpp) add_library(quickstep_types_NumericTypeSafeCoercibility ../empty_src.cpp NumericTypeSafeCoercibility.hpp) add_library(quickstep_types_NumericTypeUnifier ../empty_src.cpp NumericTypeUnifier.hpp) +add_library(quickstep_types_TextType TextType.cpp TextType.hpp) add_library(quickstep_types_Type Type.cpp Type.hpp) add_library(quickstep_types_TypeErrors ../empty_src.cpp TypeErrors.hpp) -add_library(quickstep_types_TypeFactory TypeFactory.cpp TypeFactory.hpp) +add_library(quickstep_types_TypeFactory ../empty_src.cpp TypeFactory.hpp) +add_library(quickstep_types_TypeFactoryLite TypeFactoryLite.cpp TypeFactoryLite.hpp) add_library(quickstep_types_TypeID TypeID.cpp TypeID.hpp) add_library(quickstep_types_TypeIDSelectors ../empty_src.cpp TypeIDSelectors.hpp) add_library(quickstep_types_TypeRegistrar ../empty_src.cpp TypeRegistrar.hpp) @@ -146,6 +148,14 @@ target_link_libraries(quickstep_types_FloatType quickstep_types_TypeID quickstep_types_TypedValue quickstep_utility_Macros) +target_link_libraries(quickstep_types_GenericValue + quickstep_types_Type + quickstep_types_TypeID + quickstep_types_TypeRegistrar + quickstep_types_Type_proto + quickstep_types_TypedValue + quickstep_utility_HashPair + quickstep_utility_Macros) target_link_libraries(quickstep_types_IntType glog quickstep_types_NumericSuperType @@ -165,7 +175,9 @@ target_link_libraries(quickstep_types_LongType quickstep_types_TypedValue quickstep_utility_Macros) target_link_libraries(quickstep_types_MetaType - quickstep_types_MetaTypeLite) + quickstep_types_MetaTypeLite + quickstep_types_TypeFactoryLite + quickstep_types_Type_proto) target_link_libraries(quickstep_types_MetaTypeLite quickstep_types_Type quickstep_types_TypeID @@ -191,19 +203,30 @@ target_link_libraries(quickstep_types_NumericTypeSafeCoercibility quickstep_utility_meta_TMP) target_link_libraries(quickstep_types_NumericTypeUnifier quickstep_types_NumericTypeSafeCoercibility) +target_link_libraries(quickstep_types_TextType + quickstep_types_TextType + quickstep_types_Type + quickstep_types_TypeID + quickstep_types_TypeSynthesizer + quickstep_types_TypedValue + quickstep_utility_Macros) target_link_libraries(quickstep_types_Type glog quickstep_types_Type_proto quickstep_types_TypeID quickstep_types_TypedValue quickstep_utility_Macros) -target_link_libraries(quickstep_types_TypeFactory +target_link_libraries(quickstep_types_TypeFactoryLite glog + quickstep_types_GenericValue quickstep_types_Type quickstep_types_TypeID quickstep_types_TypeUtil quickstep_types_Type_proto quickstep_utility_Macros) +target_link_libraries(quickstep_types_TypeFactory + quickstep_types_MetaType + quickstep_types_TypeFactoryLite) target_link_libraries(quickstep_types_TypeID quickstep_types_Type_proto quickstep_utility_Macros) @@ -213,6 +236,7 @@ target_link_libraries(quickstep_types_TypeIDSelectors target_link_libraries(quickstep_types_TypeRegistrar quickstep_types_DatetimeLit quickstep_types_IntervalLit + quickstep_types_NullLit quickstep_types_Type quickstep_types_TypeID quickstep_types_TypeIDSelectors @@ -229,6 +253,7 @@ target_link_libraries(quickstep_types_TypeSynthesizer quickstep_utility_PtrMap quickstep_utility_meta_Common) target_link_libraries(quickstep_types_TypeUtil + quickstep_types_ArrayType quickstep_types_BoolType quickstep_types_CharType quickstep_types_DateType @@ -240,6 +265,7 @@ target_link_libraries(quickstep_types_TypeUtil quickstep_types_LongType quickstep_types_MetaTypeLite quickstep_types_NullType + quickstep_types_TextType quickstep_types_Type quickstep_types_TypeID quickstep_types_TypeRegistrar @@ -286,6 +312,7 @@ target_link_libraries(quickstep_types_YearMonthIntervalType # Module all-in-one library: add_library(quickstep_types ../empty_src.cpp TypesModule.hpp) target_link_libraries(quickstep_types + quickstep_types_ArrayType quickstep_types_AsciiStringSuperType quickstep_types_BoolType quickstep_types_CharType @@ -296,20 +323,26 @@ target_link_libraries(quickstep_types quickstep_types_DatetimeType quickstep_types_DoubleType quickstep_types_FloatType + quickstep_types_GenericValue quickstep_types_IntType quickstep_types_IntervalLit quickstep_types_IntervalParser quickstep_types_LongType + quickstep_types_MetaType + quickstep_types_MetaTypeLite quickstep_types_NullCoercibilityCheckMacro + quickstep_types_NullLit quickstep_types_NullType quickstep_types_NumericSuperType quickstep_types_NumericTypeSafeCoercibility quickstep_types_NumericTypeUnifier + quickstep_types_TextType quickstep_types_Type quickstep_types_TypeUtil quickstep_types_Type_proto quickstep_types_TypeErrors quickstep_types_TypeFactory + quickstep_types_TypeFactoryLite quickstep_types_TypeID quickstep_types_TypeIDSelectors quickstep_types_TypeRegistrar http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/MetaType.cpp ---------------------------------------------------------------------- diff --git a/types/MetaType.cpp b/types/MetaType.cpp index e69de29..f6e44ba 100644 --- a/types/MetaType.cpp +++ b/types/MetaType.cpp @@ -0,0 +1,46 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + **/ + +#include "types/MetaType.hpp" + +#include <cstddef> +#include <string> + +#include "types/Type.pb.h" +#include "types/TypeFactoryLite.hpp" + +namespace quickstep { + +bool MetaType::checkValuesEqual(const UntypedLiteral *lhs, + const UntypedLiteral *rhs, + const Type &rhs_type) const { + if (!equals(rhs_type)) { + return false; + } + return castValueToLiteral(lhs)->equals(*castValueToLiteral(rhs)); +} + +UntypedLiteral* MetaType::unmarshallValue(const void *data, + const std::size_t data_size) const { + serialization::Type proto; + proto.ParseFromArray(data, data_size); + return new MetaTypeLiteral(&TypeFactory::ReconstructFromProto(proto)); +} + +} // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/MetaTypeLite.cpp ---------------------------------------------------------------------- diff --git a/types/MetaTypeLite.cpp b/types/MetaTypeLite.cpp index 8022534..830f364 100644 --- a/types/MetaTypeLite.cpp +++ b/types/MetaTypeLite.cpp @@ -17,11 +17,13 @@ * under the License. **/ -#include "types/MetaType.hpp" +#include "types/MetaTypeLite.hpp" +#include <cstddef> #include <string> #include "types/TypeID.hpp" +#include "types/TypedValue.hpp" #include "glog/logging.h" @@ -36,13 +38,6 @@ TypedValue MetaType::marshallValue(const UntypedLiteral *value) const { return TypedValue::CreateWithOwnedData(kMetaType, data, data_size); } -//UntypedLiteral* MetaType::unmarshallValue(const void *data, -// const std::size_t data_size) const { -// serialization::Type proto; -// proto.ParseFromArray(data, data_size); -// return -//} - std::string MetaType::printValueToString(const UntypedLiteral *value) const { DCHECK(value != nullptr); http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/MetaTypeLite.hpp ---------------------------------------------------------------------- diff --git a/types/MetaTypeLite.hpp b/types/MetaTypeLite.hpp index 776fe03..09f879f 100644 --- a/types/MetaTypeLite.hpp +++ b/types/MetaTypeLite.hpp @@ -44,6 +44,10 @@ class MetaType : public TypeSynthesizer<kMetaType> { return 16; } + bool checkValuesEqual(const UntypedLiteral *lhs, + const UntypedLiteral *rhs, + const Type &rhs_type) const override; + TypedValue marshallValue(const UntypedLiteral *value) const override; UntypedLiteral* unmarshallValue(const void *data, http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/TextType.cpp ---------------------------------------------------------------------- diff --git a/types/TextType.cpp b/types/TextType.cpp new file mode 100644 index 0000000..ce5c3a5 --- /dev/null +++ b/types/TextType.cpp @@ -0,0 +1,50 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + **/ + +#include "types/TextType.hpp" + +#include <cstddef> +#include <string> + +namespace quickstep { + +bool TextType::checkValuesEqual(const UntypedLiteral *lhs, + const UntypedLiteral *rhs, + const Type &rhs_type) const { + if (!equals(rhs_type)) { + return false; + } + return castValueToLiteral(lhs) == castValueToLiteral(rhs); +} + +TypedValue TextType::marshallValue(const UntypedLiteral *value) const { + const std::string &str = castValueToLiteral(value); + return TypedValue(kText, str.c_str(), str.length()).ensureNotReference(); +} + +UntypedLiteral* TextType::unmarshallValue(const void *data, + const std::size_t length) const { + return new std::string(static_cast<const char*>(data), length); +} + +std::string TextType::printValueToString(const UntypedLiteral *value) const { + return castValueToLiteral(value); +} + +} // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/TextType.hpp ---------------------------------------------------------------------- diff --git a/types/TextType.hpp b/types/TextType.hpp new file mode 100644 index 0000000..9ceea35 --- /dev/null +++ b/types/TextType.hpp @@ -0,0 +1,73 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + **/ + +#ifndef QUICKSTEP_TYPES_TEXT_TYPE_HPP_ +#define QUICKSTEP_TYPES_TEXT_TYPE_HPP_ + +#include <cstddef> +#include <cstdio> +#include <string> + +#include "types/Type.hpp" +#include "types/TypeID.hpp" +#include "types/TypeSynthesizer.hpp" +#include "types/TypedValue.hpp" +#include "utility/Macros.hpp" + +namespace quickstep { + +/** \addtogroup Types + * @{ + */ + +class TextType : public TypeSynthesizer<kText> { + public: + int getPrintWidth() const override { + return 32; + } + + bool checkValuesEqual(const UntypedLiteral *lhs, + const UntypedLiteral *rhs, + const Type &rhs_type) const override; + + TypedValue marshallValue(const UntypedLiteral *value) const override; + + UntypedLiteral* unmarshallValue(const void *data, + const std::size_t length) const override; + + std::string printValueToString(const UntypedLiteral *value) const override; + + bool parseTypedValueFromString(const std::string &value_string, + TypedValue *value) const override { + return false; + } + + private: + TextType(const bool nullable) + : TypeSynthesizer<kText>(nullable, 0, 0x1000) { + // TODO(refactor-type): Possibly infinite maximum size. + } + + QUICKSTEP_SYNTHESIZE_TYPE(TextType); +}; + + +} // namespace quickstep + +#endif // QUICKSTEP_TYPES_TEXT_TYPE_HPP_ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/TypeFactory.cpp ---------------------------------------------------------------------- diff --git a/types/TypeFactory.cpp b/types/TypeFactory.cpp index d60f701..e69de29 100644 --- a/types/TypeFactory.cpp +++ b/types/TypeFactory.cpp @@ -1,136 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - **/ - -#include "types/TypeFactory.hpp" - -#include <cstddef> -#include <string> - -#include "types/GenericValue.hpp" -#include "types/Type.hpp" -#include "types/Type.pb.h" -#include "types/TypeID.hpp" -#include "types/TypeUtil.hpp" -#include "utility/Macros.hpp" - -#include "glog/logging.h" - -namespace quickstep { - -bool TypeFactory::TypeRequiresLengthParameter(const TypeID id) { - return TypeUtil::IsParameterizedPod(id); -} - -const Type& TypeFactory::GetType(const TypeID id, - const bool nullable) { - DCHECK(!TypeRequiresLengthParameter(id)) - << "Called TypeFactory::GetType() for a type which requires " - << " a length parameter without specifying one."; - - return *InvokeOnTypeID<TypeIDSelectorMemoryLayout<kCxxInlinePod>>( - id, - [&](auto id) -> const Type* { // NOLINT(build/c++11) - return &TypeIDTrait<decltype(id)::value>::TypeClass::Instance(nullable); - }); -} - -const Type& TypeFactory::GetType(const TypeID id, - const std::size_t length, - const bool nullable) { - DCHECK(TypeRequiresLengthParameter(id)) - << "Provided a length parameter to TypeFactory::GetType() for " - << "a type which does not take one."; - - return *InvokeOnTypeID<TypeIDSelectorMemoryLayout<kParInlinePod, kParOutOfLinePod>>( - id, - [&](auto id) -> const Type* { // NOLINT(build/c++11) - return &TypeIDTrait<decltype(id)::value>::TypeClass::Instance(nullable, length); - }); -} - -bool TypeFactory::ProtoIsValid(const serialization::Type &proto) { - // Check that proto is fully initialized. - if (!proto.IsInitialized()) { - return false; - } - - // Check that the type_id is valid, and has length if parameterized. - const TypeID type_id = TypeIDFactory::ReconstructFromProto(proto.type_id()); - - if (type_id == kNullType) { - return proto.nullable(); - } - - if (TypeRequiresLengthParameter(type_id)) { - return proto.has_length(); - } - - return true; -} - -const Type& TypeFactory::ReconstructFromProto(const serialization::Type &proto) { - DCHECK(ProtoIsValid(proto)) - << "Attempted to create Type from an invalid proto description:\n" - << proto.DebugString(); - - const TypeID type_id = TypeIDFactory::ReconstructFromProto(proto.type_id()); - - if (TypeRequiresLengthParameter(type_id)) { - return GetType(type_id, proto.length(), proto.nullable()); - } else { - return GetType(type_id, proto.nullable()); - } -} - -const Type* TypeFactory::GetMostSpecificType(const Type &first, const Type &second) { - if (first.isSafelyCoercibleFrom(second)) { - return &first; - } else if (second.isSafelyCoercibleFrom(first)) { - return &second; - } else { - return nullptr; - } -} - -const Type* TypeFactory::GetUnifyingType(const Type &first, const Type &second) { - // TODO: cache - const Type *unifier = nullptr; - if (first.isNullable() || second.isNullable()) { - unifier = GetMostSpecificType(first.getNullableVersion(), - second.getNullableVersion()); - if (unifier == nullptr) { - if (((first.getTypeID() == kLong) && (second.getTypeID() == kFloat)) - || ((first.getTypeID() == kFloat) && (second.getTypeID() == kLong))) { - unifier = &(DoubleType::Instance(true)); - } - } - } else { - unifier = GetMostSpecificType(first, second); - if (unifier == nullptr) { - if (((first.getTypeID() == kLong) && (second.getTypeID() == kFloat)) - || ((first.getTypeID() == kFloat) && (second.getTypeID() == kLong))) { - unifier = &(DoubleType::Instance(false)); - } - } - } - - return unifier; -} - -} // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/TypeFactory.hpp ---------------------------------------------------------------------- diff --git a/types/TypeFactory.hpp b/types/TypeFactory.hpp index 89ff497..3992b6c 100644 --- a/types/TypeFactory.hpp +++ b/types/TypeFactory.hpp @@ -20,113 +20,7 @@ #ifndef QUICKSTEP_TYPES_TYPE_FACTORY_HPP_ #define QUICKSTEP_TYPES_TYPE_FACTORY_HPP_ -#include <cstddef> - -#include "types/TypeID.hpp" -#include "utility/Macros.hpp" - -namespace quickstep { - -class Type; - -namespace serialization { class Type; } - -/** \addtogroup Types - * @{ - */ - -/** - * @brief All-static factory object that provides access to Types, as well as - * methods for determining coercibility of Types. - **/ -class TypeFactory { - public: - /** - * @brief Determine if a length parameter is required when getting a Type of - * the specified TypeID. - * - * @param id The id of the desired Type. - * @return Whether a length must be specified for Types of the given id. - **/ - static bool TypeRequiresLengthParameter(const TypeID id); - - /** - * @brief Factory method to get a Type by its TypeID. - * @note This version is for Types without a length parameter (currently - * IntType, LongType, FloatType, and DoubleType). It is an error to - * call this with a Type which requires a length parameter. - * - * @param id The id of the desired Type. - * @param nullable Whether to get the nullable version of the Type. - * @return The Type corresponding to id. - **/ - static const Type& GetType(const TypeID id, const bool nullable = false); - - /** - * @brief Factory method to get a Type by its TypeID and length. - * @note This version is for Types with a length parameter (currently - * CharType and VarCharType). It is an error to call this with a Type - * which does not require a length parameter. - * - * @param id The id of the desired Type. - * @param length The length parameter of the desired Type. - * @param nullable Whether to get the nullable version of the Type. - * @return The Type corresponding to id and length. - **/ - static const Type& GetType(const TypeID id, const std::size_t length, const bool nullable = false); - - /** - * @brief Get a reference to a Type from that Type's serialized Protocol Buffer - * representation. - * - * @param proto A serialized Protocol Buffer representation of a Type, - * originally generated by getProto(). - * @return The Type described by proto. - **/ - static const Type& ReconstructFromProto(const serialization::Type &proto); - - /** - * @brief Check whether a serialization::Type is fully-formed and - * all parts are valid. - * - * @param proto A serialized Protocol Buffer representation of a Type, - * originally generated by getProto(). - * @return Whether proto is fully-formed and valid. - **/ - static bool ProtoIsValid(const serialization::Type &proto); - - /** - * @brief Determine which of two types is most specific, i.e. which - * isSafelyCoercibleFrom() the other. - * - * @param first The first type to check. - * @param second The second type to check. - * @return The most precise type, or NULL if neither Type - * isSafelyCoercibleFrom() the other. - **/ - static const Type* GetMostSpecificType(const Type &first, const Type &second); - - /** - * @brief Determine a type, if any exists, which both arguments can be safely - * coerced to. It is possible that the resulting type may not be - * either argument. - * - * @param first The first type to check. - * @param second The second type to check. - * @return The unifying type, or NULL if none exists. - **/ - static const Type* GetUnifyingType(const Type &first, const Type &second); - - private: - // Undefined default constructor. Class is all-static and should not be - // instantiated. - TypeFactory(); - - DISALLOW_COPY_AND_ASSIGN(TypeFactory); -}; - -/** @} */ - -} // namespace quickstep +#include "types/MetaType.hpp" +#include "types/TypeFactoryLite.hpp" #endif // QUICKSTEP_TYPES_TYPE_FACTORY_HPP_ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/TypeFactoryLite.cpp ---------------------------------------------------------------------- diff --git a/types/TypeFactoryLite.cpp b/types/TypeFactoryLite.cpp new file mode 100644 index 0000000..c7c6b3b --- /dev/null +++ b/types/TypeFactoryLite.cpp @@ -0,0 +1,170 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + **/ + +#include "types/TypeFactoryLite.hpp" + +#include <cstddef> +#include <string> +#include <vector> + +#include "types/GenericValue.hpp" +#include "types/Type.hpp" +#include "types/Type.pb.h" +#include "types/TypeID.hpp" +#include "types/TypeUtil.hpp" +#include "utility/Macros.hpp" + +#include "glog/logging.h" + +namespace quickstep { + +bool TypeFactory::TypeRequiresLengthParameter(const TypeID id) { + return TypeUtil::IsParameterizedPod(id); +} + +const Type& TypeFactory::GetType(const TypeID id, + const bool nullable) { + DCHECK(TypeUtil::GetMemoryLayout(id) == kCxxInlinePod) + << "Called TypeFactory::GetType() with incorrect parameters."; + + return *InvokeOnTypeID<TypeIDSelectorMemoryLayout<kCxxInlinePod>>( + id, + [&](auto id) -> const Type* { // NOLINT(build/c++11) + return &TypeIDTrait<decltype(id)::value>::TypeClass::Instance(nullable); + }); +} + +const Type& TypeFactory::GetType(const TypeID id, + const std::size_t length, + const bool nullable) { + DCHECK(TypeRequiresLengthParameter(id)) + << "Called TypeFactory::GetType() with incorrect parameters."; + + return *InvokeOnTypeID<TypeIDSelectorMemoryLayout<kParInlinePod, kParOutOfLinePod>>( + id, + [&](auto id) -> const Type* { // NOLINT(build/c++11) + return &TypeIDTrait<decltype(id)::value>::TypeClass::Instance(nullable, length); + }); +} + +const Type& TypeFactory::GetType(const TypeID id, + const std::vector<GenericValue> ¶meters, + const bool nullable) { + DCHECK(TypeUtil::GetMemoryLayout(id) == kCxxGeneric) + << "Called TypeFactory::GetType() with incorrect parameters."; + + return *InvokeOnTypeID<TypeIDSelectorMemoryLayout<kCxxGeneric>>( + id, + [&](auto id) -> const Type* { // NOLINT(build/c++11) + return &TypeIDTrait<decltype(id)::value>::TypeClass::Instance(nullable, parameters); + }); +} + +bool TypeFactory::ProtoIsValid(const serialization::Type &proto) { + // Check that proto is fully initialized. + if (!proto.IsInitialized()) { + return false; + } + + // Check that the type_id is valid, and has length if parameterized. + const TypeID type_id = TypeIDFactory::ReconstructFromProto(proto.type_id()); + + if (type_id == kNullType) { + return proto.nullable(); + } + + if (TypeRequiresLengthParameter(type_id)) { + return proto.has_length(); + } + + return true; +} + +const Type& TypeFactory::ReconstructFromProto(const serialization::Type &proto) { + DCHECK(ProtoIsValid(proto)) + << "Attempted to create Type from an invalid proto description:\n" + << proto.DebugString(); + + const TypeID type_id = TypeIDFactory::ReconstructFromProto(proto.type_id()); + + switch (TypeUtil::GetMemoryLayout(type_id)) { + case kCxxInlinePod: + return GetType(type_id, proto.nullable()); + case kParInlinePod: // Fall through + case kParOutOfLinePod: + return GetType(type_id, proto.length(), proto.nullable()); + case kCxxGeneric: { + std::vector<GenericValue> parameters; + for (int i = 0; i < proto.parameters_size(); ++i) { + parameters.emplace_back(ReconstructValueFromProto(proto.parameters(i))); + } + return GetType(type_id, parameters, proto.nullable()); + } + } +} + +GenericValue TypeFactory::ReconstructValueFromProto( + const serialization::GenericValue &proto) { + const Type &type = ReconstructFromProto(proto.type()); + if (proto.has_data()) { + return GenericValue(type, + type.unmarshallValue(proto.data().c_str(), + proto.data().size()), + true /* take_ownership */); + } else { + return GenericValue(type); + } +} + +const Type* TypeFactory::GetMostSpecificType(const Type &first, const Type &second) { + if (first.isSafelyCoercibleFrom(second)) { + return &first; + } else if (second.isSafelyCoercibleFrom(first)) { + return &second; + } else { + return nullptr; + } +} + +const Type* TypeFactory::GetUnifyingType(const Type &first, const Type &second) { + // TODO: cache + const Type *unifier = nullptr; + if (first.isNullable() || second.isNullable()) { + unifier = GetMostSpecificType(first.getNullableVersion(), + second.getNullableVersion()); + if (unifier == nullptr) { + if (((first.getTypeID() == kLong) && (second.getTypeID() == kFloat)) + || ((first.getTypeID() == kFloat) && (second.getTypeID() == kLong))) { + unifier = &(DoubleType::Instance(true)); + } + } + } else { + unifier = GetMostSpecificType(first, second); + if (unifier == nullptr) { + if (((first.getTypeID() == kLong) && (second.getTypeID() == kFloat)) + || ((first.getTypeID() == kFloat) && (second.getTypeID() == kLong))) { + unifier = &(DoubleType::Instance(false)); + } + } + } + + return unifier; +} + +} // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/TypeFactoryLite.hpp ---------------------------------------------------------------------- diff --git a/types/TypeFactoryLite.hpp b/types/TypeFactoryLite.hpp new file mode 100644 index 0000000..eeafbf2 --- /dev/null +++ b/types/TypeFactoryLite.hpp @@ -0,0 +1,141 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + **/ + +#ifndef QUICKSTEP_TYPES_TYPE_FACTORY_LITE_HPP_ +#define QUICKSTEP_TYPES_TYPE_FACTORY_LITE_HPP_ + +#include <cstddef> + +#include "types/GenericValue.hpp" +#include "types/TypeID.hpp" +#include "utility/Macros.hpp" + +namespace quickstep { + +class Type; + +namespace serialization { class Type; } + +/** \addtogroup Types + * @{ + */ + +/** + * @brief All-static factory object that provides access to Types, as well as + * methods for determining coercibility of Types. + **/ +class TypeFactory { + public: + /** + * @brief Determine if a length parameter is required when getting a Type of + * the specified TypeID. + * + * @param id The id of the desired Type. + * @return Whether a length must be specified for Types of the given id. + **/ + static bool TypeRequiresLengthParameter(const TypeID id); + + /** + * @brief Factory method to get a Type by its TypeID. + * @note This version is for Types without a length parameter (currently + * IntType, LongType, FloatType, and DoubleType). It is an error to + * call this with a Type which requires a length parameter. + * + * @param id The id of the desired Type. + * @param nullable Whether to get the nullable version of the Type. + * @return The Type corresponding to id. + **/ + static const Type& GetType(const TypeID id, const bool nullable = false); + + /** + * @brief Factory method to get a Type by its TypeID and length. + * @note This version is for Types with a length parameter (currently + * CharType and VarCharType). It is an error to call this with a Type + * which does not require a length parameter. + * + * @param id The id of the desired Type. + * @param length The length parameter of the desired Type. + * @param nullable Whether to get the nullable version of the Type. + * @return The Type corresponding to id and length. + **/ + static const Type& GetType(const TypeID id, + const std::size_t length, + const bool nullable = false); + + static const Type& GetType(const TypeID id, + const std::vector<GenericValue> ¶meters, + const bool nullable = false); + + /** + * @brief Get a reference to a Type from that Type's serialized Protocol Buffer + * representation. + * + * @param proto A serialized Protocol Buffer representation of a Type, + * originally generated by getProto(). + * @return The Type described by proto. + **/ + static const Type& ReconstructFromProto(const serialization::Type &proto); + + static GenericValue ReconstructValueFromProto(const serialization::GenericValue &proto); + + /** + * @brief Check whether a serialization::Type is fully-formed and + * all parts are valid. + * + * @param proto A serialized Protocol Buffer representation of a Type, + * originally generated by getProto(). + * @return Whether proto is fully-formed and valid. + **/ + static bool ProtoIsValid(const serialization::Type &proto); + + /** + * @brief Determine which of two types is most specific, i.e. which + * isSafelyCoercibleFrom() the other. + * + * @param first The first type to check. + * @param second The second type to check. + * @return The most precise type, or NULL if neither Type + * isSafelyCoercibleFrom() the other. + **/ + static const Type* GetMostSpecificType(const Type &first, const Type &second); + + /** + * @brief Determine a type, if any exists, which both arguments can be safely + * coerced to. It is possible that the resulting type may not be + * either argument. + * + * @param first The first type to check. + * @param second The second type to check. + * @return The unifying type, or NULL if none exists. + **/ + static const Type* GetUnifyingType(const Type &first, const Type &second); + + private: + // Undefined default constructor. Class is all-static and should not be + // instantiated. + TypeFactory(); + + DISALLOW_COPY_AND_ASSIGN(TypeFactory); +}; + +/** @} */ + +} // namespace quickstep + +#endif // QUICKSTEP_TYPES_TYPE_FACTORY_LITE_HPP_ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/TypeID.cpp ---------------------------------------------------------------------- diff --git a/types/TypeID.cpp b/types/TypeID.cpp index ce29455..08c619a 100644 --- a/types/TypeID.cpp +++ b/types/TypeID.cpp @@ -33,6 +33,7 @@ const char *kTypeNames[] = { "Datetime", "DatetimeInterval", "YearMonthInterval", + "Text", "Array", "MetaType", "NullType" http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/TypeID.hpp ---------------------------------------------------------------------- diff --git a/types/TypeID.hpp b/types/TypeID.hpp index 8203253..13e0e3c 100644 --- a/types/TypeID.hpp +++ b/types/TypeID.hpp @@ -54,6 +54,7 @@ enum TypeID { kDatetime, kDatetimeInterval, kYearMonthInterval, + kText, kArray, kMetaType, kNullType, http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/TypeRegistrar.hpp ---------------------------------------------------------------------- diff --git a/types/TypeRegistrar.hpp b/types/TypeRegistrar.hpp index 9e6c50b..3a25226 100644 --- a/types/TypeRegistrar.hpp +++ b/types/TypeRegistrar.hpp @@ -22,6 +22,7 @@ #include <cstddef> #include <cstdint> +#include <string> #include <type_traits> #include <vector> @@ -85,6 +86,8 @@ REGISTER_TYPE(CharType, kChar, SuperTypeID::kAsciiString, kParInlinePod, TypedValue); REGISTER_TYPE(VarCharType, kVarChar, SuperTypeID::kAsciiString, kParOutOfLinePod, TypedValue); +REGISTER_TYPE(TextType, kText, + SuperTypeID::kOther, kCxxGeneric, std::string); REGISTER_TYPE(ArrayType, kArray, SuperTypeID::kOther, kCxxGeneric, ArrayLiteral); REGISTER_TYPE(MetaType, kMetaType, http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/TypeSynthesizer.hpp ---------------------------------------------------------------------- diff --git a/types/TypeSynthesizer.hpp b/types/TypeSynthesizer.hpp index 29267f8..f0761d1 100644 --- a/types/TypeSynthesizer.hpp +++ b/types/TypeSynthesizer.hpp @@ -338,9 +338,7 @@ class TypeSynthesizePolicy< bool checkValuesEqual(const UntypedLiteral *lhs, const UntypedLiteral *rhs, const Type &rhs_type) const override { - // LOG(FATAL) << "Not implemented"; - // TODO. - return false; + LOG(FATAL) << "Not implemented"; } UntypedLiteral* cloneValue(const UntypedLiteral *value) const override { @@ -358,14 +356,14 @@ class TypeSynthesizePolicy< return util::Hash(static_cast<const char*>(value), sizeof(cpptype)); } - TypedValue marshallValue(const UntypedLiteral *value) const override { - LOG(FATAL) << "Not implemented"; - } - - UntypedLiteral* unmarshallValue(const void *data, - const std::size_t length) const override { - LOG(FATAL) << "Not implemented"; - } +// TypedValue marshallValue(const UntypedLiteral *value) const override { +// LOG(FATAL) << "Not implemented"; +// } +// +// UntypedLiteral* unmarshallValue(const void *data, +// const std::size_t length) const override { +// LOG(FATAL) << "Not implemented"; +// } protected: TypeSynthesizePolicy(const bool nullable, http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/TypeUtil.hpp ---------------------------------------------------------------------- diff --git a/types/TypeUtil.hpp b/types/TypeUtil.hpp index 3b16c70..52fe9ae 100644 --- a/types/TypeUtil.hpp +++ b/types/TypeUtil.hpp @@ -32,8 +32,9 @@ #include "types/FloatType.hpp" #include "types/IntType.hpp" #include "types/LongType.hpp" -#include "types/MetaType.hpp" +#include "types/MetaTypeLite.hpp" #include "types/NullType.hpp" +#include "types/TextType.hpp" #include "types/Type.hpp" #include "types/TypeID.hpp" #include "types/TypeRegistrar.hpp" @@ -51,6 +52,14 @@ namespace quickstep { class TypeUtil { public: + static MemoryLayout GetMemoryLayout(const TypeID type_id) { + return InvokeOnTypeID( + type_id, + [&](auto tid) -> MemoryLayout { // NOLINT(build/c++11) + return TypeIDTrait<decltype(tid)::value>::kMemoryLayout; + }); + } + static bool IsParameterizedPod(const TypeID type_id) { return InvokeOnTypeID( type_id, http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/3a3772d9/types/operations/unary_operations/CastOperation.hpp ---------------------------------------------------------------------- diff --git a/types/operations/unary_operations/CastOperation.hpp b/types/operations/unary_operations/CastOperation.hpp index 7270dec..c0d3357 100644 --- a/types/operations/unary_operations/CastOperation.hpp +++ b/types/operations/unary_operations/CastOperation.hpp @@ -106,11 +106,6 @@ class CastOperation : public UnaryOperation { const std::string type_str = ToLower(std::string(static_cast<const char*>(type_arg.getOutOfLineData()))); - if (type_str == "text") { - return &TypeFactory::GetType( - kVarChar, type.getPrintWidth(), type.isNullable()); - } - const re2::StringPiece type_piece(type_str); std::string type_name; std::string length_str;
