Re:Show HN: Inline SQL in any Python program
by ekzhang on Saturday October 08, 2022 @03:43PM
(#33135352)
Thanks for the feedback orf! I agree it looks strange at first, this is very experimental so I played a bit with syntax design choices. Sometimes for DSLs it's nice to have a bit of flexibility. You can compare it to the %timeit, %cython "magic" statements in Jupyter.
We could use a standard function application, but that doesn't convey the point that there's some DSL embedding going on here, such as variable substitution from the outside scope. It's also less nice to end multi-line queries with the string: """).
Other reasons: the bitwise XOR operator is almost never used in Python, so it's highly unlikely that people would confuse it as such (this is also why it's formatted without a space before the operator). Open to other proposals for syntax though.
Re:Show HN: Inline SQL in any Python program
by rubenfiszel on Saturday October 08, 2022 @03:36PM
(#33135300)
Very cool, I have lots of ideas of how to leverage it windmill.dev. We were using psycopg2 for all our examples but this is much cleaner. Thanks for sharing!
Re:Show HN: Inline SQL in any Python program
by rubenfiszel on Saturday October 08, 2022 @03:52PM
(#33135428)
Something was not clear to me from the README. When you do string interpolations as in your examples, those are actually prepared sql statements right ? Otherwise it would open the way for sql injections.
Re:Show HN: Inline SQL in any Python program
by ekzhang on Saturday October 08, 2022 @04:13PM
(#33135606)
Yep, they're prepared SQL statements. Literal string interpolation would be unacceptable in any self-respecting SQL library: it's parameters. Good question though!
Re:Show HN: Inline SQL in any Python program
by pizza on Saturday October 08, 2022 @06:35PM
(#33136682)
This is awesome. Is there a way to get it to work with e.g. dataclasses-sql? I am thinking of something such as
stuff: list[mydataclass]
sql_val^ "select * from stuff where stuff.field = 2"
over any kind of 'fmappable' container; perhaps eg.
stuff: dict[int, mydataclass]
stuff2: dict[int, mydataclass]
sql_val^ "select (s2.val, s.key) from stuff as s, stuff2 as s2 where s.key > s2.key and s2.value != s.value)"
Re:Show HN: Inline SQL in any Python program
by pizza on Tuesday October 11, 2022 @02:15PM
(#33166880)
Ah interesting. I was thinking along the lines of if you wanted to, say, package up an algorithm as a class, make a ton of these classes/algorithms, and then affix them in a bunch of ways. E.g. "find the algorithms that take in data and spit out predictions, compose them with the algorithms that have a prediction_input field" etc.
We could use a standard function application, but that doesn't convey the point that there's some DSL embedding going on here, such as variable substitution from the outside scope. It's also less nice to end multi-line queries with the string: """).
Other reasons: the bitwise XOR operator is almost never used in Python, so it's highly unlikely that people would confuse it as such (this is also why it's formatted without a space before the operator). Open to other proposals for syntax though.
Even so, pandas has the same magic substitution going on with just a standard function call, and it’s somewhat intuitive.
However, regardless of the syntax nitpicking, this is a cool project. Nice one!