ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

rainbow_csv

by mechatroner ST3

:rainbow: Sublime Text Package: Highlight columns in CSV and TSV files and run queries in SQL-like language

Labels csv, tsv, highlight

Details

Installs

  • Total 9K
  • Win 4K
  • OS X 3K
  • Linux 2K
Oct 22 Oct 21 Oct 20 Oct 19 Oct 18 Oct 17 Oct 16 Oct 15 Oct 14 Oct 13 Oct 12 Oct 11 Oct 10 Oct 9 Oct 8 Oct 7 Oct 6 Oct 5 Oct 4 Oct 3 Oct 2 Oct 1 Sep 30 Sep 29 Sep 28 Sep 27 Sep 26 Sep 25 Sep 24 Sep 23 Sep 22 Sep 21 Sep 20 Sep 19 Sep 18 Sep 17 Sep 16 Sep 15 Sep 14 Sep 13 Sep 12 Sep 11 Sep 10 Sep 9 Sep 8 Sep 7
Windows 0 10 5 2 13 18 8 11 10 4 3 17 12 12 7 8 5 4 9 16 5 9 12 3 5 11 11 11 13 9 3 5 10 13 12 9 14 5 1 8 17 9 13 8 4 6
OS X 1 14 7 2 11 12 10 5 11 1 0 18 7 9 8 4 3 1 8 10 11 11 7 3 4 7 10 9 7 10 1 1 9 10 8 14 8 6 2 6 7 20 9 10 1 3
Linux 2 4 5 5 5 2 5 8 3 2 2 3 5 2 7 7 1 4 2 2 7 2 4 0 5 6 6 4 7 4 1 4 2 5 4 5 3 1 2 4 4 5 1 2 1 2

Readme

Source
raw.​githubusercontent.​com

logo

Rainbow CSV

Main features

  • Highlight columns in *.csv, *.tsv and other separated files in different rainbow colors.
  • Provide info about columns on mouse hover.
  • Check consistency of CSV files (CSVLint)
  • Execute SQL-like RBQL queries.

screenshot

Usage

Rainbow CSV has content-based csv/tsv autodetection mechanism. This means that package will analyze plain text files even if they do not have “.csv” or “.tsv” extension.

Rainbow highlighting can also be manually enabled from Sublime context menu (see the demo gif below):
1. Select a character that you want to use as a delimiter with mouse. Delimiter can be any non-alphanumeric printable ASCII symbol, e.g. semicolon
2. Right mouse click: context menu -> Rainbow CSV -> Enable …

You can also disable rainbow highlighting and go back to the original file highlighting using the same context menu.
This feature can be used to temporary rainbow-highlight even non-table files.

Manual Rainbow Enabling/Disabling demo gif:
demo

Rainbow CSV also lets you execute SQL-like queries in RBQL language, see the demo gif below:
demo gif

To Run RBQL query press F5 or select “Rainbow CSV” -> “Run RBQL query” option from the file context menu.

Difference between “Standard” and “Simple” dialects

When manually enabling rainbow highlighting from the context menu, you have to choose between “Standard” and “Simple” dialect.
* Standard dialect will treat quoted separator as a single field. E.g. line sell,"15,128",23% will be treated as 3 columns, because the second comma is quoted. This dialect is used by Excel.
* Simple dialect doesn't care about double quotes: the number of highlighted fields is always N + 1 where N is the number of separators.

Key mappings

Key Action
F5 Start query editing for the current csv file

Configuration

To adjust plugin configuration:
1. Go to “Preferences” -> “Package Settings” -> “Rainbow CSV” -> “Settings”.
2. On the right side change the settings like you'd like.

Configuration parameters

“enable_rainbow_csv_autodetect”

Enable content-based separator autodetection. Files with “.csv” and “.tsv” extensions are always highlighted no matter what is the value of this option.

“rainbow_csv_autodetect_dialects”

List of CSV dialects to autodetect.
If “enable_rainbow_csv_autodetect” is set to false this setting is ignored

“rainbow_csv_max_file_size_bytes”

Disable Rainbow CSV for files bigger than the specified size. This can be helpful to prevent poor performance and crashes with very large files.
Manual separator selection will override this setting for the current file.
E.g. to disable on files larger than 100 MB, set "rainbow_csv_max_file_size_bytes": 100000000

“auto_adjust_rainbow_colors”

Auto adjust rainbow colors for Packages/User/RainbowCSV.sublime-color-scheme
Rainbow CSV will auto-generate color theme with high-contrast colors to make CSV columns more distinguishable.
You can disable this setting and manually customize Rainbow CSV color scheme at Packages/User/RainbowCSV.sublime-color-scheme
Do not customize Packages/User/RainbowCSV.sublime-color-scheme without disabling the setting, it will be rewritten by the plugin

“rbql_backend_language”

RBQL backend language.
Supported values: “Python”, “JS”
In order to use RBQL with JavaScript (JS) you need to have Node JS installed and added to your system path.

“rbql_output_format”

Format of RBQL result set tables.
Supported values: “tsv”, “csv”, “input”
* input: same format as the input table * tsv: tab separated values. * csv: is Excel-compatible and allows quoted commas.

Example: to always use “tsv” as output format add this line to your settings file: "rbql_output_format": "tsv",

“rbql_encoding”

RBQL encoding for files and queries.
Supported values: “latin-1”, “utf-8”

References

  • This Sublime Text plugin is an adaptation of Vim's rainbow_csv plugin

RBQL (Rainbow Query Language) Description

RBQL is a technology which provides SQL-like language that supports SELECT and UPDATE queries with Python or JavaScript expressions.
RBQL is distributed with CLI apps, text editor plugins, Python and JS libraries and can work in web browsers.

Official Site

Main Features

  • Use Python or JavaScript expressions inside SELECT, UPDATE, WHERE and ORDER BY statements
  • Result set of any query immediately becomes a first-class table on it's own
  • Supports input tables with inconsistent number of fields per record
  • Output records appear in the same order as in input unless ORDER BY is provided
  • Each record has a unique NR (line number) identifier
  • Supports all main SQL keywords
  • Supports aggregate functions and GROUP BY queries
  • Provides some new useful query modes which traditional SQL engines do not have
  • Supports both TOP and LIMIT keywords
  • Supports user-defined functions (UDF)
  • Works out of the box, no external dependencies

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

Special variables

Variable Name Variable Type Variable Description
a1, a2,…, a{N} string Value of i-th column
b1, b2,…, b{N} string Value of i-th column in join table B
NR integer Line number (1-based)
NF integer Number of fields in line

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()

Limitations

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

JOIN statements

Join table B can be referenced either by it's file path or by it's name - an arbitary string which 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 left table “A” doesn't have exactly one matching key in the right table “B”.

Limitations

  • JOIN statements must have the following form: (/path/to/table.tsv | table_name ) ON ai == bj

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.

SELECT DISTINCT COUNT statement

RBQL supports DISTINCT COUNT keyword which is like DISTINCT, but adds a new column to the “distinct” result set: number of occurrences of the entry, similar to uniq -c unix command.
SELECT DISTINCT COUNT a1 is equivalent to SELECT a1, COUNT(a1) GROUP BY a1

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(';'))

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)
  • select * order by random.random() - random sort, this is an equivalent of bash command sort -R
  • select top 20 len(a1) / 10, a2 where a2 in ["car", "plane", "boat"] - use Python's “in” to emulate SQL's “in”
  • select len(a1) / 10, a2 where a2 in ["car", "plane", "boat"] limit 20
  • update set a3 = 'US' where a3.find('of America') != -1
  • select * where NR <= 10 - this is an equivalent of bash command “head -n 10”, NR is 1-based')
  • select a1, a4 - this is an equivalent of bash command “cut -f 1,4”
  • select * order by int(a2) desc - this is an equivalent of bash command “sort -k2,2 -r -n”
  • select NR, * - enumerate lines, 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 - an example of join query
  • select distinct count len(a1) where a2 != 'US'
  • select MAX(a1), MIN(a1) where a2 != 'US' group by a2, a3

With JavaScript expressions

  • select top 100 a1, a2 * 10, a4.length where a1 == "Buy" order by parseInt(a2)
  • select * order by Math.random() - random sort, this is an equivalent of bash command sort -R
  • select top 20 a1.length / 10, a2 where ["car", "plane", "boat"].indexOf(a2) > -1
  • select a1.length / 10, a2 where ["car", "plane", "boat"].indexOf(a2) > -1 limit 20
  • update set a3 = 'US' where a3.indexOf('of America') != -1
  • select * where NR <= 10 - this is an equivalent of bash command “head -n 10”, NR is 1-based')
  • select a1, a4 - this is an equivalent of bash command “cut -f 1,4”
  • select * order by parseInt(a2) desc - this is an equivalent of bash command “sort -k2,2 -r -n”
  • select NR, * - enumerate lines, NR is 1-based
  • select a1, b1, b2 inner join ./countries.txt on a2 == b1 order by a1, a3 - an example of join query
  • select distinct count a1.length where a2 != 'US'
  • select MAX(a1), MIN(a1) where a2 != 'US' group by a2, a3

FAQ

How does RBQL work?

RBQL parses SQL-like user query, creates a new python or javascript worker module, then imports and executes it.

Explanation of simplified Python version of RBQL algorithm by example. 1. User enters the following query, which is stored as a string Q:

SELECT a3, int(a4) + 100, len(a2) WHERE a1 != 'SELL'
  1. RBQL replaces all a{i} substrings in the query string Q with a[{i - 1}] substrings. The result is the following string:
Q = "SELECT a[2], int(a[3]) + 100, len(a[1]) WHERE a[0] != 'SELL'"
  1. RBQL searches for “SELECT” and “WHERE” keywords in the query string Q, throws the keywords away, and puts everything after these keywords into two variables S - select part and W - where part, so we will get:
S = "a[2], int(a[3]) + 100, len(a[1])"
    W = "a[0] != 'SELL'"
  1. RBQL has static template script which looks like this:
for line in sys.stdin:
        a = line.rstrip('\n').split('\t')
        if %%%W_Expression%%%:
            out_fields = [%%%S_Expression%%%]
            print '\t'.join([str(v) for v in out_fields])
  1. RBQL replaces %%%W_Expression%%% with W and %%%S_Expression%%% with S so we get the following script:
for line in sys.stdin:
        a = line.rstrip('\n').split('\t')
        if a[0] != 'SELL':
            out_fields = [a[2], int(a[3]) + 100, len(a[1])]
            print '\t'.join([str(v) for v in out_fields])
  1. RBQL runs the patched script against user's data file:
./tmp_script.py < data.tsv > result.tsv

Result set of the original query (SELECT a3, int(a4) + 100, len(a2) WHERE a1 != 'SELL') is in the “result.tsv” file.
Adding support of TOP/LIMIT keywords is trivial and to support “ORDER BY” we can introduce an intermediate array.

Is this technology reliable?

It should be: RBQL scripts have only 1000 - 2000 lines combined (depending on how you count them) and there are no external dependencies. There is no complex logic, even query parsing functions are very simple. If something goes wrong RBQL will show an error instead of producing incorrect output, also there are currently 5 different warning types.

References