Back to The Four Attributes

Attributes · The Four Attributes · Practice Question

For Each Attribute, When Is It Preserved and When Is It Lost?

Sign in to track your progress

Question

Describe when each of the four attributes is kept, and when it is lost, as an in-memory list is appended to, has items dropped, or has items modified.

Solution

/ SORTED (s#)
n: `s# 1 2 3 5 8
n,: 13          / kept — 13 is not less than the last item
attr n          / `s
n,: 1           / lost — 1 breaks the ascending order
attr n          / `

/ UNIQUE (u#)
u: `u# `AAPL`GOOG`MSFT
u,: `TSLA       / kept — TSLA is new
attr u          / `u
attr -1_u       / ` — dropping items removes it
u,: `AAPL       / no error, but AAPL is now a duplicate...
attr u          / ` — ...so the attribute is removed

/ PARTED (p#)
p: `p# `A`A`B`B`C
p,: `C          / even this append, which keeps the blocks contiguous...
attr p          / ` — ...removes p#

/ GROUPED (g#)
g: `g# `A`B`A`C
g,: `B`D        / kept on append
attr g          / `g
g[0]: `Z        / kept when items are replaced
attr g          / `g
attr -1_g       / ` — dropping items removes it

Explanation

Sorted `s#: kept on an in-place append (,:) when the new items keep the list in ascending order; lost otherwise. Lost when items are dropped or modified.

Unique `u#: kept on append when the new items are still unique. Appending a duplicate does not signal an error — the append succeeds and the attribute is silently removed. Lost when items are dropped or modified.

Parted `p#: KX's documentation states that "the parted attribute is removed by any operation on the list". In memory, that includes every append, even one that would keep equal values contiguous.

Grouped `g#: kept on append, whatever the new values are, and kept when items are replaced — kdb+ updates its index. Lost when items are dropped (for example with _, or delete on a table).

Summary table:

AttributeAppend that keeps the propertyAppend that breaks itDrop itemsModify an item
`s#KeptLostLostLost
`u#KeptLost (no error)LostLost
`p#LostLostLostLost
`g#Kept— (any value is valid)LostKept

Note also that a plain join (n , x) always returns a new list without an attribute — use the in-place form n ,: x if you want the rules above to apply.

On disk, KX notes that only s# is kept when a column file is appended to. This is why p# must be re-applied after appending to an HDB partition, and why g# — kept through appends in memory — is the attribute used on the RDB's sym column.