final class ExpressionAffinity
Restricted visibility: declared "@visibility SqlSemantics\Platform\Sqlite". Code outside that scope must not name this declaration.

Derives the affinity of a result expression, which decides the column type of a table or view made from a query.

Rule: SQLITE-EXPRESSION-AFFINITY-001. "An expression that is a simple

  • reference to a column value has the same affinity as the column", also

inside parentheses and under COLLATE; the row identifier has INTEGER affinity; "an expression of the form CAST(expr AS type) has an affinity

  • that is the same as a column with a declared type of type"; a row value

has the affinity of its first member; a scalar subquery has the affinity of the first result of its rightmost arm; every other expression has no affinity. A column of a derived table or of a common table has the affinity SQLite computes for that column of its query (SQLITE-QUERY-COLUMN-AFFINITY-001, below); a column of a declared table or view has the affinity of its declared type, or none (NoAffinity).

Rule: SQLITE-QUERY-COLUMN-AFFINITY-001. The affinity of a column of a query is that of the expression at its position in the leftmost arm; when that expression has no affinity, the arms are read from left to right until one has an affinity. A query with one arm is done there. Otherwise, unless the affinity is BLOB, it is adjusted by the kinds of value the other arms produce (SQLITE-DATA-TYPE-001): a TEXT affinity becomes BLOB when another arm produces numbers, and a numeric affinity becomes BLOB when another arm produces text; a numeric affinity becomes the flexible numeric affinity when the expression of the leftmost arm is a CAST. Each row of a VALUES list is one arm. Precision: exact; the affinity is not determined when it depends on a declaration the context lacks or on a name that did not resolve. Terminates: each step enters a strictly nested query or a strict sub-expression, and the arms are finite. Source: https://sqlite.org/datatype3.html#affinity_of_expressions (and sqlite3ExprAffinity() in expr.c and sqlite3SubqueryColumnTypes() in select.c of the release). Status: Implemented.

Methods§

public function reference(Scalar $expression, Facts $facts): DerivedAffinity

Answers the affinity of a column reference: that of what it resolved to; a double-quoted word that is no column is a string without affinity.

Parameters

$expressionScalar
$factsFacts
Test cases 1
Called from 1
Calls 5
public function written(Select|ValueRow $arm, int $position, Facts $facts): ?Scalar

Answers the expression an arm writes at a position, or null for an expanded star or a position the arm does not have.

Parameters

$armSelect|ValueRow
$positionint
$factsFacts

Returns

Test cases 1
Called from 1
Calls 2
public function first(Query $query, Facts $facts): DerivedAffinity

Answers the affinity of a scalar subquery: that of the first result of the rightmost arm.

Parameters

$queryQuery
$factsFacts
Called from 1
Calls 3
public function adjusted(
    DerivedAffinity $affinity,
    int $kinds,
    ?Scalar $leftmost,
): DerivedAffinity

Adjusts the affinity of a compound column by the kinds of value the other arms produce and by a CAST in the leftmost arm.

Parameters

$affinityDerivedAffinity
$kindsint
$leftmost?Scalar
Test cases 1
Called from 1
Calls 7

Test cases 14§

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

Dedicated tests 14

Relations§

Instantiated in 7
Method calls 9