Skip to content
New issue

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

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

Already on GitHub? # to your account

select(distinct=...) may produce faulty sql for sqlite #316

Closed
cassiobotaro opened this issue Dec 18, 2015 · 6 comments
Closed

select(distinct=...) may produce faulty sql for sqlite #316

cassiobotaro opened this issue Dec 18, 2015 · 6 comments
Assignees
Labels

Comments

@cassiobotaro
Copy link
Contributor

This one works:

In [45]: db().select(db.player.country, distinct = True)
Out[45]: <Rows (40)>

In [46]: db._lastsql
Out[46]: 'SELECT DISTINCT player.country FROM player;'

But the other option given in the book does not:

In [47]: db().select(db.player.country, distinct = db.player.country)
OperationalError: near "ON": syntax error

In [48]: db._lastsql
Out[48]: 'SELECT DISTINCT ON (player.country) player.country FROM player;'

I didn't test it with other DB engines since I don't currently have one installed.

Related to web2py/web2py#1129

@cassiobotaro
Copy link
Contributor Author

Searching about this, I discover that sqlite don't has DISTINCT ON feature. How to proceed?

@gi0baro gi0baro added the bug label Dec 18, 2015
@gi0baro
Copy link
Member

gi0baro commented Dec 18, 2015

@niphlod @ilvalle @mdipierro any ideas?

@niphlod
Copy link
Member

niphlod commented Dec 18, 2015

This - and others - is the fruit of allowing sintaxes on a per-backend basis.
IMHO lots of postgresql-related features creeped in (as always, without tests) . This landed in the official docs.
To change the behaviour we'd need to change how queries are built, but I don't see any way around:

  • DAL allows it and it crashes at execution time, depending on the backend
  • DAL removes all code that is not compatible with each and every backend
  • DAL raises a syntaxerror depending on the URI

If specific-backend functionality has to be retained, DAL won't be the "universal adapter" that the docs point to (i.e. develop on postgresql, deploy on mysql, spun another instance on sqlite).

And if we retain specific functionality, there should be a clear matrix of backend/capabilities

@gi0baro
Copy link
Member

gi0baro commented Dec 18, 2015

@niphlod Ok, so we should schedule a discussion about this so that we include this in the refactoring of the adapters I've planned for the parsing topic..

@gi0baro
Copy link
Member

gi0baro commented Apr 19, 2016

@niphlod Damn, I ended the refactoring and forgotten this. We should re-start a discussion.
I vote for the SyntaxError on sqlite when using DISTINCT ON.

@gi0baro gi0baro self-assigned this Apr 19, 2016
@niphlod
Copy link
Member

niphlod commented Apr 20, 2016

start away, my previous semi-detailed comment pretty much lays every path that can be taken.

gi0baro added a commit to gi0baro/pydal that referenced this issue Apr 21, 2016
mdipierro added a commit that referenced this issue Apr 24, 2016
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants