SQLite Recipes

Change the batch size of the result set

Recipe source: sqlite_batch_rows.py

The ADBC SQLite driver allows control over the size of batches in result sets. Because the driver performs type inference, this also controls how many rows the driver will look at to figure out the type. If you know your result set has many NULL rows up front, you may consider increasing the batch size so that the driver can infer the correct types.

28import adbc_driver_sqlite.dbapi
29
30conn = adbc_driver_sqlite.dbapi.connect()

First we’ll set up a demo table with 1024 NULL values.

34with conn.cursor() as cur:
35    cur.execute("CREATE TABLE demo (val TEXT)")
36
37    cur.execute(
38        """
39    WITH RECURSIVE series(n) AS (
40        SELECT 1
41        UNION ALL
42        SELECT n + 1
43        FROM series
44        WHERE n + 1 <= 1024
45    )
46    INSERT INTO demo (val)
47    SELECT NULL
48    FROM series
49    """
50    )
51
52    cur.execute("INSERT INTO demo VALUES ('foo'), ('bar'), ('baz')")

If we query the table naively, we’ll get an error, because the driver first looks at the first 1024 values to determine the column type. But since every value is NULL, it falls back to the default type of int64, which poses a problem when it then encounters a string in the next batch.

59with conn.cursor() as cur:
60    try:
61        cur.execute("SELECT * FROM demo")
62        print(cur.fetchallarrow().schema)
63    except OSError as e:
64        print(e)
65        # Output:
66        # [SQLite] Type mismatch in column 0: expected INT64 but got STRING/BINARY
67    else:
68        raise RuntimeError("Expected an error")

We can tell the driver to increase the batch size (and hence look at more rows).

73with conn.cursor() as cur:
74    cur.adbc_statement.set_options(
75        **{
76            adbc_driver_sqlite.StatementOptions.BATCH_ROWS.value: 2048,
77        }
78    )
79    cur.execute("SELECT * FROM demo")
80    print(cur.fetchallarrow().schema)
stdout
[SQLite] Type mismatch in column 0: expected INT64 but got STRING/BINARY
val: string