Q (programming language from Kx Systems)
Encyclopedia
Q is a proprietary array processing language developed by Arthur Whitney and commercialized by Kx Systems. The language serves as the query language for kdb+, a disk based and in-memory, column-based
Column-oriented DBMS
A column-oriented DBMS is a database management system that stores its content by column rather than by row. This has advantages for data warehouses and library catalogues where aggregates are computed over large numbers of similar data items....

 database
Database
A database is an organized collection of data for one or more purposes, usually in digital form. The data are typically organized to model relevant aspects of reality , in a way that supports processes requiring this information...

. kdb+ is based upon K
K (programming language)
K is a proprietary array processing language developed by Arthur Whitney and commercialized by Kx Systems. The language serves as the foundation for kdb, an in-memory, column-based database, and other related financial products. The language, originally developed in 1993, is a variant of APL and...

, a terse variant of APL. Q is a thin wrapper around K, providing a more readable, English-like interface.

Overview

The fundamental building blocks of Q are atoms, lists and functions. Atoms are scalars
Scalar (computing)
In computing, a scalar variable or field is one that can hold only one value at a time; as opposed to composite variables like array, list, hash, record, etc. In some contexts, a scalar value may be understood to be numeric. A scalar data type is the type of a scalar variable...

 and include numeric, character and temporal data types. Lists are ordered collections of atoms (or other lists) upon which the higher level data structures dictionaries
Associative array
In computer science, an associative array is an abstract data type composed of a collection of pairs, such that each possible key appears at most once in the collection....

and tables
Table (database)
In relational databases and flat file databases, a table is a set of data elements that is organized using a model of vertical columns and horizontal rows. A table has a specified number of columns, but can have any number of rows...

are internally constructed. A dictionary is a map of a list of keys to a list of values. A table is a transposed dictionary of symbol keys and equal length lists (columns) as values. A keyed table, analogous to a table with a primary key
Unique key
In relational database design, a unique key can uniquely identify each row in a table, and is closely related to the Superkey concept. A unique key comprises a single column or a set of columns. No two distinct rows in a table can have the same value in those columns if NULL values are not used...

 placed on it, is a dictionary where the keys and values are arranged as two tables.

The following code demonstrates the relationships of the data structures (expressions to be evaluated appear prefixed with the "q)" prompt, with the output of the evaluation shown beneath):


q)`john / an atom of type symbol
`john
q)50 / an atom of type integer
50

q)`john`jack / a list of symbols
`john`jack
q)50 60 / a list of integers
50 60

q)`john`jack!50 60 / a list of symbols and a list of integers combined to form a dictionary
john| 50
jack| 60

q)`name`age!(`john`jack;50 60) / an arrangement known as a column dictionary
name| john jack
age | 50 60

q)flip `name`age!(`john`jack;50 60) / when transposed via the function "flip", the column dictionary becomes a table
name age
--------
john 50
jack 60

q)(flip (enlist `name)!enlist `john`jack)!flip (enlist `age)!enlist 50 60 / two equal length tables combined as a dictionary become a keyed table
name| age
----| ---
john| 50
jack| 60



These entities are manipulated via functions, which include the built-in functions that come with Q (which are actually defined as K
K (programming language)
K is a proprietary array processing language developed by Arthur Whitney and commercialized by Kx Systems. The language serves as the foundation for kdb, an in-memory, column-based database, and other related financial products. The language, originally developed in 1993, is a variant of APL and...

 macros) and user-defined functions. Functions are themselves a data type, and can be placed into lists, dictionaries and tables, or passed into other functions as parameters.

Examples

Like K, Q is interpreted and the result of the evaluation of an expression is immediately displayed, unless terminated with a semi-colon. The Hello world program is therefore trivial:


q)"Hello world!"
"Hello world!"


The following expression sorts a list of strings stored in the variable x descending by their lengths:


x@idesc count each x


The expression is evaluated from right to left as follows:
  1. "count each x" returns the length of each word in the list x.
  2. "idesc" returns the indices that would sort a list of values in descending order.
  3. @ use the integer values on the right to index into the original list of strings.


The factorial function can be implemented directly in Q as


{prd 1+til x}


or recursively as


{$[x=0;1;x*.z.s[x-1]]}


Note that in both cases the function implicitly takes a single argument called x - in general it is possible to use up to three implicit arguments, named x, y and z, or to give arguments local variable bindings explicitly.

In the direct implementation, the expression "til x" enumerates the integers from 0 to x-1, "1+" adds 1 to every element of the list and "prd" returns the product of the list.

In the recursive implementation, the syntax "$[condition; expr1; expr2]" is a ternary conditional - if the condition is true then expr1 is returned; otherwise expr2 is returned. The expression ".z.s" is loosely equivalent to 'this' in Java or 'self' in Python - it is a reference to the containing object, and enables functions in q to call themselves.

A function to determine if a number is prime can be written as:


{min x mod/:2_til x}


The function is evaluated from right to left:
  1. "til x" enumerate the positive integers less than x.
  2. "2_" drops the first two elements of the enumeration (0 and 1).
  3. "x mod/:" performs modulo division between the original integer and each value in the truncated list.
  4. "min" find the minimum value of the list of modulo result.


The Q programming language contains its own table query syntax called q-sql, which resembles traditional SQL
SQL
SQL is a programming language designed for managing data in relational database management systems ....

 but has important differences, mainly due to the fact that the underlying tables are column, rather than row, oriented.


q)show t:([] name:`john`jack`jill`jane; age: 50 60 50 20) / define a simple table and assign to "t"
name age
--------
john 50
jack 60
jill 50
jane 20

q)select from t where name like "ja*",age>50
name age
--------
jack 60

q)select rows:count i by age from t
age| rows
---| ----
20 | 1
50 | 2
60 | 1

See also

  • APL - the first array programming language
  • J
    J (programming language)
    The J programming language, developed in the early 1990s by Kenneth E. Iverson and Roger Hui, is a synthesis of APL and the FP and FL function-level languages created by John Backus....

     - another APL-inspired language
  • K
    K (programming language)
    K is a proprietary array processing language developed by Arthur Whitney and commercialized by Kx Systems. The language serves as the foundation for kdb, an in-memory, column-based database, and other related financial products. The language, originally developed in 1993, is a variant of APL and...

    - the language Q is built upon

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK