final class Precedence
Restricted visibility: declared "@visibility SqlSemantics". Code outside that scope must not name this declaration.

Decides whether an operand keeps its place when an operator expression is written without parentheses.

Rule: PG-PRECEDENCE-001. The levels are the precedence declarations of the PostgreSQL grammar (gram.y), weakest first: OR; AND; NOT (right); IS, ISNULL and NOTNULL; the six comparison operators; BETWEEN, IN, LIKE, ILIKE, SIMILAR and the NOT before them; ESCAPE; UNBOUNDED; IDENT and the other words with declared precedence; other operators and OPERATOR(...);

  • and -; *, / and %; ^; AT; COLLATE; the prefix + and - (right); the

subscript, parenthesis, :: and . levels. Each operator form has a left operand followed by a token of some level, and a last operand that closes its rule at the rule's level. An operand written before a token of level L keeps its place when every rule on its right edge is above L, or at L and L groups to the left; an operand written after the token of a rule of level L keeps its place when every token on its left edge is above L, or at L and L groups to the right. Precedence decides only between a rule and a token that can continue its last operand, which every operator token can; ESCAPE continues only a pattern match without one, so an operand before ESCAPE keeps its place unless such a match ends it; WITH and WITHOUT continue only an IS JSON test without a uniqueness clause. An expression that is not an operator form is a c_expr: it is closed on both edges. Terminates: each walk follows one edge of a finite tree. Source: https://www.postgresql.org/docs/17/sql-syntax-lexical.html#SQL-PRECEDENCE, https://github.com/postgres/postgres/blob/REL_17_2/src/backend/parser/gram.y. Status: Implemented.

Constants§

public const OR = 1

The level of OR.

public const AND = 2

The level of AND.

public const NOT = 3

The level of the prefix NOT.

public const IS = 4

The level of IS, ISNULL and NOTNULL.

public const COMPARISON = 5

The level of the comparison operators.

public const PATTERN = 6

The level of BETWEEN, IN, LIKE, ILIKE, SIMILAR and the NOT before them.

public const ESCAPE = 7

The level of ESCAPE.

public const UNBOUNDED = 8

The level of UNBOUNDED and NESTED.

public const IDENT = 9

The level of IDENT and the other words with declared precedence, such as PRECEDING and FOLLOWING.

public const OPERATOR = 10

The level of the other operators and OPERATOR(...).

public const ADDITIVE = 11

The level of binary + and -.

public const MULTIPLICATIVE = 12

The level of *, / and %.

public const EXPONENT = 13

The level of ^.

public const AT = 14

The level of AT TIME ZONE and AT LOCAL.

public const COLLATE = 15

The level of COLLATE.

public const UNARY = 16

The level of the prefix + and -.

public const TYPECAST = 19

The level of ::.

public const CLOSED = 99

The level of an edge that holds no operand.

Methods§

public function operator(OperatorName $operator, bool $prefix = false): int

Answers the level of an operator name written between or before operands.

Parameters

$operatorOperatorName
$prefixbool

Returns

int
Test cases 1
Called from 7
Calls 6
public function edges(Scalar $expression): array{Scalar|null, int, Scalar|null, int}|null
public function operators(Scalar $expression): array{Scalar|null, int, Scalar|null, int}|null
public function predicates(Scalar $expression): array{Scalar|null, int, Scalar|null, int}|null

Answers the edges of the predicate and sublink forms of PG-PREDICATE-001 and PG-SUBLINK-001.

Parameters

$expressionScalar

Returns

array{Scalar|null, int, Scalar|null, int}|null
Test cases 1
Called from 1
Calls 4
public function opening(Scalar $expression): int

Answers the weakest token level on the left edge of an expression.

Parameters

$expressionScalar

Returns

int
Test cases 1
Called from 1
Calls 3
public function closing(Scalar $expression): int

Answers the weakest rule level on the right edge of an expression.

Parameters

$expressionScalar

Returns

int
Test cases 1
Called from 1
Calls 3
public function before(Scalar $operand, int $level): bool

Tells whether an operand keeps its place when a token of a level follows it.

Parameters

$operandScalar
$levelint

Returns

bool
Test cases 1
Called from 19
Calls 3
public function takesEscape(Scalar $operand): bool

Tells whether a pattern match without ESCAPE lies on the right edge of an operand, where it would take a following ESCAPE.

ESCAPE continues only a pattern match, so no other rule on the edge competes for it: the parser reduces them and gives ESCAPE to the nearest open pattern match.

Parameters

$operandScalar

Returns

bool
Test cases 1
Called from 2
Calls 1
public function takesUniqueness(Scalar $operand): bool

Tells whether an IS JSON test without a key uniqueness clause lies on the right edge of an operand, where it would take a following WITH or WITHOUT.

The grammar gives WITH and WITHOUT a higher precedence than the empty json_key_uniqueness_constraint_opt of a_expr IS JSON, so the nearest open test takes a WITH UNIQUE, WITH WRAPPER, WITH CHECK OPTION or WITH DATA that follows the operand, and the last three then fail to parse.

Parameters

$operandScalar

Returns

bool
Test cases 1
Called from 5
Calls 1
public function after(Scalar $operand, int $level): bool

Tells whether an operand keeps its place as the last operand of a rule of a level.

Parameters

$operandScalar
$levelint

Returns

bool
Test cases 1
Called from 9
Calls 3

Private surface 2§

Implementation details, listed for orientation only.

private const LEFT = [self::OR, self::AND, self::OPERATOR, self::ADDITIVE, self::MULTIPLICATIVE, sel…
private const RIGHT = [self::NOT, self::UNARY]

Test cases 12§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 12

Relations§

Instantiated in 36
Method calls 50
Constant reads 25