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

DM-17720: Improve user expression handling in preflight #129

Merged
merged 3 commits into from Feb 11, 2019

Conversation

andy-slac
Copy link
Contributor

User expression is now parsed by preflight and then translated into
sqlalchemy clause expression. It now also supports using link names
instead of table.column syntax in expressions.

Copy link
Member

@TallJimbo TallJimbo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I think this is fine as is (and a very nice improvement in functionality), but I had a couple of comments that might conceivably make it better.

expression : `exprParser.Node`
Parsed user expression tree.
dimensions : `DimensionGraph`
All Dimensions included in this query.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How hard would it be to make this all dimensions known to the registry (which is now its own class, DimensionUniverse, by the way), and then expand the query to include dimensions referenced in the expression but not the dataset types?

I could imagine it being useful to be able to run single-epoch processing on all visits that overlap a tract, for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible in principle but it's probably non very trivial. Make a new ticket if you want that extra feature 🙂

Exception
Any exception raised by SQLAlchemy is propagated to caller.
"""
if isinstance(expression, (exprParser.StringLiteral, exprParser.NumericLiteral)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to transform this conditional block into delegation to a polymorphic method on the exprParser classes that returns a ClauseElement?

All of these isinstance checks make me think of a classic OO antipattern, but I could also interpret this as the kind of match on algebraic sum types that would be quite natural in a language like Rust or OCaml that supported them, and in that case I suppose I don't have an objection to trying to fake that in Python.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about adding that facility to exprParser tree classes but decided that I want to keep that code free of extra dependencies like sqlalchemy - if we want to support few additional tree transformations it would not be right design to do all of that inside tree itself. You are right that this is a common problem and some languages have better support for implementing this sort of double-dispatch code. I'll see if I can add "better" visitor patter support for those classes if it does not make things too complicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TallJimbo, I have re-implemented double dispatch using visitor pattern, could you check latest commit d11f7e4 to see if that smells better now.

Copy link
Member

@TallJimbo TallJimbo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, nice work!

XOR is not universally supported in SQL grammar and is not in sqlalchemy
which makes it hard to translate it to sqlalchemy expression. It's not
really necessary and it can be expressed by other means.
User expression is now parsed by preflight and then translated into
sqlalchemy clause expression. It now also supports using link names
instead of table.column syntax in expressions.
It adds a little bit of code but it is cleaner solution overall and it
should be safer for potential grammar extensions.
@andy-slac andy-slac merged commit 231448d into master Feb 11, 2019
@timj timj deleted the tickets/DM-17720 branch April 22, 2020 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants