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
ParameterTypeRequiredDescription
keyT#EYesIdentity 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#ENTITY1PLAIN_ID BIGINTKEY_FOR_ENTITY2 BIGINT#ENTITY2T_ANNOTATED VARCHART_PLAIN VARCHART_REMAPPED VARCHARNEXT_ANNOTATED_ID BIGINT#ENTITY1T_NEXT VARCHAR
111BIGINT#ENTITY1BIGINTBIGINT#ENTITY22BIGINT#ENTITY1
222BIGINT#ENTITY1BIGINTBIGINT#ENTITY23BIGINT#ENTITY1
333BIGINT#ENTITY1BIGINTBIGINT#ENTITY24BIGINT#ENTITY1