Skip to content

Commit

Permalink
Adds basic SQL capabilities (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
torpedro authored and Bensk1 committed May 29, 2017
1 parent b242140 commit 7295646
Show file tree
Hide file tree
Showing 15 changed files with 1,047 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "third_party/benchmark"]
path = third_party/benchmark
url = https://github.com/google/benchmark.git
[submodule "third_party/sql-parser"]
path = third_party/sql-parser
url = https://github.com/hyrise/sql-parser
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Contact: [email protected]
- Timo Djürken
- Moritz Eyssen
- Martin Fischer
- Pedro Flemming
- Michael Janke
- Max Jendruk
- Marvin Keller
Expand Down
36 changes: 25 additions & 11 deletions premake4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ solution "opossum"
defines { "OPOSSUM_NUMA_SUPPORT=0" }
end

libs[#libs+1] = "sqlparser"

configuration "Debug"
defines { "IS_DEBUG=1" }
flags { "Symbols" }
Expand Down Expand Up @@ -124,44 +126,56 @@ project "googlebenchmark"
configuration "Debug or Release"
defines {"NDEBUG", "HAVE_STD_REGEX"}

project "sqlparser"
kind "StaticLib"
buildoptions { "-O3 -Wno-sign-compare" }

-- clang throws unneeded-internal-declaration for parser generated code.
-- This warning does not exist in gcc, so we have to make a special fork here.
if _OPTIONS["compiler"] == "clang" then
buildoptions { "-O3 -Wno-sign-compare -Wno-unneeded-internal-declaration" }
end

files { "third_party/sql-parser/src/**.cpp" }

project "opossum"
kind "StaticLib"
includedirs { "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/" }
includedirs { "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/", "third_party/sql-parser/src/" }
files { "src/lib/**.hpp", "src/lib/**.cpp" }

project "opossum-asan"
kind "StaticLib"
buildoptions {"-fsanitize=address -fno-omit-frame-pointer"}
linkoptions {"-fsanitize=address"}
includedirs { "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/" }
includedirs { "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/", "third_party/sql-parser/src/" }
files { "src/lib/**.hpp", "src/lib/**.cpp", "src/bin/server_main.cpp" }

project "opossumCoverage"
kind "StaticLib"
buildoptions { "-fprofile-arcs -ftest-coverage" }
linkoptions { "-lgcov --coverage" }
includedirs { "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/" }
includedirs { "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/", "third_party/sql-parser/src/" }
files { "src/lib/**.hpp", "src/lib/**.cpp" }

-- Static lib for the opossum protobuf and grpc code generated from opossum.proto (see action 'protoc' below)
project "opossumProtobuf"
kind "StaticLib"
buildoptions ("-Wno-unused-parameter -Wno-deprecated-declarations")
includedirs { "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/" }
includedirs { "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/", "third_party/sql-parser/src/" }
files { "src/lib/network/generated/**.pb.cc" }

-- Exemplary opossum client, showing how to use grpc and protobuf at client-side
project "client"
kind "ConsoleApp"
links { "opossumProtobuf", "protobuf", "grpc++", "grpc", "z", "boost_program_options" }
includedirs { "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/" }
includedirs { "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/", "third_party/sql-parser/src/" }
libdirs { "third_party/grpc/libs/opt/", "third_party/grpc/libs/opt/protobuf" }
files { "src/bin/client.cpp" }

project "server"
kind "ConsoleApp"
links { "opossum", "opossumProtobuf", "protobuf", "grpc++", "grpc", "z", "boost_program_options" } -- z is needed on macos to link grpc
includedirs { "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/" }
includedirs { "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/", "third_party/sql-parser/src/" }
libdirs { "third_party/grpc/libs/opt/", "third_party/grpc/libs/opt/protobuf" }
links(libs)
files { "src/bin/server_main.cpp" }
Expand All @@ -176,7 +190,7 @@ project "test"
kind "ConsoleApp"

links { "opossum", "googletest", "opossumProtobuf", "protobuf", "grpc++", "grpc", "z" }
includedirs { "third_party/googletest/googletest/include", "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/" }
includedirs { "third_party/googletest/googletest/include", "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/", "third_party/sql-parser/src/" }
libdirs { "third_party/grpc/libs/opt/", "third_party/grpc/libs/opt/protobuf" }
links(libs)
files { "src/test/**.hpp", "src/test/**.cpp" }
Expand All @@ -188,7 +202,7 @@ project "asan"
links { "opossum-asan", "googletest", "opossumProtobuf", "protobuf", "grpc++", "grpc", "z" }
links(libs)
files { "src/test/**.hpp", "src/test/**.cpp" }
includedirs { "third_party/googletest/googletest/include", "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/" }
includedirs { "third_party/googletest/googletest/include", "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/", "third_party/sql-parser/src/" }
libdirs { "third_party/grpc/libs/opt/", "third_party/grpc/libs/opt/protobuf" }
buildoptions {"-fsanitize=address -fno-omit-frame-pointer"}
linkoptions { "-fsanitize=address" }
Expand All @@ -197,9 +211,9 @@ project "asan"
project "benchmark"
kind "ConsoleApp"

links { "opossum", "googlebenchmark" }
links { "opossum", "googlebenchmark", "sqlparser" }
files { "src/benchmark/**.hpp", "src/benchmark/**.cpp" }
includedirs { "third_party/benchmark/include" }
includedirs { "third_party/benchmark/include", "third_party/sql-parser/src/" }
postbuildcommands { "./build/benchmark --benchmark_format=json > benchmark.json" }

project "coverage"
Expand All @@ -210,7 +224,7 @@ project "coverage"
linkoptions {"--coverage"}
files { "src/test/**.hpp", "src/test/**.cpp" }
buildoptions { "-fprofile-arcs -ftest-coverage" }
includedirs { "third_party/googletest/googletest/include", "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/" }
includedirs { "third_party/googletest/googletest/include", "third_party/grpc/include/", "third_party/grpc/third_party/protobuf/src/", "third_party/sql-parser/src/" }
libdirs { "third_party/grpc/libs/opt/", "third_party/grpc/libs/opt/protobuf" }
postbuildcommands { "./build/coverage && rm -fr coverage; mkdir coverage && gcovr -s -r . --exclude=\"(.*types*.|.*test*.|.*\.pb\.|third_party)\" --html --html-details -o coverage/index.html" }

Expand Down
52 changes: 52 additions & 0 deletions src/benchmark/operators/sql_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <memory>
#include <string>
#include <utility>

#include "benchmark/benchmark.h"

#include "../../lib/sql/sql_query_translator.hpp"
#include "../base_fixture.cpp"
#include "SQLParser.h"

namespace opossum {

class SQLBenchmark : public BenchmarkBasicFixture {
public:
virtual void SetUp(const ::benchmark::State& state) {}

virtual void TearDown(const ::benchmark::State& state) {}
};

BENCHMARK_F(SQLBenchmark, BM_SQLTranslationTotal)(benchmark::State& state) {
clear_cache();
const std::string query = "SELECT * FROM benchmark_table_one WHERE a >= 7;";

while (state.KeepRunning()) {
SQLQueryTranslator translator;
translator.translate_query(query);
}
}

BENCHMARK_F(SQLBenchmark, BM_SQLTranslationOnlyParsing)(benchmark::State& state) {
clear_cache();
const std::string query = "SELECT * FROM benchmark_table_one WHERE a >= 7;";

while (state.KeepRunning()) {
hsql::SQLParserResult result;
hsql::SQLParser::parseSQLString(query, &result);
}
}

BENCHMARK_F(SQLBenchmark, BM_SQLTranslationOnlyTransation)(benchmark::State& state) {
clear_cache();
const std::string query = "SELECT * FROM benchmark_table_one WHERE a >= 7;";
hsql::SQLParserResult result;
hsql::SQLParser::parseSQLString(query, &result);

while (state.KeepRunning()) {
SQLQueryTranslator translator;
translator.translate_statement(*result.getStatement(0));
}
}

} // namespace opossum
4 changes: 3 additions & 1 deletion src/lib/operators/get_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

namespace opossum {

GetTable::GetTable(const std::string &name) : _name(name) {}
GetTable::GetTable(const std::string& name) : _name(name) {}

const std::string GetTable::name() const { return "GetTable"; }

uint8_t GetTable::num_in_tables() const { return 0; }

uint8_t GetTable::num_out_tables() const { return 1; }

const std::string& GetTable::table_name() const { return _name; }

std::shared_ptr<const Table> GetTable::on_execute() { return StorageManager::get().get_table(_name); }
} // namespace opossum
4 changes: 3 additions & 1 deletion src/lib/operators/get_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ namespace opossum {
// operator to retrieve a table from the StorageManager by specifying its name
class GetTable : public AbstractReadOnlyOperator {
public:
explicit GetTable(const std::string &name);
explicit GetTable(const std::string& name);

const std::string name() const override;
uint8_t num_in_tables() const override;
uint8_t num_out_tables() const override;

const std::string& table_name() const;

protected:
std::shared_ptr<const Table> on_execute() override;

Expand Down
57 changes: 57 additions & 0 deletions src/lib/sql/sql_query_operator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "sql_query_operator.hpp"

#include <memory>
#include <string>

#include "sql_query_translator.hpp"

#include "SQLParser.h"

namespace opossum {

SQLQueryOperator::SQLQueryOperator(const std::string& query) : _query(query) {
_result_op = std::make_shared<SQLResultOperator>();
_result_task = std::make_shared<OperatorTask>(_result_op);
}

const std::string SQLQueryOperator::name() const { return "SQLQueryOperator"; }

uint8_t SQLQueryOperator::num_in_tables() const { return 0; }

uint8_t SQLQueryOperator::num_out_tables() const { return 0; }

const std::shared_ptr<OperatorTask>& SQLQueryOperator::get_result_task() const { return _result_task; }

std::shared_ptr<const Table> SQLQueryOperator::on_execute(std::shared_ptr<TransactionContext> context) {
// TODO(torpedro): Check query cache for execution plan.

// TODO(torpedro): Check query cache for syntax tree.

SQLQueryTranslator translator;

hsql::SQLParserResult result;

if (!translator.parse_query(_query, &result)) {
throw translator.get_error_msg();
}

// Translate the query.
if (!translator.translate_parse_result(result)) {
throw translator.get_error_msg();
}

// Schedule all tasks.
auto tasks = translator.get_tasks();

tasks.back()->set_as_predecessor_of(_result_task);
_result_op->set_input_operator(tasks.back()->get_operator());

for (const auto& task : tasks) {
task->schedule();
}
_result_task->schedule();

return nullptr;
}

} // namespace opossum
41 changes: 41 additions & 0 deletions src/lib/sql/sql_query_operator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <memory>
#include <string>

#include "operators/abstract_operator.hpp"
#include "operators/abstract_read_only_operator.hpp"
#include "scheduler/operator_task.hpp"
#include "sql/sql_result_operator.hpp"

namespace opossum {

// The SQLQueryOperator takes a SQL query, parses and transforms it.
// The it schedules the resulting execution plan. To get the result
// of the execution plan, it exposes an SQLResultOperator task, which
// will upon completion contain the result table of the query.
class SQLQueryOperator : public AbstractOperator {
public:
explicit SQLQueryOperator(const std::string& query);

const std::string name() const override;

uint8_t num_in_tables() const override;

uint8_t num_out_tables() const override;

const std::shared_ptr<OperatorTask>& get_result_task() const;

protected:
std::shared_ptr<const Table> on_execute(std::shared_ptr<TransactionContext> context) override;

// Raw SQL query string.
const std::string _query;

// Result operator, which will be dependent on the full execution of the exec plan.
std::shared_ptr<SQLResultOperator> _result_op;

std::shared_ptr<OperatorTask> _result_task;
};

} // namespace opossum
Loading

0 comments on commit 7295646

Please sign in to comment.