Skip to content

Instantly share code, notes, and snippets.

@andreypopp
Last active September 16, 2022 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreypopp/becda3b307bb49b266d75a2d92c43fb7 to your computer and use it in GitHub Desktop.
Save andreypopp/becda3b307bb49b266d75a2d92c43fb7 to your computer and use it in GitHub Desktop.
(* An example of a predefined expression
The expression is designed to be computed in a join scope between backlinks
and page_info. Below we show how to reuse this expression with a scope of a
different shape.
*)
let hash_sum s =
Sqml.Expr.(
let link_dst_root_hash = s #- backlinks #-> link_dst_root_hash in
let src_root_hash = s #- page_info #?> src_root_hash in
if_null src_root_hash ~default:(int 0) + link_dst_root_hash)
(* finally let's see how such query is being used *)
let () =
let q =
let open Sqml.Expr in
Example.internal_backlinks
~fields:
Fields.(
fun s ->
[
(* an expression which traverses multiple scopes deep *)
field
s
#- backlinks1
#- backlinks2
#- backlinks
#-> link_dst_root_hash
~name:"link_dst_root_hash";
(* build an expression at specified scope *)
field
s #- backlinks1 #- backlinks2 #-> (fun s ->
(s #- backlinks #-> link_dst_root_hash)
+ (s #- backlinks #-> link_dst_root_hash))
~name:"link_dst_root_hash_twice";
field
s #- backlinks1 #-> (fun s ->
(s #- backlinks2 #- backlinks #-> link_dst_root_hash)
+ (s #- backlinks2 #- backlinks #-> link_dst_root_hash))
~name:"link_dst_root_hash_twice";
(* backlinks_stats is an aggregated scope but we still can access the
columns used in GROUP BY *)
field
s #- backlinks_stats #-> link_dst_root_hash
~name:"link_dst_root_hash_from_stats";
(* again, the computation against backlinks_stats, we can define
aggregations which allows accessing aggregated scope *)
field
(s #- backlinks_stats
|> max (fun agg -> agg #- backlinks #-> src_page_ur))
~name:"max_src_page_ur";
(* finally we call a predefined expression but massage scope before *)
field
(hash_sum
(* XXX: scope construction feels heavy weight but I'm not sure it
warrants support from ppx as this is not going to be used that
often *)
(scope
(object
method backlinks =
s #- backlinks1 #- backlinks2 #- backlinks
method page_info = s #- page_info #? page_info
end)))
~name:"root_hash_sum";
])
~filter:(fun s ->
s #- backlinks1 #- backlinks2 #- backlinks #-> link_dst_root_hash
= int 12)
~page_info_prewhere:(fun _ -> bool true)
~backlinks_prewhere:(fun _ -> bool true)
~backlinks_stats_order_by:(fun s ->
Order_by.
[
asc s #-> link_dst_root_hash;
asc s #-> link_dst_unparsed;
desc (s |> count (fun _ -> int 1));
])
()
in
Format.printf "%s@." q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment