Back to Gotchas & Edge Cases

Attributes · Gotchas & Edge Cases · Practice Question

What Is the Difference Between Plain Join and In-Place Append for Attributes?

Sign in to track your progress

Question

What happens to attributes when you use plain join (,) versus in-place append (,:)?

Solution

/ Plain join — ALWAYS strips the attribute, even if order is maintained
n: `s# til 10
result: n , 10 11 12
attr result      / ` — stripped, even though the result is still sorted

/ In-place append — MAY preserve the attribute if the guarantee holds
n: `s# til 10
n,: 10 11 12
attr n           / `s — preserved (values maintain sorted order)

n,: 5 6 7        / appending values smaller than current max
attr n           / ` — sorted order broken, attribute lost

/ Grouped survives in-place append regardless of order
m: `g# `AAPL`GOOG`AAPL
m,: `MSFT`AAPL
attr m           / `g — always preserved for in-place append

Explanation

There are two distinct forms of joining lists in q, and they behave very differently with respect to attributes:

Plain join (,) — result: a , b creates a new list and always returns it without any attribute, even if the structural guarantee is clearly still satisfied (e.g. joining two sorted lists in order). kdb+ does not check whether the result could validly claim the attribute; it simply produces an unattributed list.

In-place append (,:) — n,: x modifies n directly and applies the attribute preservation rules:

  • s#: preserved if the appended values are all ≥ the current maximum of n; lost otherwise
  • u#: preserved if the appended values introduce no duplicates; lost otherwise
  • g#: always preserved — no structural requirement to break
  • p#: removed by any append (KX: "the parted attribute is removed by any operation on the list")

The practical trap: a developer might write n: n , newRows expecting the attribute to be preserved. Because this creates a new list via plain join, the attribute is always dropped. The correct form is n,: newRows.

/ WRONG — looks like reassignment but uses plain join, strips attribute
n: `g# `AAPL`GOOG`MSFT
n: n , `AAPL`TSLA
attr n           / ` — attribute gone

/ CORRECT — in-place append preserves g#
n: `g# `AAPL`GOOG`MSFT
n,: `AAPL`TSLA
attr n           / `g — preserved