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-postgres-json-query-examples.sql
Created July 19, 2026 21:13
Common PostgreSQL JSON and JSONB query examples, including field access, nested objects, arrays, containment, updates, aggregation, indexing, and JSON construction.
-- PostgreSQL JSON / JSONB Examples
--
-- PostgreSQL supports both JSON and JSONB.
--
-- JSON
-- Stores the original JSON text.
--
-- JSONB
-- Stores a binary representation that is faster to query and index.
--
@brandonhimpfen
brandonhimpfen / sql-common-indexing-examples.sql
Created July 19, 2026 21:12
Common PostgreSQL indexing patterns and best practices, including B-tree, composite, partial, unique, expression, covering, GIN, BRIN, and index usage tips with practical examples.
-- PostgreSQL Common Indexing Examples
--
-- Indexes speed up data retrieval by allowing PostgreSQL to locate rows
-- without scanning an entire table.
--
-- Every index has a cost:
-- - Additional storage
-- - Slower INSERTs
-- - Slower UPDATEs
-- - Slower DELETEs
@brandonhimpfen
brandonhimpfen / sql-postgres-generate-date-series.sql
Created July 19, 2026 21:09
PostgreSQL generate_series() examples for daily, weekly, monthly, and hourly date ranges, calendar tables, missing-date reporting, and business-day filtering. PostgreSQL’s generate_series() returns values from an inclusive start through stop using the specified step, making it useful for generating calendar rows and filling gaps in time-series d…
-- PostgreSQL Date Series Examples
--
-- generate_series(start, stop, step) creates a row for each value in a range.
-- When working with dates or timestamps, the step is usually an interval.
--
-- Common uses:
-- - Calendar and reporting ranges
-- - Filling missing dates in time-series results
-- - Monthly summaries
-- - Business-day filtering
@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)