Back to Code Snippets
Steve Crawshaw
@murrayjarvis-1
Macro to glimpse the table in the current DBSQL
An attempt to replicate the functionality of dplyrs (and polars) glimpse, with a simple function which takes the name of a table in the current database as a quoted string. Shows column name, data types and the first few values. Intended for use in the CLI. usage: FROM glimpse('table_name');
Execute this SQL
CREATE OR REPLACE MACRO glimpse(table_name) AS TABLE WITH TableSchema AS ( SELECT cid, name AS column_name, type AS column_type FROM pragma_table_info(table_name) ), SampleData AS ( -- Select the first 5 rows from the target table SELECT * FROM query_table(table_name) LIMIT 5 ), SampleDataUnpivoted AS ( -- Unpivot the sample data: columns become rows UNPIVOT (SELECT list(COLUMNS(*)::VARCHAR) FROM SampleData) ON COLUMNS(*) INTO NAME column_name VALUE sample_values_list -- This will be a list of strings ) -- Final selection joining schema info with sample data SELECT ts.column_name, ts.column_type, -- Convert the list to string and remove brackets for cleaner display regexp_replace(usp.sample_values_list::VARCHAR, '^\[|\]$', '', 'g') AS sample_data FROM TableSchema ts JOIN SampleDataUnpivoted usp ON ts.column_name = usp.column_name ORDER BY ts.cid;
Copy code
Steve Crawshaw
Expand
Share link