souffle  2.0.2-371-g6315b36
MakeIndex.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2018, 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 MakeIndex.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
17 #include "ram/Aggregate.h"
18 #include "ram/Condition.h"
19 #include "ram/Constraint.h"
20 #include "ram/Expression.h"
21 #include "ram/IndexOperation.h"
22 #include "ram/IndexScan.h"
23 #include "ram/Operation.h"
24 #include "ram/Program.h"
25 #include "ram/Scan.h"
26 #include "ram/TranslationUnit.h"
27 #include "ram/analysis/Level.h"
28 #include "ram/analysis/Relation.h"
30 #include <cstddef>
31 #include <memory>
32 #include <string>
33 #include <utility>
34 #include <vector>
35 
36 namespace souffle::ram::transform {
37 
38 /**
39  * @class MakeIndexTransformer
40  * @brief Make indexable operations to indexed operations.
41  *
42  * The transformer assumes that the RAM has been levelled before.
43  * The conditions that could be used for an index must be located
44  * immediately after the scan or aggregate operation.
45  *
46  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
47  * QUERY
48  * ...
49  * FOR t1 IN A
50  * IF t1.x = 10 /\ t1.y = 20 /\ C
51  * ...
52  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
53  *
54  * will be rewritten to
55  *
56  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
57  * QUERY
58  * ...
59  * SEARCH t1 IN A INDEX t1.x=10 AND t1.y = 20
60  * IF C
61  * ...
62  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
63  */
64 class MakeIndexTransformer : public Transformer {
65 public:
66  std::string getName() const override {
67  return "MakeIndexTransformer";
68  }
69 
70  /**
71  * @brief Get expression of RAM element access
72  *
73  * @param Equivalence constraints of the format t1.x = <expression> or <expression> = t1.x
74  * @param Element that was accessed, e.g., for t1.x this would be the index of attribute x.
75  * @param Tuple identifier
76  *
77  * The method retrieves expression the expression of an equivalence constraint of the
78  * format t1.x = <expr> or <expr> = t1.x
79  */
80  using ExpressionPair = std::pair<Own<Expression>, Own<Expression>>;
81 
82  ExpressionPair getExpressionPair(const Constraint* binRelOp, size_t& element, int identifier);
83  ExpressionPair getLowerUpperExpression(Condition* c, size_t& element, int level);
84 
85  /**
86  * @param AttributeTypes to indicate type of each attribute in the relation
87  * @param Query pattern that is to be constructed
88  * @param Flag to indicate whether operation is indexable
89  * @param A list of conditions that will be transformed to query patterns
90  * @param Tuple identifier of the indexable operation
91  * @result Remaining conditions that could not be transformed to an index
92  */
93  Own<Condition> constructPattern(const std::vector<std::string>& attributeTypes, RamPattern& queryPattern,
94  bool& indexable, VecOwn<Condition> conditionList, int identifier);
95 
96  /**
97  * @brief Rewrite a scan operation to an indexed scan operation
98  * @param Scan operation that is potentially rewritten to an IndexScan
99  * @result The result is null if the scan could not be rewritten to an IndexScan;
100  * otherwise the new IndexScan operation is returned.
101  */
102  Own<Operation> rewriteScan(const Scan* scan);
103 
104  /**
105  * @brief Rewrite an index scan operation to an amended index scan operation
106  * @param An IndexScan that can be amended with new index values
107  * @result The result is null if the index scan cannot be amended;
108  * otherwise the new IndexScan operation is returned.
109  */
111 
112  /**
113  * @brief Rewrite an aggregate operation to an indexed aggregate operation
114  * @param Aggregate operation that is potentially rewritten to an indexed version
115  * @result The result is null if the aggregate could not be rewritten to an indexed version;
116  * otherwise the new indexed version of the aggregate is returned.
117  */
119 
120  /**
121  * @brief Make indexable RAM operation indexed
122  * @param RAM program that is transformed
123  * @result Flag that indicates whether the input program has changed
124  */
125  bool makeIndex(Program& program);
126 
127 protected:
128  analysis::LevelAnalysis* rla{nullptr};
129 
130  bool transform(TranslationUnit& translationUnit) override {
131  rla = translationUnit.getAnalysis<analysis::LevelAnalysis>();
133  return makeIndex(translationUnit.getProgram());
134  }
135 
137 };
138 
139 } // namespace souffle::ram::transform
souffle::ram::transform
Definition: ChoiceConversion.cpp:30
Aggregate.h
souffle::ram::Program
RAM program relation declaration and functions.
Definition: Program.h:58
souffle::ram::TranslationUnit::getAnalysis
Analysis * getAnalysis() const
Get an analysis.
Definition: TranslationUnit.h:66
souffle::ram::transform::MakeIndexTransformer::getLowerUpperExpression
ExpressionPair getLowerUpperExpression(Condition *c, size_t &element, int level)
Definition: MakeIndex.cpp:92
souffle::ram::transform::MakeIndexTransformer::rewriteIndexScan
Own< Operation > rewriteIndexScan(const IndexScan *iscan)
Rewrite an index scan operation to an amended index scan operation.
Definition: MakeIndex.cpp:333
Constraint.h
Scan.h
souffle::ram::transform::MakeIndexTransformer::getName
std::string getName() const override
@Brief get name of the transformer
Definition: MakeIndex.h:70
souffle::ram::analysis::LevelAnalysis
A Ram Analysis for determining the level of a expression/condition.
Definition: Level.h:51
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::ram::transform::MakeIndexTransformer::rewriteAggregate
Own< Operation > rewriteAggregate(const Aggregate *agg)
Rewrite an aggregate operation to an indexed aggregate operation.
Definition: MakeIndex.cpp:286
souffle::ram::Condition
Abstract class for conditions and boolean values in RAM.
Definition: Condition.h:35
souffle::identifier
std::string identifier(std::string id)
Valid C++ identifier, note that this does not ensure the uniqueness of identifiers returned.
Definition: StringUtil.h:387
souffle::ram::transform::MakeIndexTransformer::ExpressionPair
std::pair< Own< Expression >, Own< Expression > > ExpressionPair
Get expression of RAM element access.
Definition: MakeIndex.h:84
souffle::ram::transform::MakeIndexTransformer::rewriteScan
Own< Operation > rewriteScan(const Scan *scan)
Rewrite a scan operation to an indexed scan operation.
Definition: MakeIndex.cpp:308
Program.h
souffle::ram::TranslationUnit::getProgram
Program & getProgram() const
Get the RAM Program of the translation unit
Definition: TranslationUnit.h:107
souffle::ram::TranslationUnit
Translating a RAM program.
Definition: TranslationUnit.h:55
Relation.h
IndexScan.h
souffle::ram::transform::MakeIndexTransformer::constructPattern
Own< Condition > constructPattern(const std::vector< std::string > &attributeTypes, RamPattern &queryPattern, bool &indexable, VecOwn< Condition > conditionList, int identifier)
Definition: MakeIndex.cpp:121
souffle::ram::Aggregate
Aggregation function applied on some relation.
Definition: Aggregate.h:53
Condition.h
TranslationUnit.h
Transformer.h
souffle::ram::transform::MakeIndexTransformer::relAnalysis
analysis::RelationAnalysis * relAnalysis
Definition: MakeIndex.h:140
souffle::ram::Scan
Iterate all tuples of a relation.
Definition: Scan.h:47
souffle::ram::IndexScan
Search for tuples of a relation matching a criteria.
Definition: IndexScan.h:52
souffle::ram::transform::MakeIndexTransformer::transform
bool transform(TranslationUnit &translationUnit) override
@Brief transform the translation unit / used by apply @Param translationUnit that will be transformed...
Definition: MakeIndex.h:134
IndexOperation.h
souffle::ram::Constraint
Evaluates a binary constraint with respect to two Expressions.
Definition: Constraint.h:55
Operation.h
souffle::ram::RamPattern
std::pair< RamBound, RamBound > RamPattern
Definition: IndexOperation.h:42
souffle::ram::analysis::RelationAnalysis
A RAM Analysis for finding relations by name.
Definition: Relation.h:36
Expression.h
souffle::ram::transform::MakeIndexTransformer::makeIndex
bool makeIndex(Program &program)
Make indexable RAM operation indexed.
Definition: MakeIndex.cpp:361
Level.h
souffle::ram::transform::MakeIndexTransformer::getExpressionPair
ExpressionPair getExpressionPair(const Constraint *binRelOp, size_t &element, int identifier)
Definition: MakeIndex.cpp:48
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
souffle::ram::transform::MakeIndexTransformer::rla
analysis::LevelAnalysis * rla
Definition: MakeIndex.h:132