Live data from Hacker News

Generating SQL with LLMs for fun and profit

iamnotarobot.substack.com

21–29 of 29 posts

Re: Generating SQL with LLMs for fun and profit

#21
post #13

Earlier quoted context omitted.

> If a bad-actor can issue these commands against your DB, you are already toast! Don't overlook the damage potential of a fresh-faced college-hire on-call at 2am with dba access to prod

Again, you are already toast. No one should have access to prod except the promote automation.

I think for many (though not necessarily most) busineses, especially micro-ISVs running a SaaS, the business cost of doing what they "should" be doing far exceeds the actual cost of letting bad-things-happen and paying for the cleanup afterwards.

As the designated "code-and-Red Bull-guy" at the micro-ISV I work at, I'll admit that I've unintentionally nuked the production DB at 2am - but fortunately our cloud database provider could do a point-in-time restore and everything was fully operational again by 2:30am. That's because the cost of setting up infrastructure and procedures to eliminate the need to ever manually run DML/DDL against our prod databases would be... probably a multiple of my salary - and be required indefinitely into the future (as that infrastructure would have to be maintained as the database's design changes over time too) - whereas the cost of having PITR on our prod databases in Azure is... a rounding error.

So yes, our prod is going to go down in future - we can't afford not to, honestly (it's a USA-only B2B SaaS, we get literally zero usage before 6am EST and after 7pm PST).

Re: Generating SQL with LLMs for fun and profit

#22

Earlier quoted context omitted.

> If a bad-actor can issue these commands against your DB, you are already toast! Don't overlook the damage potential of a fresh-faced college-hire on-call at 2am with dba access to prod

I remember having a test suite that would connect to a local db running in a docker container and would nuke the tables and then set up the records in a known state before running through. Worked great until someone changed the connection string to point at an actual database.

One possible trick you might consider is to (manually!) add objects to the DB's schema that explicitly indicate its environment, e.g. `CREATE TABLE dbo.ThisIsProductionYouSillyPerson ( DummyCol int NOT NULL );` for prod and `CREATE TABLE dbo.ThisIsTestFeelFreeToMessAround ( DummyCol int NOT NULL );` - these tables would be excluded from the automated DB deployment code - and write test scripts that all start by checking that the `dbo.ThisIsTestFeelFreeToMessAround` table exists and that the `dbo.ThisIsProductionYouSillyPerson` table does not exist in the DB before continuing.

DB automation is great for preventing mistakes during common routine operations, but because DB automation can also go haywire and delete drop all-by-itself unless you set-up out-of-band (if that's the right term?) safeguards. Having airgapped dev-test-staging-and-prod won't help you if if you forgot the `WHERE` in an `UPDATE` in a little-used script that the prod automation uses, that testing never discovered (which happens all the time, it's scary).

I do appreciate how MySQL does come with an `UPDATE-without-key` guard, but I'm surprised none of the other RDBMS have safety-guards like that - just a simple `RequireManualConfirmationForMultiRowDml` flag on a table would help.

Re: Generating SQL with LLMs for fun and profit

#23
post #8

Earlier quoted context omitted.

No complaint. It's more of a warning about how the main players (OpenAI, LangChain) share notebooks and cookbooks that illustrate how to make the LLMs "query" the databases. At the very least one would expect some language telling people to not do that in production. And it's not unique to SQL, this is just an extreme example.

> At the very least one would expect some language telling people to not do that in production. And it's not unique to SQL, this is just an extreme example. In professional communication, is it necessary to repeat the obvious all the time? Does an article in a medical journal or a law journal need to explicitly remind its readers of 101 level stuff? If an unqualified person reads the article, misinterprets it because…

> In professional communication, is it necessary to repeat the obvious all the time?

Based on the “repeat” dev articles I’ve seen on HN over the many years and the “repeat” mistakes replicated in the actual workplace, I think it is necessary.

> Why should software engineering be any different?

I don’t think it is. But also see my point below.

I understand the example you were trying to use but it wasn’t very effective. Dev blogs are not equivalent to medical or law journals in many ways that I don’t need to list. Academic computer science white papers are a bit closer.

Thinking about this more, in my experience and across multiple fields, I always see a phenomenon where either colleagues/classmates/whoever reference a _popular_ but _problematic_ resource which leads to a shitshow.

Re: Generating SQL with LLMs for fun and profit

#24
In my tool I let users generate SQL using ChatGPT: https://www.timestored.com/pulse/tutorial/chatgpt-sql-query-... However it's their own hosted database and I show them the query for them to run beforehand. For anyone interested in pushing this further, the best paper I found was "Evaluating the Text-to-SQL Capabilities of Large Language Models". It examines which prompts work best and was how I decided on sending schema / create details etc. as part of the initial prompt. Since I create the UI and show the schema as a tree, I can generate that part of the prompt without the users involvement.

Re: Generating SQL with LLMs for fun and profit

#25

Earlier quoted context omitted.

> At the very least one would expect some language telling people to not do that in production. And it's not unique to SQL, this is just an extreme example. In professional communication, is it necessary to repeat the obvious all the time? Does an article in a medical journal or a law journal need to explicitly remind its readers of 101 level stuff? If an unqualified person reads the article, misinterprets it because…

> In professional communication, is it necessary to repeat the obvious all the time? Based on the “repeat” dev articles I’ve seen on HN over the many years and the “repeat” mistakes replicated in the actual workplace, I think it is necessary. > Why should software engineering be any different? I don’t think it is. But also see my point below. I understand the example you were trying to use but it wasn’t very effectiv…

> Dev blogs are not equivalent to medical or law journals in many ways that I don’t need to list. Academic computer science white papers are a bit closer.

Okay, there are law blogs and medicine blogs too, which are directly comparable to dev blogs. And by that I mean blogs targeted at legal and medical professionals, not blogs on those topics targeted at consumers. For example, BMJ's Frontline Gastroenterology blog [0], whose target audience is practicing and trainee gastroenterologists, and its authors write for their target audience – it is public and anyone can read it, but I don't think the authors spend too much time worrying "what if an unqualified person reads this and misinterprets it due to a lack of basic medical knowledge?"

Or similarly, consider Opinio Juris, the most popular international law blog on the Internet. When a blog post contains the sentence "As most readers will know, lex specialis was created by the International Court of Justice in the Nuclear Weapons Case, to try to explain the relationship between international humanitarian law (IHL) and international human rights law (IHRL)", [1] you know you are not reading something aimed at a general audience.

[0] https://blogs.bmj.com/fg/

[1] http://opiniojuris.org/2020/01/13/the-soleimani-case-and-the...

Re: Generating SQL with LLMs for fun and profit

#27

Earlier quoted context omitted.

> In professional communication, is it necessary to repeat the obvious all the time? Based on the “repeat” dev articles I’ve seen on HN over the many years and the “repeat” mistakes replicated in the actual workplace, I think it is necessary. > Why should software engineering be any different? I don’t think it is. But also see my point below. I understand the example you were trying to use but it wasn’t very effectiv…

> Dev blogs are not equivalent to medical or law journals in many ways that I don’t need to list. Academic computer science white papers are a bit closer. Okay, there are law blogs and medicine blogs too, which are directly comparable to dev blogs. And by that I mean blogs targeted at legal and medical professionals, not blogs on those topics targeted at consumers. For example, BMJ's Frontline Gastroenterology blog […

> but I don't think the authors spend too much time worrying "what if an unqualified person reads this and misinterprets it due to a lack of basic medical knowledge?"

1) You don’t sound too sure about this. Your previous comment sounded like speculation also. Do you actually read these blogs and/or journals?

2) Again, you’re making comparisons that aren’t equivalent. Your argument fails when you replace “unqualified person” with “unqualified target person”. My pizza delivery driver is not reading dev blogs. The junior and senior engineers on my team over the years who passed 5 rounds of interviews yet still make simple but devastating mistakes are reading these blogs.

> lex specialis

1) In your previous comment, you said that medical and law journals _don’t_ explain every basic little thing. And now you provided a quote where the law blog is explicitly explaining a very basic thing even to their _qualified target audience_. If “most readers” already know something, then what’s the point of re-explaining it? You’re proving my point instead.

2) Another comparison that isn’t equivalent. Even if an “unqualified” person were to read a _professional_ law or medical blog/journal, what’s the worst that could happen? Nothing.

The answer to that question above will definitely change if we’re talking about _nonprofessional_ content (e.g. TikTok law and medical advice). Frankly, more dev blogs veer towards the “unprofessional” side than “professional”.

Re: Generating SQL with LLMs for fun and profit

#28

Earlier quoted context omitted.

> Dev blogs are not equivalent to medical or law journals in many ways that I don’t need to list. Academic computer science white papers are a bit closer. Okay, there are law blogs and medicine blogs too, which are directly comparable to dev blogs. And by that I mean blogs targeted at legal and medical professionals, not blogs on those topics targeted at consumers. For example, BMJ's Frontline Gastroenterology blog […

> but I don't think the authors spend too much time worrying "what if an unqualified person reads this and misinterprets it due to a lack of basic medical knowledge?" 1) You don’t sound too sure about this. Your previous comment sounded like speculation also. Do you actually read these blogs and/or journals? 2) Again, you’re making comparisons that aren’t equivalent. Your argument fails when you replace “unqualified…

> 1) You don’t sound too sure about this. Your previous comment sounded like speculation also. Do you actually read these blogs and/or journals?

I have read some of them before. Not Frontiers in Gastroenterology, but I have spent a lot of time reading psychology/psychiatry journals, since they have some personal relevance. Some of my favourite papers are https://link.springer.com/article/10.1007/s40489-016-0085-x and https://www.nature.com/articles/s41398-019-0631-2 and also (not a paper, a letter to the editor) https://www.ncbi.nlm.nih.gov/pmc/articles/PMC9054657/

I have in the past read https://dontforgetthebubbles.com/ which is a paediatrics blog–again, has some personal relevance.

I also am very interested in law. I actually applied to law school once, but didn't get in, and gave up on the idea after that. If they'd accepted me, I might have been a lawyer right now rather than a software engineer. Public international law was always an area of particular fascination for me. I remember being at university, and I was supposed to be at my CS lecture, but instead I was in the library reading books like Restatement (Third) of the Foreign Relations Law of the United States and some textbook (I forget its name now) I found on the EU treaties and ECJ case law. So yes, I do read law blogs sometimes. I went through a period when I was reading SCOTUSblog a lot. Not a blog, but I actually enjoy reading stuff like this: https://legal.un.org/ilc/texts/instruments/english/draft_art...

> And now you provided a quote where the law blog is explicitly explaining a very basic thing even to their _qualified target audience_. If “most readers” already know something, then what’s the point of re-explaining it? You’re proving my point instead.

Even that quoted sentence is assuming the reader already knows what "international humanitarian law" and "international human rights law" are, and what is the difference between them. There are also many cases in that post in which (unlike lex specialis) the author uses technical legal terminology without ever explaining it: for example, his repeated invocation of jus ad bellum, or his mention of the "Inter-American System". Another example is where he cites the Vienna Convention on the Law of Treaties, which assumes the reader understands its significance.

> Even if an “unqualified” person were to read a _professional_ law or medical blog/journal, what’s the worst that could happen? Nothing.

For a medical journal – a person reads an article about using drug X to treat condition Y. They then proceed to misdiagnose themselves with condition Y, and then somehow acquire drug X without having been prescribed it, and start taking it. A person could cause quite serious medical harm to themselves in this way. Reading medical journals can also contribute to the development of illness anxiety disorder – you don't need to be a medical student to develop medical student's disease.

For a law journal - a criminal defendant reads something in a law journal and thinks it helps their case. Their lawyer tries to explain to them that they are misunderstanding it and it isn't actually relevant to their situation, but they refuse to listen. They fire their lawyer and then try to argue in court based on that misunderstanding. It is easy to see how they could end up with a significantly worse outcome as a result, maybe even many extra years in prison.

Conversely, our 10 year old sometimes write Python programs. They aren't anything special, but better than I could do at his age. I bet you his Python programs are full of security holes and nasty bugs and bad practices. Who cares, what possible harm could result? And he isn't at the stage yet of reading development blogs, but I've seen him before copying code off random websites he found, so maybe he has stumbled on to one of them. My brother is a (trainee) oncologist, but he did an introductory programming course as an undergrad, and he wrote some Python programs in that too, although he hasn't done any programming in years–what harm could have his programs done? If he started trying to modify the software in one of the radiation therapy machines, I'd be worried (but he's too responsible for that); if he decided to try writing a game in Python for fun, why should anyone worry, no matter what the quality of his code is?

Post reply on HN