Skip to content

Instantly share code, notes, and snippets.

View brandonhimpfen's full-sized avatar
🎯
Focusing

Brandon Himpfen brandonhimpfen

🎯
Focusing
View GitHub Profile
@brandonhimpfen
brandonhimpfen / sql-window-functions-examples.sql
Created July 12, 2026 22:03
Common SQL window function examples for PostgreSQL, including ROW_NUMBER, RANK, DENSE_RANK, running totals, moving averages, LAG, LEAD, FIRST_VALUE, LAST_VALUE, and NTILE.
-- SQL Window Function Examples (PostgreSQL)
--
-- Window functions perform calculations across related rows without
-- collapsing them like GROUP BY.
--
-- General syntax:
--
-- function(...) OVER (
-- PARTITION BY ...
-- ORDER BY ...
@brandonhimpfen
brandonhimpfen / sql-postgres-upsert-examples.sql
Created July 12, 2026 21:57
PostgreSQL UPSERT patterns using INSERT ... ON CONFLICT, including DO NOTHING, DO UPDATE, composite keys, conditional updates, and RETURNING examples.
-- PostgreSQL UPSERT Examples
--
-- UPSERT allows you to insert a row or update an existing row when a
-- uniqueness constraint would otherwise be violated.
--
-- Syntax:
--
-- INSERT ...
-- ON CONFLICT (...)
-- DO NOTHING
@brandonhimpfen
brandonhimpfen / node-compare-distinct-count-estimates.js
Created July 12, 2026 21:48
Compare exact and bitmap-based approximate distinct counts for CSV or JSONL columns while streaming in Node.js, including absolute and percentage error with no dependencies.
#!/usr/bin/env node
/**
* Compare exact and approximate distinct counts while streaming.
*
* The script calculates:
* - Exact distinct count using an in-memory Set
* - Approximate distinct count using a fixed-size bitmap
* - Absolute error
* - Percentage error
* - Bitmap occupancy and saturation warnings
@brandonhimpfen
brandonhimpfen / node-profile-approx-distinct-counts.js
Created July 12, 2026 21:39
Estimate distinct values in CSV or JSONL columns while streaming in Node.js using a bounded-memory bitmap and linear counting, with nested JSON paths and no dependencies.
#!/usr/bin/env node
/**
* Estimate distinct value counts in CSV or JSONL while streaming.
*
* Uses a fixed-size bitmap and linear counting:
*
* estimated cardinality = -m × ln(V / m)
*
* where:
* m = total number of bitmap bits
@brandonhimpfen
brandonhimpfen / node-profile-numeric-percentiles.js
Created July 12, 2026 21:30
Estimate numeric percentiles from CSV or JSONL while streaming in Node.js using bounded-memory reservoir sampling, with nested JSON paths and no dependencies.
#!/usr/bin/env node
/**
* Profile approximate numeric percentiles from CSV or JSONL while streaming.
*
* Uses reservoir sampling to keep a bounded, approximately uniform sample
* from each selected numeric column.
*
* Reported percentiles:
* - p01
* - p05
@brandonhimpfen
brandonhimpfen / node-profile-numeric-columns.js
Created July 12, 2026 21:15
Profile numeric CSV/JSONL columns while streaming in Node.js, including count, missing/invalid values, min, max, mean, and standard deviation—with no dependencies.
#!/usr/bin/env node
/**
* Profile numeric CSV/JSONL columns while streaming.
*
* Calculates:
* - Valid numeric count
* - Missing, empty, and invalid counts
* - Minimum and maximum
* - Sum and mean
* - Population and sample standard deviation
@brandonhimpfen
brandonhimpfen / node-profile-value-frequencies.js
Created July 5, 2026 01:21
Profile CSV/JSONL value frequencies while streaming in Node.js: count top values per column, with nested JSONL paths and no dependencies.
#!/usr/bin/env node
/**
* Profile CSV/JSONL value frequencies while streaming.
*
* Usage:
* node node-profile-value-frequencies.js input.csv --format=csv --columns=country,status
* cat input.jsonl | node node-profile-value-frequencies.js --format=jsonl --columns=user.country,status
*
* Options:
* --top=N Number of values to show per column (default: 10)
@brandonhimpfen
brandonhimpfen / node-profile-column-completeness.js
Created July 4, 2026 22:01
Profile CSV/JSONL column completeness while streaming in Node.js: count present, missing, empty, and completeness percentage with no dependencies.
#!/usr/bin/env node
/**
* Profile CSV/JSONL column completeness while streaming.
*
* Usage:
* node node-profile-column-completeness.js input.csv --format=csv
* cat input.jsonl | node node-profile-column-completeness.js --format=jsonl --columns=id,name,email
*
* Notes:
* - CSV mode uses the header row as columns unless --columns is provided.
@brandonhimpfen
brandonhimpfen / node-detect-duplicate-fields.js
Created July 4, 2026 21:55
Detect duplicate CSV headers or duplicate JSON object keys while streaming in Node.js, with no dependencies.
#!/usr/bin/env node
/**
* Detect duplicate CSV headers or duplicate JSONL keys while streaming.
*
* Usage:
* node node-detect-duplicate-fields.js input.csv --format=csv
* cat input.jsonl | node node-detect-duplicate-fields.js --format=jsonl
*
* Notes:
* - CSV mode checks only the header row.
@brandonhimpfen
brandonhimpfen / node-normalize-fields-snake-case.js
Created May 31, 2026 17:22
Normalize CSV or JSONL field names to snake_case while streaming in Node.js, with no dependencies.
#!/usr/bin/env node
/**
* Normalize CSV or JSONL field names to snake_case while streaming.
*
* Usage:
* cat input.jsonl | node node-normalize-fields-snake-case.js --format=jsonl > output.jsonl
* node node-normalize-fields-snake-case.js input.csv --format=csv > output.csv
*
* Notes:
* - JSONL mode normalizes object keys recursively.