KEY_TO_SCALAR()
All functions > CORE > KEY_TO_SCALAR()
Drop the entity annotation and keep the base type and unit
Signatures
Returns: The same value with the entity removed
KEY_TO_SCALAR(key: T#E) → T sql
| Parameter | Type | Required | Description |
|---|---|---|---|
key | T#E | Yes | Identity key |
Signature notes: A value that is already plain is unchanged and warns KEY-TO-SCALAR-NO-ENTITY. The call marks the value: CAST and :: refuse to put an entity back on it. Relabel with UNSAFE_CAST.
Notes
Hash a key with HASH01(CAST(KEY_TO_SCALAR(key) AS VARCHAR)) when HASH01 requires VARCHAR. Order two keys with IS_KEY_BEFORE, not with <.
See also
IS_KEY_BEFORE
KEY_PAIR
UNSAFE_CAST
Examples
Drop, keep or relabel an entity annotation
FeatureQL
DEFINE
entity1 := ENTITY(),
entity2 := ENTITY(),
entity1_id := INPUT(BIGINT#entity1)
RETURN
entity1_id,
plain_id := KEY_TO_SCALAR(entity1_id),
key_for_entity2 := UNSAFE_CAST(entity1_id AS _#entity2),
t_annotated := TYPEOF(entity1_id),
t_plain := TYPEOF(plain_id),
t_remapped := TYPEOF(key_for_entity2),
next_annotated_id := UNSAFE_CAST(KEY_TO_SCALAR(entity1_id) + 1 AS _#entity1),
t_next := TYPEOF(next_annotated_id)
FOR
entity1_id := BIND_VALUES(SEQUENCE(1, 3))
;Result
| ENTITY1_ID BIGINT#ENTITY1 | PLAIN_ID BIGINT | KEY_FOR_ENTITY2 BIGINT#ENTITY2 | T_ANNOTATED VARCHAR | T_PLAIN VARCHAR | T_REMAPPED VARCHAR | NEXT_ANNOTATED_ID BIGINT#ENTITY1 | T_NEXT VARCHAR |
|---|---|---|---|---|---|---|---|
| 1 | 1 | 1 | BIGINT#ENTITY1 | BIGINT | BIGINT#ENTITY2 | 2 | BIGINT#ENTITY1 |
| 2 | 2 | 2 | BIGINT#ENTITY1 | BIGINT | BIGINT#ENTITY2 | 3 | BIGINT#ENTITY1 |
| 3 | 3 | 3 | BIGINT#ENTITY1 | BIGINT | BIGINT#ENTITY2 | 4 | BIGINT#ENTITY1 |