Preserve hint block spacing in fmt-sql output#10
Merged
Conversation
memefish lexer splits `@{FORCE_INDEX=...}` into separate tokens, which
the token-based formatter rewrote as `@ { FORCE_INDEX = ... }`. Track a
hint depth and reassemble tokens inside `@{...}` without spaces, keeping
only `, ` between records to match memefish's Hint.SQL() output. Use the
raw token text so user-provided casing is preserved.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
testdata/test.ddl gained an OrderItem table in 72101cc but the TestParseDDL/tables_parsed expectations were not updated, leaving CI red on main. Update the expected table count and name list. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
fmt-sqlでFROM Table@{FORCE_INDEX=Idx}がFROM Table @ { FORCE_INDEX = Idx }のように展開されてしまう不具合を修正formatTokens()にヒントブロック専用の状態 (hintDepth) を追加し、@{...}内部のトークンをスペースなしで連結するupper(t)ではなくt.rawを使用し、ユーザが書いた大小文字を保持する設計の背景
memefish lexer はヒント構文
@{FORCE_INDEX=SingersByName}を@,{,FORCE_INDEX,=,SingersByName,}のように複数トークンに分割する。既存のneedsSpace()はこれらの記号を考慮していないため、各トークン間にスペースが挿入されていた。@<param>形式のクエリパラメータは memefish が単一トークンとして返すため、@単独トークンの直後に{が続くケースは常にヒント導入子と判断できる。この前提のもと、hintDepthカウンタで@{...}の開閉を追跡し、内部ではスペース抑制と,での record 区切りのみを行う方針とした (memefish のHint.SQL()出力と一致)。検討した代替案
noSpaceBefore/noSpaceAfterマップに@,{,},=を追加する案。却下理由: これらの記号はヒント外部 (比較演算子=, JSON 風リテラルなど将来の構文) では通常のスペーシングが必要で、単純なマップでは文脈依存の規則を表現できない。Hintノードを取り出してHint.SQL()で再構築する案。却下理由:formatter.go全体がトークンベース設計であり、AST ベース処理を混在させると整合性が崩れる。トークンレベルで解決可能なうちは現行設計を維持する方が望ましい。Test plan
go test ./... -run TestFormatSQLで既存ケースと追加5ケースが全て PASStable hint FORCE_INDEX,table hint with whitespace in source,table hint multiple records,statement hint preserves casing,join hintの各ケースで期待フォーマットを確認@paramクエリパラメータを含む既存テスト (simple select) が regression しないことを確認🤖 Generated with Claude Code