souffle  2.0.2-371-g6315b36
IndexChoice.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 IndexChoice.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
17 #include "ram/AbstractChoice.h"
18 #include "ram/Condition.h"
19 #include "ram/Expression.h"
20 #include "ram/IndexOperation.h"
21 #include "ram/Node.h"
22 #include "ram/Operation.h"
23 #include "ram/Relation.h"
24 #include "ram/RelationOperation.h"
25 #include "ram/utility/NodeMapper.h"
28 #include <cassert>
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 IndexChoice
40  * @brief Use an index to find a tuple in a relation such that a given condition holds.
41  *
42  * Only one tuple is returned (if one exists), even
43  * if multiple tuples satisfying the condition exist.
44  *
45  * For example:
46  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
47  * QUERY
48  * ...
49  * CHOICE A AS t1 ON INDEX t1.x=10 AND t1.y = 20
50  * WHERE (t1.x, t1.y) NOT IN A
51  * ...
52  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~
53  */
54 class IndexChoice : public IndexOperation, public AbstractChoice {
55 public:
56  IndexChoice(std::string rel, int ident, Own<Condition> cond, RamPattern queryPattern,
57  Own<Operation> nested, std::string profileText = "")
58 
59  : IndexOperation(rel, ident, std::move(queryPattern), std::move(nested), std::move(profileText)),
60  AbstractChoice(std::move(cond)) {
61  assert(getRangePattern().first.size() == getRangePattern().second.size() && "Arity mismatch");
62  }
63 
64  void apply(const NodeMapper& map) override {
66  for (auto& pattern : queryPattern.first) {
67  pattern = map(std::move(pattern));
68  }
69  for (auto& pattern : queryPattern.second) {
70  pattern = map(std::move(pattern));
71  }
73  }
74 
75  std::vector<const Node*> getChildNodes() const override {
76  auto res = IndexOperation::getChildNodes();
77  res.push_back(AbstractChoice::getChildNodes().at(0));
78  return res;
79  }
80 
81  IndexChoice* clone() const override {
82  RamPattern resQueryPattern;
83  for (const auto& i : queryPattern.first) {
84  resQueryPattern.first.emplace_back(i->clone());
85  }
86  for (const auto& i : queryPattern.second) {
87  resQueryPattern.second.emplace_back(i->clone());
88  }
90  std::move(resQueryPattern), souffle::clone(&getOperation()), getProfileText());
91  return res;
92  }
93 
94 protected:
95  void print(std::ostream& os, int tabpos) const override {
96  os << times(" ", tabpos);
97  os << "CHOICE " << relation << " AS t" << getTupleId();
98  printIndex(os);
99  os << " WHERE " << getCondition();
100  os << std::endl;
101  IndexOperation::print(os, tabpos + 1);
102  }
103 
104  bool equal(const Node& node) const override {
105  const auto& other = static_cast<const IndexChoice&>(node);
106  return IndexOperation::equal(other) && AbstractChoice::equal(other);
107  }
108 };
109 
110 } // namespace souffle::ram
souffle::ram::NestedOperation::profileText
const std::string profileText
Text used by the profiler.
Definition: NestedOperation.h:93
souffle::ram::IndexOperation
Definition: IndexOperation.h:48
souffle::ram::AbstractChoice::apply
void apply(const NodeMapper &map)
Definition: AbstractChoice.h:50
souffle::ram::IndexChoice::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: IndexChoice.h:108
souffle::ram::IndexOperation::getRangePattern
std::pair< std::vector< Expression * >, std::vector< Expression * > > getRangePattern() const
Get range pattern.
Definition: IndexOperation.h:70
souffle::ram::IndexOperation::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: IndexOperation.h:74
souffle::ram::AbstractChoice
Abstract class for a choice operation.
Definition: AbstractChoice.h:38
souffle::ram::NestedOperation::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: NestedOperation.h:75
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
AbstractChoice.h
souffle::ram::IndexChoice::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain list of all embedded child nodes.
Definition: IndexChoice.h:79
RelationOperation.h
souffle::ram
Definition: AstToRamTranslator.h:54
souffle::ram::RelationOperation::relation
const std::string relation
Search relation.
Definition: RelationOperation.h:60
souffle::ram::Node
Node is a superclass for all RAM IR classes.
Definition: Node.h:42
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
NodeMapper.h
souffle::ram::AbstractChoice::equal
bool equal(const Node &node) const
Definition: AbstractChoice.h:59
souffle::ram::NodeMapper
An abstract class for manipulating RAM Nodes by substitution.
Definition: NodeMapper.h:38
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::NestedOperation::getProfileText
const std::string & getProfileText() const
Get profile text.
Definition: NestedOperation.h:67
souffle::ram::IndexOperation::equal
bool equal(const Node &node) const override
Equality check for two RAM nodes.
Definition: IndexOperation.h:153
Relation.h
Condition.h
souffle::ram::IndexChoice
Use an index to find a tuple in a relation such that a given condition holds.
Definition: IndexChoice.h:58
souffle::ram::TupleOperation::getTupleId
int getTupleId() const
Get identifier.
Definition: TupleOperation.h:43
souffle::ram::IndexOperation::printIndex
void printIndex(std::ostream &os) const
Helper method for printing.
Definition: IndexOperation.h:108
souffle::ram::AbstractChoice::getChildNodes
std::vector< const Node * > getChildNodes() const
Definition: AbstractChoice.h:54
souffle::ram::IndexChoice::clone
IndexChoice * clone() const override
Create a clone (i.e.
Definition: IndexChoice.h:85
IndexOperation.h
souffle::ram::IndexChoice::print
void print(std::ostream &os, int tabpos) const override
Pretty print with indentation.
Definition: IndexChoice.h:99
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
souffle::ram::AbstractChoice::condition
Own< Condition > condition
Condition for which a tuple in the relation may hold.
Definition: AbstractChoice.h:65
Expression.h
souffle::ram::IndexChoice::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: IndexChoice.h:68
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::ram::AbstractChoice::getCondition
const Condition & getCondition() const
Getter for the condition.
Definition: AbstractChoice.h:45
souffle::ram::NestedOperation::print
void print(std::ostream &os, int tabpos) const override
Pretty print with indentation.
Definition: NestedOperation.h:80
souffle::ram::IndexChoice::IndexChoice
IndexChoice(std::string rel, int ident, Own< Condition > cond, RamPattern queryPattern, Own< Operation > nested, std::string profileText="")
Definition: IndexChoice.h:60