Scratchpad Query APIs¶
This page describes how to use the Scratchpad query APIs to interact with the database from q or Python.
The Scratchpad query APIs are a set of APIs created for you to programmatically interact with the database, in the Scratchpad.
The following functions are supported in both q and Python:
Run Query APIs with PyKX¶
You can also run the q examples on this page from Python. Use PyKX and wrap each q call in kx.q, which evaluates the q expression and returns the result.
Example:
import pykx as kx
# Return the metadata of the databases
kx.q('.com_kx_edi.getMeta[]')
# Run an SQL query
kx.q('.com_kx_edi.sql "SELECT * FROM trade LIMIT 10"')
Python
Pass the q function call to kx.q as a string, followed by any additional function arguments in the required order.
Pass Python values into Query APIs¶
.com_kx_edi.sql takes a single query string. To use values from Python, wrap the call in a q lambda and pass the values to kx.q as arguments. PyKX converts the Python values to q objects, and the lambda builds the query string from them.
Example:
import pykx as kx
table = "trade";
sym = "AAPL";
limit = 10;
# The q lambda takes the table name, symbol and row limit as arguments
result = kx.q(
'{[t;s;n] .com_kx_edi.sql "SELECT * FROM ",string[t]," WHERE sym = \'",string[s],"\' LIMIT ",string n}',
table, sym, limit
);
# Convert the result to a pandas DataFrame
df = result.pd()
Arguments
PyKX converts Python strings to q symbols. Therefore, the lambda uses string to convert t and s to q strings before joining them into the query. PyKX supports a maximum of eight arguments per function call.
Get Meta¶
Use .com_kx_edi.getMeta to return the metadata of the databases.
.com_kx_edi.getMeta[]
This returns meta information on APIs defined in the DAPs and aggregation functions in the Aggs for all DAPs and Aggs in the system. Additionally, it returns information on the assemblies, schemas, metadata, and UDAs including their parameters and return values.
Assembly Names
If you are using the assembly names from the getMeta[] function for .com_kx_edi.qsql, ensure the "-qe" is removed before invoking .com_kx_edi.qsql.
Optional Parameters:
| Name | Description | Default |
|---|---|---|
| version | A dictionary with a version key specifying the metadata version to request. Must be 1, 2, or 3. |
1 |
Returns:
| Type | Description |
|---|---|
| any[] | Metadata of the databases |
Example:
.com_kx_edi.getMeta[]
// Request a specific version (1, 2, or 3)
.com_kx_edi.getMeta[enlist[`version]!enlist 3]
// List all UDAs
.com_kx_edi.getMeta[][1;`api]
// List all assemblies
.com_kx_edi.getMeta[][1;`assembly]
// List all tables
.com_kx_edi.getMeta[][1;`schema]
For more details, refer to the getMeta documentation.
SQL¶
Use .com_kx_edi.sql to run an SQL query.
.com_kx_edi.sql[query]
The SQL API allows queries to be distributed/aggregated across multiple DAPs.
Required Parameters:
| Name | Description | Default |
|---|---|---|
| Query | A string that expresses the query you wish to run | Required |
Returns:
| Type | Description |
|---|---|
| any | Result of sql query |
Example:
.com_kx_edi.sql["SELECT * FROM table1 LIMIT 10"]
Refer to the SQL documentation for more details.
QSQL¶
Use .com_kx_edi.qsql to assemble QSQL queries based on a q expression.
.com_kx_edi.qsql[args]
This is intended to run on a data access process and execute the command sent in from the client.
The args dictionary should be structured as follows:
| Dictionary Field | Type | Details |
|---|---|---|
| args.scope | {dict} | Dictionary argument of the qSQL query containing the assembly and the tier. |
| args.scope.affinity | {string} | An affinity level for how to route queries; this is hardcoded as "soft" for qSQL queries. |
| args.scope.assembly | {string or symbol} | The assembly / database name. |
| args.scope.tier | {string} | The tier to target ("rdb", "idb", or "hdb"). See Data Tiers. Omitting this parameter results in a "distributed" query, which queries across all database tiers |
| args.scope.dap | {string} | The DAP to target. See DAPs for details. |
| args.query | {string} | A q expression. |
| args.agg | {string} | The qSQL command to run on the aggregator process, acting on the list of results provided by the data access processes. Defaults to raze if not specified. It accepts: single named unary functions, e.g. "distinct"; compositions of named unary functions, e.g. "'[distinct;raze]"; unary lambdas where the input parameter is the list of results from the data access processes, e.g. "{distinct raze x}". |
We still support the deprecated method as the DAPs currently support this format:
| Dictionary Field | Type | Details |
|---|---|---|
| args.assembly | {string or symbol} | The assembly name - DEPRECATED; moved to args.scope.assembly. |
| args.target | {string} | The tier ("rdb", "idb", or "hdb") - DEPRECATED; moved to args.scope.tier. |
Required Parameters:
| Name | Description | Default |
|---|---|---|
| args | A dictionary that contains either: the scope (as defined above) and a q query, or the assembly, target, and q query | Required |
Returns:
| Type | Description |
|---|---|
| any | Result of q expression |
Example:
// List all assemblies
q).com_kx_edi.getMeta[][1;`assembly]
assembly kxname tbls
------------------------------------------------------
example-pkg-qe trades-db trades
// The "-qe" must be omitted from the assembly name
q).com_kx_edi.qsql[`scope`query!(`assembly`tier!("example-pkg";"rdb");"select max bid from trades")]
For more information, refer to the QSQL documentation.
Get Data¶
Use .com_kx_edi.getData to retrieve records from a database table.
.com_kx_edi.getData[args]
The getData API accepts the arguments described in the GetData API reference.
Deprecated name
.com_kx_edi.queryBuilder is deprecated and will be removed in a future release. It still works and forwards its arguments to .com_kx_edi.getData. Use .com_kx_edi.getData in new code.
The Get Data API is a generic data retrieval API that can be executed against an arbitrary database table. It is intended to run on a Data Access Process and retrieve records from a configured database table using a specified dictionary of parameters, as specified below.
Required Parameters:
| Name | Description | Default |
|---|---|---|
| args | The list of arguments can be found in the getData parameters | Required |
Returns:
| Type | Description |
|---|---|
| table | Result of table query |
Example:
.com_kx_edi.getData[`table`startTS`endTS!("table1";2024.01.01D; 2024.01.02D)]
UDA Query¶
Use .com_kx_edi.uda to query a UDA.
.com_kx_edi.uda[args;name]
Refer to these example UDAs for more details
The UDA API allows you to directly query a UDA against databases within a package.
Required Parameters:
| Name | Description | Default |
|---|---|---|
| args | A dictionary of UDA defined parameters. | Required |
| name | The name of the UDA including namespace in the form of "namespace/udaName" or `namespace.udaName , such as example/daAPI or `.example.daAPI |
Required |
Returns:
| Type | Description |
|---|---|
| any | Result of UDA |
Example:
args:`table`column`multiplier`startTS`endTS!(`table1;`column1;10;2024.11.06D;2024.11.21D);
.com_kx_edi.uda[args;"example/daAPI"]