souffle  2.0.2-371-g6315b36
AbstractAggregate.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 AbstractAggregate.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
17 #include "AggregateOp.h"
18 #include "ram/Condition.h"
19 #include "ram/Expression.h"
20 #include "ram/Node.h"
22 #include <cassert>
23 #include <iosfwd>
24 #include <memory>
25 #include <ostream>
26 #include <utility>
27 #include <vector>
28 
29 namespace souffle::ram {
30 
31 /**
32  * @class AbstractAggregate
33  * @brief Abstract class for aggregation
34  *
35  * A particular function (e.g. MIN) is applied given a
36  * that a condition holds
37  */
38 class AbstractAggregate {
39 public:
40  AbstractAggregate(AggregateOp fun, Own<Expression> expr, Own<Condition> cond)
41  : function(fun), expression(std::move(expr)), condition(std::move(cond)) {
42  assert(condition != nullptr && "Condition is a null-pointer");
43  assert(expression != nullptr && "Expression is a null-pointer");
44  }
45 
46  virtual ~AbstractAggregate() = default;
47 
48  /** @brief Get condition */
49  const Condition& getCondition() const {
50  assert(condition != nullptr && "Condition of aggregate is a null-pointer");
51  return *condition;
52  }
53 
54  /** @brief Get aggregation function */
55  AggregateOp getFunction() const {
56  return function;
57  }
58 
59  /** @brief Get target expression */
60  const Expression& getExpression() const {
61  assert(expression != nullptr && "Expression of aggregate is a null-pointer");
62  return *expression;
63  }
64 
65  std::vector<const Node*> getChildNodes() const {
66  return {expression.get(), condition.get()};
67  }
68 
69 protected:
70  void print(std::ostream& os, int /* tabpos */) const {
71  switch (function) {
72  case AggregateOp::MIN:
73  case AggregateOp::FMIN:
74  case AggregateOp::UMIN: os << "min "; break;
75  case AggregateOp::MAX:
76  case AggregateOp::UMAX:
77  case AggregateOp::FMAX: os << "max "; break;
78  case AggregateOp::SUM:
79  case AggregateOp::FSUM:
80  case AggregateOp::USUM: os << "sum "; break;
81  case AggregateOp::COUNT: os << "count "; break;
82  case AggregateOp::MEAN: os << "mean "; break;
83  }
84  if (function != AggregateOp::COUNT) {
85  os << *expression << " ";
86  }
87  }
88 
89 protected:
90  bool equal(const Node& node) const {
91  const auto& other = dynamic_cast<const AbstractAggregate&>(node);
92  return function == other.function && equal_ptr(expression, other.expression) &&
93  equal_ptr(condition, other.condition);
94  }
95 
96  /** Aggregation function */
97  const AggregateOp function;
98 
99  /** Aggregation expression */
101 
102  /** Aggregation tuple condition */
104 };
105 
106 } // namespace souffle::ram
souffle::AggregateOp::MIN
@ MIN
souffle::AggregateOp::USUM
@ USUM
souffle::ram::AbstractAggregate::equal
bool equal(const Node &node) const
Definition: AbstractAggregate.h:94
souffle::AggregateOp::FSUM
@ FSUM
AggregateOp.h
souffle::ram::AbstractAggregate::getCondition
const Condition & getCondition() const
Get condition.
Definition: AbstractAggregate.h:53
souffle::ram::AbstractAggregate::getFunction
AggregateOp getFunction() const
Get aggregation function.
Definition: AbstractAggregate.h:59
souffle::AggregateOp
AggregateOp
Types of aggregation functions.
Definition: AggregateOp.h:34
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::ram::Condition
Abstract class for conditions and boolean values in RAM.
Definition: Condition.h:35
souffle::ram::AbstractAggregate::getExpression
const Expression & getExpression() const
Get target expression.
Definition: AbstractAggregate.h:64
souffle::ram::AbstractAggregate::print
void print(std::ostream &os, int) const
Definition: AbstractAggregate.h:74
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::AggregateOp::UMIN
@ UMIN
souffle::ram::AbstractAggregate::function
const AggregateOp function
Aggregation function.
Definition: AbstractAggregate.h:101
ContainerUtil.h
souffle::AggregateOp::MAX
@ MAX
souffle::ram::AbstractAggregate::expression
Own< Expression > expression
Aggregation expression.
Definition: AbstractAggregate.h:104
souffle::AggregateOp::UMAX
@ UMAX
Condition.h
souffle::AggregateOp::FMIN
@ FMIN
souffle::AggregateOp::FMAX
@ FMAX
souffle::equal_ptr
bool equal_ptr(const T *a, const T *b)
Compares two values referenced by a pointer where the case where both pointers are null is also consi...
Definition: MiscUtil.h:130
souffle::AggregateOp::COUNT
@ COUNT
souffle::AggregateOp::SUM
@ SUM
Node.h
souffle::AggregateOp::MEAN
@ MEAN
std
Definition: Brie.h:3053
souffle::ram::AbstractAggregate::~AbstractAggregate
virtual ~AbstractAggregate()=default
Expression.h
souffle::ram::AbstractAggregate::getChildNodes
std::vector< const Node * > getChildNodes() const
Definition: AbstractAggregate.h:69
souffle::ram::Expression
Abstract class for describing scalar values in RAM.
Definition: Expression.h:33
souffle::ram::AbstractAggregate::condition
Own< Condition > condition
Aggregation tuple condition.
Definition: AbstractAggregate.h:107
souffle::ram::AbstractAggregate::AbstractAggregate
AbstractAggregate(AggregateOp fun, Own< Expression > expr, Own< Condition > cond)
Definition: AbstractAggregate.h:44