Q Language – Type Casting

Q Language – Type Casting ”; Previous Next It is often required to change the data type of some data from one type to another. The standard casting function is the “$” dyadic operator. Three approaches are used to cast from one type to another (except for string) − Specify desired data type by its symbol name Specify desired data type by its character Specify desired data type by it short value. Casting Integers to Floats In the following example of casting integers to floats, all the three different ways of casting are equivalent − q)a:9 18 27 q)$[`float;a] / Specify desired data type by its symbol name, 1st way 9 18 27f q)$[“f”;a] / Specify desired data type by its character, 2nd way 9 18 27f q)$[9h;a] / Specify desired data type by its short value, 3rd way 9 18 27f Check if all the three operations are equivalent, q)($[`float;a]~$[“f”;a]) and ($[`float;a] ~ $[9h;a]) 1b Casting Strings to Symbols Casting string to symbols and vice versa works a bit differently. Let’s check it with an example − q)b: (“Hello”;”World”;”HelloWorld”) / define a list of strings q)b “Hello” “World” “HelloWorld” q)c: `$b / this is how to cast strings to symbols q)c / Now c is a list of symbols `Hello`World`HelloWorld Attempting to cast strings to symbols using the keyed words `symbol or 11h will fail with the type error − q)b “Hello” “World” “HelloWorld” q)`symbol$b ”type q)11h$b ”type Casting Strings to Non-Symbols Casting strings to a data type other than symbol is accomplished as follows − q)b:900 / b contain single atomic integer q)c:string b / convert this integer atom to string “900” q)c “900” q)`int $ c / converting string to integer will return the / ASCII equivalent of the character “9”, “0” and / “0” to produce the list of integer 57, 48 and / 48. 57 48 48i q)6h $ c / Same as above 57 48 48i q)”i” $ c / Same a above 57 48 48i q)”I” $ c 900i So to cast an entire string (the list of characters) to a single atom of data type x requires us to specify the upper case letter representing data type x as the first argument to the $ operator. If you specify the data type of x in any other way, it result in the cast being applied to each character of the string. Print Page Previous Next Advertisements ”;

KDB+ – Home

KDB+ Tutorial PDF Version Quick Guide Resources Job Search Discussion Kdb+ is a high-performance column-oriented database from Kx Systems Inc. kdb+ is designed to capture, analyze, compare, and store data − all at high speeds and on high volumes of data. The tutorial starts off with a basic introduction of Kdb+ followed by its architecture, installation, and a basic-to-advanced coverage of q programming language. Kx® and kdb+ are registered trademarks of Kx Systems, Inc Audience This reference has been prepared for beginners to help them understand the KDB+ database and write smart queries in q languages for KDB+. Prerequisites As we are going to start from scratch, you can set off without any preparation. However, it would definitely help if you have a working knowledge of any database or a programming language. Print Page Previous Next Advertisements ”;

KDB+ – Architecture

KDB+ Architecture ”; Previous Next Kdb+ is a high-performance, high-volume database designed from the outset to handle tremendous volumes of data. It is fully 64-bit, and has built-in multi-core processing and multi-threading. The same architecture is used for real-time and historical data. The database incorporates its own powerful query language, q, so analytics can be run directly on the data. kdb+tick is an architecture which allows the capture, processing, and querying of real-time and historical data. Kdb+/ tick Architecture The following illustration provides a generalized outline of a typical Kdb+/tick architecture, followed by a brief explanation of the various components and the through-flow of data. The Data Feeds are a time series data that are mostly provided by the data feed providers like Reuters, Bloomberg or directly from exchanges. To get the relevant data, the data from the data feed is parsed by the feed handler. Once the data is parsed by the feed handler, it goes to the ticker-plant. To recover data from any failure, the ticker-plant first updates/stores the new data to the log file and then updates its own tables. After updating the internal tables and the log files, the on-time loop data is continuously sent/published to the real-time database and all the chained subscribers who requested for data. At the end of a business day, the log file is deleted, a new one created and the real-time database is saved onto the historical database. Once all the data is saved onto the historical database, the real-time database purges its tables. Components of Kdb+ Tick Architecture Data Feeds Data Feeds can be any market or other time series data. Consider data feeds as the raw input to the feed-handler. Feeds can be directly from the exchange (live-streaming data), from the news/data providers like Thomson-Reuters, Bloomberg, or any other external agencies. Feed Handler A feed handler converts the data stream into a format suitable for writing to kdb+. It is connected to the data feed and it retrieves and converts the data from the feed-specific format into a Kdb+ message which is published to the ticker-plant process. Generally a feed handler is used to perform the following operations − Capture data according to a set of rules. Translate (/enrich) that data from one format to another. Catch the most recent values. Ticker Plant Ticker Plant is the most important component of KDB+ architecture. It is the ticker plant with which the real-time database or directly subscribers (clients) are connected to access the financial data. It operates in publish and subscribe mechanism. Once you obtain a subscription (license), a tick (routinely) publication from the publisher (ticker plant) is defined. It performs the following operations − Receives the data from the feed handler. Immediately after the ticker plant receives the data, it stores a copy as a log file and updates it once the ticker plant gets any update so that in case of any failure, we should not have any data loss. The clients (real-time subscriber) can directly subscribe to the ticker-plant. At the end of each business day, i.e., once the real-time database receives the last message, it stores all of today’s data onto the historical database and pushes the same to all the subscribers who have subscribed for today’s data. Then it resets all its tables. The log file is also deleted once the data is stored in the historical database or other directly linked subscriber to real time database (rtdb). As a result, the ticker-plant, the real-time database, and the historical database are operational on a 24/7 basis. Since the ticker-plant is a Kdb+ application, its tables can be queried using q like any other Kdb+ database. All ticker-plant clients should only have access to the database as subscribers. Real-Time Database A real-time database (rdb) stores today’s data. It is directly connected to the ticker plant. Typically it would be stored in memory during market hours (a day) and written out to the historical database (hdb) at the end of day. As the data (rdb data) is stored in memory, processing is extremely fast. As kdb+ recommends to have a RAM size that is four or more times the expected size of data per day, the query that runs on rdb is very fast and provides superior performance. Since a real-time database contains only today’s data, the date column (parameter) is not required. For example, we can have rdb queries like, select from trade where sym = `ibm OR select from trade where sym = `ibm, price > 100 Historical Database If we have to calculate the estimates of a company, we need to have its historical data available. A historical database (hdb) holds data of transactions done in the past. Each new day’s record would be added to the hdb at the end of day. Large tables in the hdb are either stored splayed (each column is stored in its own file) or they are stored partitioned by temporal data. Also some very large databases may be further partitioned using par.txt (file). These storage strategies (splayed, partitioned, etc.) are efficient while searching or accessing the data from a large table. A historical database can also be used for internal and external reporting purposes, i.e., for analytics. For example, suppose we want to get the company trades of IBM for a particular day from the trade (or any) table name, we need to write a query as follows − thisday: 2014.10.12 select from trade where date = thisday, sym =`ibm Note − We will write all such queries once we get some overview of the q language. Print Page Previous Next Advertisements ”;

Q Language – Functional Queries

Q Language – Functional Queries ”; Previous Next Functional (Dynamic) queries allow specifying column names as symbols to typical q-sql select/exec/delete columns. It comes very handy when we want to specify column names dynamically. The functional forms are − ?[t;c;b;a] / for select ![t;c;b;a] / for update where t is a table; a is a dictionary of aggregates; b the by-phrase; and c is a list of constraints. Note − All q entities in a, b, and c must be referenced by name, meaning as symbols containing the entity names. The syntactic forms of select and update are parsed into their equivalent functional forms by the q interpreter, so there is no performance difference between the two forms. Functional select The following code block shows how to use functional select − q)t:([]n:`ibm`msft`samsung`apple;p:40 38 45 54) q)t n p ——————- ibm 40 msft 38 samsung 45 apple 54 q)select m:max p,s:sum p by name:n from t where p>36, n in `ibm`msft`apple name | m s —— | ——— apple | 54 54 ibm | 40 40 msft | 38 38 Example 1 Let’s start with the easiest case, the functional version of “select from t” will look like − q)?[t;();0b;()] / select from t n p —————– ibm 40 msft 38 samsung 45 apple 54 Example 2 In the following example, we use the enlist function to create singletons to ensure that appropriate entities are lists. q)wherecon: enlist (>;`p;40) q)?[`t;wherecon;0b;()] / select from t where p > 40 n p —————- samsung 45 apple 54 Example 3 q)groupby: enlist[`p] ! enlist `p q)selcols: enlist [`n]!enlist `n q)?[ `t;(); groupby;selcols] / select n by p from t p | n —– | ——- 38 | msft 40 | ibm 45 | samsung 54 | apple Functional Exec The functional form of exec is a simplified form of select. q)?[t;();();`n] / exec n from t (functional form of exec) `ibm`msft`samsung`apple q)?[t;();`n;`p] / exec p by n from t (functional exec) apple | 54 ibm | 40 msft | 38 samsung | 45 Functional Update The functional form of update is completely analogous to that of select. In the following example, the use of enlist is to create singletons, to ensure that input entities are lists. q)c:enlist (>;`p;0) q)b: (enlist `n)!enlist `n q)a: (enlist `p) ! enlist (max;`p) q)![t;c;b;a] n p ————- ibm 40 msft 38 samsung 45 apple 54 Functional delete Functional delete is a simplified form of functional update. Its syntax is as follows − ![t;c;0b;a] / t is a table, c is a list of where constraints, a is a / list of column names Let us now take an example to show how functional delete work − q)![t; enlist (=;`p; 40); 0b;`symbol$()] / delete from t where p = 40 n p ————— msft 38 samsung 45 apple 54 Print Page Previous Next Advertisements ”;

KDB+ – Overview

KDB+ Overview ”; Previous Next This is a complete quide to kdb+ from kx systems, aimed primarily at those learning independently. kdb+, introduced in 2003, is the new generation of the kdb database which is designed to capture, analyze, compare, and store data. A kdb+ system contains the following two components − KDB+ − the database (k database plus) Q − the programming language for working with kdb+ Both kdb+ and q are written in k programming language (same as q but less readable). Background Kdb+/q originated as an obscure academic language but over the years, it has gradually improved its user friendliness. APL (1964, A Programming Language) A+ (1988, modified APL by Arthur Whitney) K (1993, crisp version of A+, developed by A. Whitney) Kdb (1998, in-memory column-based db) Kdb+/q (2003, q language – more readable version of k) Why and Where to Use KDB+ Why? − If you need a single solution for real-time data with analytics, then you should consider kdb+. Kdb+ stores database as ordinary native files, so it does not have any special needs regarding hardware and storage architecture. It is worth pointing out that the database is just a set of files, so your administrative work won’t be difficult. Where to use KDB+? − It’s easy to count which investment banks are NOT using kdb+ as most of them are using currently or planning to switch from conventional databases to kdb+. As the volume of data is increasing day by day, we need a system that can handle huge volumes of data. KDB+ fulfills this requirement. KDB+ not only stores an enormous amount of data but also analyzes it in real time. Getting Started With this much of background, let us now set forth and learn how to set up an environment for KDB+. We will start with how to download and install KDB+. Downloading & Installing KDB+ You can get the free 32-bit version of KDB+, with all the functionality of the 64- bit version from http://kx.com/software-download.php Agree to the license agreement, select the operating system (available for all major operating system). For Windows operating system, the latest version is 3.2. Download the latest version. Once you unzip it, you will get the folder name “windows” and inside the windows folder, you will get another folder “q”. Copy the entire q folder onto your c:/ drive. Open the Run terminal, type the location where you store the q folder; it will be like “c:/q/w32/q.exe”. Once you hit Enter, you will get a new console as follows − On the first line, you can see the version number which is 3.2 and the release date as 2015.03.05 Directory Layout The trial/free version is generally installed in directories, For linux/Mac − ~/q / main q directory (under the user’s home) ~/q/l32 / location of linux 32-bit executable ~/q/m32 / Location of mac 32-bit executable For Windows − c:/q / Main q directory c:/q/w32/ / Location of windows 32-bit executable Example Files − Once you download kdb+, the directory structure in the Windows platform would appear as follows − In the above directory structure, trade.q and sp.q are the example files which we can use as a reference point. Print Page Previous Next Advertisements ”;