souffle  2.0.2-371-g6315b36
AggregateOp.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2019, The Souffle Developers. 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 AggregateOp.h
12  *
13  * Defines aggregation operators for AST and RAM
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "souffle/TypeAttribute.h"
21 #include <cstdint>
22 #include <ostream>
23 #include <utility>
24 
25 namespace souffle {
26 
27 /** Types of aggregation functions */
28 enum class AggregateOp {
29  MAX,
30  MIN,
31  SUM,
32 
33  FMAX,
35  FSUM,
36  MEAN,
37 
38  UMAX,
39  UMIN,
40  USUM,
41 
42  COUNT,
43 };
44 
45 inline std::ostream& operator<<(std::ostream& os, AggregateOp op) {
46  switch (op) {
47  case AggregateOp::COUNT: return os << "count";
48 
49  case AggregateOp::MEAN: return os << "mean";
50 
52  case AggregateOp::UMAX:
53  case AggregateOp::FMAX: return os << "max";
54 
55  case AggregateOp::MIN:
56  case AggregateOp::UMIN:
57  case AggregateOp::FMIN: return os << "min";
58 
59  case AggregateOp::SUM:
60  case AggregateOp::USUM:
61  case AggregateOp::FSUM: return os << "sum";
62  }
63 
65 }
66 
67 // `[min, max]` # of arguments for each function
68 inline std::pair<uint8_t, uint8_t> aggregateArity(AggregateOp op) {
69  switch (op) {
70  case AggregateOp::COUNT: return {0, 0};
71 
72  case AggregateOp::FMAX:
73  case AggregateOp::FMIN:
75  case AggregateOp::MAX:
76  case AggregateOp::MEAN:
77  case AggregateOp::MIN:
78  case AggregateOp::SUM:
79  case AggregateOp::UMAX:
80  case AggregateOp::UMIN:
81  case AggregateOp::USUM: return {1, 1};
82  }
83 
85 }
86 
87 /**
88  * Get return type of the aggregate.
89  */
91  switch (op) {
92  case AggregateOp::COUNT:
93  case AggregateOp::MAX:
94  case AggregateOp::MIN:
96 
97  case AggregateOp::MEAN:
98  case AggregateOp::FMAX:
99  case AggregateOp::FMIN:
101 
102  case AggregateOp::UMAX:
103  case AggregateOp::UMIN:
105  }
106 
108 }
109 
110 inline bool isOverloadedAggregator(const AggregateOp op) {
111  switch (op) {
112  case AggregateOp::MAX:
113  case AggregateOp::MIN:
114  case AggregateOp::SUM: return true;
115 
117  case AggregateOp::COUNT: return false;
118 
119  default: return false;
120  }
121 }
122 
123 /**
124  * Convert aggregator to a give type.
125  * Eg. sum, float → fsum.
126  **/
128 #define CASE_NUMERIC(op) \
129  case AggregateOp::op: \
130  if (type == TypeAttribute::Signed) return AggregateOp::op; \
131  if (type == TypeAttribute::Unsigned) return AggregateOp::U##op; \
132  if (type == TypeAttribute::Float) return AggregateOp::F##op; \
133  fatal("invalid overload");
134 
135  switch (op) {
136  default:
137  fatal("agg op is not overloadable");
138 
139  // clang-format off
143  // clang-format on
144  }
145 
146 #undef CASE_NUMERIC
147 }
148 
149 } // namespace souffle
souffle::AggregateOp::MIN
@ MIN
souffle::AggregateOp::USUM
@ USUM
UNREACHABLE_BAD_CASE_ANALYSIS
#define UNREACHABLE_BAD_CASE_ANALYSIS
Definition: MiscUtil.h:206
CASE_NUMERIC
#define CASE_NUMERIC(op)
souffle::AggregateOp::FSUM
@ FSUM
TypeAttribute
Type attribute class.
souffle::convertOverloadedAggregator
AggregateOp convertOverloadedAggregator(const AggregateOp op, const TypeAttribute type)
Convert aggregator to a give type.
Definition: AggregateOp.h:133
souffle::AggregateOp
AggregateOp
Types of aggregation functions.
Definition: AggregateOp.h:34
MiscUtil.h
souffle::TypeAttribute::Signed
@ Signed
souffle::AggregateOp::UMIN
@ UMIN
souffle::TypeAttribute::Unsigned
@ Unsigned
souffle::AggregateOp::MAX
@ MAX
souffle::AggregateOp::UMAX
@ UMAX
souffle::AggregateOp::FMIN
@ FMIN
souffle::AggregateOp::FMAX
@ FMAX
souffle::aggregateArity
std::pair< uint8_t, uint8_t > aggregateArity(AggregateOp op)
Definition: AggregateOp.h:74
souffle::operator<<
std::ostream & operator<<(std::ostream &os, AggregateOp op)
Definition: AggregateOp.h:51
souffle::getTypeAttributeAggregate
TypeAttribute getTypeAttributeAggregate(const AggregateOp op)
Get return type of the aggregate.
Definition: AggregateOp.h:96
souffle::AggregateOp::COUNT
@ COUNT
souffle::AggregateOp::SUM
@ SUM
souffle::AggregateOp::MEAN
@ MEAN
souffle::fatal
void fatal(const char *format, const Args &... args)
Definition: MiscUtil.h:198
souffle
Definition: AggregateOp.h:25
souffle::TypeAttribute::Float
@ Float
std::type
ElementType type
Definition: span.h:640
TypeAttribute.h
souffle::isOverloadedAggregator
bool isOverloadedAggregator(const AggregateOp op)
Definition: AggregateOp.h:116