Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id DF753200B32 for ; Thu, 23 Jun 2016 22:16:48 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id DE5A8160A35; Thu, 23 Jun 2016 20:16:48 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id E3D45160A69 for ; Thu, 23 Jun 2016 22:16:46 +0200 (CEST) Received: (qmail 80338 invoked by uid 500); 23 Jun 2016 20:16:46 -0000 Mailing-List: contact commits-help@quickstep.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@quickstep.incubator.apache.org Delivered-To: mailing list commits@quickstep.incubator.apache.org Received: (qmail 80329 invoked by uid 99); 23 Jun 2016 20:16:46 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Jun 2016 20:16:46 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 35960C009F for ; Thu, 23 Jun 2016 20:16:45 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.646 X-Spam-Level: X-Spam-Status: No, score=-4.646 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx2-lw-us.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id 3piafpJK63jN for ; Thu, 23 Jun 2016 20:16:32 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx2-lw-us.apache.org (ASF Mail Server at mx2-lw-us.apache.org) with SMTP id 6ECF3611FE for ; Thu, 23 Jun 2016 20:16:30 +0000 (UTC) Received: (qmail 79330 invoked by uid 99); 23 Jun 2016 20:16:28 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Jun 2016 20:16:28 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 52A12ED314; Thu, 23 Jun 2016 20:16:28 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: hakanmemisoglu@apache.org To: commits@quickstep.incubator.apache.org Date: Thu, 23 Jun 2016 20:16:41 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [15/17] incubator-quickstep git commit: QUICKSTEP-6: New fixed precision number type: Decimal. archived-at: Thu, 23 Jun 2016 20:16:49 -0000 http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/parser/preprocessed/SqlParser_gen.hpp ---------------------------------------------------------------------- diff --git a/parser/preprocessed/SqlParser_gen.hpp b/parser/preprocessed/SqlParser_gen.hpp index 0f66d1d..bc3fa46 100644 --- a/parser/preprocessed/SqlParser_gen.hpp +++ b/parser/preprocessed/SqlParser_gen.hpp @@ -1,19 +1,19 @@ -/* A Bison parser, made by GNU Bison 2.7. */ +/* A Bison parser, made by GNU Bison 3.0.4. */ /* Bison interface for Yacc-like parsers in C - - Copyright (C) 1984, 1989-1990, 2000-2012 Free Software Foundation, Inc. - + + Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc. + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program. If not, see . */ @@ -26,13 +26,13 @@ special exception, which will cause the skeleton and the resulting Bison output files to be licensed under the GNU General Public License without this special exception. - + This special exception was added by the Free Software Foundation in version 2.2 of Bison. */ #ifndef YY_QUICKSTEP_YY_SQLPARSER_GEN_HPP_INCLUDED # define YY_QUICKSTEP_YY_SQLPARSER_GEN_HPP_INCLUDED -/* Enabling traces. */ +/* Debug traces. */ #ifndef YYDEBUG # define YYDEBUG 0 #endif @@ -40,152 +40,151 @@ extern int quickstep_yydebug; #endif -/* Tokens. */ +/* Token type. */ #ifndef YYTOKENTYPE # define YYTOKENTYPE - /* Put the tokens into the symbol table, so that GDB and other debuggers - know about them. */ - enum yytokentype { - TOKEN_COMMAND = 258, - TOKEN_NAME = 259, - TOKEN_STRING_SINGLE_QUOTED = 260, - TOKEN_STRING_DOUBLE_QUOTED = 261, - TOKEN_UNSIGNED_NUMVAL = 262, - TOKEN_OR = 263, - TOKEN_AND = 264, - TOKEN_NOT = 265, - TOKEN_EQ = 266, - TOKEN_NEQ = 267, - TOKEN_GEQ = 268, - TOKEN_GT = 269, - TOKEN_LEQ = 270, - TOKEN_LT = 271, - TOKEN_REGEXP = 272, - TOKEN_LIKE = 273, - TOKEN_BETWEEN = 274, - TOKEN_IS = 275, - UNARY_MINUS = 276, - UNARY_PLUS = 277, - TOKEN_ADD = 278, - TOKEN_ALL = 279, - TOKEN_ALTER = 280, - TOKEN_AS = 281, - TOKEN_ASC = 282, - TOKEN_BIGINT = 283, - TOKEN_BIT = 284, - TOKEN_BITWEAVING = 285, - TOKEN_BLOCKPROPERTIES = 286, - TOKEN_BLOCKSAMPLE = 287, - TOKEN_BLOOM_FILTER = 288, - TOKEN_CSB_TREE = 289, - TOKEN_BY = 290, - TOKEN_CASE = 291, - TOKEN_CHARACTER = 292, - TOKEN_CHECK = 293, - TOKEN_COLUMN = 294, - TOKEN_CONSTRAINT = 295, - TOKEN_COPY = 296, - TOKEN_CREATE = 297, - TOKEN_CURRENT = 298, - TOKEN_DATE = 299, - TOKEN_DATETIME = 300, - TOKEN_DAY = 301, - TOKEN_DECIMAL = 302, - TOKEN_DEFAULT = 303, - TOKEN_DELETE = 304, - TOKEN_DELIMITER = 305, - TOKEN_DESC = 306, - TOKEN_DISTINCT = 307, - TOKEN_DOUBLE = 308, - TOKEN_DROP = 309, - TOKEN_ELSE = 310, - TOKEN_END = 311, - TOKEN_ESCAPE_STRINGS = 312, - TOKEN_EXISTS = 313, - TOKEN_EXTRACT = 314, - TOKEN_FALSE = 315, - TOKEN_FIRST = 316, - TOKEN_FLOAT = 317, - TOKEN_FOLLOWING = 318, - TOKEN_FOR = 319, - TOKEN_FOREIGN = 320, - TOKEN_FROM = 321, - TOKEN_FULL = 322, - TOKEN_GROUP = 323, - TOKEN_HASH = 324, - TOKEN_HAVING = 325, - TOKEN_HOUR = 326, - TOKEN_IN = 327, - TOKEN_INDEX = 328, - TOKEN_INNER = 329, - TOKEN_INSERT = 330, - TOKEN_INTEGER = 331, - TOKEN_INTERVAL = 332, - TOKEN_INTO = 333, - TOKEN_JOIN = 334, - TOKEN_KEY = 335, - TOKEN_LAST = 336, - TOKEN_LEFT = 337, - TOKEN_LIMIT = 338, - TOKEN_LONG = 339, - TOKEN_MINUTE = 340, - TOKEN_MONTH = 341, - TOKEN_NULL = 342, - TOKEN_NULLS = 343, - TOKEN_OFF = 344, - TOKEN_ON = 345, - TOKEN_ORDER = 346, - TOKEN_OUTER = 347, - TOKEN_OVER = 348, - TOKEN_PARTITION = 349, - TOKEN_PARTITIONS = 350, - TOKEN_PERCENT = 351, - TOKEN_PRECEDING = 352, - TOKEN_PRIMARY = 353, - TOKEN_PRIORITY = 354, - TOKEN_QUIT = 355, - TOKEN_RANGE = 356, - TOKEN_REAL = 357, - TOKEN_REFERENCES = 358, - TOKEN_RIGHT = 359, - TOKEN_ROW = 360, - TOKEN_ROW_DELIMITER = 361, - TOKEN_ROWS = 362, - TOKEN_SECOND = 363, - TOKEN_SELECT = 364, - TOKEN_SET = 365, - TOKEN_SMA = 366, - TOKEN_SMALLINT = 367, - TOKEN_SUBSTRING = 368, - TOKEN_TABLE = 369, - TOKEN_THEN = 370, - TOKEN_TIME = 371, - TOKEN_TIMESTAMP = 372, - TOKEN_TRUE = 373, - TOKEN_TUPLESAMPLE = 374, - TOKEN_UNBOUNDED = 375, - TOKEN_UNIQUE = 376, - TOKEN_UPDATE = 377, - TOKEN_USING = 378, - TOKEN_VALUES = 379, - TOKEN_VARCHAR = 380, - TOKEN_WHEN = 381, - TOKEN_WHERE = 382, - TOKEN_WINDOW = 383, - TOKEN_WITH = 384, - TOKEN_YEAR = 385, - TOKEN_YEARMONTH = 386, - TOKEN_EOF = 387, - TOKEN_LEX_ERROR = 388 - }; + enum yytokentype + { + TOKEN_COMMAND = 258, + TOKEN_NAME = 259, + TOKEN_STRING_SINGLE_QUOTED = 260, + TOKEN_STRING_DOUBLE_QUOTED = 261, + TOKEN_UNSIGNED_NUMVAL = 262, + TOKEN_OR = 263, + TOKEN_AND = 264, + TOKEN_NOT = 265, + TOKEN_EQ = 266, + TOKEN_LT = 267, + TOKEN_LEQ = 268, + TOKEN_GT = 269, + TOKEN_GEQ = 270, + TOKEN_NEQ = 271, + TOKEN_LIKE = 272, + TOKEN_REGEXP = 273, + TOKEN_BETWEEN = 274, + TOKEN_IS = 275, + UNARY_PLUS = 276, + UNARY_MINUS = 277, + TOKEN_ADD = 278, + TOKEN_ALL = 279, + TOKEN_ALTER = 280, + TOKEN_AS = 281, + TOKEN_ASC = 282, + TOKEN_BIGINT = 283, + TOKEN_BIT = 284, + TOKEN_BITWEAVING = 285, + TOKEN_BLOCKPROPERTIES = 286, + TOKEN_BLOCKSAMPLE = 287, + TOKEN_BLOOM_FILTER = 288, + TOKEN_CSB_TREE = 289, + TOKEN_BY = 290, + TOKEN_CASE = 291, + TOKEN_CHARACTER = 292, + TOKEN_CHECK = 293, + TOKEN_COLUMN = 294, + TOKEN_CONSTRAINT = 295, + TOKEN_COPY = 296, + TOKEN_CREATE = 297, + TOKEN_CURRENT = 298, + TOKEN_DATE = 299, + TOKEN_DATETIME = 300, + TOKEN_DAY = 301, + TOKEN_DECIMAL = 302, + TOKEN_DEFAULT = 303, + TOKEN_DELETE = 304, + TOKEN_DELIMITER = 305, + TOKEN_DESC = 306, + TOKEN_DISTINCT = 307, + TOKEN_DOUBLE = 308, + TOKEN_DROP = 309, + TOKEN_ELSE = 310, + TOKEN_END = 311, + TOKEN_ESCAPE_STRINGS = 312, + TOKEN_EXISTS = 313, + TOKEN_EXTRACT = 314, + TOKEN_FALSE = 315, + TOKEN_FIRST = 316, + TOKEN_FLOAT = 317, + TOKEN_FOLLOWING = 318, + TOKEN_FOR = 319, + TOKEN_FOREIGN = 320, + TOKEN_FROM = 321, + TOKEN_FULL = 322, + TOKEN_GROUP = 323, + TOKEN_HASH = 324, + TOKEN_HAVING = 325, + TOKEN_HOUR = 326, + TOKEN_IN = 327, + TOKEN_INDEX = 328, + TOKEN_INNER = 329, + TOKEN_INSERT = 330, + TOKEN_INTEGER = 331, + TOKEN_INTERVAL = 332, + TOKEN_INTO = 333, + TOKEN_JOIN = 334, + TOKEN_KEY = 335, + TOKEN_LAST = 336, + TOKEN_LEFT = 337, + TOKEN_LIMIT = 338, + TOKEN_LONG = 339, + TOKEN_MINUTE = 340, + TOKEN_MONTH = 341, + TOKEN_NULL = 342, + TOKEN_NULLS = 343, + TOKEN_OFF = 344, + TOKEN_ON = 345, + TOKEN_ORDER = 346, + TOKEN_OUTER = 347, + TOKEN_OVER = 348, + TOKEN_PARTITION = 349, + TOKEN_PARTITIONS = 350, + TOKEN_PERCENT = 351, + TOKEN_PRECEDING = 352, + TOKEN_PRIMARY = 353, + TOKEN_PRIORITY = 354, + TOKEN_QUIT = 355, + TOKEN_RANGE = 356, + TOKEN_REAL = 357, + TOKEN_REFERENCES = 358, + TOKEN_RIGHT = 359, + TOKEN_ROW = 360, + TOKEN_ROW_DELIMITER = 361, + TOKEN_ROWS = 362, + TOKEN_SECOND = 363, + TOKEN_SELECT = 364, + TOKEN_SET = 365, + TOKEN_SMA = 366, + TOKEN_SMALLINT = 367, + TOKEN_SUBSTRING = 368, + TOKEN_TABLE = 369, + TOKEN_THEN = 370, + TOKEN_TIME = 371, + TOKEN_TIMESTAMP = 372, + TOKEN_TRUE = 373, + TOKEN_TUPLESAMPLE = 374, + TOKEN_UNBOUNDED = 375, + TOKEN_UNIQUE = 376, + TOKEN_UPDATE = 377, + TOKEN_USING = 378, + TOKEN_VALUES = 379, + TOKEN_VARCHAR = 380, + TOKEN_WHEN = 381, + TOKEN_WHERE = 382, + TOKEN_WINDOW = 383, + TOKEN_WITH = 384, + TOKEN_YEAR = 385, + TOKEN_YEARMONTH = 386, + TOKEN_EOF = 387, + TOKEN_LEX_ERROR = 388 + }; #endif - +/* Value type. */ #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED -typedef union YYSTYPE + +union YYSTYPE { -/* Line 2058 of yacc.c */ -#line 120 "../SqlParser.ypp" +#line 120 "../SqlParser.ypp" /* yacc.c:1909 */ quickstep::ParseString *string_value_; @@ -285,41 +284,30 @@ typedef union YYSTYPE quickstep::ParsePriority *opt_priority_clause_; +#line 288 "SqlParser_gen.hpp" /* yacc.c:1909 */ +}; -/* Line 2058 of yacc.c */ -#line 291 "SqlParser_gen.hpp" -} YYSTYPE; +typedef union YYSTYPE YYSTYPE; # define YYSTYPE_IS_TRIVIAL 1 -# define yystype YYSTYPE /* obsolescent; will be withdrawn */ # define YYSTYPE_IS_DECLARED 1 #endif +/* Location type. */ #if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED -typedef struct YYLTYPE +typedef struct YYLTYPE YYLTYPE; +struct YYLTYPE { int first_line; int first_column; int last_line; int last_column; -} YYLTYPE; -# define yyltype YYLTYPE /* obsolescent; will be withdrawn */ +}; # define YYLTYPE_IS_DECLARED 1 # define YYLTYPE_IS_TRIVIAL 1 #endif -#ifdef YYPARSE_PARAM -#if defined __STDC__ || defined __cplusplus -int quickstep_yyparse (void *YYPARSE_PARAM); -#else -int quickstep_yyparse (); -#endif -#else /* ! YYPARSE_PARAM */ -#if defined __STDC__ || defined __cplusplus + int quickstep_yyparse (yyscan_t yyscanner, quickstep::ParseStatement **parsedStatement); -#else -int quickstep_yyparse (); -#endif -#endif /* ! YYPARSE_PARAM */ #endif /* !YY_QUICKSTEP_YY_SQLPARSER_GEN_HPP_INCLUDED */ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt index 0ccdfd7..16a6f1e 100644 --- a/types/CMakeLists.txt +++ b/types/CMakeLists.txt @@ -35,6 +35,8 @@ add_library(quickstep_types_DateOperatorOverloads ../empty_src.cpp DateOperatorO add_library(quickstep_types_DatetimeIntervalType DatetimeIntervalType.cpp DatetimeIntervalType.hpp) add_library(quickstep_types_DatetimeLit ../empty_src.cpp DatetimeLit.hpp) add_library(quickstep_types_DatetimeType DatetimeType.cpp DatetimeType.hpp) +add_library(quickstep_types_DecimalLit ../empty_src.cpp DecimalLit.hpp) +add_library(quickstep_types_DecimalType DecimalType.cpp DecimalType.hpp) add_library(quickstep_types_DoubleType DoubleType.cpp DoubleType.hpp) add_library(quickstep_types_FloatType FloatType.cpp FloatType.hpp) add_library(quickstep_types_IntType IntType.cpp IntType.hpp) @@ -94,6 +96,16 @@ target_link_libraries(quickstep_types_DatetimeType quickstep_types_port_timegm quickstep_utility_CheckSnprintf quickstep_utility_Macros) +target_link_libraries(quickstep_types_DecimalType + glog + quickstep_types_DecimalLit + quickstep_types_NullCoercibilityCheckMacro + quickstep_types_NumericSuperType + quickstep_types_Type + quickstep_types_TypeID + quickstep_types_TypedValue + quickstep_utility_EqualsAnyConstant + quickstep_utility_Macros) target_link_libraries(quickstep_types_DoubleType quickstep_types_NullCoercibilityCheckMacro quickstep_types_NumericSuperType @@ -158,6 +170,7 @@ target_link_libraries(quickstep_types_TypeFactory quickstep_types_CharType quickstep_types_DatetimeIntervalType quickstep_types_DatetimeType + quickstep_types_DecimalType quickstep_types_DoubleType quickstep_types_FloatType quickstep_types_IntType @@ -173,6 +186,7 @@ target_link_libraries(quickstep_types_TypedValue farmhash glog quickstep_types_DatetimeLit + quickstep_types_DecimalLit quickstep_types_IntervalLit quickstep_types_TypeID quickstep_types_Type_proto @@ -213,6 +227,8 @@ target_link_libraries(quickstep_types quickstep_types_DatetimeIntervalType quickstep_types_DatetimeLit quickstep_types_DatetimeType + quickstep_types_DecimalLit + quickstep_types_DecimalType quickstep_types_DoubleType quickstep_types_FloatType quickstep_types_IntType @@ -295,6 +311,17 @@ target_link_libraries(DatetimeType_unittest quickstep_utility_MemStream) add_test(DatetimeType_unittest DatetimeType_unittest) +add_executable(DecimalType_unittest "${CMAKE_CURRENT_SOURCE_DIR}/tests/DecimalType_unittest.cpp") +target_link_libraries(DecimalType_unittest + gtest + gtest_main + quickstep_types_DecimalLit + quickstep_types_DecimalType + quickstep_types_Type + quickstep_types_TypeFactory + quickstep_types_TypedValue) +add_test(DecimalType_unittest DecimalType_unittest) + add_executable(DoubleType_unittest "${CMAKE_CURRENT_SOURCE_DIR}/tests/DoubleType_unittest.cpp") target_link_libraries(DoubleType_unittest glog http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/DecimalLit.hpp ---------------------------------------------------------------------- diff --git a/types/DecimalLit.hpp b/types/DecimalLit.hpp new file mode 100644 index 0000000..db21b4d --- /dev/null +++ b/types/DecimalLit.hpp @@ -0,0 +1,293 @@ +/** + * 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. + * limitations under the License. + **/ + +#ifndef QUICKSTEP_TYPES_DECIMAL_LIT_HPP_ +#define QUICKSTEP_TYPES_DECIMAL_LIT_HPP_ + +#include +#include + +namespace quickstep { + +/** \addtogroup Type + * @{ + */ + +/** + * @brief A literal representing fixed-precision decimal. + **/ +struct DecimalLit { + /** + * @brief Underlying data type. + **/ + typedef std::int64_t data_type; + + DecimalLit() = default; + + explicit DecimalLit(const int value) + : data_(static_cast(value * kMaxFractionInt)) { + } + + explicit DecimalLit(const std::int64_t value) + : data_(static_cast(value * kMaxFractionInt)) { + } + + explicit DecimalLit(const float value) + : data_(static_cast(value * kMaxFractionInt)) { + } + + explicit DecimalLit(const double value) + : data_(static_cast(value * kMaxFractionInt)) { + } + + inline explicit operator int() const { + return static_cast(getIntegerPart()); + } + + inline explicit operator std::int64_t() const { + return static_cast(getIntegerPart()); + } + + inline explicit operator float() const { + return static_cast(data_) / kMaxFractionInt; + } + + inline explicit operator double() const { + return static_cast(data_) / kMaxFractionInt; + } + + data_type data_; + + /** + * @brief Number of decimals after point. + **/ + static constexpr std::int64_t kPrecisionWidth = 2; + + /** + * @brief Normalization factor between Decimal type and floating point types. + * It is always equal to pow(10, kPrecisionWidth). + **/ + static constexpr std::int64_t kMaxFractionInt = 100; + + /** + * @brief Getter for the fractional part of the Decimal type. + * + * @return Fractional part of the Decimal number as unsigned. + **/ + inline std::uint64_t getFractionalPart() const { + return static_cast(static_cast(std::abs(data_)) % kMaxFractionInt); + } + + /** + * @brief Getter for the integer part of the Decimal type. + * + * @return Integer part of the Decimal number as signed. + **/ + inline std::int64_t getIntegerPart() const { + return static_cast(data_ / kMaxFractionInt); + } + + /** + * @brief Operator overloading for "less than". + * + * @param other Other DecimalLit to be compared. + * @return True if this is less than other, + * false otherwise. + **/ + inline bool operator<(const DecimalLit& other) const { + return data_ < other.data_; + } + + /** + * @brief Operator overloading for "greater than". + * + * @param other Other DecimalLit to be compared. + * @return True if this is greater than other, + * false otherwise. + **/ + inline bool operator>(const DecimalLit& other) const { + return data_ > other.data_; + } + + /** + * @brief Operator overloading for "less than or equal to". + * + * @param other Other DecimalLit to be compared. + * @return True if this is less than or equal to other, + * false otherwise. + **/ + inline bool operator<=(const DecimalLit& other) const { + return data_ <= other.data_; + } + + /** + * @brief Operator overloading for "greater than or equal to". + * + * @param other Other DecimalLit to be compared. + * @return True if this is greater than or equal to other, + * false otherwise. + **/ + inline bool operator>=(const DecimalLit& other) const { + return data_ >= other.data_; + } + + /** + * @brief Operator overloading for "equal to". + * + * @param other Other DecimalLit to be compared. + * @return True if this is equal to other, + * false otherwise. + **/ + inline bool operator==(const DecimalLit& other) const { + return data_ == other.data_; + } + + /** + * @brief Operator overloading for "not equal to". + * + * @param other Other DecimalLit to be compared. + * @return True if this is not equal to other, + * false otherwise. + **/ + inline bool operator!=(const DecimalLit& other) const { + return data_ != other.data_; + } + + /** + * @brief Operator overloading for "negate". + * + * @return Negative of this. + **/ + inline DecimalLit operator-() const { + DecimalLit result; + result.data_ = -result.data_; + return result; + } + + /** + * @brief Operator overloading for "plus". + * + * @param other Other DecimalLit to be added. + * @return Sum of this and other. + **/ + inline DecimalLit operator+(const DecimalLit& other) const { + DecimalLit result; + result.data_ = data_ + other.data_; + return result; + } + + /** + * @brief Operator overloading for "subtract". + * + * @param other Other DecimalLit to be subtract. + * @return Subtraction of other from this. + **/ + inline DecimalLit operator-(const DecimalLit& other) const { + DecimalLit result; + result.data_ = data_ - other.data_; + return result; + } + + /** + * @brief Operator overloading for "multiply". + * + * @param other Other DecimalLit to be multiplied. + * @return Multiplication of this and other. + **/ + inline DecimalLit operator*(const DecimalLit& other) const { + DecimalLit result; + result.data_ = (data_ * other.data_) / kMaxFractionInt; + return result; + } + + /** + * @brief Operator overloading for "division". + * + * @param other Divisor DecimalLit. + * @return Division of this with other. + **/ + inline DecimalLit operator/(const DecimalLit& other) const { + DecimalLit result; + result.data_ = (data_ * kMaxFractionInt) / other.data_; + return result; + } + + /** + * @brief Operator overloading for "modulo". + * + * @param other Mod DecimalLit. + * @return This modulo other. + **/ + inline DecimalLit operator%(const DecimalLit& other) const { + DecimalLit result; + result.data_ = data_ % other.data_; + return result; + } + + /** + * @brief Operator overloading for "inplace add". + * + * @param other DecimalLit to be added. + * @return Reference to this. + **/ + inline DecimalLit& operator+=(const DecimalLit& other) { + data_ += other.data_; + return *this; + } + + /** + * @brief Operator overloading for "inplace subtract". + * + * @param other DecimalLit to be subtracted. + * @return Reference to this. + **/ + inline DecimalLit& operator-=(const DecimalLit& other) { + data_ -= other.data_; + return *this; + } + + /** + * @brief Operator overloading for "inplace multiply". + * + * @param other DecimalLit to be multiplied. + * @return Reference to this. + **/ + inline DecimalLit& operator*=(const DecimalLit& other) { + data_ = (data_ * other.data_) / kMaxFractionInt; + return *this; + } + + /** + * @brief Operator overloading for "inplace divide". + * + * @param other DecimalLit to be divided. + * @return Reference to this. + **/ + inline DecimalLit& operator/=(const DecimalLit& other) { + data_ = (data_ * kMaxFractionInt) / other.data_; + return *this; + } +}; + +//** @} */ + +} // namespace quickstep + +#endif // QUICKSTEP_TYPES_DECIMAL_LIT_HPP_ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/DecimalType.cpp ---------------------------------------------------------------------- diff --git a/types/DecimalType.cpp b/types/DecimalType.cpp new file mode 100644 index 0000000..661dad1 --- /dev/null +++ b/types/DecimalType.cpp @@ -0,0 +1,120 @@ +/** + * 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. + * limitations under the License. + **/ + +#include "types/DecimalType.hpp" + +#include +#include +#include +#include +#include + +#include "types/DecimalLit.hpp" +#include "types/NullCoercibilityCheckMacro.hpp" +#include "types/Type.hpp" +#include "types/TypeID.hpp" +#include "types/TypedValue.hpp" +#include "utility/EqualsAnyConstant.hpp" + +#include "glog/logging.h" + +namespace quickstep { + +class Type; + +const TypeID DecimalType::kStaticTypeID = kDecimal; + +bool DecimalType::isSafelyCoercibleFrom(const Type &original_type) const { + QUICKSTEP_NULL_COERCIBILITY_CHECK(); + return QUICKSTEP_EQUALS_ANY_CONSTANT(original_type.getTypeID(), + kInt, kDecimal); +} + +TypedValue DecimalType::coerceValue(const TypedValue &original_value, + const Type &original_type) const { + DCHECK(isCoercibleFrom(original_type)) + << "Can't coerce value of Type " << original_type.getName() + << " to Type " << getName(); + + if (original_value.isNull()) { + return makeNullValue(); + } + + switch (original_type.getTypeID()) { + case kInt: + return TypedValue(DecimalLit(original_value.getLiteral())); + case kLong: + return TypedValue(DecimalLit(original_value.getLiteral())); + case kFloat: + return TypedValue(DecimalLit(original_value.getLiteral())); + case kDouble: + return original_value; + default: + LOG(FATAL) << "Attempted to coerce Type " << original_type.getName() + << " (not recognized as a numeric Type) to " << getName(); + } +} + +std::string DecimalType::printValueToString(const TypedValue &value) const { + DCHECK(!value.isNull()); + + DecimalLit decimal = value.getLiteral(); + std::stringstream ss; + ss << decimal.getIntegerPart() << "." + << std::setfill('0') << std::setw(DecimalLit::kPrecisionWidth) + << decimal.getFractionalPart(); + return ss.str(); +} + +void DecimalType::printValueToFile(const TypedValue &value, + FILE *file, + const int padding) const { + DCHECK(!value.isNull()); + + DecimalLit decimal = value.getLiteral(); + + std::fprintf(file, "%*ld.%0*lu", + static_cast(padding - + (DecimalLit::kPrecisionWidth + + 1 /* Less one space for point. */)), + decimal.getIntegerPart(), + static_cast(DecimalLit::kPrecisionWidth), + decimal.getFractionalPart()); +} + +bool DecimalType::parseValueFromString(const std::string &value_string, + TypedValue *value) const { + double parsed_double; + int read_chars; + + int matched = std::sscanf(value_string.c_str(), + "%lf%n", + &parsed_double, + &read_chars); + + if (matched != 1 || read_chars != static_cast(value_string.length())) { + return false; + } + + *value = TypedValue(DecimalLit(parsed_double)); + return true; +} + +} // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/DecimalType.hpp ---------------------------------------------------------------------- diff --git a/types/DecimalType.hpp b/types/DecimalType.hpp new file mode 100644 index 0000000..a028290 --- /dev/null +++ b/types/DecimalType.hpp @@ -0,0 +1,124 @@ +/** + * 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. + * limitations under the License. + **/ + +#ifndef QUICKSTEP_TYPES_DECIMAL_TYPE_HPP_ +#define QUICKSTEP_TYPES_DECIMAL_TYPE_HPP_ + +#include +#include + +#include "types/DecimalLit.hpp" +#include "types/NumericSuperType.hpp" +#include "types/TypeID.hpp" +#include "types/TypedValue.hpp" +#include "utility/Macros.hpp" + +namespace quickstep { + +class Type; + +/** \addtogroup Types + * @{ + */ + +/** + * @brief A type representing a fixed-precision number. + **/ +class DecimalType : public NumericSuperType { + public: + static const TypeID kStaticTypeID; + + /** + * @brief Get a reference to the non-nullable singleton instance of this + * Type. + * + * @return A reference to the non-nullable singleton instance of this Type. + **/ + static const DecimalType& InstanceNonNullable() { + static DecimalType instance(false); + return instance; + } + + /** + * @brief Get a reference to the nullable singleton instance of this Type. + * + * @return A reference to the nullable singleton instance of this Type. + **/ + static const DecimalType& InstanceNullable() { + static DecimalType instance(true); + return instance; + } + + static const DecimalType& Instance(const bool nullable) { + if (nullable) { + return InstanceNullable(); + } else { + return InstanceNonNullable(); + } + } + + const Type& getNullableVersion() const override { + return InstanceNullable(); + } + + const Type& getNonNullableVersion() const override { + return InstanceNonNullable(); + } + + bool isSafelyCoercibleFrom(const Type &original_type) const override; + + int getPrintWidth() const override { + return kPrintWidth; + } + + std::string printValueToString(const TypedValue &value) const override; + + void printValueToFile(const TypedValue &value, + FILE *file, + const int padding = 0) const override; + + bool parseValueFromString(const std::string &value_string, + TypedValue *value) const override; + + TypedValue coerceValue(const TypedValue &original_value, + const Type &original_type) const override; + + TypedValue makeZeroValue() const override { + return TypedValue(DecimalLit(0)); + } + + private: + static constexpr int kPrintWidth = + std::numeric_limits::digits10 + + 1 // Decimal point '.' + + 1; // Minus sign '-' + + explicit DecimalType(const bool nullable) + : NumericSuperType(kDecimal, nullable) { + } + + DISALLOW_COPY_AND_ASSIGN(DecimalType); +}; + +/** @} */ + +} // namespace quickstep + +#endif // QUICKSTEP_TYPES_DECIMAL_TYPE_HPP_ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/NumericSuperType.hpp ---------------------------------------------------------------------- diff --git a/types/NumericSuperType.hpp b/types/NumericSuperType.hpp index ec487e7..ac4183b 100644 --- a/types/NumericSuperType.hpp +++ b/types/NumericSuperType.hpp @@ -51,7 +51,7 @@ class NumericSuperType : public Type { } TypedValue makeZeroValue() const override { - return TypedValue(static_cast(0)); + return TypedValue(CppType()); } protected: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/NumericTypeUnifier.hpp ---------------------------------------------------------------------- diff --git a/types/NumericTypeUnifier.hpp b/types/NumericTypeUnifier.hpp index 6ea8510..c2fb949 100644 --- a/types/NumericTypeUnifier.hpp +++ b/types/NumericTypeUnifier.hpp @@ -19,6 +19,7 @@ namespace quickstep { +class DecimalType; class DoubleType; class FloatType; class IntType; @@ -76,6 +77,11 @@ struct NumericTypeUnifier { }; template<> +struct NumericTypeUnifier { + typedef DecimalType type; +}; + +template<> struct NumericTypeUnifier { typedef LongType type; }; @@ -96,6 +102,11 @@ struct NumericTypeUnifier { }; template<> +struct NumericTypeUnifier { + typedef DecimalType type; +}; + +template<> struct NumericTypeUnifier { typedef FloatType type; }; @@ -116,6 +127,11 @@ struct NumericTypeUnifier { }; template<> +struct NumericTypeUnifier { + typedef DecimalType type; +}; + +template<> struct NumericTypeUnifier { typedef DoubleType type; }; @@ -135,6 +151,36 @@ struct NumericTypeUnifier { typedef DoubleType type; }; +template<> +struct NumericTypeUnifier { + typedef DecimalType type; +}; + +template<> +struct NumericTypeUnifier { + typedef DecimalType type; +}; + +template<> +struct NumericTypeUnifier { + typedef DecimalType type; +}; + +template<> +struct NumericTypeUnifier { + typedef DecimalType type; +}; + +template<> +struct NumericTypeUnifier { + typedef DecimalType type; +}; + +template<> +struct NumericTypeUnifier { + typedef DecimalType type; +}; + } // namespace quickstep #endif // QUICKSTEP_TYPES_NUMERIC_TYPE_UNIFIER_HPP_ http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/Type.cpp ---------------------------------------------------------------------- diff --git a/types/Type.cpp b/types/Type.cpp index 8981bc2..4f199f1 100644 --- a/types/Type.cpp +++ b/types/Type.cpp @@ -41,6 +41,9 @@ serialization::Type Type::getProto() const { case kDouble: proto.set_type_id(serialization::Type::DOUBLE); break; + case kDecimal: + proto.set_type_id(serialization::Type::DECIMAL); + break; case kDatetime: proto.set_type_id(serialization::Type::DATETIME); break; http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/Type.hpp ---------------------------------------------------------------------- diff --git a/types/Type.hpp b/types/Type.hpp index fc6f4e2..33f38aa 100644 --- a/types/Type.hpp +++ b/types/Type.hpp @@ -34,6 +34,7 @@ namespace quickstep { struct DatetimeIntervalLit; struct DatetimeLit; +struct DecimalLit; struct YearMonthIntervalLit; /** \addtogroup Types @@ -370,6 +371,8 @@ class Type { return TypedValue(*static_cast(value_ptr)); case kDouble: return TypedValue(*static_cast(value_ptr)); + case kDecimal: + return TypedValue(*static_cast(value_ptr)); case kDatetime: return TypedValue(*static_cast(value_ptr)); case kDatetimeInterval: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/Type.proto ---------------------------------------------------------------------- diff --git a/types/Type.proto b/types/Type.proto index dbf248e..337d962 100644 --- a/types/Type.proto +++ b/types/Type.proto @@ -25,10 +25,11 @@ message Type { DOUBLE = 3; CHAR = 4; VAR_CHAR = 5; - DATETIME = 6; - DATETIME_INTERVAL = 7; - YEAR_MONTH_INTERVAL = 8; - NULL_TYPE = 9; + DECIMAL = 6; + DATETIME = 7; + DATETIME_INTERVAL = 8; + YEAR_MONTH_INTERVAL = 9; + NULL_TYPE = 10; } required TypeID type_id = 1; http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/TypeFactory.cpp ---------------------------------------------------------------------- diff --git a/types/TypeFactory.cpp b/types/TypeFactory.cpp index a8f2961..e0b52d8 100644 --- a/types/TypeFactory.cpp +++ b/types/TypeFactory.cpp @@ -22,6 +22,7 @@ #include "types/CharType.hpp" #include "types/DatetimeIntervalType.hpp" #include "types/DatetimeType.hpp" +#include "types/DecimalType.hpp" #include "types/DoubleType.hpp" #include "types/FloatType.hpp" #include "types/IntType.hpp" @@ -49,6 +50,8 @@ const Type& TypeFactory::GetType(const TypeID id, return FloatType::Instance(nullable); case kDouble: return DoubleType::Instance(nullable); + case kDecimal: + return DecimalType::Instance(nullable); case kDatetime: return DatetimeType::Instance(nullable); case kDatetimeInterval: @@ -90,6 +93,7 @@ bool TypeFactory::ProtoIsValid(const serialization::Type &proto) { case serialization::Type::LONG: case serialization::Type::FLOAT: case serialization::Type::DOUBLE: + case serialization::Type::DECIMAL: case serialization::Type::DATETIME: case serialization::Type::DATETIME_INTERVAL: case serialization::Type::YEAR_MONTH_INTERVAL: @@ -119,6 +123,8 @@ const Type& TypeFactory::ReconstructFromProto(const serialization::Type &proto) return FloatType::Instance(proto.nullable()); case serialization::Type::DOUBLE: return DoubleType::Instance(proto.nullable()); + case serialization::Type::DECIMAL: + return DecimalType::Instance(proto.nullable()); case serialization::Type::DATETIME: return DatetimeType::Instance(proto.nullable()); case serialization::Type::DATETIME_INTERVAL: @@ -155,6 +161,8 @@ const Type* TypeFactory::GetUnifyingType(const Type &first, const Type &second) if (((first.getTypeID() == kLong) && (second.getTypeID() == kFloat)) || ((first.getTypeID() == kFloat) && (second.getTypeID() == kLong))) { unifier = &(DoubleType::Instance(true)); + } else if (first.getTypeID() == kDecimal || second.getTypeID() == kDecimal) { + unifier = &(DecimalType::Instance(true)); } } } else { @@ -163,6 +171,8 @@ const Type* TypeFactory::GetUnifyingType(const Type &first, const Type &second) if (((first.getTypeID() == kLong) && (second.getTypeID() == kFloat)) || ((first.getTypeID() == kFloat) && (second.getTypeID() == kLong))) { unifier = &(DoubleType::Instance(false)); + } else if (first.getTypeID() == kDecimal || second.getTypeID() == kDecimal) { + unifier = &(DecimalType::Instance(false)); } } } http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/TypeFactory.hpp ---------------------------------------------------------------------- diff --git a/types/TypeFactory.hpp b/types/TypeFactory.hpp index ed9be5e..029d2bb 100644 --- a/types/TypeFactory.hpp +++ b/types/TypeFactory.hpp @@ -52,6 +52,7 @@ class TypeFactory { case kLong: case kFloat: case kDouble: + case kDecimal: case kDatetime: case kDatetimeInterval: case kYearMonthInterval: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/TypeID.cpp ---------------------------------------------------------------------- diff --git a/types/TypeID.cpp b/types/TypeID.cpp index 1aeea04..901b232 100644 --- a/types/TypeID.cpp +++ b/types/TypeID.cpp @@ -26,6 +26,7 @@ const char *kTypeNames[] = { "Double", "Char", "VarChar", + "Decimal", "Datetime", "DatetimeInterval", "YearMonthInterval", http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/TypeID.hpp ---------------------------------------------------------------------- diff --git a/types/TypeID.hpp b/types/TypeID.hpp index 23d32da..ab52b96 100644 --- a/types/TypeID.hpp +++ b/types/TypeID.hpp @@ -34,6 +34,7 @@ enum TypeID { kDouble, kChar, kVarChar, + kDecimal, kDatetime, kDatetimeInterval, kYearMonthInterval, http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/TypedValue.cpp ---------------------------------------------------------------------- diff --git a/types/TypedValue.cpp b/types/TypedValue.cpp index bc1ebd9..2989c37 100644 --- a/types/TypedValue.cpp +++ b/types/TypedValue.cpp @@ -49,6 +49,7 @@ bool TypedValue::isPlausibleInstanceOf(const TypeSignature type) const { case kLong: case kFloat: case kDouble: + case kDecimal: case kDatetime: case kDatetimeInterval: case kYearMonthInterval: @@ -104,6 +105,12 @@ serialization::TypedValue TypedValue::getProto() const { proto.set_double_value(getLiteral()); } break; + case kDecimal: + proto.set_type_id(serialization::Type::DECIMAL); + if (!isNull()) { + proto.set_decimal_value(value_union_.decimal_value.data_); + } + break; case kDatetime: proto.set_type_id(serialization::Type::DATETIME); if (!isNull()) { @@ -171,6 +178,15 @@ TypedValue TypedValue::ReconstructFromProto(const serialization::TypedValue &pro return proto.has_double_value() ? TypedValue(static_cast(proto.double_value())) : TypedValue(kDouble); + case serialization::Type::DECIMAL: { + if (proto.has_decimal_value()) { + DecimalLit result; + result.data_ = proto.decimal_value(); + return TypedValue(result); + } else { + return TypedValue(kDecimal); + } + } case serialization::Type::DATETIME: if (proto.has_datetime_value()) { DatetimeLit datetime; http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/TypedValue.hpp ---------------------------------------------------------------------- diff --git a/types/TypedValue.hpp b/types/TypedValue.hpp index 6e22111..06d69ad 100644 --- a/types/TypedValue.hpp +++ b/types/TypedValue.hpp @@ -25,6 +25,7 @@ #include #include "types/DatetimeLit.hpp" +#include "types/DecimalLit.hpp" #include "types/IntervalLit.hpp" #include "types/TypeID.hpp" #include "types/TypedValue.pb.h" @@ -126,6 +127,11 @@ class TypedValue { value_union_.double_value = literal_double == 0 ? 0 : literal_double; } + explicit TypedValue(const DecimalLit literal_decimal) + : value_info_(static_cast(kDecimal)) { + value_union_.decimal_value = literal_decimal; + } + /** * @brief Constructor for a literal value of DatetimeType. **/ @@ -276,6 +282,7 @@ class TypedValue { case kLong: case kFloat: case kDouble: + case kDecimal: case kDatetime: case kDatetimeInterval: case kYearMonthInterval: @@ -307,6 +314,7 @@ class TypedValue { return sizeof(value_union_.int_value) <= sizeof(std::size_t); case kLong: case kDouble: + case kDecimal: case kDatetime: case kDatetimeInterval: case kYearMonthInterval: @@ -384,6 +392,7 @@ class TypedValue { return sizeof(int); case kLong: case kDouble: + case kDecimal: case kDatetime: case kDatetimeInterval: case kYearMonthInterval: @@ -469,6 +478,7 @@ class TypedValue { || getTypeID() == kLong || getTypeID() == kFloat || getTypeID() == kDouble + || getTypeID() == kDecimal || getTypeID() == kDatetime || getTypeID() == kDatetimeInterval || getTypeID() == kYearMonthInterval)); @@ -564,6 +574,7 @@ class TypedValue { case kLong: case kFloat: case kDouble: + case kDecimal: case kDatetime: case kDatetimeInterval: case kYearMonthInterval: @@ -659,6 +670,7 @@ class TypedValue { case kLong: case kFloat: case kDouble: + case kDecimal: case kDatetime: case kDatetimeInterval: case kYearMonthInterval: @@ -778,6 +790,7 @@ class TypedValue { float float_value; double double_value; const void* out_of_line_data; + DecimalLit decimal_value; DatetimeLit datetime_value; DatetimeIntervalLit datetime_interval_value; YearMonthIntervalLit year_month_interval_value; @@ -804,6 +817,7 @@ class TypedValue { static_assert(sizeof(ValueUnion) == sizeof(std::int64_t) && sizeof(ValueUnion) == sizeof(double) + && sizeof(ValueUnion) == sizeof(DecimalLit) && sizeof(ValueUnion) == sizeof(DatetimeLit) && sizeof(ValueUnion) == sizeof(DatetimeIntervalLit) && sizeof(ValueUnion) == sizeof(YearMonthIntervalLit), @@ -853,6 +867,13 @@ inline double TypedValue::getLiteral() const { } template <> +inline DecimalLit TypedValue::getLiteral() const { + DCHECK_EQ(kDecimal, getTypeID()); + DCHECK(!isNull()); + return value_union_.decimal_value; +} + +template <> inline DatetimeLit TypedValue::getLiteral() const { DCHECK_EQ(kDatetime, getTypeID()); DCHECK(!isNull()); http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/TypedValue.proto ---------------------------------------------------------------------- diff --git a/types/TypedValue.proto b/types/TypedValue.proto index 78a38cb..a2b52f3 100644 --- a/types/TypedValue.proto +++ b/types/TypedValue.proto @@ -28,7 +28,8 @@ message TypedValue { optional float float_value = 4; optional double double_value = 5; optional bytes out_of_line_data = 6; - optional int64 datetime_value = 7; - optional int64 datetime_interval_value = 8; - optional int64 year_month_interval_value = 9; + optional int64 decimal_value = 7; + optional int64 datetime_value = 8; + optional int64 datetime_interval_value = 9; + optional int64 year_month_interval_value = 10; } http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/operations/binary_operations/AddBinaryOperation.cpp ---------------------------------------------------------------------- diff --git a/types/operations/binary_operations/AddBinaryOperation.cpp b/types/operations/binary_operations/AddBinaryOperation.cpp index 6e6e839..18118d0 100644 --- a/types/operations/binary_operations/AddBinaryOperation.cpp +++ b/types/operations/binary_operations/AddBinaryOperation.cpp @@ -44,7 +44,8 @@ bool AddBinaryOperation::canApplyToTypes(const Type &left, const Type &right) co case kInt: // Fall through. case kLong: case kFloat: - case kDouble: { + case kDouble: + case kDecimal: { return (right.getSuperTypeID() == Type::kNumeric); } case kDatetime: { @@ -229,12 +230,14 @@ TypedValue AddBinaryOperation::applyToChecked(const TypedValue &left, case kInt: case kLong: case kFloat: - case kDouble: { + case kDouble: + case kDecimal: { switch (right_type.getTypeID()) { case kInt: case kLong: case kFloat: case kDouble: + case kDecimal: return applyToCheckedNumericHelper(left, left_type, right, right_type); default: @@ -304,7 +307,8 @@ UncheckedBinaryOperator* AddBinaryOperation::makeUncheckedBinaryOperatorForTypes case kInt: case kLong: case kFloat: - case kDouble: { + case kDouble: + case kDecimal: { if (right.getSuperTypeID() == Type::kNumeric) { return makeNumericBinaryOperatorOuterHelper(left, right); } http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/operations/binary_operations/ArithmeticBinaryOperation.hpp ---------------------------------------------------------------------- diff --git a/types/operations/binary_operations/ArithmeticBinaryOperation.hpp b/types/operations/binary_operations/ArithmeticBinaryOperation.hpp index 3cad289..931c805 100644 --- a/types/operations/binary_operations/ArithmeticBinaryOperation.hpp +++ b/types/operations/binary_operations/ArithmeticBinaryOperation.hpp @@ -22,6 +22,7 @@ #include +#include "types/DecimalType.hpp" #include "types/DoubleType.hpp" #include "types/FloatType.hpp" #include "types/IntType.hpp" @@ -309,6 +310,14 @@ UncheckedBinaryOperator* ArithmeticBinaryOperation::makeNumericBinaryOperatorOut return makeNumericBinaryOperatorInnerHelper( left, right); } + case kDecimal: + if (left.isNullable()) { + return makeNumericBinaryOperatorInnerHelper(left, right); + } else { + return makeNumericBinaryOperatorInnerHelper(left, right); + } default: throw OperationInapplicableToType(getName(), 2, left.getName().c_str(), right.getName().c_str()); } @@ -362,6 +371,16 @@ UncheckedBinaryOperator* ArithmeticBinaryOperation::makeNumericBinaryOperatorInn typename LeftType::cpptype, left_nullable, typename DoubleType::cpptype, false>(); } + case kDecimal: + if (right.isNullable()) { + return new OperatorType::type, + typename LeftType::cpptype, left_nullable, + typename DecimalType::cpptype, true>(); + } else { + return new OperatorType::type, + typename LeftType::cpptype, left_nullable, + typename DecimalType::cpptype, false>(); + } default: throw OperationInapplicableToType(getName(), 2, left.getName().c_str(), right.getName().c_str()); } http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/8570a906/types/operations/binary_operations/ArithmeticBinaryOperators.hpp ---------------------------------------------------------------------- diff --git a/types/operations/binary_operations/ArithmeticBinaryOperators.hpp b/types/operations/binary_operations/ArithmeticBinaryOperators.hpp index e5e1493..408a16b 100644 --- a/types/operations/binary_operations/ArithmeticBinaryOperators.hpp +++ b/types/operations/binary_operations/ArithmeticBinaryOperators.hpp @@ -35,6 +35,7 @@ #include "storage/ValueAccessorUtil.hpp" #endif // QUICKSTEP_ENABLE_VECTOR_COPY_ELISION_SELECTION +#include "types/DecimalLit.hpp" #include "types/TypedValue.hpp" #include "types/containers/ColumnVector.hpp" #include "types/operations/binary_operations/BinaryOperation.hpp" @@ -75,6 +76,27 @@ struct AddFunctor { } }; +template <> +struct AddFunctor { + inline DecimalLit operator() (const DecimalLit &left, const DecimalLit &right) const { + return left + right; + } +}; + +template +struct AddFunctor { + inline DecimalLit operator() (const DecimalLit &left, const RightArgument &right) const { + return left + DecimalLit(right); + } +}; + +template +struct AddFunctor { + inline DecimalLit operator() (const LeftArgument &left, const DecimalLit &right) const { + return DecimalLit(left) + right; + } +}; + template struct SubtractFunctor { inline auto operator() (const LeftArgument &left, const RightArgument &right) const -> decltype(left - right) { return left - right; @@ -98,6 +120,27 @@ struct SubtractFunctor { } }; +template <> +struct SubtractFunctor { + inline DecimalLit operator() (const DecimalLit &left, const DecimalLit &right) const { + return left - right; + } +}; + +template +struct SubtractFunctor { + inline DecimalLit operator() (const DecimalLit &left, const RightArgument &right) const { + return left - DecimalLit(right); + } +}; + +template +struct SubtractFunctor { + inline DecimalLit operator() (const LeftArgument &left, const DecimalLit &right) const { + return DecimalLit(left) - right; + } +}; + template struct MultiplyFunctor { inline auto operator() (const LeftArgument &left, const RightArgument &right) const -> decltype(left * right) { return left * right; @@ -121,6 +164,27 @@ struct MultiplyFunctor { } }; +template <> +struct MultiplyFunctor { + inline DecimalLit operator() (const DecimalLit &left, const DecimalLit &right) const { + return left * right; + } +}; + +template +struct MultiplyFunctor { + inline DecimalLit operator() (const DecimalLit &left, const RightArgument &right) const { + return left * DecimalLit(right); + } +}; + +template +struct MultiplyFunctor { + inline DecimalLit operator() (const LeftArgument &left, const DecimalLit &right) const { + return DecimalLit(left) * right; + } +}; + template struct DivideFunctor { inline auto operator() (const LeftArgument &left, const RightArgument &right) const -> decltype(left / right) { return left / right; @@ -144,6 +208,27 @@ struct DivideFunctor { } }; +template <> +struct DivideFunctor { + inline DecimalLit operator() (const DecimalLit &left, const DecimalLit &right) const { + return left / right; + } +}; + +template +struct DivideFunctor { + inline DecimalLit operator() (const DecimalLit &left, const RightArgument &right) const { + return left / DecimalLit(right); + } +}; + +template +struct DivideFunctor { + inline DecimalLit operator() (const LeftArgument &left, const DecimalLit &right) const { + return DecimalLit(left) / right; + } +}; + template struct IntegerModuloFunctor { inline auto operator() (const LeftArgument &left, const RightArgument &right) const -> decltype(left % right) { return left % right; @@ -162,6 +247,27 @@ template struct FloatModuloFunct } }; +template <> +struct FloatModuloFunctor { + inline DecimalLit operator() (const DecimalLit &left, const DecimalLit &right) const { + return left % right; + } +}; + +template +struct FloatModuloFunctor { + inline DecimalLit operator() (const DecimalLit &left, const RightArgument &right) const { + return left % DecimalLit(right); + } +}; + +template +struct FloatModuloFunctor { + inline DecimalLit operator() (const LeftArgument &left, const DecimalLit &right) const { + return DecimalLit(left) % right; + } +}; + template