souffle  2.0.2-371-g6315b36
IndexAggregate.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file IndexAggregate.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
17 #include "AggregateOp.h"
18 #include "ram/AbstractAggregate.h"
19 #include "ram/Condition.h"
20 #include "ram/Expression.h"
21 #include "ram/IndexOperation.h"
22 #include "ram/Node.h"
23 #include "ram/Operation.h"
24 #include "ram/Relation.h"
25 #include "ram/utility/NodeMapper.h"
26 #include "ram/utility/Utils.h"
29 #include <iosfwd>
30 #include <memory>
31 #include <ostream>
32 #include <string>
33 #include <utility>
34 #include <vector>
35 
36 namespace souffle::ram {
37 
38 /**
39  * @class IndexAggregate
40  * @brief Indexed aggregation on a relation. The index allows us to iterate over a restricted range
41  *
42  * For example:
43  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44  * t0.0=sum t0.1 SEARCH t0 ∈ S ON INDEX t0.0 = number(1)
45  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46  */
47 class IndexAggregate : public IndexOperation, public AbstractAggregate {
48 public:
49  IndexAggregate(Own<Operation> nested, AggregateOp fun, std::string rel, Own<Expression> expression,
50  Own<Condition> condition, RamPattern queryPattern, int ident)
51  : IndexOperation(rel, ident, std::move(queryPattern), std::move(nested)),
52  AbstractAggregate(fun, std::move(expression), std::move(condition)) {}
53 
54  std::vector<const Node*> getChildNodes() const override {
55  auto res = IndexOperation::getChildNodes();
56  auto children = AbstractAggregate::getChildNodes();
57  res.insert(res.end(), children.begin(), children.end());
58  return res;
59  }
60 
61  IndexAggregate* clone() const override {
62  RamPattern pattern;
63  for (const auto& i : queryPattern.first) {
64  pattern.first.emplace_back(i->clone());
65  }
66  for (const auto& i : queryPattern.second) {
67  pattern.second.emplace_back(i->clone());
68  }
69  return new IndexAggregate(souffle::clone(&getOperation()), function, relation,
70  souffle::clone(expression), souffle::clone(condition), std::move(pattern), getTupleId());
71  }
72 
73  void apply(const NodeMapper& map) override {
75  condition = map(std::move(condition));
76  expression = map(std::move(expression));
77  }
78 
79 protected:
80  void print(std::ostream& os, int tabpos) const override {
81  os << times(" ", tabpos);
82  os << "t" << getTupleId() << ".0=";
83  AbstractAggregate::print(os, tabpos);
84  os << "SEARCH t" << getTupleId() << " ∈ " << relation;
85  printIndex(os);
86  if (!isTrue(condition.get())) {
87  os << " WHERE " << getCondition();
88  }
89  os << std::endl;
90  IndexOperation::print(os, tabpos + 1);
91  }
92 
93  bool equal(const Node& node) const override {
94  const auto& other = static_cast<const IndexAggregate&>(node);
95  return IndexOperation::equal(other) && AbstractAggregate::equal(other);
96  }
97 };
98 
99 } // namespace souffle::ram
souffle::ram::IndexOperation
Definition: IndexOperation.h:48
souffle::ram::IndexAggregate::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: IndexAggregate.h:58
souffle::ram::AbstractAggregate::equal
bool equal(const Node &node) const
Definition: AbstractAggregate.h:94
AggregateOp.h
souffle::ram::IndexOperation::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: IndexOperation.h:74
souffle::ram::AbstractAggregate::getCondition
const Condition & getCondition() const
Get condition.
Definition: AbstractAggregate.h:53
souffle::AggregateOp
AggregateOp
Types of aggregation functions.
Definition: AggregateOp.h:34
souffle::ram::NestedOperation::getOperation
Operation & getOperation() const
Get nested operation.
Definition: NestedOperation.h:62
souffle::map
auto map(const std::vector< A > &xs, F &&f)
Applies a function to each element of a vector and returns the results.
Definition: ContainerUtil.h:158
MiscUtil.h
souffle::ram::IndexAggregate::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: IndexAggregate.h:97
souffle::ram::AbstractAggregate::print
void print(std::ostream &os, int) const
Definition: AbstractAggregate.h:74
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::RelationOperation::relation
const std::string relation
Search relation.
Definition: RelationOperation.h:60
souffle::ram::AbstractAggregate
Abstract class for aggregation.
Definition: AbstractAggregate.h:42
souffle::ram::IndexOperation::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: IndexOperation.h:85
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
NodeMapper.h
souffle::times
detail::multiplying_printer< T > times(const T &value, unsigned num)
A utility printing a given value multiple times.
Definition: StreamUtil.h:322
i
size_t i
Definition: json11.h:663
souffle::ram::IndexAggregate::print
void print(std::ostream &os, int tabpos) const override
Pretty print with indentation.
Definition: IndexAggregate.h:84
souffle::ram::IndexOperation::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: IndexOperation.h:153
Relation.h
souffle::ram::AbstractAggregate::expression
Own< Expression > expression
Aggregation expression.
Definition: AbstractAggregate.h:104
Condition.h
souffle::ram::IndexAggregate
Indexed aggregation on a relation. The index allows us to iterate over a restricted range.
Definition: IndexAggregate.h:51
souffle::ram::TupleOperation::getTupleId
int getTupleId() const
Get identifier.
Definition: TupleOperation.h:43
Utils.h
souffle::ram::IndexOperation::printIndex
void printIndex(std::ostream &os) const
Helper method for printing.
Definition: IndexOperation.h:108
souffle::ram::isTrue
bool isTrue(const Condition *cond)
Determines if a condition represents true.
Definition: Utils.h:45
souffle::ram::IndexAggregate::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: IndexAggregate.h:77
IndexOperation.h
Node.h
std
Definition: Brie.h:3053
Operation.h
souffle::ram::IndexOperation::queryPattern
RamPattern queryPattern
Values of index per column of table (if indexable)
Definition: IndexOperation.h:161
souffle::ram::RamPattern
std::pair< RamBound, RamBound > RamPattern
Definition: IndexOperation.h:42
StreamUtil.h
AbstractAggregate.h
souffle::ram::IndexAggregate::IndexAggregate
IndexAggregate(Own< Operation > nested, AggregateOp fun, std::string rel, Own< Expression > expression, Own< Condition > condition, RamPattern queryPattern, int ident)
Definition: IndexAggregate.h:53
Expression.h
souffle::ram::IndexAggregate::clone
IndexAggregate * clone() const override
Create a clone (i.e.
Definition: IndexAggregate.h:65
souffle::ram::AbstractAggregate::getChildNodes
std::vector< const Node * > getChildNodes() const
Definition: AbstractAggregate.h:69
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::ram::NestedOperation::print
void print(std::ostream &os, int tabpos) const override
Pretty print with indentation.
Definition: NestedOperation.h:80
souffle::ram::AbstractAggregate::condition
Own< Condition > condition
Aggregation tuple condition.
Definition: AbstractAggregate.h:107