You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Materialized View does not enforce columns names - 1.13.0 #1545
When compiling, sqlc does not verify that the columns exist in the mat view.
See the schema below and the query, the query compiles fine, but running that on the DB throws column "earned" does not exist
Relevant log output
No response
Database schema
CREATETABLEkpis (
ts TIMESTAMPTZ,
event_id TEXTNOT NULL,
svc TEXTNOT NULL,
stage TEXT,
region TEXT,
instance TEXT,
player_id TEXT,
slip_id TEXT,
game TEXT,
data JSONB NOT NULL
);
CREATE MATERIALIZED VIEW IF NOT EXISTS ten_day_kills
WITH (timescaledb.continuous) ASSELECT time_bucket(INTERVAL '10 day', ts) AS bucket,
COUNT(*) AS kill_count,
(data->>'winning_player_id')::TEXT pid
FROM kpis
WHERE event_id ='player.defeated'GROUP BY pid, bucket
;
SQL queries
SELECT COALESCE(SUM(earned), 0) as total_earned
FROM ten_day_kills
WHERE pid = @player_id
;
It also seems like it doesn't enforce not null checks on aliased columns. The first MV below has pid as a required query param, but the second has it as a sql.NullString
CREATE MATERIALIZED VIEW IF NOT EXISTS ten_day_earnings
WITH (timescaledb.continuous) ASSELECT time_bucket(INTERVAL '10 day', ts) AS bucket,
(data->>'winning_player_id')::TEXTAS pid,
SUM((data->>'tokens_gained')::INT8) AS earned
FROM kpis
WHERE event_id ='player.defeated'AND (data::JSON->>'player_id') = player_id
GROUP BY pid, bucket
;
CREATE MATERIALIZED VIEW IF NOT EXISTS ten_day_losings
WITH (timescaledb.continuous) ASSELECT time_bucket(INTERVAL '10 day', ts) AS bucket,
player_id AS pid,
SUM((data->>'lobby_price')::INT8) AS lost
FROM kpis
WHERE event_id ='player.defeated'AND (data::JSON->>'player_id') = player_id
GROUP BY pid, bucket
;
Activity
danthegoodman1 commentedon Apr 11, 2022
The following also compiles, even though
total_earis not defineddanthegoodman1 commentedon Apr 11, 2022
It also seems like it doesn't enforce not null checks on aliased columns. The first MV below has
pidas a required query param, but the second has it as asql.NullStringdanthegoodman1 commentedon May 10, 2022
@kyleconroy just wanted to bump this as running into this is pretty painful, since I otherwise assume sqlc guarantees correctness :)
test: Add two test cases
fix(compiler): Pull in array information from analyzer (#2864)
kyleconroy commentedon Oct 24, 2023
This is fixed in v1.23.0 by enabling the database-backed query analyzer. We added a test case for this issue so it won’t break in the future.
You can play around with the working example on the playground
fix(compiler): Pull in array information from analyzer (sqlc-dev#2864)