Last active
April 8, 2024 03:27
-
-
Save tdfirth/49e56c1043f8a4ed1d931e7225a30f62 to your computer and use it in GitHub Desktop.
Revisions
-
tdfirth revised this gist
Feb 22, 2024 . 1 changed file with 38 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ ┌─────────────────────────────┐ │┌───────────────────────────┐│ ││ Physical Plan ││ │└───────────────────────────┘│ └─────────────────────────────┘ ┌───────────────────────────┐ │ ORDER_BY │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ ORDERS: │ │ o."timestamp" DESC │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ PROJECTION │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ user_id │ │ order_id │ │ post_id │ │ order_time │ │ post_view_time │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ ASOF_JOIN │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ INNER │ │ user_id = user_id ├──────────────┐ │ timestamp >= timestamp │ │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ │ EC: 23 │ │ └─────────────┬─────────────┘ │ ┌─────────────┴─────────────┐┌─────────────┴─────────────┐ │READ_CSV_AUTO (MULTI-T... ││READ_CSV_AUTO (MULTI-T... │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ││ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ user_id ││ user_id │ │ timestamp ││ timestamp │ │ id ││ id │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ││ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ EC: 7 ││ EC: 23 │ └───────────────────────────┘└───────────────────────────┘ -
tdfirth revised this gist
Feb 22, 2024 . 1 changed file with 63 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ ┌─────────────────────────────┐ │┌───────────────────────────┐│ ││ Physical Plan ││ │└───────────────────────────┘│ └─────────────────────────────┘ ┌───────────────────────────┐ │ ORDER_BY │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ ORDERS: │ │ candidates.order_time DESC│ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ PROJECTION │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ user_id │ │ order_id │ │ post_id │ │ order_time │ │ post_view_time │ │ rn │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ PROJECTION │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ #0 │ │ #1 │ │ #2 │ │ #4 │ │ #5 │ │ #6 │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ FILTER │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ (rn = 1) │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ EC: 23 │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ WINDOW │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ ROW_NUMBER() OVER │ │(PARTITION BY id ORDER... │ │ DESC NULLS LAST) │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ HASH_JOIN │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ INNER │ │ user_id = user_id ├──────────────┐ │ timestamp <= timestamp │ │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ │ EC: 23 │ │ └─────────────┬─────────────┘ │ ┌─────────────┴─────────────┐┌─────────────┴─────────────┐ │READ_CSV_AUTO (MULTI-T... ││READ_CSV_AUTO (MULTI-T... │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ││ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ user_id ││ user_id │ │ timestamp ││ timestamp │ │ id ││ id │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ││ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ EC: 23 ││ EC: 7 │ └───────────────────────────┘└───────────────────────────┘ -
tdfirth revised this gist
Feb 22, 2024 . 2 changed files with 0 additions and 101 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,38 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,63 +0,0 @@ -
tdfirth revised this gist
Feb 22, 2024 . 2 changed files with 101 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ ┌─────────────────────────────┐ │┌───────────────────────────┐│ ││ Physical Plan ││ │└───────────────────────────┘│ └─────────────────────────────┘ ┌───────────────────────────┐ │ ORDER_BY │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ ORDERS: │ │ o."timestamp" DESC │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ PROJECTION │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ user_id │ │ order_id │ │ post_id │ │ order_time │ │ post_view_time │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ ASOF_JOIN │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ INNER │ │ user_id = user_id ├──────────────┐ │ timestamp >= timestamp │ │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ │ EC: 23 │ │ └─────────────┬─────────────┘ │ ┌─────────────┴─────────────┐┌─────────────┴─────────────┐ │READ_CSV_AUTO (MULTI-T... ││READ_CSV_AUTO (MULTI-T... │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ││ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ user_id ││ user_id │ │ timestamp ││ timestamp │ │ id ││ id │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ││ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ EC: 7 ││ EC: 23 │ └───────────────────────────┘└───────────────────────────┘ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ ┌─────────────────────────────┐ │┌───────────────────────────┐│ ││ Physical Plan ││ │└───────────────────────────┘│ └─────────────────────────────┘ ┌───────────────────────────┐ │ ORDER_BY │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ ORDERS: │ │ candidates.order_time DESC│ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ PROJECTION │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ user_id │ │ order_id │ │ post_id │ │ order_time │ │ post_view_time │ │ rn │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ PROJECTION │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ #0 │ │ #1 │ │ #2 │ │ #4 │ │ #5 │ │ #6 │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ FILTER │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ (rn = 1) │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ EC: 23 │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ WINDOW │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ ROW_NUMBER() OVER │ │(PARTITION BY id ORDER... │ │ DESC NULLS LAST) │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ HASH_JOIN │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ INNER │ │ user_id = user_id ├──────────────┐ │ timestamp <= timestamp │ │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ │ EC: 23 │ │ └─────────────┬─────────────┘ │ ┌─────────────┴─────────────┐┌─────────────┴─────────────┐ │READ_CSV_AUTO (MULTI-T... ││READ_CSV_AUTO (MULTI-T... │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ││ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ user_id ││ user_id │ │ timestamp ││ timestamp │ │ id ││ id │ │ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ││ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │ EC: 23 ││ EC: 7 │ └───────────────────────────┘└───────────────────────────┘ -
tdfirth renamed this gist
Feb 22, 2024 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
tdfirth renamed this gist
Feb 22, 2024 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
tdfirth revised this gist
Feb 22, 2024 . 2 changed files with 1 addition and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ How to think about asof queries. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +0,0 @@ -
tdfirth revised this gist
Feb 22, 2024 . 7 changed files with 5 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ # Asof SQL queries I've seen a few people get confused about asof queries recently, and they're starting to land in a couple of mainstream platforms! Snowflake added support very recently (it's still in closed beta) and duckdb added them a little while back. They've been around for a while though, the first time I encountered them was with a system called q/kdb+, which is commonly used in hedge funds and other financial organizations. Matching up data on timestamps is extremely common in that domain, so it's no surprise that tooling there has made this first class for some time. -
tdfirth renamed this gist
Feb 22, 2024 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
tdfirth revised this gist
Feb 22, 2024 . 6 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes. -
tdfirth created this gist
Feb 22, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ user_id,order_id,post_id,order_time,post_view_time 1,1,2,2024-02-21,2024-02-20 2,3,7,2024-02-20,2024-02-20 3,4,13,2024-02-19,2024-02-18 4,6,18,2024-02-18,2024-02-18 1,2,4,2024-02-17,2024-02-16 3,5,15,2024-02-15,2024-02-14 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ select o.user_id as user_id, o.id as order_id, p.id as post_id, o.timestamp as order_time, p.timestamp as post_view_time from "orders.csv" o asof join "post_views.csv" p on o.user_id = p.user_id and o.timestamp >= p.timestamp order by order_time desc; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ id,user_id,timestamp 1,1,2/21/2024 2,1,2/17/2024 3,2,2/20/2024 4,3,2/19/2024 5,3,2/15/2024 6,4,2/18/2024 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ id,user_id,post,timestamp 1,1,1,2/22/2024 2,1,2,2/20/2024 3,1,3,2/18/2024 4,1,4,2/16/2024 5,1,5,2/14/2024 6,2,1,2/22/2024 7,2,2,2/20/2024 8,2,3,2/18/2024 9,2,4,2/16/2024 10,2,5,2/14/2024 11,3,1,2/22/2024 12,3,2,2/20/2024 13,3,3,2/18/2024 14,3,4,2/16/2024 15,3,5,2/14/2024 16,4,1,2/22/2024 17,4,2,2/20/2024 18,4,3,2/18/2024 19,4,4,2/16/2024 20,4,5,2/14/2024 21,5,1,2/22/2024 22,5,2,2/20/2024 23,5,3,2/18/2024 24,5,4,2/16/2024 25,5,5,2/14/2024 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ with candidates as ( select o.user_id as user_id, o.id as order_id, p.id as post_id, o.timestamp as order_time, p.timestamp as post_view_time, row_number() over (partition by o.id order by p.timestamp desc) rn from "orders.csv" o inner join "post_views.csv" p on o.user_id = p.user_id and o.timestamp >= p.timestamp ) select * from candidates where rn = 1 order by order_time desc; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ user_id,order_id,post_id,order_time,post_view_time,rn 1,1,2,2024-02-21,2024-02-20,1 2,3,7,2024-02-20,2024-02-20,1 3,4,13,2024-02-19,2024-02-18,1 4,6,18,2024-02-18,2024-02-18,1 1,2,4,2024-02-17,2024-02-16,1 3,5,15,2024-02-15,2024-02-14,1