buildyourowndatabase - hasso plattner institute...lambda expressions syntax: 38 auto f = [ captures]...

47
Build your own Database Week 4

Upload: others

Post on 13-Feb-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Build your own Database

Week 4

Page 2: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Agenda

• Q&A Sprint 2

• Review Sprint 1

• Experiments with Chunks

• std::optional

• Lambdas

2

Page 3: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Sprint 2

Questions?

3

Page 4: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Formatting / Linting

4

Page 5: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Code Style

5

std::vector<std::string> m_columnNames;std::vector<std::string> m_columnTypes;std::vector<Chunk> m_chunks;unsigned int m_chunkSize;

pbpaste | cat | highlight -O rtf --src-lang c++ | pbcopy

this->chunk_size()

Page 6: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Clean Commits

6

Page 7: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Conceptual Things

7

uint64_t Table::row_count() const {return (_table_chunks.size() - 1) * this->chunk_size() +

_table_chunks.back().size();}

Fornow,thisisnotincorrect,butinthefuture,itwillnotworkanymore.

Page 8: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Conceptual Things

8

std::vector<std::string> StorageManager::table_names() const {std::vector<std::string> names;auto get_name = [](const auto& entry) { return entry.first; };std::transform(m_tables.begin(), m_tables.end(), std::back_inserter(names), get_name); return names;

}

269characters,lambdas,std::transform,std::back_inserter

std::vector<std::string> StorageManager::table_names() const {std::vector<std::string> names;for (const auto& table_item : _tables) {

table_names.emplace_back(table_item.first);}return names;

}

209characters

Page 9: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

C++ things

9

Let’splayadifferentgame– whatdidwelike aboutthis?

std::vector<std::string> StorageManager::table_names() const {std::vector<std::string> names;names.reserve(m_tables.size());// […]

for (const auto& chunk : m_chunks) {count += chunk.size();

}

Page 10: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Const

10

Page 11: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks

11

Main

Delta

Chunk

Chunk

Chunk

Chunk

Chunk

Page 12: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks

• Chunks are stable once they are compressed

• Simplified data placement in general and especially on NUMA systems

• Enhanced query execution

• Potentially increased memory consumption by duplicated meta data structures

12

Benefits

Drawbacks

Page 13: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Chunks – Non-Uniform Memory Access

13

CPU 1

CPU 2

CPU 3

CPU 4

Mem

Mem

Mem

Mem

Interconnect (e.g., QPI) Blade 2

Blade 3 Blade 4

Page 14: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Chunks – Non-Uniform Memory Access

14

CPU1 CPU2

CPU3 CPU4

Interconnect(e.g.,QPI) Blade2

Blade3 Blade4

Page 15: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks

• Chunks are stable once they are compressed

• Simplified data placement in general and especially on NUMA systems

• Enhanced query execution

• Potentially increased memory consumption by duplicated meta data structures

15

Benefits

Drawbacks

Page 16: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Chunks – Enhanced Query Execution

16

Chunk #1

John

Mary

Frank

Peter

Chunk #2

Peter

Hasso

Ann

Lisa

Chunk #3

Theresa

Donald

Angela

Peter

MetaData

MetaData

MetaData

Page 17: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Chunks – Enhanced Query Execution

17

Chunk #1

John

Mary

Frank

Peter

Chunk #2

Peter

Hasso

Ann

Lisa

Chunk #3

Theresa

Donald

Angela

Peter

MetaData

MetaData

MetaData

SELECT*FROMcustomersWHEREfirstname =‘Hasso’

Page 18: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Chunks – Enhanced Query Execution

18

Chunk #1

John

Mary

Frank

Peter

Chunk #2

Peter

Hasso

Ann

Lisa

Chunk #3

Theresa

Donald

Angela

Peter

SELECT*FROMcustomersWHEREfirstname =‘Hasso’

MetaData

MetaData

MetaData

Page 19: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Chunks – Enhanced Query Execution

19

Chunk #1

John

Mary

Frank

Peter

Chunk #3

Theresa

Donald

Angela

Peter

MetaData

MetaData

MetaData

SELECT*FROMcustomersWHEREfirstname =‘Hasso’

Chunk #2

Peter

Hasso

Ann

Lisa

Page 20: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Chunks – Enhanced Query Execution

20

Chunk #1

John

Mary

Frank

Peter

Chunk #2

Peter

Hasso

Ann

Lisa

Chunk #3

Theresa

Donald

Angela

Peter

SELECT*FROMcustomersWHEREfirstname =‘Peter’

MetaData

MetaData

MetaData

Page 21: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Chunks – Enhanced Query Execution

21

MetaData

MetaData

MetaData

SELECT*FROMcustomersWHEREfirstname =‘Peter’

Chunk #2

Peter

Hasso

Ann

Lisa

Chunk #1

John

Mary

Frank

Peter

Chunk #3

Theresa

Donald

Angela

Peter

Page 22: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks

• Chunks are stable once they are compressed

• Simplified data placement in general and especially on NUMA systems

• Enhanced query execution

• Potentially increased memory consumption by duplicated meta data structures

22

Benefits

Drawbacks

Page 23: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks1. How does chunking affect the memory footprint?

2. If the last chunk is uncompressed, what is its effect on the

total memory consumption?

3. What are performance implications of chunks?

1. Considering single-threaded and multi-threaded (NUMA)

execution

23

Page 24: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks• Methodology:

– Utilize real-world data from a productive SAP system

– Extract actual queries from system‘s plan cache

– Load 100M rows of data into Opossum/Hyrise

– Measure memory footprint/query performance

– Repeat experiments for different chunk sizes

24

Page 25: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks:Memory Footprint

25

Page 26: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks:Memory Footprint

26

Page 27: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks:Memory Footprint

27

Page 28: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks:Memory Footprint

28

Page 29: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks:Memory Footprint

29

Page 30: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks:Memory Footprint

30

Page 31: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks:Performance (single-threaded)

31

Page 32: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks:Performance (single-threaded)

32

Page 33: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Experiments with Chunks:Performance (NUMA)

33

Page 34: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Optionals• „Manages an optional contained value, i.e. a value that may

or may not be present.“

• Example use case: A table scan that supports between and, therefore, needs two search value parameters

• Syntax:

34

#include <optional>// Templated object of type std::optional<T>std::optional<AllTypeVariant> opt;std::optional<AllTypeVariant> opt2 = std::nullopt;std::optional<AllTypeVariant> opt3 = 17;if (opt) {

do_something(*opt);}

Page 35: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Optionals

What is the result of sizeof(std::optional<uint32_t>)?

35

template <typename T>class optional {bool _initialized;T _storage;

};

std::pair<T,bool>

Any ideas how to implement that?

Page 36: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Lambda ExpressionsA simplified table scan…

36

for (auto i = 0; i < value_column.size(); ++i) {switch (_scan_type) {case ScanType::OpEquals: {return value_column.get(i) == search_value;break;

}case ScanType::OpNotEquals: {return value_column.get(i) != search_value;break;

}case ScanType::OpLessThan: {return value_column.get(i) < search_value;break;

// [...]

Page 37: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Lambda ExpressionsWith lambda expressions

37

auto comparator = get_comparator(_scan_type);for (auto i = 0; i < value_column.size(); ++i) {return comparator(value_column.get(i), search_value);

}

auto get_comparator(ScanType type) {switch (type) {case ScanType::OpEquals: {_return = [](auto left, auto right) { return left == right; };break;

}case ScanType::OpNotEquals: {_return = [](auto left, auto right) { return left != right; };break;

}// [...]

}}

+separationofconcerns+checksonlyonce+reuse

Page 38: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Lambda ExpressionsSyntax:

38

auto f = [ captures ] ( params ) -> ret { body };

Codegoeshere

Returnvalueofthelambda(ifyouleaveitout,thecompilerdoesitforyou)

Parametersthatarepassedwhenthelambdaiscalled

Youmustuseautohere

Canstorelambdasinvariables(andevenmembers)

Variablesthatyoutakefromthecurrentscope

Page 39: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Lambda Expressions

39

auto f = [ captures ] ( params ) -> ret { body };

int main() {auto f = []() {

std::cout << "Hallo Welt" << std::endl;};

f();}

Page 40: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Lambda Expressions

40

auto f = [ captures ] ( params ) -> ret { body };

int main() {auto f = [](const std::string& name) {

std::cout << "Hallo " << name << std::endl;};

f("Alexander");}

Page 41: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Lambda Expressions

41

auto f = [ captures ] ( params ) -> ret { body };

int main() {std::string my_name{"Larry"};

auto f = [my_name](const std::string& name) {std::cout << "Hallo " << name << ", ich bin "

<< my_name << std::endl;};

f("Alexander");}

Page 42: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Lambda Expressions

42

auto f = [ captures ] ( params ) -> ret { body };

int main() {std::string my_name{"Larry"};

auto f = [&my_name](const std::string& name) {std::cout << "Hallo " << name << ", ich bin "

<< my_name << std::endl;};

f("Alexander");}

Page 43: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Lambda Expressions

43

auto get_lambda() {std::string my_name{"Larry"};return [my_name]() {

std::cout << "Ich bin " << my_name << std::endl;};

}

int main() {f = get_lambda();

// my_name is undefined here

f();}

Page 44: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Lambda Expressions

44

auto get_lambda() {std::string my_name{"Larry"};

return [&my_name]() {std::cout << "Ich bin " << my_name << std::endl;

};}

int main() {f = get_lambda();

// my_name is undefined here

f();}

Page 45: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Lambda Expressions

45

Fromhttps://blog.feabhas.com/2014/03/demystifying-c-lambdas/Agreatresourceifyouwanttolearnmoreaboutlambdas

Page 46: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Lambda Expressions

46

Page 47: BuildyourownDatabase - Hasso Plattner Institute...Lambda Expressions Syntax: 38 auto f = [ captures] ( params) -> ret{ body}; Code goes here Return value of the lambda (if you leave

Next Week

• Relational Algebra

• Operators

• Presentation of Sprint 3

47