74
Large-Scale Statistics with MonetDB and R Hannes Mühleisen DAMDID 2015, 2015-10-13

Large-Scale Statistics with MonetDB and R

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Large-Scale Statistics with MonetDB and R

Large-Scale Statistics with

MonetDB and R

Hannes Mühleisen

DAMDID 2015, 2015-10-13

Page 2: Large-Scale Statistics with MonetDB and R

About Me

2

• Postdoc at CWI Database architectures since 2012

• Amsterdam is nice. We have open positions.

• Special interest in data management for statistical analysis

• Various research & software projects in this space

Page 3: Large-Scale Statistics with MonetDB and R

Outline• Column Store / MonetDB Introduction

• Connecting R and MonetDB

• Advanced Topics

• “R as a Query Language”

• “Capturing The Laws of Data Nature”

3

Page 4: Large-Scale Statistics with MonetDB and R

Column Stores / MonetDB Introduction

4

Page 5: Large-Scale Statistics with MonetDB and R

Postgres, Oracle, DB2, etc.:

NX

Constitution

Galaxy

Defiant

Intrepid

1

1

1

1

1

3

8

3

6

1

class speed flux

NX Constitution GalaxyDefiant Intrepid

1 11 1 1

3 83 6 1

Conceptional

Physical (on Disk)

5

Page 6: Large-Scale Statistics with MonetDB and R

Column Store:

NXConstitutionGalaxyDefiantIntrepid

11111

38361

class speed flux

NX Constitution Galaxy Defiant Intrepid

1 1 1 1 1

3 8 3 6 1

Compression!

6

Page 7: Large-Scale Statistics with MonetDB and R

What is MonetDB?

• Strict columnar architecture OLAP RDBMS (SQL)

• Started by Martin Kersten and Peter Boncz ~1994

• Free & Open Open source, active development ongoing

• www.monetdb.org

7

Peter A. Boncz, Martin L. Kersten, and Stefan Manegold. 2008. Breaking the memory wall in MonetDB. Communications of the ACM 51, 12 (December 2008), 77-85. DOI=10.1145/1409360.1409380

Page 8: Large-Scale Statistics with MonetDB and R

MonetDB today• Expanded C code

• MAL “DB assembly” & optimisers

• SQL to MAL compiler

• Memory-Mapped files

• Automatic indexing

8

Page 9: Large-Scale Statistics with MonetDB and R

9

EXPLAIN SELECT * FROM mtcars;

| X_2 := sql.mvc(); | | X_3:bat[:oid,:oid] := sql.tid(X_2,"sys","mtcars"); | | X_6:bat[:oid,:dbl] := sql.bind(X_2,"sys","mtcars","mpg",0); | | (X_9,r1_9) := sql.bind(X_2,"sys","mtcars","mpg",2); | | X_12:bat[:oid,:dbl] := sql.bind(X_2,"sys","mtcars","mpg",1); | | X_14 := sql.delta(X_6,X_9,r1_9,X_12); | | X_15 := algebra.leftfetchjoin(X_3,X_14); | | X_16:bat[:oid,:dbl] := sql.bind(X_2,"sys","mtcars","cyl",0); | | (X_18,r1_18) := sql.bind(X_2,"sys","mtcars","cyl",2); | | X_20:bat[:oid,:dbl] := sql.bind(X_2,"sys","mtcars","cyl",1); | | X_21 := sql.delta(X_16,X_18,r1_18,X_20); | | X_22 := algebra.leftfetchjoin(X_3,X_21); |

Some MAL

“Invisible JOIN”

• Optimisers run on MAL code

• Efficient Column-at-a-time implementations

Page 10: Large-Scale Statistics with MonetDB and R

●●

●●

● ●

● ●

●●

●●

●●

0.01

1.00

100.00

Query

Aver

age

time

(s)

monetdbpostgres

TPC−H SF−100 Hot runs

Performance...

log!10

Page 11: Large-Scale Statistics with MonetDB and R

But statistics with SQL?

11

Page 12: Large-Scale Statistics with MonetDB and R

12

Efficiency

Flex

ibilit

y

Statistical Toolkits

Data Management

Systems2

Integrate not Reinvent

?

Page 13: Large-Scale Statistics with MonetDB and R

Collect data

Load dataFilter,

transform & aggregate data

Analyze & Plot

Publish paper

13

Page 14: Large-Scale Statistics with MonetDB and R

Collect data

Load dataFilter,

transform & aggregate data

Analyze & Plot

Publish paper

Growing

Not really Analysis features

14

Page 15: Large-Scale Statistics with MonetDB and R

Collect data

Load dataFilter,

transform & aggregate data

Analyze & Plot

Publish paper

Statistical Toolkit

Data Management

System2

15

Page 16: Large-Scale Statistics with MonetDB and R

Filter, transform &

aggregate dataAnalyze & Plot

Statistical Toolkit

Data Management

System2

16

Page 17: Large-Scale Statistics with MonetDB and R

17

• JDB

+ Native operators, lazy evaluation

• JDB

+ Cheap data transfer

Bridge the Gap

Page 18: Large-Scale Statistics with MonetDB and R

18

Page 19: Large-Scale Statistics with MonetDB and R

19

MonetDB.R connector on CRAN since 2013

Embedded R in MonetDB Part of MonetDB since 2014

Previous Work

MonetDBLite for R Preview release available

Page 20: Large-Scale Statistics with MonetDB and R

20

Embedded Python/NumPy Next MonetDB release

Also…

Page 21: Large-Scale Statistics with MonetDB and R

MonetDB.R connector

21

Hannes Mühleisen and Thomas Lumley: Best of Both Worlds – Relational Databases and Statistics 25th International Conference on Scientific and Statistical Database Management (SSDBM2013)

Page 22: Large-Scale Statistics with MonetDB and R

DBI

• DBI is for R what JDBC is for Java

• Low-level interface to talk to SQL databases

• Drivers available for most relational databases

• Typically socket connection between R and DB

22

df <- dbGetQuery(con, "SELECT * FROM table")

Page 23: Large-Scale Statistics with MonetDB and R

DBI

• Works, but (generally)

• Serialising/Unserialising large datasets is slow

• Data ingest is slow

• SQL knowledge required

23

Page 24: Large-Scale Statistics with MonetDB and R

dplyr• Data reorganisation package in “Hadleyverse”

• Works with data.frame, data.table, SQL DBs

• Maps relational operations (selection, projection, join, grouping etc.) to native R operators

• Lazy evaluation, call chaining

• MonetDB.R includes a dplyr compatibility layer

24

Page 25: Large-Scale Statistics with MonetDB and R

dplyr

25

ni <- select(n, first_name, last_name, race_desc, sex, birth_age)

ow <- filter(ni, as.integer(birth_age) > 66, sex=="MALE", race_desc == “WHITE")

print(ow)

SELECT "first_name" AS "first_name", "last_name" AS "last_name", "race_desc" AS "race_desc", "sex" AS "sex", "birth_age" AS "birth_age" FROM "ncvoter" WHERE CAST("birth_age" AS INTEGER) > 66.0 AND "sex" = 'MALE' AND "race_desc" = 'WHITE' LIMIT 10

Generated:

In R:

Page 26: Large-Scale Statistics with MonetDB and R

• Better, but

• Most (All) R packages cannot work with dplyr tables, so at some point data needs to be transferred.

• What if this dataset is large?

26

dplyr

Page 27: Large-Scale Statistics with MonetDB and R

Embedded R in MonetDB

27

Page 28: Large-Scale Statistics with MonetDB and R

σ

π

σ

Statistical analysis as operators in relational queries

+

Relationally Integrated

28

Page 29: Large-Scale Statistics with MonetDB and R

CREATE FUNCTION rapi01(i INTEGER) RETURNS TABLE (i INTEGER, d DOUBLE) LANGUAGE R { data.frame(i=seq(1,i),d=42.0) };

SELECT i,d FROM rapi01(42) AS r WHERE i>40;

Table-producing

29

Page 30: Large-Scale Statistics with MonetDB and R

CREATE FUNCTION rapi02 (i INTEGER, j INTEGER, z INTEGER) RETURNS INTEGER LANGUAGE R { i*sum(j)*z };

SELECT rapi02(i,j,2) AS r02 FROM rval;

Transformationsπ

30

Page 31: Large-Scale Statistics with MonetDB and R

CREATE FUNCTION rapi03(i INTEGER, z INTEGER) RETURNS BOOLEAN LANGUAGE R { i>z };

SELECT * FROM rval WHERE rapi03(i,2);

Filteringσ

31

Page 32: Large-Scale Statistics with MonetDB and R

CREATE AGGREGATE kmeans(data FLOAT, ncluster INTEGER) RETURNS INTEGER LANGUAGE R { kmeans(data,ncluster)$cluster };

SELECT cluster FROM (SELECT MIN(x) AS minx, MAX(x) AS maxx, kmeans(x,5) AS cluster FROM xdata GROUP BY cluster) as cdata ORDER BY cluster;

Aggregation

32

Page 33: Large-Scale Statistics with MonetDB and R

● ● ●

● ●

● ● ●●

● ● ● ●●

● ● ● ●

● ●●

PL/R−naive

PL/R−tuned

MonetDB

R−full

R−col

RInt

0

10

20

30

40

1 K 10 K 100 K 1 M 10 M 100 M1 K 10 K 100 K 1 M1 K 10 K 100 K 1 M 10 M 100 M1 K 10 K 100 K 1 M 10 M 100 M1 K 10 K 100 K 1 M 10 M 100 M1 K 10 K 100 K 1 M 10 M 100 MRows (log)

Tim

e (s

)

Performance…

33

Page 34: Large-Scale Statistics with MonetDB and R

34

> rf.fit <- randomForest(income~., data=training, mtry=2, ntree=10)

Code Shipping

MonetDB.R 1.0.0, soon

> predictions <- mdbapply(con, “t1", function(d) { p <- predict(rf.fit, type=“prob", newdata=d)[,2] p[p > .9] })

Page 35: Large-Scale Statistics with MonetDB and R

MonetDBLite

35

Page 36: Large-Scale Statistics with MonetDB and R

36

MonetDBLite• Socket serialization/deserialization for client/server protocol is

slow for large result sets.

• Too slow for many machine learning problems!

• Running a database server is cumbersome and overkill for a single R client

• Solution: Run entire database inside the R process

• Only copy ingest data / query results around in memory, fast

• Same interface as MonetDB.R, DBI/dplyr

https://goo.gl/jelaOy

Page 37: Large-Scale Statistics with MonetDB and R

lineitem table with 10M rows, SELECT * FROM lineitem

37

0

5

9

14

18

Old (MAPI Socket) MonetDBLite0.4 s

17.2 s

Quick Benchmark

s

Page 38: Large-Scale Statistics with MonetDB and R

Zero-Copy

38

Jonathan Lajus and Hannes Mühleisen: Efficient Data Management and Statistics with Zero-Copy Integration 26th International Conference on Scientific and Statistical Database Management (SSDBM2014)

Page 39: Large-Scale Statistics with MonetDB and R

BAT

Descriptor

Column

Descriptor

0 1

2

...

42 43

44

...

Column

Descriptor

Arrays

head

tail

Reference

42 43 44 ...Reference

SEXP Header

Array

R SEXP

MonetDB BAT

39

Page 40: Large-Scale Statistics with MonetDB and R

BATDescriptor

ColumnDescriptortail

42 43 44 ...Reference

SEXP HeaderR

ReferenceMonetDB

Dress-up

+ Garbage Collection Fun40

Page 41: Large-Scale Statistics with MonetDB and R

Advanced Topics

41

Page 42: Large-Scale Statistics with MonetDB and R

R as a Query Language

42

Hannes Mühleisen, Alex Bertram and Maarten-Jan Kallen: Relational Optimizations for Statistical Analysis, Journal of Statistical Software (under review)

Page 43: Large-Scale Statistics with MonetDB and R

What is Renjin?

• R on the JVM

• Compatibility is paramount, not just academic exercise (e.g. automatic Fortran/C translations)

• R anywhere on any data format (e.g. Cloud environments)

• Increased performance through lazy evaluation, parallel execution, …

• Easy to plug any Java code into R analysis, easy to plug Renjin into java projects

43

Page 44: Large-Scale Statistics with MonetDB and R

Abstraction in Renjin> a <- 1:10^9 > a[1000000] <- NA #harr harr

> system.time(print(anyNA(a)))[[3]] [1] TRUE [1] 0.001 > system.time(print(any(is.na(a))))[[3]] [1] TRUE [1] 2.23

> system.time(print(any(is.na(a))))[[3]] [1] TRUE [1] 0.05

GNU R

Renjin44

Page 45: Large-Scale Statistics with MonetDB and R

“R as a query language”

• Observation 1: Lots of data wrangling happening in R scripts

45

subset()

merge()

aggregate()

dplyr::, data.table::

[

$

Page 46: Large-Scale Statistics with MonetDB and R

“R as a query language”

• Observation 2: Things get slow quickly as vectors get longer

• Lots of optimisation opportunities, but how?

• State of the art: Tactical optimisation/Band aids

46

Page 47: Large-Scale Statistics with MonetDB and R

“R as a query language”

• Proposal: Treat R scripts as declaration of intent (not as a procedural contract written in blood)

• Then we can optimise strategically!

47

Page 48: Large-Scale Statistics with MonetDB and R

Rule-based query optimisation

48

Page 49: Large-Scale Statistics with MonetDB and R

• Selection Pushdown

• Data-parallel scheduling

• Function specialisation/vectorisation

• Common expression elimination/caching

• Redundant computation elimination

49

Optimisations

Page 50: Large-Scale Statistics with MonetDB and R

50

Static analysis?

Page 51: Large-Scale Statistics with MonetDB and R

51

+

a 42

[

min max

/

a <- 1:1000b <- a + 42c <- b[1:10]d <- min(c) / max(c) print(d)

Deferred Evaluation

Page 52: Large-Scale Statistics with MonetDB and R

52

[ (subset)

n=10

factorial

n=1000

a

n=1000

[ (subset)

n=10

a

n=1000

factorial

n=10

Pushdownb <- factorial(a) c <- b[1:10] print(c)

Page 53: Large-Scale Statistics with MonetDB and R

53

● ● ●●

● ● ●

GNU R

Renjin 0

2

4

6

106 107 108

Dataset Size (elements, log scale)

Exec

utio

n Ti

me

(s)

Pushdown

Page 54: Large-Scale Statistics with MonetDB and R

54

/

- (cached)

a[i] (cached)

Recycling

/

- -

mina[i] max

a

for (i in 1:100) print((a[i] - min(a))/(max(a)-min(a)))

Page 55: Large-Scale Statistics with MonetDB and R

55

●●

●●

Renjin

Renjin + R.

GNU R

0

20

40

60

106 107 108

Dataset Size (elements, log scale)

Exec

utio

n Ti

me

(s)

Recycling

Page 56: Large-Scale Statistics with MonetDB and R

56

svymeanagep <- svymean(~agep, svydsgn, se=TRUE)

for(i in 1:ncol(wts)) { repmeans[i,]<-t(colSums(wts[,i]*x*pw)/ sum(pw*wts[,i])) } […] v<-crossprod(sweep(thetas,2, meantheta,"-")*sqrt(rscales))*scale

Page 57: Large-Scale Statistics with MonetDB and R

*

crossprod 0.2

*

*

wts[,1]x

*

p

colSums

*

sum

/

*

wts[,2]

*

colSums

*

sum

/

*

wts[,3]

*

colSums

*

sum

/

*

wts[,4]

*

colSums

*

sum

/

*

wts[,5]

*

colSums

*

sum

/

repmeans

rep

47512

*

colSums sum

/

rep

5

t

- [5]

svymean

Page 58: Large-Scale Statistics with MonetDB and R

58

*

crossprod 0.2

*

*

wts[,1]x

colSums

/

(cached)

*

wts[,2]

colSums

/

(cached)

*

wts[,3]

colSums

/

(cached)

*

wts[,4]

colSums

/

(cached)

*

wts[,5]

colSums

/

(cached)

repmeans

*

(cached)

colSums

/

(cached)

rep

5

t

- [5]

svymean

Page 59: Large-Scale Statistics with MonetDB and R

59

●●

●●● ●●

●●

● ●

GNU R

Renjin −opt

Renjin

Renjin 1t

0

25

50

75

100

47512 1060060 9093077Dataset Size (elements, log scale)

Exec

utio

n Ti

me

(s)

svymean

Page 60: Large-Scale Statistics with MonetDB and R

Capturing the Laws of Data Nature

60

Hannes Mühleisen, Martin Kersten and Stefan Manegold: Capturing the Laws of (Data) Nature, 7th Biennial Conference on Innovative Data Systems Research (CIDR), Jan. 2015

Page 61: Large-Scale Statistics with MonetDB and R

Statistical Models?• Everyone has models, they encode our

understanding of the world

• Everyone has data to train/fit and validate a model

• So far, data management community has ignored these models

• But they hold precious domain knowledge!

61

Page 62: Large-Scale Statistics with MonetDB and R

Configuration Measurement

62

Page 63: Large-Scale Statistics with MonetDB and R

Model!

Grouped by-source operation

Convergence Hints

63

Page 64: Large-Scale Statistics with MonetDB and R

Measurement Configuration

Fitted parameters

64

Page 65: Large-Scale Statistics with MonetDB and R

65

Page 66: Large-Scale Statistics with MonetDB and R

●●

●●

● ●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

● ●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●●

● ●●

●●

●●●

●●

●●

●●

●●

0.10 0.12 0.14 0.16 0.18 0.20

2.0

2.5

3.0

3.5

Frequency (GHz)

Inte

nsity

(Jy)

source=17562, alpha=-0.692, p=0.81266

Page 67: Large-Scale Statistics with MonetDB and R

Model to function conversion (automatic)

Move to DB (automatic)

67

Page 68: Large-Scale Statistics with MonetDB and R

Approximate Answer with zero IO*68

Page 69: Large-Scale Statistics with MonetDB and R

Integrate & Intercept• Integrate model fitting infrastructure into data

management system.

• Also: Huge performance benefits for analysts!

• Intercept model fitting and validation operations by the user and store the model for later use.

• Storage format: Model code + Parameters

69

Page 70: Large-Scale Statistics with MonetDB and R

I ⇡ p · ⌫↵ ? S ⌫ I S ⌫ I

R2 = 0.92 !

I ⇡ p · ⌫↵ ?

R2 = 0.92 !

S p ↵

I ⇡ p · ⌫↵

S = 42, ⌫ = 0.14, I =?

I = 3.0± 0.05 !

(1) (2)

(3)

(4)

(5)

70

Page 71: Large-Scale Statistics with MonetDB and R

But…• What do we do if model parameters are not

specified in the query?

• Sample data?

• Given multiple parameters, it is far from certain that all combinations of values are allowed in the model.

• Construct filter?

71

Page 72: Large-Scale Statistics with MonetDB and R

Data & Model Changes• What should we do if the user gives us a better

model?

• Recompressing could be very expensive

• Threshold for improvement?

• Changes in the data affect the model quality, too

• Switch models?

• Constant Monitoring?

72

Page 73: Large-Scale Statistics with MonetDB and R

Multiple, partial or grouped• There could be many models for a table with

overlapping parameters

• Which one to pick?

• Models do not have to cover the entire table/column

• “Patching”?

• Models could be fitted on aggregation results

• Keep group counts?

73

Page 74: Large-Scale Statistics with MonetDB and R

Thank You Questions?

http://hannes.muehleisen.org

@hfmuehleisen