Repository navigation
マージソートで並べ替える SortPlan を入れる - #34
Merged
Merged
Conversation
…elds ソートの run の分割とマージ、マージ結合で、2 つのスキャンの現在の行の前後を決めるのに使う。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…, merging them as it goes ソートの最後のマージを読み出しながら行い、結果を一時テーブルに書き直さずに済ませる。 入力が 0 行でも run は 1 本あるとし、run の本数が 1〜2 本でなければエラーにする。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s by merge sort グループ化とマージ結合が、入力を並べ替えてから読めるようにする。 コストはソート済みの行を一度読む分だけとし、run の数は行の並びで決まり見積もれないため前処理は含めない。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
プランの行をフィールドの並びの順に読む
SortPlanを、マージソートで実装した。SimpleDB のRecordComparator/SortScan/SortPlanに相当する。#33 の一時テーブルの上に作っている。この PR ではまだ planner からは使わない(
order byもない)。次以降の PR で、グループ化(GroupByPlan)とマージ結合(MergeJoinPlan)がこれを使う。materializeRecordComparator:フィールドの並びで 2 つのスキャンの現在の行を比べる(最初のフィールドで決まらなければ次のフィールド、の順)SortScan:ソート済みの run を 1〜2 本開き、読みながらマージして順に返すplanSortPlan:Openで、入力を run に分け、run が 2 本以下になるまで 2 本ずつマージして一時テーブルに書き、最後の 2 本をSortScanで返すcopyRecord:MaterializePlanのcopyRecordsから 1 行を写す部分を切り出し、run の書き出しとマージでも使う設計上の判断
SortPlan.BlocksAccessedはソート済みの行を 1 回読むコスト(MaterializePlanと同じ値)だけで、前処理(run への分割とマージ)を含めない。 SimpleDB と同じ。前処理はOpenの 1 回だけで、BlocksAccessedは上の演算子が読み直すたびのコストを表すため(一時テーブルと MaterializePlan を入れる #33 のMaterializePlanと同じ考え方)。加えて、マージの回数は run の数で決まり、run の数は行の並び方で 1 本から行数まで変わるので、統計からは見積もれない。SortScanが読みながら行う。 最後のマージの結果を一時テーブルに書いて読み直す分(2B)が省ける。SortScanがruns.get(0)で落ちる。SortScanは run が 1〜2 本でなければエラーを返す。SortScan.HasFieldは 1 本目の run に聞く。 SimpleDB は現在の run に聞くので、行を読む前に聞くと nil を参照する。どの run も入力と同じスキーマなので、1 本目で答えられる。SortScanと、2 本の run のマージの両方で同じ規則にした。SortScanの位置の保存と復元(savePosition/restorePosition)はこの PR に入れない。 マージ結合でしか使わないので、マージ結合の PR で入れる。SimpleDB の実装は record id しか保存しないため、復元した後に「現在の run」と「各 run に行が残っているか」が古いまま残る。そこで、マージ結合の PR ではこれらも一緒に保存する。既知の制限
SortPlanはまだどの planner からも使われない。order byも解析しない。NewSortPlanでは確かめない。ないフィールドを指定すると、Openで行を比べるときにエラーになる。テスト
RecordComparator:最初のフィールドで決まる場合、同じなら次のフィールドで決まる場合、フィールドの並び順が効くこと、すべて同じなら 0、指定していないフィールドは見ないこと、int と varchar を比べるとErrConstantTypeMismatchSortScan:run 1 本、2 本のマージ、片方が先に尽きる場合、2 本に同じ id がある場合、先頭の run が空の場合、空の run 1 本、読み直し、name も一緒に読めること、run が 0 本・3 本ならエラー、HasFieldSortPlan:すでに順に並んだ表(run 1 本)、逆順の 20 行(run 20 本から 4 回マージ)、順番がばらばらで重複のある表、空の表、name でのソート(name10がname2より前になる、文字列の順)、BlocksAccessed/RecordsOutput/DistinctValues/SchemaRecordComparator:全フィールドが同じときに -1 を返す、指定していないフィールドも比べるSortScan:比べる向きを逆にする、2 本目の run を見ない、読み直しで run を巻き戻さない、HasFieldが常に false / true、GetStringを常に 1 本目から読む、同じ値の 2 本目の行を飛ばすSortPlan:順序が崩れても run を切らない、マージを 1 回でやめる、空の入力で run を作らない🤖 Generated with Claude Code