design pattern in presto source code

51
Copyright © 2017 Yahoo Japan Corporation. All Rights Reserved. 2017年11月20日 ヤフー株式会社 クエリエンジンチーム 曾臻 [email protected] Design patterns in presto

Upload: yahoo

Post on 21-Jan-2018

259 views

Category:

Technology


0 download

TRANSCRIPT

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

2017年11月20日

ヤフー株式会社 クエリエンジンチーム 曾臻

[email protected]

Design patterns

in presto

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Agenda

2

• 自己紹介

• Presto紹介

• Design Patterns in presto

• Template Method• Visitor

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

自己紹介

名前 : 曾 臻

所属 :ヤフー株式会社 D&Sソリューション統括本部 クエリエンジン

経歴 : Presto開発・運用広告システム開発SIer

3

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Agenda

4

• 自己紹介

• Presto紹介

• Design Patterns in presto

• Template Method• Visitor

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Presto紹介

• 100% オープンソースの分散式ANSI SQL クエリエンジン

• Facebookが公開した

• パフォーマンス&スケーラブル

• Hadoopだけではなく、違うプラットフォームのデータもアクセス可能

• Apache licensed, hosted on GitHub

5

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Agenda

6

• 自己紹介

• Presto紹介

• Design Patterns in presto

• Template Method• Visitor

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Design Patterns in presto

PrestoはBuilder、Factory、Iterator、Singletonなどのパターンを活用している。

今回は主にTemplate MethodとVisitorパターンを紹介したい。

7

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Design Patterns in presto

8Source:[Head First Design Patterns] - O'Reilly Media

オブジェクトの生成に関するパターン

プログラムの構造に関するパターン

オブジェクトの振る舞いに関するパターン

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Agenda

9

• 自己紹介

• Presto紹介

• Design Patterns in presto

• Template Method• Visitor

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Methodとは何?

定義:

ある処理のおおまかなアルゴリズムをあらかじめ決めておいて、そのアルゴリズムの具体的な設計をサブクラスに任せることである。

10 Source : https://ja.wikipedia.org/wiki/Template_Method_%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method例1

Template Method パターンの目的は、ある処理のおおまかなアルゴリズムをあらかじめ決めておいて、そのアルゴリズムの具体的な設計をサブクラスに任せることである。

11

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method例1

12https://gist.github.com/zz22394/1e5a097253db10f606dbbed8075c00f5

具象クラスは複数個作って良い

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Presto

• Prestoソースコードの中のTemplate Methodパターンを分析してみる。• AbstractOperatorBenchmark• AbstractArrayBlock

13

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Presto

14

AbstractOperatorBenchmark

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Presto

15

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Presto

16

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Presto

17

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Presto

18

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Presto

19

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Java

• JavaのTemplate Methodの実例を話す

• HttpServlet• JUnit

20

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Java

21http://www.codejava.net/java-ee/servlet/uml-class-diagram-of-httpservlet-api

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Java

22

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Java

23

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Java

24

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Java

25

• Junit

http://junit.sourceforge.net/doc/cookstour/cookstour.htm

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method in Java

26

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Template Method のまとめ

27

• 抽象クラスのテンプレートでアルゴリズムの不変な部分を実装し、具象クラス内で可変な部分を実装。

• 共通処理は抽象クラスに集約されるので、ソースコードが再利用される

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Agenda

28

• 自己紹介

• Presto紹介

• Design Patterns in presto

• Template Method• Visitor

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor パターンとは?

29

Visitor パターンは、オブジェクト指向プログラミング およびソフトウェア工学 において、 アルゴリズムをオブジェクトの構造から分離するためのデザインパターンである。分離による実用的な結果として、既存のオブジェクトに対する新たな操作を構造を変更せずに追加することができる。

基本的には Visitor パターンは一群のクラスに対して新たな仮想関数をクラス自体を変更せずに追加できるようにする。そのために、全ての仮想関数を適切に特化させた Visitor クラスを作成する。Visitor はインスタンスへの参照を入力として受け取り、ダブルディスパッチを用いて目的を達する。

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor パターンクラス図

30

https://ja.wikipedia.org/wiki/Visitor_%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor パターンの例1

31

Visitor:アルゴリズムをオブジェクトの構造から分離する

練習問題:

車Objectがある。車の運転と検査仕組みをそれぞれ書く。

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor パターンの例1

32

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor パターンの例1

33

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor パターンの例1

34

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor パターンの例1

35

InspectVisitor/DriveVisitorがそれぞれの形でvisitXXXメソッドを実装した

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor パターンの例1

36

InspectVisitor/DriveVisitorがそれぞれの形でvisitXXXメソッドを実装した

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor パターンの例1

37

メリット:データ構造と処理が分離された。ある日「洗車機能を追加したい」と言われたら、新しいVisitorで簡単に対応できる。

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor in presto

38

• Visitor パターン例

• Visitor in presto

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor in presto

39

• Prestoの実行計画生成の流れ

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor in presto

40

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor in presto

41

背景:NodeのAST(抽象文法木)はどうやって作られた?

例:あるSQL文

SELECT ROW_NO,START_TIME FROM TBL_01

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor in presto

42

ANTLR4

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor in presto

43

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor in presto

44

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor in presto

45

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor in presto

46

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor in presto

47

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor in presto

48

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor in presto

49

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

Visitor パターンのまとめ

50

• 複雑なパターン

• 利用のハードルは比較的に高い

• 向いているユースケース:データオブジェクト構成が大きく変わらない、且つデータオブジェクトに対しての操作がよく変わる(あるいは複数パターンの操作がある)

Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .Copyrig ht © 2017 Yahoo Japan Corporation. All Rig hts Reserved .

EOP