Skip to content

New Documentation Site!

We are excited to announce the launch of our enhanced product documentation site for PyKX at docs.kx.com. It offers improved search capabilities, organized navigation, and developer-focused content. Please, take a moment to explore the site and share your feedback with us.

Schema generation

This page documents the API for generating table schemas that are compatible with both upsert and insert.

builder

builder(schema, *, key=None)

Generate an empty schema for a keyed or unkeyed q table.

Parameters:

Name Type Description Default
schema Dict

The definition of the schema to be created mapping a 'str' to a pykx.* type object. Each pykx.* value must be one of the types defined in pykx.schema._ktype_to_conversion.

required
key Optional[Union[str, List[str]]]

The column name(s) in schema to be treated as primary keys.

None

Returns:

Type Description
k.K

A pykx.Table or pykx.KeyedTable matching the provided schema with zero rows.

Examples:

Create a simple pykx.Table with four columns of different types

>>> import pykx as kx
>>> qtab = kx.schema.builder({
        'col1' : kx.GUIDAtom,
        'col2': kx.TimeAtom,
        'col3': kx.BooleanAtom,
        'col4': kx.FloatAtom
        })
>>> qtab
pykx.Table(pykx.q('
col1 col2 col3 col4
-------------------
'))
>>> kx.q.meta(qtab)
pykx.KeyedTable(pykx.q('
c   | t f a
----| -----
col1| g
col2| t
col3| b
col4| f
'))

Create a pykx.KeyedTable with a single primary key.

>>> import pykx as kx
>>> qtab = kx.schema.builder({
        'col1': kx.TimestampAtom,
        'col2': kx.FloatAtom,
        'col3': kx.IntAtom},
        key = 'col1'
        )
>>> qtab
pykx.KeyedTable(pykx.q('
col1| col2 col3
----| ---------
'))
>>> kx.q.meta(qtab)
pykx.KeyedTable(pykx.q('
c   | t f a
----| -----
col1| p
col2| f
col3| i
'))

Create a pykx.KeyedTable with multiple primary keys.

>>> import pykx as kx
>>> qtab = kx.schema.builder({
        'col1': kx.TimestampAtom,
        'col2': kx.SymbolAtom,
        'col3': kx.IntAtom,
        'col4': kx.List},
        key = ['col1', 'col2']
        )
>>> qtab
pykx.KeyedTable(pykx.q('
col1 col2| col3 col4
---------| ---------
'))
>>> kx.q.meta(qtab)
pykx.KeyedTable(pykx.q('
c   | t f a
----| -----
col1| p
col2| s
col3| i
col4|
'))