final class Cast
    implements Scalar, OutputNaming
    uses Snapshot
Public API: explicitly declared with @visibility public.

A conversion of a value to a type: x::t or CAST(x AS t).

Mirrors PostgreSQL's TypeCast node. Both spellings are the same node; the spelling is kept because the token sequences differ.

Rule: PG-CAST-001. Facts: the type the type name denotes; the NULL fact of the operand, since a cast of NULL is NULL and a cast of a value is a value. An unaliased result column is named after the operand when the operand names one firmly, and otherwise after the type. The operand of :: must keep its place without parentheses (PG-PRECEDENCE-001). Source: https://www.postgresql.org/docs/17/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS, https://www.postgresql.org/docs/17/sql-createcast.html. Status: Implemented.

Reading the type of a castdoctest
$query = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze("SELECT '1'::bigint");
    [$query->field(0)->type->descriptor->name(), $query->field(0)->name->value] // => ['bigint', 'int8']
Rejecting an operand that would need parenthesesdoctest
$sum = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('SELECT 1 + 2')->field(0)->expression;
    $type = new \SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName(new \SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\KeywordDesignation(\SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\TypeKeyword::Integer));
    new \SqlSemantics\Platform\PostgreSql\Statement\Expression\Cast($sum, $type, \SqlSemantics\Platform\PostgreSql\Statement\Expression\CastSpelling::Operator) // throws \SqlSemantics\Platform\PostgreSql\Model\Diagnostic\InvalidConstruction

Properties§

public Scalar $operand
public TypeName $type
public CastSpelling $spelling

Methods§

public function __construct(
    public Scalar $operand,
    public TypeName $type,
    public CastSpelling $spelling,
)

Parameters

$operandScalarThe converted value
$typeTypeNameThe target type
$spellingCastSpellingHow the cast is written
Calls 5
public function outputName(): ?Name

Names an unaliased result column after the operand, or after the type.

Returns

Test cases 2
Calls 2
public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact

Derives the operand and the target type.

Parameters

$derivationDerivation
$environmentEnvironment

Returns

Calls 5
public function render(Output $out): void

Writes the operand, :: and the type, or the CAST form.

Parameters

$outOutput
Test cases 1
Calls 3

Test cases 8§

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

Dedicated tests 5
Other tests reaching this symbol 3

Relations§

Instantiated in 2
Type checks 3