Skip to content

Commit 644f129

Browse files
committed
Merge branch 'main' of github:team-gonyo/gonyo-db into feature/chapter14
2 parents 11d34a0 + ee26d3f commit 644f129

13 files changed

Lines changed: 58 additions & 26 deletions

plan/group_by_plan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (g *GroupByPlan) Open() (scan.Scan, error) {
4747
return query.NewGroupByScan(s, g.groupFields, g.aggregationFunc), nil
4848
}
4949

50-
func (g *GroupByPlan) BlocksAccessed() int {
50+
func (g *GroupByPlan) BlocksAccessed() (int, error) {
5151
return g.plan.BlocksAccessed()
5252
}
5353

plan/index_join_plan.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ func (ijp *IndexJoinPlan) Open() (*query.IndexJoinScan, error) {
5151
return query.NewIndexJoinScan(scan1, idx, ijp.joinField, ts)
5252
}
5353

54-
func (ijp *IndexJoinPlan) BlocksAccessed() int {
55-
return ijp.plan1.BlocksAccessed() + (ijp.plan1.RecordsOutput() * ijp.ii.BlocksAccessed()) + ijp.RecordsOutput()
54+
func (ijp *IndexJoinPlan) BlocksAccessed() (int, error) {
55+
plan1BlockAccesses, err := ijp.plan1.BlocksAccessed()
56+
if err != nil {
57+
return 0, err
58+
}
59+
return plan1BlockAccesses + (ijp.plan1.RecordsOutput() * ijp.ii.BlocksAccessed()) + ijp.RecordsOutput(), nil
5660
}
5761

5862
func (ijp *IndexJoinPlan) RecordsOutput() int {

plan/materialize_plan.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,19 @@ func (m *MaterializePlan) Open() (scan.Scan, error) {
7575
return dest, nil
7676
}
7777

78-
func (m *MaterializePlan) BlocksAccessed() int {
78+
func (m *MaterializePlan) BlocksAccessed() (int, error) {
7979
// create a dummy layout object to calculate slot size
8080
// FIXME: エラー握りつぶし!!
8181
// Plan#BlocksAccessed()のシグネチャを変えないとエラーをエスカレーションできない
8282
// だが、12章のIndexPlanもPlanを実装していて、そっちがマージされてからじゃないとコンフリクトしそう
8383
// 溜まったPRが落ち着いて、直せるようになった時点で対処すること
8484
layout, err := record.NewLayoutFromSchema(m.Schema())
8585
if err != nil {
86-
// 特に理由のない1000000
87-
return 1000000
86+
return 0, err
8887
}
8988

9089
recordPerBlock := (float64(m.tx.BlockSize())) / float64(layout.SlotSize())
91-
return int(math.Ceil(float64(m.srcPlan.RecordsOutput()) / recordPerBlock))
90+
return int(math.Ceil(float64(m.srcPlan.RecordsOutput()) / recordPerBlock)), nil
9291
}
9392

9493
func (m *MaterializePlan) RecordsOutput() int {

plan/merge_join_plan.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func NewMergeJoinPlan(tx *tx.Transaction, p1, p2 Plan, fieldName1, fieldName2 st
3535
}
3636

3737
func (m *MergeJoinPlan) Open() (scan.Scan, error) {
38-
s1, err := m.p1.Open()
38+
rawS1, err := m.p1.Open()
3939
if err != nil {
4040
return nil, err
4141
}
@@ -44,6 +44,10 @@ func (m *MergeJoinPlan) Open() (scan.Scan, error) {
4444
if err != nil {
4545
return nil, err
4646
}
47+
s1, ok := rawS1.(*query.SortScan)
48+
if !ok {
49+
return nil, fmt.Errorf("failed to cast rawS1(%v) to *query.SortScan", rawS1)
50+
}
4751
s2, ok := rawS2.(*query.SortScan)
4852
if !ok {
4953
return nil, fmt.Errorf("failed to cast rawS2(%v) to *query.SortScan", rawS2)
@@ -52,8 +56,16 @@ func (m *MergeJoinPlan) Open() (scan.Scan, error) {
5256
return query.NewMergeJoinScan(s1, s2, m.fieldName1, m.fieldName2), nil
5357
}
5458

55-
func (m *MergeJoinPlan) BlocksAccessed() int {
56-
return m.p1.BlocksAccessed() + m.p2.BlocksAccessed()
59+
func (m *MergeJoinPlan) BlocksAccessed() (int, error) {
60+
p1BlockAccesses, err := m.p1.BlocksAccessed()
61+
if err != nil {
62+
return 0, err
63+
}
64+
p2BlockAccesses, err := m.p2.BlocksAccessed()
65+
if err != nil {
66+
return 0, err
67+
}
68+
return p1BlockAccesses + p2BlockAccesses, nil
5769
}
5870

5971
func (m *MergeJoinPlan) RecordsOutput() int {

plan/plan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Plan interface {
1010
Open() (scan.Scan, error)
1111

1212
// Plan実行によるブロックアクセス数を推定
13-
BlocksAccessed() int
13+
BlocksAccessed() (int, error)
1414

1515
// Plan実行による出力レコード数を推定
1616
RecordsOutput() int

plan/product_plan.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,16 @@ func (pp *ProductPlan) Open() (scan.Scan, error) {
7575
return query.NewProductScan(s1, s2)
7676
}
7777

78-
func (pp *ProductPlan) BlocksAccessed() int {
79-
return pp.p1.BlocksAccessed() + (pp.p1.RecordsOutput() * pp.p2.BlocksAccessed())
78+
func (pp *ProductPlan) BlocksAccessed() (int, error) {
79+
p1BlockAccesses, err := pp.p1.BlocksAccessed()
80+
if err != nil {
81+
return 0, err
82+
}
83+
p2BlockAccesses, err := pp.p2.BlocksAccessed()
84+
if err != nil {
85+
return 0, err
86+
}
87+
return p1BlockAccesses + (pp.p1.RecordsOutput() * p2BlockAccesses), nil
8088
}
8189

8290
func (pp *ProductPlan) RecordsOutput() int {

plan/project_plan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (pp *ProjectPlan) Open() (scan.Scan, error) {
4949
return query.NewProjectScan(s, pp.schema.Fields()), nil
5050
}
5151

52-
func (pp *ProjectPlan) BlocksAccessed() int {
52+
func (pp *ProjectPlan) BlocksAccessed() (int, error) {
5353
return pp.p.BlocksAccessed()
5454
}
5555

plan/select_plan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (sp *SelectPlan) Open() (scan.Scan, error) {
2828
return query.NewSelectScan(s, sp.pred), nil
2929
}
3030

31-
func (sp *SelectPlan) BlocksAccessed() int {
31+
func (sp *SelectPlan) BlocksAccessed() (int, error) {
3232
return sp.p.BlocksAccessed()
3333
}
3434

plan/sort_plan.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type SortPlan struct {
1414
comparator *RecordComparator
1515
}
1616

17+
var _ Plan = (*SortPlan)(nil)
18+
1719
func NewSortPlan(plan Plan, sortFields []string, tx *tx.Transaction) *SortPlan {
1820
return &SortPlan{
1921
plan: plan,
@@ -208,7 +210,7 @@ func (s *SortPlan) mergeTwoRuns(p1, p2 query.TempTable) (query.TempTable, error)
208210
return *result, nil
209211
}
210212

211-
func (s *SortPlan) BlocksAccessed() int {
213+
func (s *SortPlan) BlocksAccessed() (int, error) {
212214
// ソート時のコストは考慮しない
213215
mp := NewMaterializePlan(s.tx, s.plan)
214216
return mp.BlocksAccessed()
@@ -225,5 +227,3 @@ func (s *SortPlan) DistinctValues(fldName string) int {
225227
func (s *SortPlan) Schema() *record.Schema {
226228
return s.schema
227229
}
228-
229-
var _ Plan = (*SortPlan)(nil)

plan/table_plan.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func (tp *TablePlan) Open() (scan.Scan, error) {
3939
return scan.NewTableScan(tp.tx, tp.tblName, tp.layout)
4040
}
4141

42-
func (tp *TablePlan) BlocksAccessed() int {
43-
return tp.si.BlocksAccessed()
42+
func (tp *TablePlan) BlocksAccessed() (int, error) {
43+
return tp.si.BlocksAccessed(), nil
4444
}
4545

4646
func (tp *TablePlan) RecordsOutput() int {

0 commit comments

Comments
 (0)