@@ -30,6 +30,9 @@ func explainTablesInSelectQueryElement(sb *strings.Builder, n *ast.TablesInSelec
3030
3131func explainTableExpression (sb * strings.Builder , n * ast.TableExpression , indent string , depth int ) {
3232 children := 1 // table
33+ if n .Sample != nil {
34+ children ++
35+ }
3336 fmt .Fprintf (sb , "%sTableExpression (children %d)\n " , indent , children )
3437 // If there's a subquery with an alias, pass the alias to the subquery output
3538 if subq , ok := n .Table .(* ast.Subquery ); ok {
@@ -51,6 +54,46 @@ func explainTableExpression(sb *strings.Builder, n *ast.TableExpression, indent
5154 } else {
5255 Node (sb , n .Table , depth + 1 )
5356 }
57+ // Output SAMPLE clause if present
58+ if n .Sample != nil {
59+ explainSampleClause (sb , n .Sample , indent + " " , depth + 1 )
60+ }
61+ }
62+
63+ func explainSampleClause (sb * strings.Builder , n * ast.SampleClause , indent string , depth int ) {
64+ // Format the sample ratio as "SampleRatio num / den" or just the expression
65+ sb .WriteString (indent )
66+ sb .WriteString ("SampleRatio " )
67+ formatSampleRatio (sb , n .Ratio )
68+ sb .WriteString ("\n " )
69+ }
70+
71+ func formatSampleRatio (sb * strings.Builder , expr ast.Expression ) {
72+ // Handle binary expressions like 1 / 2
73+ if binExpr , ok := expr .(* ast.BinaryExpr ); ok && binExpr .Op == "/" {
74+ formatSampleRatioOperand (sb , binExpr .Left )
75+ sb .WriteString (" / " )
76+ formatSampleRatioOperand (sb , binExpr .Right )
77+ } else {
78+ formatSampleRatioOperand (sb , expr )
79+ }
80+ }
81+
82+ func formatSampleRatioOperand (sb * strings.Builder , expr ast.Expression ) {
83+ if lit , ok := expr .(* ast.Literal ); ok {
84+ switch v := lit .Value .(type ) {
85+ case int64 :
86+ fmt .Fprintf (sb , "%d" , v )
87+ case uint64 :
88+ fmt .Fprintf (sb , "%d" , v )
89+ case float64 :
90+ fmt .Fprintf (sb , "%g" , v )
91+ default :
92+ fmt .Fprintf (sb , "%v" , v )
93+ }
94+ } else {
95+ fmt .Fprintf (sb , "%v" , expr )
96+ }
5497}
5598
5699// explainViewExplain handles EXPLAIN queries used as table sources, converting to viewExplain function
0 commit comments