Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ repos:
language: rust
pass_filenames: false

#
# TODO: This needs a more thorough configuration, e.g. spacing, alignment, capitalization, etc
#
# https://docs.sqlfluff.com/en/stable/production/pre_commit.html
# - repo: https://github.com/sqlfluff/sqlfluff
# rev: 3.3.1
# hooks:
# - id: sqlfluff-fix
# name: sqlfluff-fix
# args: [ "--dialect", "postgres" ]
# files: '^.*\.sql$'
- repo: https://github.com/sqlfluff/sqlfluff
rev: 3.3.1
hooks:
- id: sqlfluff-fix
# AL07: Avoid table aliases in from clauses and join conditions <- we sometimes do this for long names
# LT05: Line is too long <- not useful. mostly triggers for comments
# LT12: Files must end with a single trailing newline <- already done by trailing-whitespace
args: [--dialect=postgres, "--exclude-rules=AL07,LT05,LT12"]
files: \.sql$

# TODO: Add NPM hooks for formatting and linting
60 changes: 31 additions & 29 deletions demo/db/initdb/01.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ create extension postgis;

create table trips
(
vendorid numeric,
pickup_datetime timestamp,
dropoff_datetime timestamp,
passenger_count numeric,
trip_distance numeric,
ratecodeid numeric,
store_and_fwd_flag text,
pulocationid numeric,
dolocationid numeric,
payment_type numeric,
fare_amount numeric,
extra numeric,
mta_tax numeric,
tip_amount numeric,
tolls_amount numeric,
improvement_surcharge numeric,
total_amount numeric,
congestion_surcharge numeric,
airport_fee numeric
vendorid numeric,
pickup_datetime timestamp,
dropoff_datetime timestamp,
passenger_count numeric,
trip_distance numeric,
ratecodeid numeric,
store_and_fwd_flag text,
pulocationid numeric,
dolocationid numeric,
payment_type numeric,
fare_amount numeric,
extra numeric,
mta_tax numeric,
tip_amount numeric,
tolls_amount numeric,
improvement_surcharge numeric,
total_amount numeric,
congestion_surcharge numeric,
airport_fee numeric
);

create function tilebbox(z integer, x integer, y integer, srid integer DEFAULT 3857) returns geometry
create function tilebbox(
z integer, x integer, y integer, srid integer default 3857
) returns geometry
immutable
language plpgsql
as $$
Expand All @@ -46,14 +48,15 @@ begin
return ST_Transform(bbox, srid);
end if;
end;
$$
;
$$;

create or replace function get_trips(z integer, x integer, y integer, query_params json) returns bytea
stable
strict
parallel safe
language plpgsql
create or replace function get_trips(
z integer, x integer, y integer, query_params json
) returns bytea
stable
strict
parallel safe
language plpgsql
as $$
DECLARE
bounds GEOMETRY(POLYGON, 3857) := TileBBox(z, x, y, 3857);
Expand Down Expand Up @@ -92,7 +95,6 @@ BEGIN
RETURN res;

END;
$$
;
$$;

alter function get_trips(integer, integer, integer, json) owner to postgres;
12 changes: 7 additions & 5 deletions demo/db/initdb/03.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
drop table if exists trips_by_hour;
create table trips_by_hour as
select
pulocationid,
count(*) as trips_count,
round(avg(total_amount)) trips_price,
round(avg(extract(epoch from (dropoff_datetime - pickup_datetime)) / 60))::INTEGER trips_duration,
date_trunc('hour', pickup_datetime) as pickup_datetime
pulocationid,
round(
avg(extract(epoch from (dropoff_datetime - pickup_datetime)) / 60)
)::INTEGER as trips_duration,
count(*) as trips_count,
round(avg(total_amount)) as trips_price,
date_trunc('hour', pickup_datetime) as pickup_datetime
from trips
group by pulocationid, date_trunc('hour', pickup_datetime);
142 changes: 88 additions & 54 deletions martin/src/pg/scripts/query_available_function.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,94 @@
-- input_names: a JSON array of input parameter names
-- input_types: a JSON array of input parameter types
WITH
--
inputs AS (
-- list of input parameters for each function, returned as a jsonb array [{name: type}, ...]
SELECT specific_name,
jsonb_agg(COALESCE(parameter_name::text, '_') ORDER BY ordinal_position) as input_names,
jsonb_agg(data_type::text ORDER BY ordinal_position) as input_types
FROM information_schema.parameters
WHERE parameter_mode = 'IN'
AND specific_schema NOT IN ('pg_catalog', 'information_schema')
GROUP BY specific_name),
--
outputs AS (
-- list of output parameters for each function, returned as a jsonb array [{name: type}, ...]
SELECT specific_name,
jsonb_agg(data_type::text ORDER BY ordinal_position) as out_params,
jsonb_agg(parameter_name::text ORDER BY ordinal_position) as out_names
FROM information_schema.parameters
WHERE parameter_mode = 'OUT'
AND specific_schema NOT IN ('pg_catalog', 'information_schema')
GROUP BY specific_name),
--
comments AS (
-- list of all comments associated with the function
SELECT pg_namespace.nspname AS schema,
pg_proc.proname AS name,
obj_description(pg_proc.oid, 'pg_proc') AS description
FROM pg_proc
JOIN pg_namespace ON pg_proc.pronamespace = pg_namespace.oid)
SELECT routines.specific_schema AS schema,
routines.routine_name AS name,
routines.data_type AS output_type,
outputs.out_params AS output_record_types,
out_names AS output_record_names,
inputs.input_types AS input_types,
inputs.input_names AS input_names,
comments.description AS description
--
inputs AS (
-- list of input parameters for each function, returned as a jsonb array [{name: type}, ...]
SELECT
specific_name,
jsonb_agg(
coalesce(parameter_name::text, '_')
ORDER BY ordinal_position
) AS input_names,
jsonb_agg(
data_type::text
ORDER BY ordinal_position
) AS input_types
FROM information_schema.parameters
WHERE
parameter_mode = 'IN'
AND specific_schema NOT IN ('pg_catalog', 'information_schema')
GROUP BY specific_name
),

--
outputs AS (
-- list of output parameters for each function, returned as a jsonb array [{name: type}, ...]
SELECT
specific_name,
jsonb_agg(
data_type::text
ORDER BY ordinal_position
) AS out_params,
jsonb_agg(
parameter_name::text
ORDER BY ordinal_position
) AS out_names
FROM information_schema.parameters
WHERE
parameter_mode = 'OUT'
AND specific_schema NOT IN ('pg_catalog', 'information_schema')
GROUP BY specific_name
),

--
comments AS (
-- list of all comments associated with the function
SELECT
pg_namespace.nspname AS schema, -- noqa: RF04
pg_proc.proname AS name, -- noqa: RF04
obj_description(pg_proc.oid, 'pg_proc') AS description
FROM pg_proc
INNER JOIN pg_namespace ON pg_proc.pronamespace = pg_namespace.oid
)

SELECT
routines.specific_schema AS schema, -- noqa: RF04
routines.routine_name AS name, -- noqa: RF04
routines.data_type AS output_type,
outputs.out_params AS output_record_types,
outputs.out_names AS output_record_names,
inputs.input_types,
inputs.input_names,
comments.description
FROM information_schema.routines
JOIN inputs ON routines.specific_name = inputs.specific_name
LEFT JOIN outputs ON routines.specific_name = outputs.specific_name
LEFT JOIN comments ON comments.schema = routines.specific_schema AND comments.name = routines.routine_name
WHERE jsonb_array_length(input_names) IN (3, 4) -- 3 or 4 input parameters
AND lower(input_names ->> 0) IN ('z', 'zoom') -- the first int param is either z or zoom
AND input_types ->> 0 = 'integer'
AND lower(input_names ->> 1) = 'x' -- the second int param is x
AND input_types ->> 1 = 'integer'
AND lower(input_names ->> 2) = 'y' -- the third param is y
AND input_types ->> 2 = 'integer'
-- the 4th optional parameter can be any name, and must be either json or jsonb
AND (input_types ->> 3 = 'json' OR input_types ->> 3 = 'jsonb' OR (input_types ->> 3) IS NULL)
-- the output must be either a single bytea value or a table, with the table row being either [bytea] or [bytea, text]
AND (
(data_type = 'bytea' AND out_params IS NULL)
OR (data_type = 'bytea' AND out_params = '["bytea"]'::jsonb)
OR (data_type = 'record' AND out_params = '["bytea"]'::jsonb)
OR (data_type = 'record' AND out_params = '["bytea", "text"]'::jsonb)
INNER JOIN inputs ON routines.specific_name = inputs.specific_name
LEFT JOIN outputs ON routines.specific_name = outputs.specific_name
LEFT JOIN
comments
ON
routines.specific_schema = comments.schema
AND routines.routine_name = comments.name
WHERE
jsonb_array_length(inputs.input_names) IN (3, 4) -- 3 or 4 input parameters
-- the first int param is either z or zoom
AND lower(inputs.input_names ->> 0) IN ('z', 'zoom')
AND inputs.input_types ->> 0 = 'integer'
AND lower(inputs.input_names ->> 1) = 'x' -- the second int param is x
AND inputs.input_types ->> 1 = 'integer'
AND lower(inputs.input_names ->> 2) = 'y' -- the third param is y
AND inputs.input_types ->> 2 = 'integer'
-- the 4th optional parameter can be any name, and must be either json or jsonb
AND (
inputs.input_types ->> 3 = 'json'
OR inputs.input_types ->> 3 = 'jsonb'
OR (inputs.input_types ->> 3) IS NULL
)
-- the output must be either a single bytea value or a table, with the table row being either [bytea] or [bytea, text]
AND (
(routines.data_type = 'bytea' AND outputs.out_params IS NULL)
OR (routines.data_type = 'bytea' AND outputs.out_params = '["bytea"]'::jsonb)
OR (routines.data_type = 'record' AND outputs.out_params = '["bytea"]'::jsonb)
OR (routines.data_type = 'record' AND outputs.out_params = '["bytea", "text"]'::jsonb)
)
ORDER BY routines.specific_schema, routines.routine_name;
Loading
Loading