example between_ helper and building SQL queries. #414
-
Hi Toby, If I were to implement a def between_(column, low, high):
"""
Helper to create an instance of the BETWEEN Operator.
Example:
>>> between_(x, 1, 10)
'x BETWEEN 1 AND 10'
Args:
column: an instance of Column.
low: the low end of the range.
high: the high end of the range.
Returns:
Between: an instance of Between
"""
return Between(this=column,
low=maybe_parse(low),
high=maybe_parse(high)) Are there examples that show creating various AST objects? How do I indicate that I need to parse a Literal instead of an identifier? I'm interested in generating SQL/HIVE queries programmatically. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
you can probably just call it between without the underscore can you do x between y and z? they don't necesasrily have to be literals right? you also need to do maybe_parse the column |
Beta Was this translation helpful? Give feedback.
-
yes, if you make a PR and add tests i will gladly accept it, thanks |
Beta Was this translation helpful? Give feedback.
-
Along the lines of def in_(column, expressions):
"""
Helper for sql IN operator.
Example:
>>> in_("x", (10, 20, 30)).sql()
'x IN (10, 20, 30)'
>>> parse_one("SELECT x FROM t").where(in_(x, (10, 20, 30))).sql()
'SELECT x FROM t WHERE x IN (10, 20, 30)'
Args:
column: an instance of Column or a column identifier.
expressions: a sequence of expression instances or literals.
Returns:
In: an instance of In
"""
return In(this=maybe_parse(column),
expressions=tuple(maybe_parse(e) for e in expressions)
) However, |
Beta Was this translation helpful? Give feedback.
you can probably just call it between without the underscore
can you do x between y and z? they don't necesasrily have to be literals right?
you also need to do maybe_parse the column