14#include <unordered_map>
15#include <unordered_set>
32void usage(std::ostream &out,
const char *name)
34 out <<
"A tool to generate queries for all connected subgraphs of a query's query graph.\n"
35 <<
"USAGE:\n\t" << name <<
" <SCHEMA.sql> [<QUERY.sql>]"
39int main(
int argc,
const char **argv)
44#define ADD(TYPE, VAR, INIT, SHORT, LONG, DESCR, CALLBACK)\
47 AP.add<TYPE>(SHORT, LONG, DESCR, CALLBACK);\
50 ADD(
bool,
args.show_help,
false,
52 "prints this help message",
53 [&](
bool) { args.show_help = true; });
59 usage(std::cout, argv[0]);
60 std::cout <<
"WHERE\n" << AP;
61 std::exit(EXIT_SUCCESS);
65 if (AP.
args().size() == 0 or AP.
args().size() > 2) {
66 usage(std::cout, argv[0]);
67 std::exit(EXIT_FAILURE);
75 std::filesystem::path path_to_schema(AP.
args()[0]);
79 std::cerr <<
"No database selected.\n";
80 std::exit(EXIT_FAILURE);
84 const std::string input = [&AP]() -> std::string {
85 if (AP.
args().size() == 1) {
86 return std::string(std::istreambuf_iterator<char>(std::cin), {});
88 std::filesystem::path path(AP.
args()[1]);
90 std::ifstream in(path);
92 std::cerr <<
"Could not open file '" << path <<
'\'';
93 const auto errsv = errno;
95 std::cerr <<
": " << strerror(errsv);
96 std::cerr << std::endl;
97 std::exit(EXIT_FAILURE);
99 return std::string(std::istreambuf_iterator<char>(in), {});
104 const std::unique_ptr<m::ast::SelectStmt> select = [&diag, &input]() -> std::unique_ptr<m::ast::SelectStmt> {
106 if (not m::is<m::ast::SelectStmt>(stmt.get())) {
107 std::cerr <<
"Expected a SELECT statement.\n";
108 std::exit(EXIT_FAILURE);
110 return std::unique_ptr<m::ast::SelectStmt>(m::as<m::ast::SelectStmt>(stmt.release()));
125 out <<
"SELECT COUNT(*)\n";
129 for (
auto start = slice.
begin(), it = start; it != slice.
end(); ++it) {
130 if (it != start) out <<
", ";
131 const auto source = G.
sources()[*it].get();
132 if (
const m::BaseTable *T = cast<const m::BaseTable>(source)) {
133 out << T->table().name();
134 if (T->alias().has_value())
135 out <<
" AS " << T->alias();
137 std::cerr <<
"ERROR: Nested queries are not supported." << std::endl;
138 std::exit(EXIT_FAILURE);
143 bool is_first_in_where =
true;
146 for (
auto &J : G.
joins()) {
147 auto &sources = J->sources();
148 for (
auto source : sources) {
149 if (not slice[source.get().id()])
152 if (is_first_in_where) {
154 is_first_in_where =
false;
158 J->condition().to_sql(out);
164 for (
auto idx : slice) {
165 const auto &source = *G.
sources()[idx];
166 const auto &selection = source.filter();
167 if (not selection.empty()) {
168 if (is_first_in_where) {
170 is_first_in_where =
false;
174 selection.to_sql(out);
A parser for command line arguments.
void parse_args(int argc, const char **argv)
Parses the arguments from argv.
const std::vector< const char * > & args() const
Returns all positional arguments.
void M_EXPORT execute_file(Diagnostic &diag, const std::filesystem::path &path)
Execute the SQL file at path.
std::unique_ptr< ast::Stmt > M_EXPORT statement_from_string(Diagnostic &diag, const std::string &str)
Use lexer, parser, and semantic analysis to create a Stmt from str.
void emit_CSG_queries(std::ostream &out, const m::QueryGraph &G, const m::AdjacencyMatrix &M)
void emit_query_slice(std::ostream &out, const m::QueryGraph &G, m::Subproblem slice)
void usage(std::ostream &out, const char *name)
#define ADD(TYPE, VAR, INIT, SHORT, LONG, DESCR, CALLBACK)
bool show_help
‍whether to show a help message
An adjacency matrix for a given query graph.
void for_each_CSG_undirected(SmallBitset super, SmallBitset source, std::function< void(SmallBitset)> callback) const
Enumerate all connected subgraphs (CSGs) of the graph induced by vertex super set super,...
A BaseTable is a DataSource that is materialized and stored persistently by the database system.
The catalog contains all Databases and keeps track of all meta information of the database system.
bool has_database_in_use() const
Returns true if any Database is currently in use.
static Catalog & Get()
Return a reference to the single Catalog instance.
static Options & Get()
Return a reference to the single Options instance.
The query graph represents all data sources and joins in a graph structure.
const auto & joins() const
const auto & sources() const
static std::unique_ptr< QueryGraph > Build(const ast::Stmt &stmt)
Implements a small and efficient set over integers in the range of 0 to 63 (including).
static SmallBitset All(std::size_t n)
Factory method for creating a SmallBitset with first n bits set.