|
| 1 | +// Copyright 2026 GoSQLX Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package naming |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/ajitpratap0/GoSQLX/pkg/linter" |
| 21 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/ast" |
| 22 | +) |
| 23 | + |
| 24 | +const distinctColumnThreshold = 5 |
| 25 | + |
| 26 | +// DistinctOnManyColumnsRule (L030) warns when DISTINCT is used with many columns. |
| 27 | +// DISTINCT on many columns is often a sign of a missing GROUP BY or denormalized |
| 28 | +// data. It also forces a sort over all projected columns, which is expensive. |
| 29 | +type DistinctOnManyColumnsRule struct{ linter.BaseRule } |
| 30 | + |
| 31 | +// NewDistinctOnManyColumnsRule creates a new L030 rule instance. |
| 32 | +func NewDistinctOnManyColumnsRule() *DistinctOnManyColumnsRule { |
| 33 | + return &DistinctOnManyColumnsRule{ |
| 34 | + BaseRule: linter.NewBaseRule( |
| 35 | + "L030", |
| 36 | + "Distinct on Many Columns", |
| 37 | + "DISTINCT on many columns suggests a missing GROUP BY or data quality issue", |
| 38 | + linter.SeverityWarning, |
| 39 | + false, |
| 40 | + ), |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Check inspects SELECT statements for DISTINCT with many columns. |
| 45 | +func (r *DistinctOnManyColumnsRule) Check(ctx *linter.Context) ([]linter.Violation, error) { |
| 46 | + if ctx.AST == nil { |
| 47 | + return nil, nil |
| 48 | + } |
| 49 | + var violations []linter.Violation |
| 50 | + for _, stmt := range ctx.AST.Statements { |
| 51 | + sel, ok := stmt.(*ast.SelectStatement) |
| 52 | + if !ok { |
| 53 | + continue |
| 54 | + } |
| 55 | + if !sel.Distinct { |
| 56 | + continue |
| 57 | + } |
| 58 | + colCount := len(sel.Columns) |
| 59 | + if colCount >= distinctColumnThreshold { |
| 60 | + violations = append(violations, linter.Violation{ |
| 61 | + Rule: r.ID(), |
| 62 | + RuleName: r.Name(), |
| 63 | + Severity: r.Severity(), |
| 64 | + Message: fmt.Sprintf("DISTINCT on %d columns is expensive and may indicate a missing GROUP BY or join issue", colCount), |
| 65 | + Location: sel.Pos, |
| 66 | + Suggestion: "Consider using GROUP BY with aggregate functions, or investigate whether the query structure can be simplified", |
| 67 | + }) |
| 68 | + } |
| 69 | + } |
| 70 | + return violations, nil |
| 71 | +} |
| 72 | + |
| 73 | +// Fix is a no-op: replacing DISTINCT with GROUP BY requires semantic understanding. |
| 74 | +func (r *DistinctOnManyColumnsRule) Fix(content string, violations []linter.Violation) (string, error) { |
| 75 | + return content, nil |
| 76 | +} |
0 commit comments