All Projects → mechatroner → Rbql

mechatroner / Rbql

Licence: mit
🦜RBQL - Rainbow Query Language: SQL-like language for (not only) CSV file processing. Supports SQL queries with Python and JavaScript expressions

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Rbql

Sqlitebiter
A CLI tool to convert CSV / Excel / HTML / JSON / Jupyter Notebook / LDJSON / LTSV / Markdown / SQLite / SSV / TSV / Google-Sheets to a SQLite database file.
Stars: ✭ 601 (+409.32%)
Mutual labels:  csv, tsv, sqlite
Q
q - Run SQL directly on CSV or TSV files
Stars: ✭ 8,809 (+7365.25%)
Mutual labels:  csv, tsv, sql
Sqawk
Like Awk but with SQL and table joins
Stars: ✭ 263 (+122.88%)
Mutual labels:  csv, tsv, sql
Rainbow csv
🌈Rainbow CSV - Vim plugin: Highlight columns in CSV and TSV files and run queries in SQL-like language
Stars: ✭ 337 (+185.59%)
Mutual labels:  csv, tsv, sql
Pytablewriter
pytablewriter is a Python library to write a table in various formats: CSV / Elasticsearch / HTML / JavaScript / JSON / LaTeX / LDJSON / LTSV / Markdown / MediaWiki / NumPy / Excel / Pandas / Python / reStructuredText / SQLite / TOML / TSV.
Stars: ✭ 422 (+257.63%)
Mutual labels:  csv, tsv, sqlite
Visidata
A terminal spreadsheet multitool for discovering and arranging data
Stars: ✭ 4,606 (+3803.39%)
Mutual labels:  csv, tsv, sqlite
Sq
swiss-army knife for data
Stars: ✭ 275 (+133.05%)
Mutual labels:  csv, tsv, sql
Datasette
An open source multi-tool for exploring and publishing data
Stars: ✭ 5,640 (+4679.66%)
Mutual labels:  csv, sql, sqlite
Nano Sql
Universal database layer for the client, server & mobile devices. It's like Lego for databases.
Stars: ✭ 717 (+507.63%)
Mutual labels:  csv, sql, sqlite
Indonesia Postal And Area
Indonesia Postal Code & Area (BPS)
Stars: ✭ 64 (-45.76%)
Mutual labels:  csv, sqlite
Ebean
Ebean ORM
Stars: ✭ 1,172 (+893.22%)
Mutual labels:  sql, sqlite
Csvpack
csvpack library / gem - tools 'n' scripts for working with tabular data packages using comma-separated values (CSV) datafiles in text with meta info (that is, schema, datatypes, ..) in datapackage.json; download, read into and query CSV datafiles with your SQL database (e.g. SQLite, PostgreSQL, ...) of choice and much more
Stars: ✭ 71 (-39.83%)
Mutual labels:  csv, sqlite
Countries States Cities Database
🌍 World countries, states, regions, provinces, cities, towns in JSON, SQL, XML, PLIST, YAML, and CSV. All Countries, States, Cities with ISO2, ISO3, Country Code, Phone Code, Capital, Native Language, Timezones, Latitude, Longitude, Region, Subregion, Flag Emoji, and Currency. #countries #states #cities
Stars: ✭ 1,130 (+857.63%)
Mutual labels:  csv, sql
Sqlite orm
❤️ SQLite ORM light header only library for modern C++
Stars: ✭ 1,121 (+850%)
Mutual labels:  sql, sqlite
Andl
Andl is A New Database Language
Stars: ✭ 71 (-39.83%)
Mutual labels:  sql, sqlite
Sql.js
A javascript library to run SQLite on the web.
Stars: ✭ 9,594 (+8030.51%)
Mutual labels:  sql, sqlite
Xeus Sql
xeus-sql is a Jupyter kernel for general SQL implementations.
Stars: ✭ 85 (-27.97%)
Mutual labels:  sql, sqlite
Sql Kit
*️⃣ Build SQL queries in Swift. Extensible, protocol-based design that supports DQL, DML, and DDL.
Stars: ✭ 115 (-2.54%)
Mutual labels:  sql, sqlite
Tsv Utils
eBay's TSV Utilities: Command line tools for large, tabular data files. Filtering, statistics, sampling, joins and more.
Stars: ✭ 1,215 (+929.66%)
Mutual labels:  csv, tsv
Electrocrud
Database CRUD Application Built on Electron | MySQL, Postgres, SQLite
Stars: ✭ 1,267 (+973.73%)
Mutual labels:  sql, sqlite

RBQL: Rainbow Query Language

RBQL is an eval-based SQL-like query engine for (not only) CSV file processing. It provides SQL-like language that supports SELECT queries with Python or JavaScript expressions.
RBQL is best suited for data transformation, data cleaning, and analytical queries.
RBQL is distributed with CLI apps, text editor plugins, Python and JS libraries.

Official Site

Supported formats

Matrix of data formats that RBQL supports out of the box. R=Read, W=Write

Data Format Python JS
CSV, TSV, etc RW RW
Sqlite databases R
Native 2D arrays/lists RW RW

If you use RBQL as a library you can write implementation for a couple of classes to support additional formats.

Main Features

  • Use Python or JavaScript expressions inside SELECT, UPDATE, WHERE and ORDER BY statements
  • Supports multiple input formats
  • Result set of any query immediately becomes a first-class table on its own
  • No need to provide FROM statement in the query - input table is defined by the current context
  • Supports all main SQL keywords
  • Supports aggregate functions and GROUP BY queries
  • Supports user-defined functions (UDF)
  • Provides some new useful query modes which traditional SQL engines do not have
  • Lightweight, dependency-free, works out of the box

Limitations:

  • RBQL doesn't support nested queries, but they can be emulated with consecutive queries
  • Number of tables in all JOIN queries is always 2 (input table and join table), use consecutive queries to join 3 or more tables

Supported SQL Keywords (Keywords are case insensitive)

  • SELECT
  • UPDATE
  • WHERE
  • ORDER BY ... [ DESC | ASC ]
  • [ LEFT | INNER ] JOIN
  • DISTINCT
  • GROUP BY
  • TOP N
  • LIMIT N

All keywords have the same meaning as in SQL queries. You can check them online

RBQL variables

RBQL for CSV files provides the following variables which you can use in your queries:

  • a1, a2,..., a{N}
    Variable type: string
    Description: value of i-th field in the current record in input table
  • b1, b2,..., b{N}
    Variable type: string
    Description: value of i-th field in the current record in join table B
  • NR
    Variable type: integer
    Description: Record number (1-based)
  • NF
    Variable type: integer
    Description: Number of fields in the current record
  • a.name, b.Person_age, ... a.{Good_alphanumeric_column_name}
    Variable type: string
    Description: Value of the field referenced by it's "name". You can use this notation if the field in the first (header) CSV line has a "good" alphanumeric name
  • a["object id"], a['9.12341234'], b["%$ !! 10 20"] ... a["Arbitrary column name!"]
    Variable type: string
    Description: Value of the field referenced by it's "name". You can use this notation to reference fields by arbitrary values in the first (header) CSV line, even when there is no header at all

Notes:

  • You can mix all variable types in a single query, example: select a1, b2 JOIN /path/to/b.csv ON a['Item Id'] == b.Identifier WHERE NR > 1 and int(a.Weight) * 100 > int(b["weight of the item"])
  • Referencing fields by header names does not automatically skip the header line (you can use where NR > 1 trick to skip it)
  • If you want to use RBQL as a library for your own app you can define your own custom variables and do not have to support the above mentioned CSV-related variables.

UPDATE statement

UPDATE query produces a new table where original values are replaced according to the UPDATE expression, so it can also be considered a special type of SELECT query. This prevents accidental data loss from poorly written queries.
UPDATE SET is synonym to UPDATE, because in RBQL there is no need to specify the source table.

Aggregate functions and queries

RBQL supports the following aggregate functions, which can also be used with GROUP BY keyword:
COUNT, ARRAY_AGG, MIN, MAX, SUM, AVG, VARIANCE, MEDIAN

Limitation: aggregate functions inside Python (or JS) expressions are not supported. Although you can use expressions inside aggregate functions.
E.g. MAX(float(a1) / 1000) - valid; MAX(a1) / 1000 - invalid.
There is a workaround for the limitation above for ARRAY_AGG function which supports an optional parameter - a callback function that can do something with the aggregated array. Example:
select a2, ARRAY_AGG(a1, lambda v: sorted(v)[:5]) group by a2 - Python; select a2, ARRAY_AGG(a1, v => v.sort().slice(0, 5)) group by a2 - JS

JOIN statements

Join table B can be referenced either by its file path or by its name - an arbitrary string which the user should provide before executing the JOIN query.
RBQL supports STRICT LEFT JOIN which is like LEFT JOIN, but generates an error if any key in the left table "A" doesn't have exactly one matching key in the right table "B".
Limitation: JOIN statements can't contain Python/JS expressions and must have the following form: <JOIN_KEYWORD> (/path/to/table.tsv | table_name ) ON a... == b... [AND a... == b... [AND ... ]]

SELECT EXCEPT statement

SELECT EXCEPT can be used to select everything except specific columns. E.g. to select everything but columns 2 and 4, run: SELECT * EXCEPT a2, a4
Traditional SQL engines do not support this query mode.

UNNEST() operator

UNNEST(list) takes a list/array as an argument and repeats the output record multiple times - one time for each value from the list argument.
Example: SELECT a1, UNNEST(a2.split(';'))

LIKE() function

RBQL does not support LIKE operator, instead it provides "like()" function which can be used like this: SELECT * where like(a1, 'foo%bar')

User Defined Functions (UDF)

RBQL supports User Defined Functions
You can define custom functions and/or import libraries in two special files:

  • ~/.rbql_init_source.py - for Python
  • ~/.rbql_init_source.js - for JavaScript

Examples of RBQL queries

With Python expressions

  • select top 100 a1, int(a2) * 10, len(a4) where a1 == "Buy" order by int(a2) desc
  • select * order by random.random() where NR > 1 - skip header record and random sort
  • select len(a.vehicle_price) / 10, a2 where NR > 1 and a['Vehicle type'] in ["car", "plane", "boat"] limit 20 - referencing columns by names from header record, skipping the header and using Python's "in" to emulate SQL's "in"
  • update set a3 = 'NPC' where a3.find('Non-playable character') != -1
  • select NR, * - enumerate records, NR is 1-based
  • select * where re.match(".*ab.*", a1) is not None - select entries where first column has "ab" pattern
  • select a1, b1, b2 inner join ./countries.txt on a2 == b1 order by a1, a3 - example of join query
  • select MAX(a1), MIN(a1) where a.Name != 'John' group by a2, a3 - example of aggregate query
  • select *a1.split(':') - Using Python3 unpack operator to split one column into many. Do not try this with other SQL engines!

With JavaScript expressions

  • select top 100 a1, a2 * 10, a4.length where a1 == "Buy" order by parseInt(a2) desc
  • select * order by Math.random() where NR > 1 - skip header record and random sort
  • select top 20 a.vehicle_price.length / 10, a2 where NR > 1 && ["car", "plane", "boat"].indexOf(a['Vehicle type']) > -1 limit 20 - referencing columns by names from header record and skipping the header
  • update set a3 = 'NPC' where a3.indexOf('Non-playable character') != -1
  • select NR, * - enumerate records, NR is 1-based
  • select a1, b1, b2 inner join ./countries.txt on a2 == b1 order by a1, a3 - example of join query
  • select MAX(a1), MIN(a1) where a.Name != 'John' group by a2, a3 - example of aggregate query
  • select ...a1.split(':') - Using JS "destructuring assignment" syntax to split one column into many. Do not try this with other SQL engines!

RBQL design principles and architecture

RBQL core idea is based on dynamic code generation and execution with exec and eval functions. Here are the main steps that RBQL engine performs when processing a query:

  1. Shallow parsing: split the query into logical expressions such as "SELECT", "WHERE", "ORDER BY", etc.
  2. Embed the expression segments into the main loop template code
  3. Execute the hydrated loop code

Here you can find a very basic working script (only 15 lines of Python code) which implements this idea: mini_rbql.py

The diagram below gives an overview of the main RBQL components and data flow: RBQL Diagram

Advantages of RBQL over traditional SQL engines

  • Provides power and flexibility of general purpose Python and JS languages in relational expressions (including regexp, math, file system, json, xml, random and many other libraries that these languages provide)
  • Can work with different data sources including CSV files, sqlite tables, native 2D arrays/lists (traditional SQL engines are usually tightly coupled with their databases)
  • Result set of any query immediately becomes a first-class table on its own
  • Supports both TOP and LIMIT keywords
  • Provides additional NR (record number) variable which is especially useful for input sources where record order is well defined (such as CSV files)
  • Supports input tables with inconsistent number of fields per record
  • Allows to generate result sets with variable number of fields per record e.g. by using split() function and unpack operator (Python) / destructuring assignment (JS)
  • UPDATE is a special case of SELECT query - this prevents accidental data loss
  • No need to use FROM statement - the table name is defined by the context. This improves query typing speed and allows immediate autocomplete for variables inside SELECT statement (in traditional SQL engines autocomplete will not work until you write FROM statement, which goes after SELECT statement)
  • SELECT, WHERE, ORDER BY, and other statements can be rearranged in any way you like
  • Supports EXCEPT statement
  • Provides a fully-functional client-side browser demo application
  • Almost nonexistent entry barrier both for SQL users and JS/Python users
  • Integration with popular text editors (VSCode, Vim, Sublime Text, Atom)
  • Small, maintainable, dependency-free, eco-friendly and hackable code base: RBQL engine fits into a single file with less than 2000 LOC

Disadvantages of RBQL compared to traditional SQL engines

  • Not suitable for transactional workload
  • RBQL doesn't support nested queries, but they can be emulated with consecutive queries
  • Number of tables in all JOIN queries is always 2 (input table and join table), use consecutive queries to join 3 or more tables
  • Does not support HAVING statement

FAQ

How do I skip header record in CSV files?

You can use the following trick: add ... where NR > 1 ... to your query

And if you are doing math operation you can modify your query like this, example:
select int(a3) * 1000, a2 -> select int(a3) * 1000 if NR > 1 else a3, a2

References

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].