<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: ThinkRedstone</title>
    <description>The latest articles on Forem by ThinkRedstone (@thinkredstone).</description>
    <link>https://forem.com/thinkredstone</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1217280%2F378af085-d65f-4862-af8f-631f2a00149f.png</url>
      <title>Forem: ThinkRedstone</title>
      <link>https://forem.com/thinkredstone</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/thinkredstone"/>
    <language>en</language>
    <item>
      <title>The Hallucinated Rows Incident</title>
      <dc:creator>ThinkRedstone</dc:creator>
      <pubDate>Thu, 23 Nov 2023 15:10:31 +0000</pubDate>
      <link>https://forem.com/thinkredstone/the-hallucinated-rows-incident-9ai</link>
      <guid>https://forem.com/thinkredstone/the-hallucinated-rows-incident-9ai</guid>
      <description>&lt;p&gt;Herein lies the tale of the serialization bug that caused one of the weirdest crashes in the company's history- the infamous "Not enough values in db" panic. Delve with me into the depths of implementing but a single SQL operator- &lt;code&gt;ORDER BY... LIMIT&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Big Diff With Our Engine
&lt;/h2&gt;

&lt;p&gt;At Epsio Labs, we develop an incremental SQL engine- in brief, an SQL engine that operates on changes in the data, instead of always using the whole dataset. Explaining why that's useful I'll leave to our &lt;a href="https://docs.epsio.io/"&gt;documentation&lt;/a&gt;, but to understand this bug it is important you understand a little how our engine works.&lt;/p&gt;

&lt;p&gt;The Epsio Engine represents data as a &lt;code&gt;Diff&lt;/code&gt;- each &lt;code&gt;Diff&lt;/code&gt; is a key-value pair, specifying a modification to the value of some specific key. Internally, we usually write a diff like so: &lt;code&gt;"rock": +1&lt;/code&gt;. This diff can be read as "add one to the value of the string &lt;code&gt;rock&lt;/code&gt;". The important property of a diff is that diffs on equal keys can be consolidated together into one diff by adding their modifications together- consider for example this list of diffs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"rock"      : +3
"other_rock": +4
"rock"      : -2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This list can be consolidated to a shorter one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"rock"      : +1
"other_rock": +4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;by adding together the modifications of the two different entries for &lt;code&gt;rock&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An equally important concept is that of the default or null value- if we consolidate together &lt;code&gt;"rock": +2"&lt;/code&gt; and &lt;code&gt;"rock": -2&lt;/code&gt;, we would get &lt;code&gt;"rock": 0&lt;/code&gt;.&lt;br&gt;
But of course, like we all learn in primary school, adding zero to a key is like doing nothing at all- and so if the modification of a diff is zero, we simply delete that diff.&lt;/p&gt;

&lt;p&gt;Obviously, the typing for the modification of a diff is actually generic - so long as the type fulfills these two properties, any valid type can be used, not only signed integers&lt;sup id="fnref1"&gt;1&lt;/sup&gt;. For the curious, In our rust-based engine, a modification type is represented using the following trait constraint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;Modification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;PartialEq&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;Default&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;Add&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;Self&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Representing The Real World as Diffs
&lt;/h3&gt;

&lt;p&gt;Imagine a person named Yumi. Yumi has the sacred task of stacking stones (don't ask why, it's for good World Building reasons). In order to best stack stones, Yumi gives the stones to a stone-stacking machine, which measures different parameters for each stone- the weight, the volume, the shape- and Yumi later uses the machine to help pick the next best stone to add to the stack.&lt;/p&gt;

&lt;p&gt;For simplicity's sake, let's assume the machine only tracks the weight of each stone- in such a case, the stacking machine might have a table similar to this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;stone_name&lt;/th&gt;
&lt;th&gt;weight&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;big_rock&lt;/td&gt;
&lt;td&gt;1.45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;medium_stone&lt;/td&gt;
&lt;td&gt;0.97&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;small_pebble&lt;/td&gt;
&lt;td&gt;0.12&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;How would we represent these distinct lines as diffs? Well, easily enough- Each row will be the key of the diff, with an integer modification representing the amount of times the row appears in the table. For example, the above table&lt;br&gt;
translates to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;("big_rock", 1.45)    : +1
("medium_stone", 0.97): +1
("small_pebble", 0.12): +1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If Yumi loses &lt;code&gt;small_pebble&lt;/code&gt;, she can simply give the machine the diff &lt;code&gt;("small_pebble", 0.12): -1&lt;/code&gt;, which would cancel out the previous +1 to the key of &lt;code&gt;("small_pebble", 0.12)&lt;/code&gt;, deleting the key since its value is 0. If Yumi finds another &lt;code&gt;medium_stone&lt;/code&gt; which weighs &lt;code&gt;0.97&lt;/code&gt;, she can use the diff&lt;br&gt;
&lt;code&gt;("medium_stone", 0.97): +1&lt;/code&gt; to mark the addition of another stone with those properties, resulting in the diff &lt;code&gt;("medium_stone", 0.97): +2&lt;/code&gt; which represents the fact that there are two rows in the table with this value. After these two actions, Yumi's machine will have these diffs in total:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;("big_rock", 1.45)    : +1
("medium_stone", 0.97): +2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which would represent this table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;stone_name&lt;/th&gt;
&lt;th&gt;weight&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;big_rock&lt;/td&gt;
&lt;td&gt;1.45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;medium_stone&lt;/td&gt;
&lt;td&gt;0.97&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;medium_stone&lt;/td&gt;
&lt;td&gt;0.97&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Operations on Diffs
&lt;/h3&gt;

&lt;p&gt;When data is represented as diffs, a query on the data can be thought of as an operation on those diffs - this a complicated subject deserving a story of its&lt;br&gt;
own, but at its core what the Epsio engine does is represent SQL queries as a series of operations on an incoming stream of diffs&lt;sup id="fnref2"&gt;2&lt;/sup&gt;.&lt;br&gt;
A simple operation, like counting the number of rows, can be easily represented as summing the modifications of diffs we got as an input, and outputting a diff whose key is this result. For the previous example, this counting operation would output &lt;code&gt;3: +1&lt;/code&gt;, which translates to this table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;count&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If we then give as input the diff &lt;code&gt;("big_rock", 1.45): -1&lt;/code&gt; (i.e. deleting a row), our count operation would output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3: -1
2: +1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which can be read as "delete the row with value &lt;code&gt;3&lt;/code&gt;, and add a row with value &lt;code&gt;2&lt;/code&gt;".&lt;/p&gt;

&lt;p&gt;Sorted that out? Great, now we can move to sorting.&lt;/p&gt;

&lt;h2&gt;
  
  
  That's A Lot Of Stone To Sort Through
&lt;/h2&gt;

&lt;p&gt;Right, so now Yumi's machine has an assortment of diffs, which represent all the different stones Yumi can use for her stacks. What remains is choosing the right stone to add to the top of the stack- but of course, there are a lot of stones to consider, and so Yumi might decide to ease the load; she might choose to limit herself to consider only the biggest 3 stones in the machine's database. In SQL, this query is easy enough to write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;stones_table&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;weight&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But how would we actually calculate it using diffs?&lt;/p&gt;

&lt;h3&gt;
  
  
  The Stone Sorting Algorithm
&lt;/h3&gt;

&lt;p&gt;Let's imagine the wanted result- given this input data:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;stone_name&lt;/th&gt;
&lt;th&gt;weight&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;huge_rock&lt;/td&gt;
&lt;td&gt;2.45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;big_rock&lt;/td&gt;
&lt;td&gt;1.45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;medium_stone&lt;/td&gt;
&lt;td&gt;0.97&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;small_stone&lt;/td&gt;
&lt;td&gt;0.77&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;tiny_pebble&lt;/td&gt;
&lt;td&gt;0.13&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;We want to output these diffs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;("huge_rock", 2.45): +1
("big_rock", 1.45): +1
("medium_stone", 0.97): +1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To output the top 3 rocks, our engine has to first store all the rocks in some sorted way. To do this, we of course picked &lt;a href="https://rocksdb.org/"&gt;RocksDB&lt;/a&gt;, an embedded lexicographically sorted key-value store, which acts as the sorting operation's persistent state. In our RocksDB state, the diffs are keyed by the value of &lt;code&gt;weight&lt;/code&gt;, and since RocksDB is sorted, our stored diffs are automatically sorted by their weight.&lt;/p&gt;

&lt;p&gt;Let's assume Yumi already inputted into the machine three diffs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;("huge_rock", 2.45): +1
("big_rock", 1.45): +1
("tiny_pebble", 0.13): +1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since there are only 3 diffs, all three would be outputted, and of course all inputted diffs are also written to RocksDB for persistent storage.&lt;/p&gt;

&lt;p&gt;Now Yumi inputs another stone into the machine, and thus another diff into the&lt;br&gt;
engine- for example, &lt;code&gt;("medium_stone", 0.97): +1&lt;/code&gt;.&lt;br&gt;
What we do (speaking as our royal engine) is query RocksDB for the top 3 diffs we previously stored.&lt;br&gt;
The top 3 diffs in storage should be equal to our previous output, and are also all the data that is relevant to calculate the new top 3 given the new diffs.&lt;/p&gt;

&lt;p&gt;We add into this list the newly inputted diffs, and sort it in memory. Then we can compare the sorted list of both new and previous diffs to the list of only previous diffs- if the top 3 remained the same after introducing the new diffs, then all inputted diffs are currently irrelevant- we store them in RocksDB in case one of the top 3 is deleted, but we don't generate any output.&lt;/p&gt;

&lt;p&gt;If, however, the top 3 diffs in the combined list are different to what's stored in RocksDB, then we know the top 3 have changed- and we need to update our output of this change: for each diff that is no longer in the top 3, we output its negative, and for each new diff in the top 3 we output its positive. Given the new diff Yumi inputted at the start of this section (which was &lt;code&gt;("medium_stone", 0.97): +1&lt;/code&gt;, in case you forgot); and given the state as it was when she did so, our output would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;("tiny_pebble", 0.13): -1
("medium_stone", 0.97): +1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These two diffs are representing the change from &lt;code&gt;tiny_pebble&lt;/code&gt; into &lt;code&gt;medium_stone&lt;/code&gt; in the table of top 3 stones by weight. Note here that if we consolidated all&lt;br&gt;
diffs outputted by the sort operation, we should always remain with up to 3 positive diffs- the &lt;code&gt;tiny_pebble&lt;/code&gt; negative diff cancels out the previously outputted &lt;code&gt;tiny_pebble&lt;/code&gt; positive diff, meaning the total count of diffs should still be 3- after all, a "top 3" table with four rows in not exactly what Yumi is looking for.&lt;/p&gt;

&lt;p&gt;Here, you might already see a hint to the devastation that is about to befall Yumi's machine- what happens when the sorting we do in-memory disagrees with the order RocksDB sorts the same diffs?&lt;/p&gt;
&lt;h2&gt;
  
  
  Floating Points and Broken storekeys
&lt;/h2&gt;

&lt;p&gt;Before we dive right into that, let's expand on how &lt;em&gt;exactly&lt;/em&gt; are diffs stored. Inherently, RocksDB supports storing only bytes- it has no concept of more complicated objects, and so in practice serialization libraries are used to convert complicated objects to and from their representation in RocksDB.&lt;/p&gt;

&lt;p&gt;The engine uses &lt;a href="https://docs.rs/rust_decimal/latest/rust_decimal/"&gt;&lt;code&gt;rust_decimal::Decimal&lt;/code&gt;&lt;/a&gt; to&lt;br&gt;
represent high precision decimal numbers, like the &lt;code&gt;weight&lt;/code&gt; property.&lt;br&gt;
Serialization of RocksDB keys is done by the &lt;a href="https://docs.rs/storekey/0.5.0/storekey/"&gt;&lt;code&gt;storekey&lt;/code&gt;&lt;/a&gt; crate.&lt;br&gt;
To know how Yumi's machine stores diffs, we can now ask-&lt;br&gt;
How does &lt;code&gt;storekey&lt;/code&gt; serialize &lt;code&gt;rust_decimal&lt;/code&gt;? &lt;br&gt;&lt;br&gt;
Well, using &lt;a href="https://github.com/evcxr/evcxr"&gt;evcxr&lt;/a&gt; to run Rust in Jupyter, the answer is as a null-terminated string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;str&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;FromStr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;from_utf8&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;rust_decimal&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;storekey&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"3.141"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;from_utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nn"&gt;storekey&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output[1]: "3.141\0"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This might seem completely nonsensical at first glance, but it actually makes a lot of sense- the ASCII value of digits does correspond to their lexicographical order (0 is&lt;br&gt;
0x30, 9 is 0x39); since the dot (0x2e) is smaller than all digits, 2.01 will sort before 201; and since shorter numbers sort first, 2 will sort before both. If we went ahead and used this default behaviour (which we unintentionally did), for almost all stones, Yumi's machine would give completely&lt;br&gt;
correct responses. No, the real problem is much less obvious and much more &lt;em&gt;sinister&lt;/em&gt;- and it ties into how decimal numbers represent their precision.&lt;/p&gt;
&lt;h3&gt;
  
  
  Precision
&lt;/h3&gt;

&lt;p&gt;When Yumi places a stone on a scale, the scale outputs a number- for example, 1.56. But as anyone who has tried to bake with &lt;em&gt;precisely&lt;/em&gt; 1 kilogram of flour can attest to, the scale never rounds- if a stone weights exactly 2, Yumi's scale will still write out 2.00. This is because the precision of the scale is 2 digits after the dot, meaning it knows for a fact the stone doesn't weight 2.01- it might weigh 2.009 or 2.001, but the scale can guarantee those first&lt;br&gt;
two digits after the dot are zero. Knowing the exact precision of a measurement or calculation is often very valuable, which is why &lt;code&gt;rust_decimal&lt;/code&gt; supports it- only in this case, it proved quite fatal.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Bug
&lt;/h2&gt;

&lt;p&gt;So, here's an interesting question- what happens when Yumi gives her machine a stone which weights &lt;em&gt;exactly&lt;/em&gt; 0.970? Let's take look at how the input table is supposed to look, with this weird &lt;code&gt;evil_stone&lt;/code&gt;- &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;stone_name&lt;/th&gt;
&lt;th&gt;weight&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;huge_rock&lt;/td&gt;
&lt;td&gt;2.45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;large_rock&lt;/td&gt;
&lt;td&gt;1.97&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;big_rock&lt;/td&gt;
&lt;td&gt;1.45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;medium_stone&lt;/td&gt;
&lt;td&gt;0.97&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;evil_stone&lt;/td&gt;
&lt;td&gt;0.970&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If everything worked correctly, this stone wouldn't matter- it isn't in the top 3, it shouldn't affect the output at all. But, and this might surprise you, not everything worked correctly- if we input this table in a random order, what we sometimes get is in fact this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error while executing IVM 5c9e2a10-5d9a-4b41-8c29-63e3a076c24c:
    thread 'tokio-runtime-worker' panicked at 'Not enough values in db to pop 1 values (popped 0)'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://youtu.be/hOLnLDEnUdU"&gt;Huh?&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Issue
&lt;/h3&gt;

&lt;p&gt;Let's run the algorithm by hand with the input. After Yumi surrendered four random stones to the machine, we have this data in our RocksDB:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;stone_name&lt;/th&gt;
&lt;th&gt;weight&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;huge_rock&lt;/td&gt;
&lt;td&gt;2.45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;big_rock&lt;/td&gt;
&lt;td&gt;1.45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;evil_stone&lt;/td&gt;
&lt;td&gt;0.970&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;medium_stone&lt;/td&gt;
&lt;td&gt;0.97&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Alas, at this point Yumi's fate is already sealed- the fatal bug has been armed, and is about to cause quite a panic. When Yumi added these four stones, our engine should have outputted the top 3 stones- the top 2 are clearly &lt;code&gt;huge_rock&lt;/code&gt; and &lt;code&gt;big_rock&lt;/code&gt;, but what does the engine output for the third? Well, the engine sorts in memory, and when two decimals are equal in value- even if one is more precise-&lt;code&gt;rust_decimal&lt;/code&gt; considers them equal: &lt;code&gt;Decimal(0.97) == Decimal(0.970)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And so, when sorting two equal values, &lt;code&gt;Vec::sort&lt;/code&gt; (which is what the engine uses) will essentially pick one at random- add a little careful tracing, and bam! When it chooses &lt;code&gt;medium_stone&lt;/code&gt; is when the engine goes to live in a farm upstate.&lt;/p&gt;

&lt;p&gt;This might still seem fine at first glance- indeed, if Yumi stopped inputting stones after these first 4 nothing would&lt;br&gt;
seem broken- but a problem arises when a fifth new stone is added:&lt;br&gt;&lt;br&gt;
When &lt;code&gt;large_rock&lt;/code&gt; is added, we query RocksDB for the top 3 values. These should equal our output in the previous step - but didn't we let &lt;code&gt;Vec::sort&lt;/code&gt; pick the last stone basically at random? What does RocksDB pick? Well, RocksDB is sorted lexicographically- longer values are larger, and so &lt;code&gt;0.970&lt;/code&gt; is always bigger than &lt;code&gt;0.97&lt;/code&gt;. RocksDB has no fancy decimal libraries, for it &lt;code&gt;0.970&lt;/code&gt; is just a string. The third stone in RocksDB will &lt;em&gt;always&lt;/em&gt; be &lt;code&gt;evil_stone&lt;/code&gt;- meaning when &lt;code&gt;Vec::sort&lt;/code&gt; picked &lt;code&gt;medium_stone&lt;/code&gt; as the third stone in the previous step, a desynchronization is created between our actual previous output and the output we will calculate we sent.&lt;/p&gt;
&lt;h3&gt;
  
  
  This Makes Even a Machine Panic
&lt;/h3&gt;

&lt;p&gt;When we enter a new diff in this state (e.g. &lt;code&gt;("large_rock", 1.83): +1&lt;/code&gt;), these are the top 3&lt;br&gt;
results in RocksDB:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;stone_name&lt;/th&gt;
&lt;th&gt;weight&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;huge_rock&lt;/td&gt;
&lt;td&gt;2.45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;big_rock&lt;/td&gt;
&lt;td&gt;1.45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;evil_stone&lt;/td&gt;
&lt;td&gt;0.970&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;When we add this diff into the table and re-sort, the top 3 diffs change- &lt;code&gt;evil_stone&lt;/code&gt; is no longer in the top 3, replaced by &lt;code&gt;large_rock&lt;/code&gt;, so we must emit diffs that signify this change:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;("large_rock", 1.83): +1
("evil_stone", 0.970): -1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, during sorting, &lt;code&gt;Vec::sort&lt;/code&gt; (which believes &lt;code&gt;0.97&lt;/code&gt; and &lt;code&gt;0.970&lt;/code&gt; are equal) picked &lt;code&gt;medium_stone&lt;/code&gt; as the third diff, which means that the &lt;code&gt;("evil_stone", 0.970): -1&lt;/code&gt; diff is deleting the wrong entry- we &lt;em&gt;never outputted&lt;/em&gt; a positive &lt;code&gt;("evil_stone", 0.970): +1&lt;/code&gt;, we outputted &lt;code&gt;("medium_stone", 0.97): +1&lt;/code&gt; instead- so what are we outputting this negative &lt;code&gt;evil_stone&lt;/code&gt; diff for?&lt;/p&gt;

&lt;p&gt;This is where the error message at the start of this tale rears its unwelcome head- when this deletion of &lt;code&gt;evil_stone&lt;/code&gt; arrives at the final output of the engine, it has to be converted back into a regular row operation; in this case, the deletion of a &lt;code&gt;("evil_stone", 0.970)&lt;/code&gt; row from the engine's internal result database. But of course, there is no such row- our internal DB can't pop a value of which zero exist, and so it panics, like we saw before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error while executing IVM 5c9e2a10-5d9a-4b41-8c29-63e3a076c24c:
    thread 'tokio-runtime-worker' panicked at 'MACHINE! I know of no `evil_stone`! STOP asking me to delete it!
                                               Not enough values in db to pop 1 values (popped 0)'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although I'm only paraphrasing, our internal results DB is actually very polite.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So what did we learn?&lt;/p&gt;

&lt;p&gt;If we take a step back, the reason this bug exists is that we sorted our input in two different ways- once in-memory using &lt;code&gt;Vec::sort&lt;/code&gt;, and once by serializing our data and letting RocksDB sort it. We assumed these operations are equal, but with the introduction of precision in decimal numbers it turns out they're not.&lt;/p&gt;

&lt;p&gt;So try to not do that, if you can- to put it dryly, Don't Repeat Yourself.&lt;br&gt;&lt;br&gt;
But in this specific case, the issue is that we &lt;em&gt;need&lt;/em&gt; to use both methods- ditching RocksDB is obviously impossible, and using RocksDB to sort everything makes our sort 3x slower (I know this because the previous sort algorithm did just that). So what did we actually do? Well, that's a story for another time- and another post.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wait, but what about Yumi, her stone-stacking machine, and its absolutely incredible engine?
&lt;/h3&gt;

&lt;p&gt;I'm glad you asked. If you want to try Epsio's &lt;em&gt;blazingly fast&lt;/em&gt; (but actually just cheating) SQL engine, &lt;a href="https://www.epsio.io/"&gt;check us out here&lt;/a&gt;.&lt;sup id="fnref3"&gt;3&lt;/sup&gt;&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;The mathematically astute among you might realize that formally a modification can be typed as any &lt;a href="https://en.wikipedia.org/wiki/Abelian_group"&gt;commutative group&lt;/a&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;The exact reason why it is so vital to our Engine to represent data as diffs which can be consolidated is beyond the scope of this tale, you can simply assume it has to be this way. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;And if you want to know what happens to Yumi, I wholeheartedly recommend Brandon Sanderson's &lt;a href="https://www.goodreads.com/book/show/60726999-yumi-and-the-nightmare-painter"&gt;Yumi and the Nightmare Painter&lt;/a&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>sql</category>
      <category>rust</category>
      <category>programming</category>
      <category>database</category>
    </item>
  </channel>
</rss>
