MCPcopy Create free account
hub / github.com/bytebase/bytebase / rewriteSelectLimit

Function rewriteSelectLimit

backend/plugin/db/starrocks/query.go:172–201  ·  view source on GitHub ↗
(sql string, stmt *ast.SelectStmt, limitCount int)

Source from the content-addressed store, hash-verified

170}
171
172func rewriteSelectLimit(sql string, stmt *ast.SelectStmt, limitCount int) (string, error) {
173 if stmt.Limit != nil {
174 limitLoc := literalLoc(stmt.Limit)
175 if limitLoc.Start < 0 {
176 return "", errors.New("cannot rewrite non-constant LIMIT expression")
177 }
178
179 if countStart, countEnd, ok := findCommaLimitCount(sql, limitLoc); ok {
180 existingCount, _ := strconv.Atoi(sql[countStart:countEnd])
181 if existingCount > 0 && existingCount <= limitCount {
182 return sql, nil
183 }
184 return sql[:countStart] + fmt.Sprintf("%d", limitCount) + sql[countEnd:], nil
185 }
186
187 existingLimit := extractLimitValue(stmt.Limit)
188 if existingLimit >= 0 && existingLimit <= limitCount {
189 return sql, nil
190 }
191 return sql[:limitLoc.Start] + fmt.Sprintf("%d", limitCount) + sql[limitLoc.End:], nil
192 }
193
194 if stmt.Into != nil {
195 return sql[:stmt.Into.Loc.Start] + fmt.Sprintf("LIMIT %d ", limitCount) + sql[stmt.Into.Loc.Start:], nil
196 }
197 if hasUnparsedTail(sql, stmt.Loc.End) {
198 return "", errors.New("statement has unparsed tail content")
199 }
200 return sql[:stmt.Loc.End] + fmt.Sprintf(" LIMIT %d", limitCount) + sql[stmt.Loc.End:], nil
201}
202
203func rewriteSetOpLimit(sql string, stmt *ast.SetOpStmt, limitCount int) (string, error) {
204 if stmt.Limit != nil {

Callers 1

Calls 4

literalLocFunction · 0.85
findCommaLimitCountFunction · 0.85
extractLimitValueFunction · 0.85
hasUnparsedTailFunction · 0.85

Tested by

no test coverage detected