Sqlite varint decoding Stage #SZ4

I’m stuck on Stage #SZ4

I’m trying to understand where does this part come from? How does 8147 become 199?

81 47  // Serial type for sqlite_schema.sql (varint):      199
       // Size of sqlite_schema.sql =                      (199-13)/2 = 93

@rodio Reading varints conceptually works like this:

  1. Extract the lowest 7 bits from every byte
  2. Concat the 7-bit groups
  3. Interpret the concatenated binary number

In this case:

0x81 == 1000 0001 (The leading 1 means continue reading.)
extract: 000 0001

0x47 == 0100 0111 (The leading 0 means stop reading.)
extract: 100 0111 

Concat 000 0001 + 100 0111 
    == 000 0001   100 0111
    == 000 000   1100 0111
    ==           1100 0111 (This is 199 in decimal.)

Even though the provided explanation solves the issue, it does not explain why the official SQLite4 documentation for varint encoding/decoding provides an algorithm that does not match this.

See SQLite4: Variable-Length Integers .
The algorithm informs that for V <= 240, the output is just V as-is.
So, 199 should be encoded as-is in hexadecimal form: 0xC7 and not in a pair of bytes: 0x8147.

1 Like

@Tom-L-M Thanks for the SQLite4 doc! There are different flavors of varint encodings, for example SQLite3 vs SQLite4.

According to the home page of SQLite4:

SQLite4 was an experimental rewrite of SQLite that was active from 2012 through 2014. All development work on SQLite4 has ended… SQLite4 was never released. There are no plans to revive it. You should be using SQLite3.

Our examples and tester match the official SQLite3 implementation.

1 Like

@Tom-L-M That was my mistake initially as well! I found the paragraph about varints in SQLite3 documentation to be too complicated and googled for another explanation and found myself on the SQLite4 page.

1 Like

I remember getting confused by this doc too!

Would love to see if (a) we can mention this in the instructions and (b) we can work with the SQLite folks to get a warning added to that page (and any other similar docs).

@andy1li can we look into these two please?

2 Likes

@rohitpaulk Will do!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.