Skip to content

Commit 3413e63

Browse files
committed
docs: rewrite Datalog example + docs page with verified output
1 parent c3195c6 commit 3413e63

4 files changed

Lines changed: 48 additions & 52 deletions

File tree

website/docs/queries-pivot.html

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,27 +217,21 @@ <h2 id="window-functions">Window Functions</h2>
217217
<p>Window functions compute values across a set of related rows without collapsing the result. In Rayfall, windowed computations are expressed through <code>select</code> with rolling or cumulative expressions.</p>
218218

219219
<h3>Rolling Computations</h3>
220+
<p>Running computations like cumulative sums and moving averages can be computed using <code>scan</code> directly on column vectors:</p>
220221
<pre><code>(<span class="hl-kw">set</span> trades (<span class="hl-fn">table</span> [<span class="hl-sym">Sym</span> <span class="hl-sym">Time</span> <span class="hl-sym">Price</span>]
221222
(<span class="hl-fn">list</span> [<span class="hl-str">x</span> <span class="hl-str">x</span> <span class="hl-str">x</span>]
222223
[<span class="hl-num">12:00:01</span> <span class="hl-num">12:00:04</span> <span class="hl-num">12:00:06</span>]
223224
[<span class="hl-num">89.17</span> <span class="hl-num">70.5</span> <span class="hl-num">80.54</span>])))
224225

225-
<span class="hl-comment">; Cumulative sum</span>
226-
(<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> trades
227-
<span class="hl-sym">cols:</span> {<span class="hl-sym">Sym:</span> Sym <span class="hl-sym">Time:</span> Time <span class="hl-sym">Price:</span> Price
228-
<span class="hl-sym">cum_price:</span> (<span class="hl-fn">scan</span> <span class="hl-fn">+</span> Price)}})</code></pre>
229-
230-
<h3>Moving Averages</h3>
231-
<pre><code><span class="hl-comment">; Moving average with scan and division</span>
232-
(<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> trades
233-
<span class="hl-sym">cols:</span> {<span class="hl-sym">Sym:</span> Sym <span class="hl-sym">Time:</span> Time <span class="hl-sym">Price:</span> Price
234-
<span class="hl-sym">running_avg:</span> (<span class="hl-fn">/</span> (<span class="hl-fn">scan</span> <span class="hl-fn">+</span> Price) (<span class="hl-fn">+</span> <span class="hl-num">1</span> (<span class="hl-fn">til</span> (<span class="hl-fn">count</span> Price))))}})</code></pre>
226+
<span class="hl-comment">; Cumulative sum on the Price column</span>
227+
(<span class="hl-fn">scan</span> <span class="hl-fn">+</span> (<span class="hl-fn">at</span> trades <span class="hl-str">'Price</span>))
228+
<span class="hl-comment">; [89.17 159.67 240.21]</span></code></pre>
235229

236230
<h3>Rank and Order</h3>
237-
<pre><code><span class="hl-comment">; Rank prices within the table</span>
238-
(<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> trades
239-
<span class="hl-sym">cols:</span> {<span class="hl-sym">Sym:</span> Sym <span class="hl-sym">Price:</span> Price
240-
<span class="hl-sym">rank:</span> (<span class="hl-fn">iasc</span> (<span class="hl-fn">iasc</span> Price))}})</code></pre>
231+
<pre><code><span class="hl-comment">; Rank prices — double iasc gives dense rank</span>
232+
(<span class="hl-kw">set</span> prices [<span class="hl-num">89.17</span> <span class="hl-num">70.5</span> <span class="hl-num">80.54</span>])
233+
(<span class="hl-fn">iasc</span> (<span class="hl-fn">iasc</span> prices))
234+
<span class="hl-comment">; [2 0 1]</span></code></pre>
241235

242236
<h3>Grouped Window</h3>
243237
<p>Combine <code>by:</code> with windowed expressions to compute per-group running totals:</p>

website/docs/queries-select.html

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ <h3 id="select-basic">Basic Projection</h3>
116116
<span class="hl-comment">; MSFT 420 900</span></code></pre>
117117

118118
<p>Select specific columns with computed expressions:</p>
119-
<pre><code>(<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> t <span class="hl-sym">cols:</span> {<span class="hl-sym">sym:</span> sym <span class="hl-sym">notional:</span> (<span class="hl-fn">*</span> price volume)}})
119+
<pre><code>(<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> t <span class="hl-sym">sym:</span> sym <span class="hl-sym">notional:</span> (<span class="hl-fn">*</span> price volume)})
120120
<span class="hl-comment">; sym notional</span>
121121
<span class="hl-comment">; --- --------</span>
122122
<span class="hl-comment">; AAPL 75000</span>
@@ -147,30 +147,32 @@ <h3 id="select-groupby">Group-by Aggregation</h3>
147147
[<span class="hl-num">100</span> <span class="hl-num">200</span> <span class="hl-num">50</span> <span class="hl-num">75</span> <span class="hl-num">300</span>])))
148148

149149
<span class="hl-comment">; Group by sym, aggregate price and qty</span>
150-
(<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> trades <span class="hl-sym">by:</span> {<span class="hl-sym">sym:</span> sym}
151-
<span class="hl-sym">cols:</span> {<span class="hl-sym">avg_price:</span> (<span class="hl-fn">avg</span> price)
152-
<span class="hl-sym">total_qty:</span> (<span class="hl-fn">sum</span> qty)
153-
<span class="hl-sym">n:</span> (<span class="hl-fn">count</span> price)}})
150+
(<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> trades <span class="hl-sym">by:</span> sym
151+
<span class="hl-sym">avg_price:</span> (<span class="hl-fn">avg</span> price)
152+
<span class="hl-sym">total_qty:</span> (<span class="hl-fn">sum</span> qty)
153+
<span class="hl-sym">n:</span> (<span class="hl-fn">count</span> price)})
154154
<span class="hl-comment">; sym avg_price total_qty n</span>
155155
<span class="hl-comment">; --- --------- --------- -</span>
156-
<span class="hl-comment">; AAPL 150.3 600 3</span>
157-
<span class="hl-comment">; GOOG 281 125 2</span></code></pre>
156+
<span class="hl-comment">; AAPL 150.33 600 3</span>
157+
<span class="hl-comment">; GOOG 281.0 125 2</span></code></pre>
158158

159159
<p>Group by multiple keys:</p>
160-
<pre><code>(<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> trades <span class="hl-sym">by:</span> {<span class="hl-sym">sym:</span> sym <span class="hl-sym">side:</span> side}
161-
<span class="hl-sym">cols:</span> {<span class="hl-sym">total:</span> (<span class="hl-fn">sum</span> qty)}})
160+
<pre><code>(<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> trades <span class="hl-sym">by:</span> [sym side]
161+
<span class="hl-sym">total:</span> (<span class="hl-fn">sum</span> qty)})
162162
<span class="hl-comment">; sym side total</span>
163163
<span class="hl-comment">; --- ---- -----</span>
164164
<span class="hl-comment">; AAPL Buy 400</span>
165165
<span class="hl-comment">; AAPL Sell 200</span>
166166
<span class="hl-comment">; GOOG Buy 125</span></code></pre>
167167

168168
<h3 id="select-where-by">Filter + Group-by</h3>
169-
<p>Combine <code>where:</code> and <code>by:</code> in the same query. The filter is applied before grouping:</p>
170-
<pre><code>(<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> trades
171-
<span class="hl-sym">where:</span> (<span class="hl-fn">==</span> side <span class="hl-str">'Buy</span>)
172-
<span class="hl-sym">by:</span> {<span class="hl-sym">sym:</span> sym}
173-
<span class="hl-sym">cols:</span> {<span class="hl-sym">buy_qty:</span> (<span class="hl-fn">sum</span> qty)}})
169+
<p>Filter first, then group by. Chain two <code>select</code> calls for correct results:</p>
170+
<pre><code><span class="hl-comment">; Step 1: filter to Buy rows only</span>
171+
(<span class="hl-kw">set</span> buys (<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> trades <span class="hl-sym">where:</span> (<span class="hl-fn">==</span> side <span class="hl-str">"Buy"</span>)}))
172+
173+
<span class="hl-comment">; Step 2: group and aggregate</span>
174+
(<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> buys <span class="hl-sym">by:</span> sym
175+
<span class="hl-sym">buy_qty:</span> (<span class="hl-fn">sum</span> qty)})
174176
<span class="hl-comment">; sym buy_qty</span>
175177
<span class="hl-comment">; --- -------</span>
176178
<span class="hl-comment">; AAPL 400</span>
@@ -208,7 +210,7 @@ <h3 id="update-where">Conditional Update</h3>
208210

209211
<h3 id="update-by">Update with Group-by</h3>
210212
<p>Combine <code>where:</code> and <code>by:</code> for grouped conditional updates:</p>
211-
<pre><code>(<span class="hl-kw">update</span> {<span class="hl-sym">price:</span> <span class="hl-num">0</span> <span class="hl-sym">from:</span> <span class="hl-str">'tab</span> <span class="hl-sym">by:</span> sym <span class="hl-sym">where:</span> (<span class="hl-fn">&gt;</span> volume <span class="hl-num">400</span>)})</code></pre>
213+
<pre><code>(<span class="hl-kw">update</span> {<span class="hl-sym">price:</span> <span class="hl-num">0</span> <span class="hl-sym">from:</span> <span class="hl-str">'tab</span> <span class="hl-sym">where:</span> (<span class="hl-fn">&gt;</span> volume <span class="hl-num">400</span>)})</code></pre>
212214

213215
<!-- ============================================================ -->
214216
<h2 id="insert">Insert</h2>
@@ -276,12 +278,12 @@ <h3 id="upsert-inplace">In-place Upsert</h3>
276278
<pre><code>(<span class="hl-fn">upsert</span> <span class="hl-str">'t</span> <span class="hl-num">1</span> (<span class="hl-fn">dict</span> [<span class="hl-sym">Value</span> <span class="hl-sym">Name</span> <span class="hl-sym">ID</span>] (<span class="hl-fn">list</span> <span class="hl-num">170.0</span> <span class="hl-str">'Quinn</span> <span class="hl-num">17</span>)))</code></pre>
277279

278280
<!-- ============================================================ -->
279-
<h2 id="delete">Delete</h2>
280-
<p>The <code>delete</code> function removes rows matching a predicate. It returns a new table without the matched rows.</p>
281+
<h2 id="delete">Delete Rows</h2>
282+
<p>To remove rows matching a predicate, use <code>select</code> with the inverse condition:</p>
281283
<pre><code>(<span class="hl-kw">set</span> t (<span class="hl-fn">table</span> [<span class="hl-sym">x</span> <span class="hl-sym">y</span>] (<span class="hl-fn">list</span> [<span class="hl-num">1</span> <span class="hl-num">2</span> <span class="hl-num">3</span> <span class="hl-num">4</span>] [<span class="hl-str">A</span> <span class="hl-str">B</span> <span class="hl-str">C</span> <span class="hl-str">D</span>])))
282284

283-
<span class="hl-comment">; Delete rows where x > 2</span>
284-
(<span class="hl-kw">set</span> t (<span class="hl-fn">delete</span> t (<span class="hl-fn">&gt;</span> x <span class="hl-num">2</span>)))
285+
<span class="hl-comment">; Remove rows where x &gt; 2 by keeping rows where x &lt;= 2</span>
286+
(<span class="hl-kw">set</span> t (<span class="hl-kw">select</span> {<span class="hl-sym">from:</span> t <span class="hl-sym">where:</span> (<span class="hl-fn">&lt;=</span> x <span class="hl-num">2</span>)}))
285287
<span class="hl-comment">; x y</span>
286288
<span class="hl-comment">; - -</span>
287289
<span class="hl-comment">; 1 A</span>
@@ -300,7 +302,7 @@ <h2 id="reference">Quick Reference</h2>
300302
<tbody>
301303
<tr>
302304
<td><code>select</code></td>
303-
<td><code>(select {from: t cols: {...} where: pred by: {...}})</code></td>
305+
<td><code>(select {from: t col: expr where: pred by: key})</code></td>
304306
<td>Query with projection, filtering, and grouping</td>
305307
</tr>
306308
<tr>
@@ -319,9 +321,9 @@ <h2 id="reference">Quick Reference</h2>
319321
<td>Insert or update by key column</td>
320322
</tr>
321323
<tr>
322-
<td><code>delete</code></td>
323-
<td><code>(delete t pred)</code></td>
324-
<td>Remove matching rows</td>
324+
<td><em>delete rows</em></td>
325+
<td><code>(select {from: t where: (not pred)})</code></td>
326+
<td>Remove matching rows via inverse filter</td>
325327
</tr>
326328
</tbody>
327329
</table>

0 commit comments

Comments
 (0)