susumu.yata
null+****@clear*****
Fri Sep 12 17:22:38 JST 2014
susumu.yata 2014-09-12 17:22:38 +0900 (Fri, 12 Sep 2014) New Revision: 7b57266d47c88cd184e7eabb3be4dc8c07928a45 https://github.com/groonga/grnxx/commit/7b57266d47c88cd184e7eabb3be4dc8c07928a45 Message: Support expressions like (Ref.Ref).X. (#55) Modified files: lib/grnxx/expression.cpp Modified: lib/grnxx/expression.cpp (+18 -168) =================================================================== --- lib/grnxx/expression.cpp 2014-09-12 16:36:12 +0900 (e9a06e5) +++ lib/grnxx/expression.cpp 2014-09-12 17:22:38 +0900 (afc2d55) @@ -29,7 +29,9 @@ class Node { // Return the result data type. virtual DataType data_type() const = 0; // Return the reference table. - virtual const Table *ref_table() const = 0; + virtual const Table *ref_table() const { + return nullptr; + } // Extract true records. // @@ -68,9 +70,6 @@ class TypedNode : public Node { DataType data_type() const { return TypeTraits<Value>::data_type(); } - const Table *ref_table() const { - return nullptr; - } bool filter(Error *error, ArrayCRef<Record>, @@ -109,9 +108,6 @@ class TypedNode<Bool> : public Node { DataType data_type() const { return TypeTraits<Value>::data_type(); } - const Table *ref_table() const { - return nullptr; - } // Derived classes must override this member function. virtual bool filter(Error *error, @@ -153,52 +149,6 @@ class TypedNode<Bool> : public Node { //} template <> -class TypedNode<Int> : public Node { - public: - using Value = Int; - - explicit TypedNode(const Table *ref_table = nullptr) - : Node(), - ref_table_(ref_table) {} - virtual ~TypedNode() {} - - DataType data_type() const { - return TypeTraits<Value>::data_type(); - } - const Table *ref_table() const { - return ref_table_; - } - - bool filter(Error *error, - ArrayCRef<Record>, - ArrayRef<Record> *) { - // Other than TypedNode<Bool> don't support filter(). - GRNXX_ERROR_SET(error, INVALID_OPERATION, "Invalid operation"); - return false; - } - - bool adjust(Error *error, ArrayRef<Record>) { - // Other than TypedNode<Float> don't support adjust(). - GRNXX_ERROR_SET(error, INVALID_OPERATION, "Invalid operation"); - return false; - } - - // Evaluate the expression subtree. - // - // The evaluation results are stored into "*results". - // - // On success, returns true. - // On failure, returns false and stores error information into "*error" if - // "error" != nullptr. - virtual bool evaluate(Error *error, - ArrayCRef<Record> records, - ArrayRef<Value> results) = 0; - - protected: - const Table *ref_table_; -}; - -template <> class TypedNode<Float> : public Node { public: using Value = Float; @@ -209,9 +159,6 @@ class TypedNode<Float> : public Node { DataType data_type() const { return TypeTraits<Value>::data_type(); } - const Table *ref_table() const { - return nullptr; - } bool filter(Error *error, ArrayCRef<Record>, @@ -246,50 +193,6 @@ bool TypedNode<Float>::adjust(Error *error, ArrayRef<Record> records) { return true; } -template <> -class TypedNode<Vector<Int>> : public Node { - public: - using Value = Vector<Int>; - - explicit TypedNode(const Table *ref_table = nullptr) - : Node(), - ref_table_(ref_table) {} - virtual ~TypedNode() {} - - DataType data_type() const { - return TypeTraits<Value>::data_type(); - } - const Table *ref_table() const { - return ref_table_; - } - - bool filter(Error *error, ArrayCRef<Record>, ArrayRef<Record> *) { - // Other than TypedNode<Bool> don't support filter(). - GRNXX_ERROR_SET(error, INVALID_OPERATION, "Invalid operation"); - return false; - } - - bool adjust(Error *error, ArrayRef<Record>) { - // Other than TypedNode<Float> don't support adjust(). - GRNXX_ERROR_SET(error, INVALID_OPERATION, "Invalid operation"); - return false; - } - - // Evaluate the expression subtree. - // - // The evaluation results are stored into "*results". - // - // On success, returns true. - // On failure, returns false and stores error information into "*error" if - // "error" != nullptr. - virtual bool evaluate(Error *error, - ArrayCRef<Record> records, - ArrayRef<Value> results) = 0; - - protected: - const Table *ref_table_; -}; - // -- DatumNode -- template <typename T> @@ -543,6 +446,9 @@ class ColumnNode : public TypedNode<T> { NodeType node_type() const { return COLUMN_NODE; } + const Table *ref_table() const { + return column_->ref_table(); + } bool evaluate(Error *, ArrayCRef<Record> records, @@ -609,40 +515,6 @@ bool ColumnNode<Bool>::filter(Error *, } template <> -class ColumnNode<Int> : public TypedNode<Int> { - public: - using Value = Int; - - static unique_ptr<Node> create(Error *error, const Column *column) { - unique_ptr<Node> node(new (nothrow) ColumnNode(column)); - if (!node) { - GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed"); - } - return node; - } - - explicit ColumnNode(const Column *column) - : TypedNode<Value>(column->ref_table()), - column_(static_cast<const ColumnImpl<Value> *>(column)) {} - - NodeType node_type() const { - return COLUMN_NODE; - } - - bool evaluate(Error *, - ArrayCRef<Record> records, - ArrayRef<Value> results) { - for (Int i = 0; i < records.size(); ++i) { - results[i] = column_->get(records.get_row_id(i)); - } - return true; - } - - private: - const ColumnImpl<Value> *column_; -}; - -template <> class ColumnNode<Float> : public TypedNode<Float> { public: using Value = Float; @@ -682,40 +554,6 @@ class ColumnNode<Float> : public TypedNode<Float> { const ColumnImpl<Value> *column_; }; -template <> -class ColumnNode<Vector<Int>> : public TypedNode<Vector<Int>> { - public: - using Value = Vector<Int>; - - static unique_ptr<Node> create(Error *error, const Column *column) { - unique_ptr<Node> node(new (nothrow) ColumnNode(column)); - if (!node) { - GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed"); - } - return node; - } - - explicit ColumnNode(const Column *column) - : TypedNode<Value>(column->ref_table()), - column_(static_cast<const ColumnImpl<Value> *>(column)) {} - - NodeType node_type() const { - return COLUMN_NODE; - } - - bool evaluate(Error *, - ArrayCRef<Record> records, - ArrayRef<Value> results) { - for (Int i = 0; i < records.size(); ++i) { - results[i] = column_->get(records.get_row_id(i)); - } - return true; - } - - private: - const ColumnImpl<Value> *column_; -}; - // -- OperatorNode -- template <typename T> @@ -2373,6 +2211,10 @@ class SubscriptNode : public BinaryNode<T, Vector<T>, Int> { SubscriptNode(unique_ptr<Node> &&arg1, unique_ptr<Node> &&arg2) : BinaryNode<Value, Arg1, Arg2>(std::move(arg1), std::move(arg2)) {} + const Table *ref_table() const { + return this->arg1_->ref_table(); + } + bool evaluate(Error *error, ArrayCRef<Record> records, ArrayRef<Value> results); @@ -2556,6 +2398,10 @@ class ReferenceNode : public BinaryNode<T, Int, T> { : BinaryNode<Value, Arg1, Arg2>(std::move(arg1), std::move(arg2)), temp_records_() {} + const Table *ref_table() const { + return this->arg2_->ref_table(); + } + bool evaluate(Error *error, ArrayCRef<Record> records, ArrayRef<Value> results); @@ -2751,6 +2597,10 @@ class ReferenceVectorNode : BinaryNode<Value, Arg1, Arg2>(std::move(arg1), std::move(arg2)), temp_records_() {} + const Table *ref_table() const { + return this->arg2_->ref_table(); + } + bool evaluate(Error *error, ArrayCRef<Record> records, ArrayRef<Value> results); -------------- next part -------------- HTML����������������������������...Download