Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table.xindexes using PRAGMA index_xinfo(table) #261

Closed
simonw opened this issue May 29, 2021 · 5 comments
Closed

table.xindexes using PRAGMA index_xinfo(table) #261

simonw opened this issue May 29, 2021 · 5 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Owner

simonw commented May 29, 2021

PRAGMA index_xinfo(table) DOES return that data:

(Pdb) [c[0] for c in fresh_db.execute("PRAGMA > index_xinfo('idx_dogs_age_name')").description]
['seqno', 'cid', 'name', 'desc', 'coll', 'key']
(Pdb) fresh_db.execute("PRAGMA index_xinfo('idx_dogs_age_name')").fetchall()
[(0, 2, 'age', 1, 'BINARY', 1), (1, 0, 'name', 0, 'BINARY', 1), (2, -1, None, 0, 'BINARY', 0)]

See https://sqlite.org/pragma.html#pragma_index_xinfo

Example output: https://covid-19.datasettes.com/covid?sql=select+*+from+pragma_index_xinfo%28%27idx_ny_times_us_counties_date%27%29
Originally posted by @simonw in #260 (comment)

@simonw simonw added the enhancement New feature or request label May 29, 2021
@simonw
Copy link
Owner Author

simonw commented Jun 3, 2021

This would be a breaking change - and the fact that it returns auxiliary columns isn't particularly useful for most cases - "Auxiliary columns are additional columns needed to locate the table entry that corresponds to each index entry".

Instead, I'm going to add a new property table.xindexes which exposes this.

@simonw simonw changed the title table.indexes should use 'PRAGMA index_xinfo(table)` table.xindexes using 'PRAGMA index_xinfo(table)` Jun 3, 2021
@simonw simonw changed the title table.xindexes using 'PRAGMA index_xinfo(table)` table.xindexes using PRAGMA index_xinfo(table) Jun 3, 2021
@simonw
Copy link
Owner Author

simonw commented Jun 3, 2021

In prototyping this out I realize that I actually want to get back the name of each index, then for each of them the detailed list of index columns. Here's the test from my initial prototype:

def test_xindexes(fresh_db):
    fresh_db.executescript(
        """
        create table Gosh (c1 text, c2 text, c3 text);
        create index Gosh_c1 on Gosh(c1);
        create index Gosh_c2c3 on Gosh(c2, c3 desc);
    """
    )
    assert fresh_db["Gosh"].xindexes == [
        (
            "Gosh_c2c3",
            [
                XIndex(seqno=0, cid=1, name="c2", desc=0, coll="BINARY", key=1),
                XIndex(seqno=1, cid=2, name="c3", desc=1, coll="BINARY", key=1),
                XIndex(seqno=2, cid=-1, name=None, desc=0, coll="BINARY", key=0),
            ],
        ),
        (
            "Gosh_c1",
            [
                XIndex(seqno=0, cid=0, name="c1", desc=0, coll="BINARY", key=1),
                XIndex(seqno=1, cid=-1, name=None, desc=0, coll="BINARY", key=0),
            ],
        ),
    ]

@simonw
Copy link
Owner Author

simonw commented Jun 3, 2021

I'm going to return XIndex(name, columns) - where columns is a list of XIndexColumn.

@simonw
Copy link
Owner Author

simonw commented Jun 3, 2021

New design:

def test_xindexes(fresh_db):
    fresh_db.executescript(
        """
        create table Gosh (c1 text, c2 text, c3 text);
        create index Gosh_c1 on Gosh(c1);
        create index Gosh_c2c3 on Gosh(c2, c3 desc);
    """
    )
    assert fresh_db["Gosh"].xindexes == [
        XIndex(
            name="Gosh_c2c3",
            columns=[
                XIndexColumn(seqno=0, cid=1, name="c2", desc=0, coll="BINARY", key=1),
                XIndexColumn(seqno=1, cid=2, name="c3", desc=1, coll="BINARY", key=1),
                XIndexColumn(seqno=2, cid=-1, name=None, desc=0, coll="BINARY", key=0),
            ],
        ),
        XIndex(
            name="Gosh_c1",
            columns=[
                XIndexColumn(seqno=0, cid=0, name="c1", desc=0, coll="BINARY", key=1),
                XIndexColumn(seqno=1, cid=-1, name=None, desc=0, coll="BINARY", key=0),
            ],
        ),
    ]

@simonw simonw closed this as completed in 9c67cb9 Jun 3, 2021
@simonw
Copy link
Owner Author

simonw commented Jun 3, 2021

simonw added a commit that referenced this issue Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant