classExpressionAffinity
final class ExpressionAffinity
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 of(Scalar $expression, Facts $facts): DerivedAffinityAnswers the affinity of an expression.
Returns
Test cases 1
Called from 4
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::of() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:86
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::field() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:120
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::arm() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:227
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ValueKinds::of() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ValueKinds.php:105
Calls 5
- new DerivedAffinity line 83
- class-const Affinity::Numeric() line 83
- method-call ExpressionAffinity::of() line 86
- method-call ExpressionAffinity::first() line 89
- method-call ExpressionAffinity::reference() line 92
public function reference(Scalar $expression, Facts $facts): DerivedAffinityAnswers 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.
Returns
Test cases 1
Called from 1
Calls 5
- method-call Facts::covers() line 103
- method-call Facts::scalar() line 103
- method-call ExpressionAffinity::resolved() line 105
- method-call ExpressionAffinity::field() line 108
- new DerivedAffinity line 111
public function field(Field $field, Facts $facts): DerivedAffinityAnswers the affinity of an output field: that of its expression, or of the column an expanded star exposes.
Returns
Test cases 1
Called from 2
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::reference() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:108
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::arm() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:231
Calls 3
- method-call ExpressionAffinity::of() line 120
- method-call ExpressionAffinity::resolved() line 123
- new DerivedAffinity line 123
public function resolved(ResolvedColumn $resolution, Facts $facts): DerivedAffinityAnswers the affinity of a resolved column: of its declaration, or of the column of the query that computes it.
Parameters
$resolution | ResolvedColumn | |
$facts | Facts |
Returns
Called from 3
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::reference() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:105
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::field() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:123
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ValueKinds::arm() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ValueKinds.php:163
Calls 5
- method-call ExpressionAffinity::declared() line 132
- method-call ExpressionAffinity::computing() line 134
- method-call ExpressionAffinity::position() line 136
- new DerivedAffinity line 138
- method-call ExpressionAffinity::column() line 138
public function declared(Column $column): DerivedAffinityAnswers the affinity of a declared column: that of its declared type, none for a view column without affinity, and that of the type name for any other descriptor.
Parameters
$column | Column |
Returns
Test cases 1
Called from 2
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::resolved() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:132
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::resolved() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:142
Calls 3
- new DerivedAffinity line 152
- method-call ColumnTyping::textAffinity() line 155
- new ColumnTyping line 155
Answers the query that computes the columns of a relation occurrence: a derived query or the definition of a common table.
Returns
Called from 2
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::resolved() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:114
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::resolved() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:134
Calls 2
- method-call Facts::covers() line 166
- method-call Facts::relation() line 169
public function position(Query $query, OutputSlot $slot, Facts $facts): ?intAnswers the position of the output field of a query that a slot re-exposes, or null when the slot comes from nowhere in it.
Parameters
$query | Query | |
$slot | OutputSlot | |
$facts | Facts |
Returns
?intTest cases 1
Called from 2
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::resolved() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:116
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::resolved() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:136
Calls 2
- method-call Facts::covers() line 179
- method-call Facts::query() line 183
Answers the arms of a query from left to right; each row of a VALUES list is an arm.
Parameters
$query | Query |
Throws
InvariantViolation When the query is not a selection, a row list or a compound of thoseCalled from 5
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::text() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:92
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::resolved() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:117
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::leftmost() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:157
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::first() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:252
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::column() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:262
Calls 3
- function-call
array_mapline 205 - new InvariantViolation line 207
- function-call
array_pushline 212
public function arm(Select|ValueRow $arm, int $position, Facts $facts): DerivedAffinityAnswers the affinity of the result of an arm at a position; an expanded star is a column reference.
Returns
Test cases 1
Called from 3
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::first() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:254
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::column() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:265
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::column() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:272
Calls 5
- method-call ExpressionAffinity::of() line 227
- new DerivedAffinity line 227
- method-call Facts::covers() line 229
- method-call Facts::query() line 229
- method-call ExpressionAffinity::field() line 231
Answers the expression an arm writes at a position, or null for an expanded star or a position the arm does not have.
Returns
Test cases 1
Called from 1
Calls 2
- method-call Facts::covers() line 242
- method-call Facts::query() line 242
public function first(Query $query, Facts $facts): DerivedAffinityAnswers the affinity of a scalar subquery: that of the first result of the rightmost arm.
Returns
Called from 1
Calls 3
- method-call ExpressionAffinity::arms() line 252
- method-call ExpressionAffinity::arm() line 254
- function-call
countline 254
public function column(Query $query, int $position, Facts $facts): DerivedAffinityAnswers the affinity SQLite computes for a column of a query (SQLITE-QUERY-COLUMN-AFFINITY-001).
Returns
Test cases 4
ExpressionAffinityTest::testColumnAdjustsACompoundByTheOtherArmscallsExpressionAffinityTest::testColumnIsNotDeterminedWhenAnArmDependsOnAMissingDeclarationcallsExpressionAffinityTest::testFirstReadsTheFirstResultOfTheRightmostArmcallsExpressionAffinityTest::testOfLooksThroughParenthesesAndCollateAndReadsCastsRowValuesAndSubqueriescalls
Called from 3
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ExpressionAffinity::resolved() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ExpressionAffinity.php:138
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\QueryColumns::tableColumns() packages/sql-semantics-sqlite/src/Rules/Definition/Query/QueryColumns.php:171
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\QueryColumns::viewColumns() packages/sql-semantics-sqlite/src/Rules/Definition/Query/QueryColumns.php:206
Calls 7
- method-call ExpressionAffinity::arms() line 262
- method-call ExpressionAffinity::arm() line 265
- method-call ValueKinds::arm() line 267
- new ValueKinds line 267
- new DerivedAffinity line 269
- method-call ExpressionAffinity::adjusted() line 285
- method-call ExpressionAffinity::written() line 285
public function adjusted(
DerivedAffinity $affinity,
int $kinds,
?Scalar $leftmost,
): DerivedAffinityAdjusts the affinity of a compound column by the kinds of value the other arms produce and by a CAST in the leftmost arm.
Parameters
$affinity | DerivedAffinity | |
$kinds | int | |
$leftmost | ?Scalar |
Returns
Test cases 1
Called from 1
Calls 7
- class-const Affinity::Text() line 293
- class-const ValueKinds::NUMBER() line 293
- new DerivedAffinity line 294
- class-const Affinity::Blob() line 294
- method-call DerivedAffinity::numeric() line 296
- class-const ValueKinds::TEXT() line 296
- class-const Affinity::Numeric() line 303
Test cases 14§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 14
ExpressionAffinityTest::testAdjustedAppliesTheRulesOfACompoundColumncallsExpressionAffinityTest::testArmReadsAPositionOfASelectionOrARowcallsExpressionAffinityTest::testArmsFlattensWithClausesCompoundsAndValuesRowscallsExpressionAffinityTest::testColumnAdjustsACompoundByTheOtherArmscallsExpressionAffinityTest::testColumnIsNotDeterminedWhenAnArmDependsOnAMissingDeclarationcallsExpressionAffinityTest::testComputingAnswersTheQueryOfADerivedTableOrACommonTablecallsExpressionAffinityTest::testDeclaredReadsEachKindOfTypeDescriptorcallsExpressionAffinityTest::testFieldReadsTheExpressionOrTheColumnAnExpandedStarExposescallsExpressionAffinityTest::testFirstReadsTheFirstResultOfTheRightmostArmcallsExpressionAffinityTest::testOfLooksThroughParenthesesAndCollateAndReadsCastsRowValuesAndSubqueriescallsExpressionAffinityTest::testPositionFindsTheOutputFieldASlotReExposescallsExpressionAffinityTest::testReferenceReadsADoubleQuotedWordThatIsNoColumnAsAStringWithoutAffinitycallsExpressionAffinityTest::testResolvedReadsADeclarationADerivedTableAndACommonTablecallsExpressionAffinityTest::testWrittenReadsTheExpressionOfAPositionAndNothingForAStarcalls
Relations§
Instantiated in 7
- new SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::text() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:92
- new SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::resolved() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:113
- new SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::leftmost() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:157
- new SqlSemantics\Platform\Sqlite\Rules\Definition\Query\QueryColumns::tableColumns() packages/sql-semantics-sqlite/src/Rules/Definition/Query/QueryColumns.php:171
- new SqlSemantics\Platform\Sqlite\Rules\Definition\Query\QueryColumns::viewColumns() packages/sql-semantics-sqlite/src/Rules/Definition/Query/QueryColumns.php:206
- new SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ValueKinds::of() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ValueKinds.php:105
- new SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ValueKinds::arm() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ValueKinds.php:163
Method calls 9
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::text() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:92
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::resolved() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:114
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::resolved() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:116
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::resolved() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:117
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ColumnTyping::leftmost() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ColumnTyping.php:157
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\QueryColumns::tableColumns() packages/sql-semantics-sqlite/src/Rules/Definition/Query/QueryColumns.php:171
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\QueryColumns::viewColumns() packages/sql-semantics-sqlite/src/Rules/Definition/Query/QueryColumns.php:206
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ValueKinds::of() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ValueKinds.php:105
- method-call SqlSemantics\Platform\Sqlite\Rules\Definition\Query\ValueKinds::arm() packages/sql-semantics-sqlite/src/Rules/Definition/Query/ValueKinds.php:163