Skip to content

Instantly share code, notes, and snippets.

@brito
brito / tally.gsheet.js
Created August 3, 2021 23:06
Count top values from a range of values (mode at the top). Useful to detect data anomalies, patterns and degree of variability in values (how different things are, how many nulls, etc)
/* Tally v 1.0 */
function tally (range, limit = 10){
let total = range.length,
values = {}
for (let [k, v] of range) {
k = ('' + k).replace(/\n[\s\S]*/, '…')
values[k] = values[k] ? values[k] + 1 : 1
}
return Object.entries(values)
@brito
brito / replaces.js
Created April 2, 2021 00:06
Transform a string using multiple regex
/*
Transform a string using multiple regex
@customfunction
*/
function replaces(operators,operands) {
return operators[0].map ?
operators.reduce((a,b) => replaces(b,a), operands)
: operands.replace(new RegExp(operators[0]), operators[1])
}
@brito
brito / aftoanastochasmos.sql
Created May 23, 2020 02:16
Kitchen sink analysis of relations in schemas
/* __ _ _ _ __
__ _ / _| |_ ___ __ _ _ __ __ _ ___| |_ ___ ___| |__ __ _ ___ _ __ ___ /_/ ___
/ _` | |_| __/ _ \ / _` | '_ \ / _` / __| __/ _ \ / __| '_ \ / _` / __| '_ ` _ \ / _ \/ __|
| (_| | _| || (_) | (_| | | | | (_| \__ \ || (_) | (__| | | | (_| \__ \ | | | | | (_) \__ \
\__,_|_| \__\___/ \__,_|_| |_|\__,_|___/\__\___/ \___|_| |_|\__,_|___/_| |_| |_|\___/|___/
αυτοαναστοχασμός ergo legit */
select
_column, _type,
_nulls,
@brito
brito / bar.sql
Created May 23, 2020 02:15
Simple ASCII bar visualization of a single quantity
/*
Simple ASCII bar visualization of a single quantity
eg */ select * from _bar(53); /*
*/
create function _bar(_quantity int, _glyph text = '■')
returns text as $$
select repeat(_glyph,_quantity/3) || ' ' || _quantity
$$ language sql immutable;
@brito
brito / slider.sql
Created May 23, 2020 02:14
Simple ASCII slider visualization of two quantities and their relative ratio
/*
Simple ASCII slider visualization of two quantities and their relative ratio
eg */ select * from _slider(3,5); /*
*/
create function _slider(_left int, _right int, _glyphs text[] = '{-,|,-}')
returns text as $$
select
repeat(_glyphs[1], _left)
|| _glyphs[2] ||
@brito
brito / histogram.sql
Created May 23, 2020 02:14
Column histogram
/*
Column histogram
eg */ select * from _meta (
'{app_public}',
'{data_report_session,data_raw_session,data_report}'),
_histogram(_schema,_relname,_column,_type);/*
*/
create function _histogram(_schema name,_relname name,_column name,_type text)
returns setof json as $function$ begin
return query execute format(case
@brito
brito / iqt.sql
Created May 23, 2020 02:13
Column interquartile range and box-and-whisker plot
/*
Column interquartile range and box-and-whisker plot
eg */ select * from _meta (
'{app_public}',
'{data_report_session,data_raw_session,data_report}'),
_iqt(_schema,_relname,_column,_type);/*
*/
create function _iqt(_schema name, _relname name, _column name, _type text)
returns table (_quartiles json, _whiskers text) as $function$
select xmltable.* from (select query_to_xml(format(case
@brito
brito / tally.sql
Created May 23, 2020 02:13
Top 10 count distinct values in a column
/*
Top 10 count distinct values in a column
eg */ select * from _meta (
'{app_public}',
'{data_report_session,data_raw_session,data_report}'),
_tally(_schema,_relname,_column);/*
*/
create function _tally(_schema name,_relname name,_column name)
returns table (_tally json) as $function_tally$
select xmltable.* from (select query_to_xml(format($$
@brito
brito / meta.sql
Created May 23, 2020 02:12
List column names and types for relations in schemas
/*
List column names and types for _relations in _schemas
eg */ select * from _meta (
'{app_public}',
'{data_report_session,data_raw_session,data_report}');/*
*/
create function _meta (_schemas text[], _relations text[])
returns table (_schema name, _relname name, _kind text, _column name, _type name) as $$
@brito
brito / lcs.js
Last active December 9, 2019 23:13
Hacky longest common subsequence
// longest common subsequence
// without additions nor deletions
var list = `five
one_two_three
two_three_four
three_four_five
one_two
foo_bar
bar_baz`