Skip to content

Commit

Permalink
Add TPC-H Q19
Browse files Browse the repository at this point in the history
Add the physical plan for TPC-H Q19. Add Q19 to the different
test/runner/benchmark binaries.
  • Loading branch information
wagjamin committed Nov 5, 2023
1 parent 556d5fb commit be5f75c
Show file tree
Hide file tree
Showing 17 changed files with 592 additions and 18 deletions.
9 changes: 5 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Generate DWARF 4 in debug to work on older GDB versions
# flto required as xxhash is also built with flto to allow efficient inlining
# of the hash functions.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gdwarf-4 -stdlib=libc++ -flto")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gdwarf-4 -stdlib=libc++")
# For benchmarks: easier profiling & links against system installed googlebench
# if that was build with non libcxx.
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gdwarf-4 -flto -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 -fsanitize=address")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -g -O3")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 -fsanitize=address -flto=thin")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -g -O3 -flto")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -flto")

# ---------------------------------------------------------------------------
# Dependencies
Expand Down Expand Up @@ -126,6 +126,7 @@ set(SRC_CC
"${CMAKE_SOURCE_DIR}/src/codegen/Value.cpp"
"${CMAKE_SOURCE_DIR}/src/codegen/Statement.cpp"
"${CMAKE_SOURCE_DIR}/src/codegen/backend_c/BackendC.cpp"
"${CMAKE_SOURCE_DIR}/src/codegen/backend_c/FunctionsC.cpp"
"${CMAKE_SOURCE_DIR}/src/codegen/backend_c/ScopedWriter.cpp"
"${CMAKE_SOURCE_DIR}/src/runtime/Runtime.cpp"
"${CMAKE_SOURCE_DIR}/src/exec/ExecutionContext.cpp"
Expand Down
36 changes: 36 additions & 0 deletions reproduce/sql/q19.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- No modifications
select
sum(l_extendedprice* (1 - l_discount)) as revenue
from
lineitem,
part
where
(
p_partkey = l_partkey
and p_brand = 'Brand#12'
and p_container in ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG')
and l_quantity >= 1 and l_quantity <= 1 + 10
and p_size between 1 and 5
and l_shipmode in ('AIR', 'AIR REG')
and l_shipinstruct = 'DELIVER IN PERSON'
)
or
(
p_partkey = l_partkey
and p_brand = 'Brand#23'
and p_container in ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK')
and l_quantity >= 10 and l_quantity <= 10 + 10
and p_size between 1 and 10
and l_shipmode in ('AIR', 'AIR REG')
and l_shipinstruct = 'DELIVER IN PERSON'
)
or
(
p_partkey = l_partkey
and p_brand = 'Brand#34'
and p_container in ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG')
and l_quantity >= 20 and l_quantity <= 20 + 10
and p_size between 1 and 15
and l_shipmode in ('AIR', 'AIR REG')
and l_shipinstruct = 'DELIVER IN PERSON'
);
2 changes: 1 addition & 1 deletion src/algebra/ExpressionOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ IR::TypeArc ExpressionOp::derive(ComputeNode::Type code, const std::vector<IR::T
ComputeNode::Type::Less, ComputeNode::Type::LessEqual,
ComputeNode::Type::Greater, ComputeNode::Type::GreaterEqual,
ComputeNode::Type::StrEquals, ComputeNode::Type::And,
ComputeNode::Type::Or};
ComputeNode::Type::Or, ComputeNode::Type::InList};
if (bool_returning.contains(code)) {
return IR::Bool::build();
}
Expand Down
1 change: 1 addition & 0 deletions src/algebra/ExpressionOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct ExpressionOp : public RelAlgOp {
GreaterEqual,
Constant,
StrEquals,
InList,
};

// Constructor for regular binary operations.
Expand Down
2 changes: 1 addition & 1 deletion src/algebra/suboperators/RuntimeParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace inkfuse {
} \
\
IR::ExprPtr pname##ResolveErased(const Suboperator& op, const IR::TypeArc& type, CompilationContext& context) const { \
if (!pname) { \
if (!pname || !pname->supportsInlining()) { \
const auto& program = context.getProgram(); \
auto state_expr = context.accessGlobalState(op); \
auto casted_state = IR::CastExpr::build( \
Expand Down
7 changes: 5 additions & 2 deletions src/algebra/suboperators/expressions/ExpressionHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const std::unordered_map<Type, std::string> expr_names{
{Type::Or, "or"},
{Type::Eq, "eq"},
{Type::Neq, "neq"},
{Type::StrEquals, "streq"}};
{Type::StrEquals, "streq"},
{Type::InList, "string_in"},
};

/// Map from algebra expression types to IR expressions in the jitted code.
const std::unordered_map<Type, IR::ArithmeticExpr::Opcode> code_map{
Expand All @@ -34,7 +36,8 @@ const std::unordered_map<Type, IR::ArithmeticExpr::Opcode> code_map{
{Type::And, IR::ArithmeticExpr::Opcode::And},
{Type::Or, IR::ArithmeticExpr::Opcode::Or},
{Type::Neq, IR::ArithmeticExpr::Opcode::Neq},
{Type::StrEquals, IR::ArithmeticExpr::Opcode::StrEquals}
{Type::StrEquals, IR::ArithmeticExpr::Opcode::StrEquals},
{Type::InList, IR::ArithmeticExpr::Opcode::StrInList}
};

}
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ struct ArithmeticExpr : public BinaryExpr {
Greater,
GreaterEqual,
HashCombine,
/// String equals - not really an arithmethic function, but easiest to put here for now.
/// String equals - not really an arithmetic function, but easiest to put here for now.
StrEquals,
/// In list with strings - not really an arithmetic function, but easiest to put here for now.
StrInList,
};

/// Opcode of this expression.
Expand Down
57 changes: 56 additions & 1 deletion src/codegen/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace IR {
/// Value of a certain type
struct Value {
virtual TypeArc getType() const = 0;

virtual bool supportsInlining() const { return true; }
virtual std::string str() const { return ""; };

virtual std::unique_ptr<Value> copy() = 0;
Expand Down Expand Up @@ -184,7 +186,60 @@ struct StringVal : public Value {
}

private:
StringVal(std::string value): value(std::move(value)), value_ptr(this->value.data()) {
StringVal(std::string value) : value(std::move(value)), value_ptr(this->value.data()) {
}
};

/// A list of strings.Can be extended to a general-purpose value list in the future.
struct StringList : public Value {
struct StringListView {
const char** start;
uint64_t size;
};

static ValuePtr build(std::vector<std::string> strings_) {
return ValuePtr(new StringList(std::move(strings_)));
}

TypeArc getType() const override {
// Pointer behind which the actual `StringListView` hides.
return IR::Pointer::build(IR::Char::build());
};

// String list cannot be inlined. It needs to stay an abstract char*.
bool supportsInlining() const override { return false; };

std::string str() const override {
throw std::runtime_error("str() on StringList not implemented");
};

std::unique_ptr<Value> copy() override {
return StringList::build(strings);
};

void* rawData() override {
// Return the raw state that can be interpreted by the runtime.
assert(raw_view.size < 1000);
assert(erased_view == &raw_view);
return &erased_view;
}

std::vector<std::string> strings;
std::unique_ptr<const char*[]> raw_chars;
StringListView raw_view;
void* erased_view;

private:
StringList(std::vector<std::string> strings_) : strings(std::move(strings_)) {
raw_chars = std::make_unique<const char*[]>(strings.size());
for (size_t k = 0; k < strings.size(); ++k) {
raw_chars[k] = strings[k].c_str();
}
raw_view = StringListView{
.start = raw_chars.get(),
.size = strings.size(),
};
erased_view = &raw_view;
}
};

Expand Down
14 changes: 12 additions & 2 deletions src/codegen/backend_c/BackendC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ std::unique_ptr<IR::BackendProgram> BackendC::generate(const IR::Program& progra
ScopedWriter writer;

// Step 1: Set up the preamble.
createPreamble(writer);
createPreamble(writer, program.program_name == "global_runtime");

// Step 2: Set up the includes.
for (const auto& include : program.getIncludes()) {
Expand All @@ -231,12 +231,16 @@ std::unique_ptr<IR::BackendProgram> BackendC::generate(const IR::Program& progra
return std::make_unique<BackendProgramC>(*this, writer.str(), program.program_name);
}

void BackendC::createPreamble(ScopedWriter& writer) {
void BackendC::createPreamble(ScopedWriter& writer, bool is_runtime) {
// Include the integer types needed.
writer.stmt(false).stream() << "#include <stdint.h>";
// We need to access strcmp.
writer.stmt(false).stream() << "#include <string.h>";
writer.stmt(false).stream() << "#include <stdbool.h>\n";

if (is_runtime) {
writer.stmt(false).stream() << runtime_functions;
}
}

void BackendC::compileInclude(const IR::Program& include, ScopedWriter& writer) {
Expand Down Expand Up @@ -493,6 +497,12 @@ void BackendC::compileExpression(const IR::Expr& expr, ScopedWriter::Statement&
stmt.stream() << ", ";
compileExpression(*type.children[1], stmt);
stmt.stream() << ") == 0)";
} else if (type.code == IR::ArithmeticExpr::Opcode::StrInList) {
stmt.stream() << "in_strlist(";
compileExpression(*type.children[0], stmt);
stmt.stream() << ", ";
compileExpression(*type.children[1], stmt);
stmt.stream() << ")";
} else {
// Regular arithmethic operation that's directly supported by C.
assert(opcode_map.count(type.code));
Expand Down
5 changes: 4 additions & 1 deletion src/codegen/backend_c/BackendC.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ struct BackendC : public IR::Backend {
std::unique_ptr<IR::BackendProgram> generate(const IR::Program& program) override;

private:
/// Runtime function implementations that are added to the global runtime implementation.
static char* runtime_functions;

/// Compile an included other program.
void compileInclude(const IR::Program& include, ScopedWriter& writer);

/// Set up the preamble.
static void createPreamble(ScopedWriter& writer);
static void createPreamble(ScopedWriter& writer, bool is_runtime);

/// Add a type description to the backing string stream.
static void typeDescription(const IR::Type& type, ScopedWriter::Statement& writer);
Expand Down
22 changes: 22 additions & 0 deletions src/codegen/backend_c/FunctionsC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "BackendC.h"
namespace inkfuse {

/// C code that's dumped into global_runtime.h during initial runtime
/// code generation.
char* BackendC::runtime_functions = R"PRE(
struct InLiteralList {
const char** start;
uint64_t size;
};
bool in_strlist(const char* strlist, char* arg) {
const struct InLiteralList* list = (const struct InLiteralList*) strlist;
bool res = false;
for (uint64_t k = 0; k < list->size; ++k) {
res |= (strcmp(list->start[k], arg) == 0);
}
return res;
}
)PRE";

};
Loading

0 comments on commit be5f75c

Please sign in to comment.