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

sqlite3 cursor.description seems to rely on incomplete statement parsing for detection #65917

Closed
zzzeek mannequin opened this issue Jun 11, 2014 · 14 comments
Closed

sqlite3 cursor.description seems to rely on incomplete statement parsing for detection #65917

zzzeek mannequin opened this issue Jun 11, 2014 · 14 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@zzzeek
Copy link
Mannequin

zzzeek mannequin commented Jun 11, 2014

BPO 21718
Nosy @rbtcollins, @bitdancer, @berkerpeksag
Files
  • with2.diff: fix
  • f67fa9c898a4713850e16934046f0fe2cba8c44c.patch
  • issue21718_tests.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2016-08-21.16:29:53.083>
    created_at = <Date 2014-06-11.13:11:09.718>
    labels = ['type-bug', 'library']
    title = 'sqlite3 cursor.description seems to rely on incomplete statement parsing for detection'
    updated_at = <Date 2016-08-21.16:32:28.512>
    user = 'https://bugs.python.org/zzzeek'

    bugs.python.org fields:

    activity = <Date 2016-08-21.16:32:28.512>
    actor = 'tanner'
    assignee = 'ghaering'
    closed = True
    closed_date = <Date 2016-08-21.16:29:53.083>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2014-06-11.13:11:09.718>
    creator = 'zzzeek'
    dependencies = []
    files = ['36952', '40184', '42354']
    hgrepos = []
    issue_num = 21718
    keywords = ['patch']
    message_count = 14.0
    messages = ['220263', '220268', '229537', '231424', '231439', '233385', '240124', '240135', '243933', '248066', '248639', '262791', '273301', '273303']
    nosy_count = 7.0
    nosy_names = ['ghaering', 'rbcollins', 'r.david.murray', 'zzzeek', 'python-dev', 'berker.peksag', 'tanner']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue21718'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6']

    @zzzeek
    Copy link
    Mannequin Author

    zzzeek mannequin commented Jun 11, 2014

    Per DBAPI and pysqlite docs, .description must be available for any SELECT statement regardless of whether or not rows are returned. However, this fails for SELECT statements that aren't simple "SELECT"s, such as those that use CTEs and therefore start out with "WITH:":

    import sqlite3
    conn = sqlite3.connect(":memory:")
    
    cursor = conn.cursor()
    cursor.execute("""
        create table foo (id integer primary key, data varchar(20))
    """)

    cursor.execute("""
    insert into foo (id, data) values (10, 'ten')
    """)

    cursor.execute("""
        with bar as (select * from foo)
        select * from bar where id = 10
    """)

    assert cursor.description is not None

    cursor.execute("""
        with bar as (select * from foo)
        select * from bar where id = 11
    """)

    assert cursor.description is not None

    the second statement returns no rows and cursor.description is None. Libraries like SQLAlchemy which rely on this to determine that the statement supports fetchone() and similar are blocked.

    @zzzeek zzzeek mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jun 11, 2014
    @bitdancer
    Copy link
    Member

    It is true that the sqlite interface does not support WITH currently. It is an interesting question whether or not we could change this as a bug fix or not...I suppose it depends on whether or not it changes any behavior other than making .description work.

    @tanner
    Copy link
    Mannequin

    tanner mannequin commented Oct 16, 2014

    this patch fixes the bug.
    It parses the with WITH statement.

    @tanner
    Copy link
    Mannequin

    tanner mannequin commented Nov 20, 2014

    ping
    I'd appreciate a review of my patch.

    @bitdancer
    Copy link
    Member

    It's more a matter of my finding time to fully research the problem and solution than an issue of reviewing the patch itself. There is also potential cross-talk with other patches involving sqllite statement parsing. Since the sqlite wrapper doesn't currently have an active maintainer, I'm afraid it may be a bit before any review of these issues happen. If you can get other community members (doesn't have to be committers) to do reviews (of the concepts as well as the patch itself) that would be helpful. (python-list might be a place to recruit some interest.)

    (Personally I find the fact the the wrapper has to do statement parsing at all to be problematic, but there may be no good solution to that problem when you consider backward compatibility issues.)

    @ghaering
    Copy link
    Mannequin

    ghaering mannequin commented Jan 4, 2015

    I have now committed a fix in the pysqlite project at github. ghaering/pysqlite@f67fa9c

    I'll eventually merge it into the Python tree.

    @ghaering ghaering mannequin self-assigned this Jan 4, 2015
    @tanner
    Copy link
    Mannequin

    tanner mannequin commented Apr 5, 2015

    Are you going to merge it into 2.7.10?

    @bitdancer
    Copy link
    Member

    It looks to me like a patch that could be merged as a bug fix.

    @tanner
    Copy link
    Mannequin

    tanner mannequin commented May 23, 2015

    is this going to be fixed in 2.7.10?

    @rbtcollins
    Copy link
    Member

    @gerhard would you like that ported to cPython for you?

    @tom - I think that if the patch applies to 2.7.x we should apply it there since its very unlikely to break non-buggy code.

    @bitdancer
    Copy link
    Member

    Attached is the patch ported to python2.7. However, the test doesn't fail for me before I apply (compile) the fix.

    @berkerpeksag
    Copy link
    Member

    I adapted the reproducer in msg220263 and added more tests.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 21, 2016

    New changeset 45ef062734d6 by Berker Peksag in branch '3.5':
    Issue bpo-21718: cursor.description is now available for queries using CTEs
    https://hg.python.org/cpython/rev/45ef062734d6

    New changeset cf18375732ae by Berker Peksag in branch 'default':
    Issue bpo-21718: Merge from 3.5
    https://hg.python.org/cpython/rev/cf18375732ae

    @tanner
    Copy link
    Mannequin

    tanner mannequin commented Aug 21, 2016

    will the fix be backported to 2.7?

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants