MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / parseOrderByClause

Method parseOrderByClause

pkg/sql/parser/select_clauses.go:526–583  ·  view source on GitHub ↗

parseOrderByClause parses "ORDER BY [ASC|DESC] [NULLS FIRST|LAST] [, ...]". Returns nil slice (no error) when ORDER BY is absent.

()

Source from the content-addressed store, hash-verified

524// parseOrderByClause parses "ORDER BY <expr> [ASC|DESC] [NULLS FIRST|LAST] [, ...]".
525// Returns nil slice (no error) when ORDER BY is absent.
526func (p *Parser) parseOrderByClause() ([]ast.OrderByExpression, error) {
527 if !p.isType(models.TokenTypeOrder) {
528 return nil, nil
529 }
530 p.advance() // Consume ORDER
531
532 if !p.isType(models.TokenTypeBy) {
533 return nil, p.expectedError("BY")
534 }
535 p.advance() // Consume BY
536
537 var orderByExprs []ast.OrderByExpression
538 for {
539 expr, err := p.parseExpression()
540 if err != nil {
541 return nil, err
542 }
543
544 entry := ast.OrderByExpression{
545 Expression: expr,
546 Ascending: true,
547 NullsFirst: nil,
548 }
549
550 if p.isType(models.TokenTypeAsc) {
551 entry.Ascending = true
552 p.advance()
553 } else if p.isType(models.TokenTypeDesc) {
554 entry.Ascending = false
555 p.advance()
556 }
557
558 nullsFirst, err := p.parseNullsClause()
559 if err != nil {
560 return nil, err
561 }
562 entry.NullsFirst = nullsFirst
563
564 // ClickHouse WITH FILL per-entry tail:
565 // ORDER BY expr [ASC|DESC] WITH FILL [FROM x] [TO y] [STEP z]
566 // Consume permissively; the clause is not modeled on the AST yet.
567 if p.dialect == string(keywords.DialectClickHouse) &&
568 p.isType(models.TokenTypeWith) &&
569 strings.EqualFold(p.peekToken().Token.Value, "FILL") {
570 p.advance() // WITH
571 p.advance() // FILL
572 p.skipClickHouseWithFillTail()
573 }
574
575 orderByExprs = append(orderByExprs, entry)
576
577 if !p.isType(models.TokenTypeComma) {
578 break
579 }
580 p.advance()
581 }
582 return orderByExprs, nil
583}

Callers 1

parseSelectStatementMethod · 0.95

Calls 7

isTypeMethod · 0.95
advanceMethod · 0.95
expectedErrorMethod · 0.95
parseExpressionMethod · 0.95
parseNullsClauseMethod · 0.95
peekTokenMethod · 0.95

Tested by

no test coverage detected