classCast
final class Cast
implements Scalar, OutputNaming
uses Snapshot
@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.
$query = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze("SELECT '1'::bigint");
[$query->field(0)->type->descriptor->name(), $query->field(0)->name->value] // => ['bigint', 'int8']$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\InvalidConstructionProperties§
public Scalar $operandpublic TypeName $typepublic CastSpelling $spellingMethods§
public function __construct(
public Scalar $operand,
public TypeName $type,
public CastSpelling $spelling,
)Parameters
$operand | Scalar | The converted value |
$type | TypeName | The target type |
$spelling | CastSpelling | How the cast is written |
Calls 5
- static-call Check::input() line 55
- class-const CastSpelling::Function() line 55
- method-call Precedence::before() line 55
- new Precedence line 55
- class-const Precedence::TYPECAST() line 55
public function outputName(): ?NameNames an unaliased result column after the operand, or after the type.
Returns
?NameTest cases 2
Calls 2
- method-call ColumnNaming::name() line 63
- new ColumnNaming line 63
public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFactDerives the operand and the target type.
Parameters
$derivation | Derivation | |
$environment | Environment |
Returns
Calls 5
- method-call Derivation::scalar() line 71
- method-call TypeName::deriveClause() line 72
- method-call TypeName::typeFact() line 73
- method-call Derivation::report() line 75
- new ScalarFact line 78
public function render(Output $out): voidWrites the operand, :: and the type, or the CAST form.
Parameters
$out | Output |
Test cases 1
Calls 3
- class-const CastSpelling::Operator() line 86
- method-call Output::node() line 87
- method-call Output::keyword() line 91
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
- new SqlSemantics\Platform\PostgreSql\Lowering\Expression\OperatorRule::postfix() packages/sql-semantics-postgres/src/Lowering/Expression/OperatorRule.php:105
- new SqlSemantics\Platform\PostgreSql\Lowering\Invocation\SyntaxRule::special() packages/sql-semantics-postgres/src/Lowering/Invocation/SyntaxRule.php:112
Type checks 3
- instanceof SqlSemantics\Platform\PostgreSql\Rules\Expression\ColumnNaming::figure() packages/sql-semantics-postgres/src/Rules/Expression/ColumnNaming.php:55
- instanceof SqlSemantics\Platform\PostgreSql\Rules\Expression\Precedence::operators() packages/sql-semantics-postgres/src/Rules/Expression/Precedence.php:216
- instanceof SqlSemantics\Platform\PostgreSql\Rules\Expression\Precedence::restricted() packages/sql-semantics-postgres/src/Rules/Expression/Precedence.php:284