Skip to content

Commit

Permalink
examples: use a view in join-inner example
Browse files Browse the repository at this point in the history
  • Loading branch information
psFried committed Aug 21, 2023
1 parent a7f660d commit efaf5e5
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions examples/derive-patterns/join-inner.flow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,30 @@ collections:
sqlite:
migrations:
- |
create table join_state (
key text not null primary key,
CREATE TABLE join_state (
key TEXT NOT NULL PRIMARY KEY,
-- Stores the left hand side of the join
lhs integer,
lhs INTEGER,
-- Stores the right hand side of the join, using a JSON array
-- since this is a one-to-many join
rhs json
rhs JSON
);
CREATE VIEW joined_output AS SELECT key, JSON_OBJECT(
'Key', key,
'LHS', lhs,
'RHS', JSON(rhs)
) as json
FROM join_state
WHERE lhs IS NOT NULL AND rhs IS NOT NULL;
transforms:
- name: fromInts
source: patterns/ints
shuffle: { key: [/Key] }
lambda: |
insert into join_state (key, lhs) values ($Key, $Int)
on conflict (key) do update set lhs = lhs + $Int;
INSERT INTO join_state (key, lhs) VALUES ($Key, $Int)
ON CONFLICT (key) DO UPDATE SET lhs = lhs + $Int;
-- now emit the joined result
SELECT JSON_OBJECT(
'Key', $Key,
'LHS', lhs,
'RHS', JSON(rhs)
)
FROM join_state
WHERE key = $Key AND lhs IS NOT NULL AND rhs IS NOT NULL;
SELECT json FROM joined_output WHERE key = $Key;
- name: fromStrings
source: patterns/strings
Expand All @@ -45,13 +46,7 @@ collections:
INSERT INTO join_state (key, rhs) VALUES ($Key, JSON_ARRAY($String))
ON CONFLICT (key) DO UPDATE SET rhs = JSON_INSERT(COALESCE(rhs, '[]'), '$[#]', $String);
-- now emit the joined result
SELECT JSON_OBJECT(
'Key', $Key,
'LHS', lhs,
'RHS', JSON(rhs)
)
FROM join_state
WHERE key = $Key AND lhs IS NOT NULL AND rhs IS NOT NULL;
SELECT json FROM joined_output WHERE key = $Key;
tests:
patterns/test/inner-join:
Expand Down

0 comments on commit efaf5e5

Please sign in to comment.