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

Function rewriteSetOpLimit

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

Source from the content-addressed store, hash-verified

201}
202
203func rewriteSetOpLimit(sql string, stmt *ast.SetOpStmt, limitCount int) (string, error) {
204 if stmt.Limit != nil {
205 limitLoc := literalLoc(stmt.Limit)
206 if limitLoc.Start < 0 {
207 return "", errors.New("cannot rewrite non-constant LIMIT expression")
208 }
209
210 if countStart, countEnd, ok := findCommaLimitCount(sql, limitLoc); ok {
211 existingCount, _ := strconv.Atoi(sql[countStart:countEnd])
212 if existingCount > 0 && existingCount <= limitCount {
213 return sql, nil
214 }
215 return sql[:countStart] + fmt.Sprintf("%d", limitCount) + sql[countEnd:], nil
216 }
217
218 existingLimit := extractLimitValue(stmt.Limit)
219 if existingLimit >= 0 && existingLimit <= limitCount {
220 return sql, nil
221 }
222 return sql[:limitLoc.Start] + fmt.Sprintf("%d", limitCount) + sql[limitLoc.End:], nil
223 }
224 if hasUnparsedTail(sql, stmt.Loc.End) {
225 return "", errors.New("statement has unparsed tail content")
226 }
227 return sql[:stmt.Loc.End] + fmt.Sprintf(" LIMIT %d", limitCount) + sql[stmt.Loc.End:], nil
228}
229
230func extractLimitValue(node ast.Node) int {
231 lit, ok := node.(*ast.Literal)

Callers 1

Calls 4

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

Tested by

no test coverage detected