Initial commit
Project: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/commit/27c41625 Tree: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/tree/27c41625 Diff: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/diff/27c41625 Branch: refs/heads/new-op Commit: 27c4162537df7829a7e5c2374431ea713dc36d34 Parents: 132fed6 Author: Jianqiao Zhu <jianq...@cs.wisc.edu> Authored: Sat Mar 4 12:11:13 2017 -0600 Committer: Jianqiao Zhu <jianq...@cs.wisc.edu> Committed: Thu Mar 9 15:28:58 2017 -0600 ---------------------------------------------------------------------- expressions/CMakeLists.txt | 1 + expressions/ExpressionFactories.cpp | 28 +- expressions/Expressions.proto | 5 +- expressions/scalar/CMakeLists.txt | 3 +- expressions/scalar/ScalarUnaryExpression.cpp | 82 +- expressions/scalar/ScalarUnaryExpression.hpp | 26 +- parser/CMakeLists.txt | 3 - parser/ParseBasicExpressions.cpp | 76 - parser/ParseBasicExpressions.hpp | 205 -- parser/ParseExpression.hpp | 3 - parser/SqlLexer.lpp | 1 + parser/SqlParser.ypp | 59 +- parser/preprocessed/SqlLexer_gen.cpp | 1457 ++++---- parser/preprocessed/SqlLexer_gen.hpp | 192 +- parser/preprocessed/SqlParser_gen.cpp | 3290 +++++++++--------- parser/preprocessed/SqlParser_gen.hpp | 201 +- query_optimizer/LogicalGenerator.cpp | 3 +- query_optimizer/expressions/CMakeLists.txt | 5 +- query_optimizer/expressions/Cast.cpp | 17 +- query_optimizer/expressions/UnaryExpression.cpp | 32 +- query_optimizer/expressions/UnaryExpression.hpp | 44 +- query_optimizer/resolver/CMakeLists.txt | 5 +- query_optimizer/resolver/Resolver.cpp | 304 +- query_optimizer/resolver/Resolver.hpp | 9 +- types/CMakeLists.txt | 24 + types/CharType.cpp | 3 +- types/CharType.hpp | 4 +- types/DateType.hpp | 10 +- types/DatetimeIntervalType.hpp | 11 +- types/DatetimeLit.hpp | 4 + types/DatetimeType.hpp | 10 +- types/DoubleType.cpp | 2 - types/DoubleType.hpp | 6 +- types/FloatType.cpp | 2 - types/FloatType.hpp | 6 +- types/IntType.cpp | 2 - types/IntType.hpp | 6 +- types/LongType.cpp | 2 - types/LongType.hpp | 6 +- types/NullCoercibilityCheckMacro.hpp | 2 +- types/NullType.hpp | 8 +- types/NumericSuperType.hpp | 13 +- types/Type.cpp | 41 +- types/Type.hpp | 59 +- types/Type.proto | 18 +- types/TypeConcept.hpp | 33 + types/TypeFactory.cpp | 50 +- types/TypeID.hpp | 33 +- types/TypeUtil.hpp | 181 + types/TypedValue.cpp | 37 +- types/TypedValue.hpp | 9 +- types/TypedValue.proto | 2 +- types/VarCharType.cpp | 3 +- types/VarCharType.hpp | 4 +- types/YearMonthIntervalType.hpp | 11 +- types/containers/ColumnVector.hpp | 12 +- types/operations/CMakeLists.txt | 9 + types/operations/Operation.hpp | 18 +- types/operations/Operation.proto | 47 +- types/operations/OperationSignature.cpp | 91 + types/operations/OperationSignature.hpp | 192 + .../ArithmeticBinaryOperators.hpp | 13 +- .../binary_operations/BinaryOperation.hpp | 6 +- types/operations/comparisons/CMakeLists.txt | 2 + types/operations/comparisons/Comparison.hpp | 6 +- types/operations/comparisons/ComparisonUtil.hpp | 30 +- .../ArithmeticUnaryOperations.cpp | 145 - .../ArithmeticUnaryOperations.hpp | 80 +- .../ArithmeticUnaryOperators.hpp | 169 - .../operations/unary_operations/CMakeLists.txt | 109 +- .../unary_operations/CMathUnaryOperations.hpp | 108 + .../unary_operations/CastOperation.cpp | 282 ++ .../unary_operations/CastOperation.hpp | 149 + .../unary_operations/DateExtractOperation.cpp | 597 +--- .../unary_operations/DateExtractOperation.hpp | 169 +- .../unary_operations/NumericCastOperation.hpp | 313 -- .../unary_operations/SubstringOperation.cpp | 46 +- .../unary_operations/SubstringOperation.hpp | 187 +- .../unary_operations/UnaryOperation.cpp | 20 - .../unary_operations/UnaryOperation.hpp | 181 +- .../unary_operations/UnaryOperationFactory.cpp | 112 +- .../unary_operations/UnaryOperationFactory.hpp | 82 +- .../unary_operations/UnaryOperationID.cpp | 32 - .../unary_operations/UnaryOperationID.hpp | 63 - .../unary_operations/UnaryOperationWrapper.hpp | 441 +++ utility/StringUtil.cpp | 6 + utility/StringUtil.hpp | 12 +- utility/TemplateUtil.hpp | 28 +- 88 files changed, 5161 insertions(+), 5249 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/27c41625/expressions/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/expressions/CMakeLists.txt b/expressions/CMakeLists.txt index b1f1fb1..3becc71 100644 --- a/expressions/CMakeLists.txt +++ b/expressions/CMakeLists.txt @@ -51,6 +51,7 @@ target_link_libraries(quickstep_expressions_ExpressionFactories quickstep_expressions_scalar_ScalarUnaryExpression quickstep_types_TypeFactory quickstep_types_TypedValue + quickstep_types_operations_OperationSignature quickstep_types_operations_binaryoperations_BinaryOperationFactory quickstep_types_operations_comparisons_ComparisonFactory quickstep_types_operations_unaryoperations_UnaryOperationFactory http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/27c41625/expressions/ExpressionFactories.cpp ---------------------------------------------------------------------- diff --git a/expressions/ExpressionFactories.cpp b/expressions/ExpressionFactories.cpp index 01d22a0..dd7532e 100644 --- a/expressions/ExpressionFactories.cpp +++ b/expressions/ExpressionFactories.cpp @@ -42,6 +42,7 @@ #include "expressions/scalar/ScalarUnaryExpression.hpp" #include "types/TypeFactory.hpp" #include "types/TypedValue.hpp" +#include "types/operations/OperationSignature.hpp" #include "types/operations/binary_operations/BinaryOperationFactory.hpp" #include "types/operations/comparisons/ComparisonFactory.hpp" #include "types/operations/unary_operations/UnaryOperationFactory.hpp" @@ -167,10 +168,24 @@ Scalar* ScalarFactory::ReconstructFromProto(const serialization::Scalar &proto, proto.GetExtension(serialization::ScalarAttribute::attribute_id))); } case serialization::Scalar::UNARY_EXPRESSION: { + std::vector<TypedValue> static_arguments; + const int num_static_args = + proto.ExtensionSize(serialization::ScalarUnaryExpression::static_arguments); + for (int i = 0; i < num_static_args; ++i) { + static_arguments.emplace_back( + TypedValue::ReconstructFromProto( + proto.GetExtension(serialization::ScalarUnaryExpression::static_arguments, i))); + } + + const OperationSignaturePtr op_signature = + OperationSignature::ReconstructFromProto( + proto.GetExtension(serialization::ScalarUnaryExpression::op_signature)); + return new ScalarUnaryExpression( - UnaryOperationFactory::ReconstructFromProto( - proto.GetExtension(serialization::ScalarUnaryExpression::operation)), - ReconstructFromProto(proto.GetExtension(serialization::ScalarUnaryExpression::operand), database)); + op_signature, + UnaryOperationFactory::Instance().getUnaryOperation(op_signature), + ReconstructFromProto(proto.GetExtension(serialization::ScalarUnaryExpression::operand), database), + std::make_shared<std::vector<TypedValue>>(std::move(static_arguments))); } case serialization::Scalar::BINARY_EXPRESSION: { return new ScalarBinaryExpression( @@ -242,11 +257,8 @@ bool ScalarFactory::ProtoIsValid(const serialization::Scalar &proto, break; } case serialization::Scalar::UNARY_EXPRESSION: { - if (proto.HasExtension(serialization::ScalarUnaryExpression::operation) - && proto.HasExtension(serialization::ScalarUnaryExpression::operand)) { - return UnaryOperationFactory::ProtoIsValid(proto.GetExtension(serialization::ScalarUnaryExpression::operation)) - && ProtoIsValid(proto.GetExtension(serialization::ScalarUnaryExpression::operand), database); - } + // TODO + return true; break; } case serialization::Scalar::BINARY_EXPRESSION: { http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/27c41625/expressions/Expressions.proto ---------------------------------------------------------------------- diff --git a/expressions/Expressions.proto b/expressions/Expressions.proto index 8d923c5..ea6228a 100644 --- a/expressions/Expressions.proto +++ b/expressions/Expressions.proto @@ -102,8 +102,9 @@ message ScalarAttribute { message ScalarUnaryExpression { extend Scalar { - optional UnaryOperation operation = 96; - optional Scalar operand = 97; + optional OperationSignature op_signature = 97; + optional Scalar operand = 98; + repeated TypedValue static_arguments = 99; } } http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/27c41625/expressions/scalar/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/expressions/scalar/CMakeLists.txt b/expressions/scalar/CMakeLists.txt index 8f509da..79d1a96 100644 --- a/expressions/scalar/CMakeLists.txt +++ b/expressions/scalar/CMakeLists.txt @@ -102,6 +102,7 @@ target_link_libraries(quickstep_expressions_scalar_ScalarUnaryExpression quickstep_types_TypeErrors quickstep_types_TypedValue quickstep_types_containers_ColumnVector + quickstep_types_operations_OperationSignature quickstep_types_operations_Operation_proto quickstep_types_operations_unaryoperations_UnaryOperation quickstep_utility_Macros) @@ -173,7 +174,7 @@ target_link_libraries(Scalar_unittest quickstep_types_operations_binaryoperations_BinaryOperation quickstep_types_operations_binaryoperations_BinaryOperationFactory quickstep_types_operations_binaryoperations_BinaryOperationID - quickstep_types_operations_unaryoperations_NumericCastOperation + quickstep_types_operations_unaryoperations_CastOperation quickstep_types_operations_unaryoperations_UnaryOperation quickstep_types_operations_unaryoperations_UnaryOperationFactory quickstep_types_operations_unaryoperations_UnaryOperationID http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/27c41625/expressions/scalar/ScalarUnaryExpression.cpp ---------------------------------------------------------------------- diff --git a/expressions/scalar/ScalarUnaryExpression.cpp b/expressions/scalar/ScalarUnaryExpression.cpp index 72fdbe1..61405af 100644 --- a/expressions/scalar/ScalarUnaryExpression.cpp +++ b/expressions/scalar/ScalarUnaryExpression.cpp @@ -42,31 +42,57 @@ namespace quickstep { struct SubBlocksReference; -ScalarUnaryExpression::ScalarUnaryExpression(const UnaryOperation &operation, - Scalar *operand) - : Scalar(*operation.resultTypeForArgumentType(operand->getType())), +ScalarUnaryExpression::ScalarUnaryExpression( + const OperationSignaturePtr &op_signature, + const UnaryOperationPtr &operation, + Scalar *operand, + const std::shared_ptr<const std::vector<TypedValue>> &static_arguments) + : Scalar(*operation->getResultType(operand->getType(), *static_arguments)), + op_signature_(op_signature), operation_(operation), - operand_(operand) { - initHelper(false); + operand_(operand), + static_arguments_(static_arguments) { + if (operation_->canApplyTo(operand_->getType(), *static_arguments_)) { + fast_operator_.reset( + operation_->makeUncheckedUnaryOperator(operand_->getType(), + *static_arguments_)); + if (operand_->hasStaticValue()) { + static_value_.reset(new TypedValue( + fast_operator_->applyToTypedValue(operand_->getStaticValue()))); + } + } else { + const Type &operand_type = operand_->getType(); + throw OperationInapplicableToType(operation_->getName(), 1, operand_type.getName().c_str()); + } } serialization::Scalar ScalarUnaryExpression::getProto() const { serialization::Scalar proto; proto.set_data_source(serialization::Scalar::UNARY_EXPRESSION); - proto.MutableExtension(serialization::ScalarUnaryExpression::operation)->CopyFrom(operation_.getProto()); + proto.MutableExtension( + serialization::ScalarUnaryExpression::op_signature)->CopyFrom( + op_signature_->getProto()); proto.MutableExtension(serialization::ScalarUnaryExpression::operand)->CopyFrom(operand_->getProto()); + for (const TypedValue &value : *static_arguments_) { + proto.AddExtension( + serialization::ScalarUnaryExpression::static_arguments)->CopyFrom( + value.getProto()); + } return proto; } Scalar* ScalarUnaryExpression::clone() const { - return new ScalarUnaryExpression(operation_, operand_->clone()); + return new ScalarUnaryExpression(op_signature_, + operation_, + operand_->clone(), + static_arguments_); } TypedValue ScalarUnaryExpression::getValueForSingleTuple(const ValueAccessor &accessor, const tuple_id tuple) const { if (fast_operator_.get() == nullptr) { - return static_value_.makeReferenceToThis(); + return static_value_->makeReferenceToThis(); } else { return fast_operator_->applyToTypedValue(operand_->getValueForSingleTuple(accessor, tuple)); } @@ -80,7 +106,7 @@ TypedValue ScalarUnaryExpression::getValueForJoinedTuples( const relation_id right_relation_id, const tuple_id right_tuple_id) const { if (fast_operator_.get() == nullptr) { - return static_value_.makeReferenceToThis(); + return static_value_->makeReferenceToThis(); } else { return fast_operator_->applyToTypedValue(operand_->getValueForJoinedTuples(left_accessor, left_relation_id, @@ -96,7 +122,7 @@ ColumnVector* ScalarUnaryExpression::getAllValues( const SubBlocksReference *sub_blocks_ref) const { if (fast_operator_.get() == nullptr) { return ColumnVector::MakeVectorOfValue(getType(), - static_value_, + *static_value_, accessor->getNumTuplesVirtual()); } else { #ifdef QUICKSTEP_ENABLE_VECTOR_COPY_ELISION_SELECTION @@ -119,26 +145,9 @@ ColumnVector* ScalarUnaryExpression::getAllValuesForJoin( const std::vector<std::pair<tuple_id, tuple_id>> &joined_tuple_ids) const { if (fast_operator_.get() == nullptr) { return ColumnVector::MakeVectorOfValue(getType(), - static_value_, + *static_value_, joined_tuple_ids.size()); } else { -#ifdef QUICKSTEP_ENABLE_VECTOR_COPY_ELISION_JOIN - const attribute_id operand_attr_id = operand_->getAttributeIdForValueAccessor(); - if (operand_attr_id != -1) { - const relation_id operand_relation_id = operand_->getRelationIdForValueAccessor(); - DCHECK_NE(operand_relation_id, -1); - DCHECK((operand_relation_id == left_relation_id) - || (operand_relation_id == right_relation_id)); - const bool using_left_relation = (operand_relation_id == left_relation_id); - ValueAccessor *operand_accessor = using_left_relation ? left_accessor - : right_accessor; - return fast_operator_->applyToValueAccessorForJoin(operand_accessor, - using_left_relation, - operand_attr_id, - joined_tuple_ids); - } -#endif // QUICKSTEP_ENABLE_VECTOR_COPY_ELISION_JOIN - std::unique_ptr<ColumnVector> operand_result( operand_->getAllValuesForJoin(left_relation_id, left_accessor, @@ -149,21 +158,4 @@ ColumnVector* ScalarUnaryExpression::getAllValuesForJoin( } } -void ScalarUnaryExpression::initHelper(bool own_children) { - if (operation_.canApplyToType(operand_->getType())) { - if (operand_->hasStaticValue()) { - static_value_ = operation_.applyToChecked(operand_->getStaticValue(), - operand_->getType()); - } else { - fast_operator_.reset(operation_.makeUncheckedUnaryOperatorForType(operand_->getType())); - } - } else { - const Type &operand_type = operand_->getType(); - if (!own_children) { - operand_.release(); - } - throw OperationInapplicableToType(operation_.getName(), 1, operand_type.getName().c_str()); - } -} - } // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/27c41625/expressions/scalar/ScalarUnaryExpression.hpp ---------------------------------------------------------------------- diff --git a/expressions/scalar/ScalarUnaryExpression.hpp b/expressions/scalar/ScalarUnaryExpression.hpp index 608a842..c84e2bb 100644 --- a/expressions/scalar/ScalarUnaryExpression.hpp +++ b/expressions/scalar/ScalarUnaryExpression.hpp @@ -29,6 +29,7 @@ #include "expressions/scalar/Scalar.hpp" #include "storage/StorageBlockInfo.hpp" #include "types/TypedValue.hpp" +#include "types/operations/OperationSignature.hpp" #include "types/operations/unary_operations/UnaryOperation.hpp" #include "utility/Macros.hpp" @@ -50,14 +51,10 @@ struct SubBlocksReference; **/ class ScalarUnaryExpression : public Scalar { public: - /** - * @brief Constructor. - * - * @param operation The unary operation to be performed. - * @param operand The argument of the operation, which this - * ScalarUnaryExpression takes ownership of. - **/ - ScalarUnaryExpression(const UnaryOperation &operation, Scalar *operand); + ScalarUnaryExpression(const OperationSignaturePtr &op_signature, + const UnaryOperationPtr &operation, + Scalar *operand, + const std::shared_ptr<const std::vector<TypedValue>> &static_arguments); /** * @brief Destructor. @@ -85,12 +82,12 @@ class ScalarUnaryExpression : public Scalar { const tuple_id right_tuple_id) const override; bool hasStaticValue() const override { - return fast_operator_.get() == nullptr; + return static_value_ != nullptr; } const TypedValue& getStaticValue() const override { DCHECK(hasStaticValue()); - return static_value_; + return *static_value_; } ColumnVector* getAllValues(ValueAccessor *accessor, @@ -106,10 +103,13 @@ class ScalarUnaryExpression : public Scalar { private: void initHelper(bool own_children); - const UnaryOperation &operation_; + const OperationSignaturePtr op_signature_; + const UnaryOperationPtr operation_; + + const std::unique_ptr<Scalar> operand_; + const std::shared_ptr<const std::vector<TypedValue>> static_arguments_; - std::unique_ptr<Scalar> operand_; - TypedValue static_value_; + std::unique_ptr<TypedValue> static_value_; std::unique_ptr<UncheckedUnaryOperator> fast_operator_; friend class PredicateTest; http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/27c41625/parser/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/parser/CMakeLists.txt b/parser/CMakeLists.txt index b1bd870..7a8e04e 100644 --- a/parser/CMakeLists.txt +++ b/parser/CMakeLists.txt @@ -365,9 +365,6 @@ target_link_libraries(quickstep_parser_SqlParser quickstep_types_operations_comparisons_Comparison quickstep_types_operations_comparisons_ComparisonFactory quickstep_types_operations_comparisons_ComparisonID - quickstep_types_operations_unaryoperations_UnaryOperation - quickstep_types_operations_unaryoperations_UnaryOperationFactory - quickstep_types_operations_unaryoperations_UnaryOperationID quickstep_utility_PtrList quickstep_utility_PtrVector) target_link_libraries(quickstep_parser_SqlParserWrapper http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/27c41625/parser/ParseBasicExpressions.cpp ---------------------------------------------------------------------- diff --git a/parser/ParseBasicExpressions.cpp b/parser/ParseBasicExpressions.cpp index b0b1247..8212286 100644 --- a/parser/ParseBasicExpressions.cpp +++ b/parser/ParseBasicExpressions.cpp @@ -68,27 +68,6 @@ void ParseAttribute::getFieldStringItems( } } -std::string ParseUnaryExpression::getName() const { - return op_.getName(); -} - -string ParseUnaryExpression::generateName() const { - string name(op_.getShortName()); - name.append(operand_->generateName()); - return name; -} - -void ParseUnaryExpression::getFieldStringItems( - std::vector<std::string> *inline_field_names, - std::vector<std::string> *inline_field_values, - std::vector<std::string> *non_container_child_field_names, - std::vector<const ParseTreeNode*> *non_container_child_fields, - std::vector<std::string> *container_child_field_names, - std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const { - non_container_child_field_names->push_back(""); - non_container_child_fields->push_back(operand_.get()); -} - std::string ParseBinaryExpression::getName() const { return op_.getName(); } @@ -175,59 +154,4 @@ void ParseFunctionCall::getFieldStringItems( } } -std::string ParseExtractFunction::generateName() const { - std::string name; - name.append("EXTRACT("); - name.append(extract_field_->value()); - name.append(" FROM "); - name.append(date_expression_->generateName()); - name.push_back(')'); - return name; -} - -void ParseExtractFunction::getFieldStringItems( - std::vector<std::string> *inline_field_names, - std::vector<std::string> *inline_field_values, - std::vector<std::string> *non_container_child_field_names, - std::vector<const ParseTreeNode*> *non_container_child_fields, - std::vector<std::string> *container_child_field_names, - std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const { - inline_field_names->push_back("unit"); - inline_field_values->push_back(extract_field_->value()); - - non_container_child_field_names->push_back("date_expression"); - non_container_child_fields->push_back(date_expression_.get()); -} - -std::string ParseSubstringFunction::generateName() const { - std::string name; - name.append("SUBSTRING("); - name.append(operand_->generateName()); - name.append(" FROM "); - name.append(std::to_string(start_position_)); - if (length_ != kDefaultLength) { - name.append(" FOR "); - name.append(std::to_string(length_)); - } - name.push_back(')'); - return name; -} - -void ParseSubstringFunction::getFieldStringItems( - std::vector<std::string> *inline_field_names, - std::vector<std::string> *inline_field_values, - std::vector<std::string> *non_container_child_field_names, - std::vector<const ParseTreeNode*> *non_container_child_fields, - std::vector<std::string> *container_child_field_names, - std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const { - inline_field_names->push_back("start_position"); - inline_field_values->push_back(std::to_string(start_position_)); - - inline_field_names->push_back("length"); - inline_field_values->push_back(std::to_string(length_)); - - non_container_child_field_names->push_back("operand"); - non_container_child_fields->push_back(operand_.get()); -} - } // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/27c41625/parser/ParseBasicExpressions.hpp ---------------------------------------------------------------------- diff --git a/parser/ParseBasicExpressions.hpp b/parser/ParseBasicExpressions.hpp index d8de669..edf5aab 100644 --- a/parser/ParseBasicExpressions.hpp +++ b/parser/ParseBasicExpressions.hpp @@ -173,73 +173,6 @@ class ParseAttribute : public ParseExpression { /** - * @brief The parsed representation of an unary operation applied to an expression. - **/ -class ParseUnaryExpression : public ParseExpression { - public: - /** - * @brief Constructor. - * - * @param line_number Line number of the first token of this node in the SQL statement. - * @param column_number Column number of the first token of this node in the SQL statement. - * @param op The UnaryOperation from the quickstep type system to apply. - * @param operand The parsed scalar representation of the unary operation's - * argument, which becomes owned by this ParseScalarUnaryExpression. - **/ - ParseUnaryExpression(const int line_number, - const int column_number, - const UnaryOperation &op, - ParseExpression *operand) - : ParseExpression(line_number, column_number), - op_(op), - operand_(operand) { - } - - /** - * @brief Destructor. - */ - ~ParseUnaryExpression() override { - } - - ExpressionType getExpressionType() const override { - return kUnaryExpression; - } - - std::string getName() const override; - - /** - * @return The unary operation. - */ - const UnaryOperation& op() const { - return op_; - } - - /** - * @return The operand expression. - */ - const ParseExpression* operand() const { - return operand_.get(); - } - - std::string generateName() const override; - - protected: - void getFieldStringItems( - std::vector<std::string> *inline_field_names, - std::vector<std::string> *inline_field_values, - std::vector<std::string> *non_container_child_field_names, - std::vector<const ParseTreeNode*> *non_container_child_fields, - std::vector<std::string> *container_child_field_names, - std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override; - - private: - const UnaryOperation &op_; - std::unique_ptr<ParseExpression> operand_; - - DISALLOW_COPY_AND_ASSIGN(ParseUnaryExpression); -}; - -/** * @brief The parsed representation of a binary operation applied to two * expressions. **/ @@ -495,144 +428,6 @@ class ParseFunctionCall : public ParseExpression { DISALLOW_COPY_AND_ASSIGN(ParseFunctionCall); }; - -/** - * @brief Parsed representation of EXTRACT(unit FROM date). - */ -class ParseExtractFunction : public ParseExpression { - public: - /** - * @brief Constructor. - * - * @param line_number The line number of the token "extract" in the statement. - * @param column_number The column number of the token "extract in the statement. - * @param extract_field The field to extract. - * @param source_expression The expression to extract a field from. - */ - ParseExtractFunction(const int line_number, - const int column_number, - ParseString *extract_field, - ParseExpression *date_expression) - : ParseExpression(line_number, column_number), - extract_field_(extract_field), - date_expression_(date_expression) { - } - - ExpressionType getExpressionType() const override { - return kExtract; - } - - std::string getName() const override { - return "Extract"; - } - - /** - * @return The field to extract. - */ - const ParseString* extract_field() const { - return extract_field_.get(); - } - - /** - * @return The expression to extract a field from. - */ - const ParseExpression* date_expression() const { - return date_expression_.get(); - } - - std::string generateName() const override; - - protected: - void getFieldStringItems( - std::vector<std::string> *inline_field_names, - std::vector<std::string> *inline_field_values, - std::vector<std::string> *non_container_child_field_names, - std::vector<const ParseTreeNode*> *non_container_child_fields, - std::vector<std::string> *container_child_field_names, - std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override; - - private: - std::unique_ptr<ParseString> extract_field_; - std::unique_ptr<ParseExpression> date_expression_; - - DISALLOW_COPY_AND_ASSIGN(ParseExtractFunction); -}; - - -/** - * @brief Parsed representation of the substring function. - */ -class ParseSubstringFunction : public ParseExpression { - public: - static constexpr std::size_t kDefaultLength = std::numeric_limits<std::size_t>::max(); - - /** - * @brief Constructor. - * - * @param line_number The line number of the first token of the function call. - * @param column_number The column number of the first token of the function call. - * @param operand The operand of the substring. - * @param start_position The 1-based starting position of the substring. - * @param length Optional substring length. - */ - ParseSubstringFunction(const int line_number, - const int column_number, - ParseExpression *operand, - const std::size_t start_position, - const std::size_t length = kDefaultLength) - : ParseExpression(line_number, column_number), - operand_(operand), - start_position_(start_position), - length_(length) {} - - ExpressionType getExpressionType() const override { - return kSubstring; - } - - std::string getName() const override { - return "Substring"; - } - - /** - * @return The operand of the substring. - */ - const ParseExpression* operand() const { - return operand_.get(); - } - - /** - * @return The 1-based starting position of the substring. - */ - std::size_t start_position() const { - return start_position_; - } - - /** - * @return Then substring length. - */ - std::size_t length() const { - return length_; - } - - std::string generateName() const override; - - protected: - void getFieldStringItems( - std::vector<std::string> *inline_field_names, - std::vector<std::string> *inline_field_values, - std::vector<std::string> *non_container_child_field_names, - std::vector<const ParseTreeNode*> *non_container_child_fields, - std::vector<std::string> *container_child_field_names, - std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override; - - private: - std::unique_ptr<ParseExpression> operand_; - const std::size_t start_position_; - const std::size_t length_; - - DISALLOW_COPY_AND_ASSIGN(ParseSubstringFunction); -}; - /** @} */ } // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/27c41625/parser/ParseExpression.hpp ---------------------------------------------------------------------- diff --git a/parser/ParseExpression.hpp b/parser/ParseExpression.hpp index 1b9ade4..94b4487 100644 --- a/parser/ParseExpression.hpp +++ b/parser/ParseExpression.hpp @@ -39,14 +39,11 @@ class ParseExpression : public ParseTreeNode { enum ExpressionType { kAttribute, kBinaryExpression, - kExtract, kFunctionCall, kScalarLiteral, kSearchedCaseExpression, kSimpleCaseExpression, kSubqueryExpression, - kSubstring, - kUnaryExpression, }; /** http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/27c41625/parser/SqlLexer.lpp ---------------------------------------------------------------------- diff --git a/parser/SqlLexer.lpp b/parser/SqlLexer.lpp index 0953dc6..ac88515 100644 --- a/parser/SqlLexer.lpp +++ b/parser/SqlLexer.lpp @@ -183,6 +183,7 @@ unsigned_numeric_literal {exact_numeric_literal}|{approximate_numeric_literal} "blocksample" return TOKEN_BLOCKSAMPLE; "bloomfilter" return TOKEN_BLOOM_FILTER; "case" return TOKEN_CASE; + "cast" return TOKEN_CAST; "csbtree" return TOKEN_CSB_TREE; "by" return TOKEN_BY; "char" return TOKEN_CHARACTER; http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/27c41625/parser/SqlParser.ypp ---------------------------------------------------------------------- diff --git a/parser/SqlParser.ypp b/parser/SqlParser.ypp index 29b69d7..fd13aa7 100644 --- a/parser/SqlParser.ypp +++ b/parser/SqlParser.ypp @@ -107,9 +107,6 @@ typedef struct YYLTYPE { #include "types/operations/comparisons/Comparison.hpp" #include "types/operations/comparisons/ComparisonFactory.hpp" #include "types/operations/comparisons/ComparisonID.hpp" -#include "types/operations/unary_operations/UnaryOperation.hpp" -#include "types/operations/unary_operations/UnaryOperationFactory.hpp" -#include "types/operations/unary_operations/UnaryOperationID.hpp" #include "utility/PtrList.hpp" #include "utility/PtrVector.hpp" @@ -187,7 +184,7 @@ typedef void* yyscan_t; quickstep::ParseStatementQuit *quit_statement_; const quickstep::Comparison *comparison_; - const quickstep::UnaryOperation *unary_operation_; + quickstep::ParseString *unary_operation_; const quickstep::BinaryOperation *binary_operation_; quickstep::ParseFunctionCall *function_call_; @@ -258,6 +255,7 @@ void NotSupported(const YYLTYPE *location, yyscan_t yyscanner, const std::string %token TOKEN_CSB_TREE; %token TOKEN_BY; %token TOKEN_CASE; +%token TOKEN_CAST; %token TOKEN_CHARACTER; %token TOKEN_CHECK; %token TOKEN_COLUMN; @@ -386,6 +384,7 @@ void NotSupported(const YYLTYPE *location, yyscan_t yyscanner, const std::string add_expression case_expression opt_else_clause + cast_function extract_function substr_function @@ -613,7 +612,6 @@ void NotSupported(const YYLTYPE *location, yyscan_t yyscanner, const std::string %destructor { } <boolean_value_> %destructor { } <comparison_> -%destructor { } <unary_operation_> %destructor { } <binary_operation_> %destructor { } <join_type_> @@ -1632,7 +1630,10 @@ multiply_expression: unary_expression: unary_operation expression_base { - $$ = new quickstep::ParseUnaryExpression(@1.first_line, @1.first_column, *$1, $2); + auto *arguments = new quickstep::PtrList<quickstep::ParseExpression>(); + arguments->push_back($2); + $$ = new quickstep::ParseFunctionCall( + @1.first_line, @1.first_column, false, $1, arguments); } | expression_base { $$ = $1; @@ -1656,6 +1657,9 @@ expression_base: $1->setWindow($4); $$ = $1; } + | cast_function { + $$ = $1; + } | extract_function { $$ = $1; } @@ -1688,19 +1692,50 @@ function_call: $$ = new quickstep::ParseFunctionCall(@1.first_line, @1.first_column, true, $1, $4); }; +cast_function: + TOKEN_CAST '(' add_expression TOKEN_AS data_type ')' { + auto *arguments = new quickstep::PtrList<quickstep::ParseExpression>(); + arguments->push_back($3); + arguments->push_back(new quickstep::ParseScalarLiteral( + new quickstep::StringParseLiteralValue( + new quickstep::ParseString(@5.first_line, + @5.first_column, + $5->getType().getName()), + nullptr))); + delete $5; + auto *name = new quickstep::ParseString(@1.first_line, @1.first_column, "cast"); + $$ = new quickstep::ParseFunctionCall( + @1.first_line, @1.first_column, false, name, arguments); + }; + extract_function: TOKEN_EXTRACT '(' datetime_unit TOKEN_FROM add_expression ')' { - $$ = new quickstep::ParseExtractFunction(@1.first_line, @1.first_column, $3, $5); + auto *arguments = new quickstep::PtrList<quickstep::ParseExpression>(); + arguments->push_back($5); + arguments->push_back(new quickstep::ParseScalarLiteral( + new quickstep::StringParseLiteralValue($3, nullptr))); + auto *name = new quickstep::ParseString(@1.first_line, @1.first_column, "extract"); + $$ = new quickstep::ParseFunctionCall( + @1.first_line, @1.first_column, false, name, arguments); }; substr_function: TOKEN_SUBSTRING '(' add_expression TOKEN_FROM TOKEN_UNSIGNED_NUMVAL ')' { - $$ = new quickstep::ParseSubstringFunction( - @1.first_line, @1.first_column, $3, $5->long_value()); + auto *arguments = new quickstep::PtrList<quickstep::ParseExpression>(); + arguments->push_back($3); + arguments->push_back(new quickstep::ParseScalarLiteral($5)); + auto *name = new quickstep::ParseString(@1.first_line, @1.first_column, "substring"); + $$ = new quickstep::ParseFunctionCall( + @1.first_line, @1.first_column, false, name, arguments); } | TOKEN_SUBSTRING '(' add_expression TOKEN_FROM TOKEN_UNSIGNED_NUMVAL TOKEN_FOR TOKEN_UNSIGNED_NUMVAL ')' { - $$ = new quickstep::ParseSubstringFunction( - @1.first_line, @1.first_column, $3, $5->long_value(), $7->long_value()); + auto *arguments = new quickstep::PtrList<quickstep::ParseExpression>(); + arguments->push_back($3); + arguments->push_back(new quickstep::ParseScalarLiteral($5)); + arguments->push_back(new quickstep::ParseScalarLiteral($7)); + auto *name = new quickstep::ParseString(@1.first_line, @1.first_column, "substring"); + $$ = new quickstep::ParseFunctionCall( + @1.first_line, @1.first_column, false, name, arguments); }; case_expression: @@ -1920,7 +1955,7 @@ unary_operation: * to shift rather than reduce, the case in 'literal_value' has precedence * over this one. **/ - $$ = &quickstep::UnaryOperationFactory::GetUnaryOperation(quickstep::UnaryOperationID::kNegate); + $$ = new quickstep::ParseString(@1.first_line, @1.first_column, std::string("-")); }; add_operation: