News for hackers. Stuff that matters.
Re:Advent of Code 2024 in pure SQL by scop on Thursday January 02, 2025 @02:35PM (#42577887)
I reacted to this title the way I react a new menu item at Taco Bell: a strange mixture of desire, shame, and admiration for human ingenuity.
Re:Advent of Code 2024 in pure SQL by qsort on Thursday January 02, 2025 @02:52PM (#42578084)
I work a lot with databases and I've seen... stuff. It's not as bad as you might think if you know what you are doing. Most RDBMSs support recursive CTEs, it feels like writing Prolog with a slightly sadistic syntax. For something like AoC the most difficult part is probably parsing the input.
Re:Advent of Code 2024 in pure SQL by dunham on Thursday January 02, 2025 @03:26PM (#42578459)
Speaking of parsing, back around y2k we were building an app that used XML everywhere, which was the style at the time, and our DBA wanted to write an xml parser in SQL (the api would involve sending XML to the database). That got vetoed.

IMO, this kind of thing is what AoC is good for - you get to play with weird/obscure stuff without affecting your day job code.

Re:Advent of Code 2024 in pure SQL by jamie_ca on Thursday January 02, 2025 @03:40PM (#42578603)
I did something with JSON back before there was reasonable native support - it's certainly not robust, but it handled a few syntax variants for a use case where we had an extra attribute column that serialized JSON, and wanted to surface one of the fields as a proper column on the table.

https://blog.tracefunc.com/2011/11/19/parsing-json-in-sql-ht...

Re:Advent of Code 2024 in pure SQL by jmg_ on Thursday January 02, 2025 @07:04PM (#42580498)
Funnily, I’m actively working on rewriting a stored procedure which parses an XML snippet as one of its arguments

Luckily it’s not a handwritten XML parser though: https://learn.microsoft.com/en-us/sql/t-sql/functions/openxm...

Re:Advent of Code 2024 in pure SQL by yazmeya on Friday January 03, 2025 @04:35PM (#42589738)
Just around the same time I was working at a place that used Oracle's web app extension, with CGI endpoints written completely in PL/SQL. I did end up writing an XML parser/serializer for it.
Re:Advent of Code 2024 in pure SQL by emmelaich on Thursday January 02, 2025 @07:39PM (#42580768)
Thanks for that comment.

I laughed aloud at "It's not as bad as you might think if you know what you are doing."

... because that pretty much describes all human activity :-)

Re:Advent of Code 2024 in pure SQL by AdieuToLogic on Thursday January 02, 2025 @07:46PM (#42580833)
> Most RDBMSs support recursive CTEs, it feels like writing Prolog with a slightly sadistic syntax.

Which makes sense as both are declarative logic-based languages. IMHO, SQL and Prolog fundamentally have much in common.

Re:Advent of Code 2024 in pure SQL by marxisttemp on Friday January 03, 2025 @06:43AM (#42584783)
I did a semester at the university of Edinburgh and took database systems and logic programming at the same time, and I definitely felt the synergy between them.
Re:Advent of Code 2024 in pure SQL by UltraSane on Thursday January 02, 2025 @11:27PM (#42582427)
A company I worked for uses Syteline ERP which heavily relies on SQL Server. But the DBA was constantly complaining about how the Syteline SQL was. One major issue was long running transactions taking 10 minutes locking rows/tables for too long and using a lot of memory. You would think very expensive ERP systems would have decent SQL.
Re:Advent of Code 2024 in pure SQL by ulrikrasmussen on Friday January 03, 2025 @12:00AM (#42582619)
It is closer to Datalog I think, or can you express cut? CTEs are fairly restricted compared to logic programmming languages though, at least for Postgres. In particular, relations cannot be mutually recursive and your rules may only be linearly recursive in themselves (i.e. can contain only one instance of themselves in the right hand side). Postgres is overly restrictive in the latter and requires at most once recursive reference over all subqueries in the UNION even though it would be safe to only restrict the number of recursive calls for each subquery (each corresponding to a separate Datalog rule for the same relation). It is possible to work around that restriction using a local WITH expression (a hack really), but then you are also on your own since it disables all checks and allows you to write rules which uses actual nonlinear recursion and will give incorrect result sets when evaluated.

I really would like Postgres to have proper support for writing Datalog queries, and with better and more efficient incremental evaluation algorithms as opposed to the iterative semi-naive algorithm that is only supported now.

Re:Advent of Code 2024 in pure SQL by harrall on Friday January 03, 2025 @12:57AM (#42582947)
Haven’t written SQL in a while (and I used to write a lot) but I think SQL Server recursive CTEs are fairly unbounded so it’s just a Postgres limitation unfortunately.

(I’m a fan of MS SQL but it’s Microsoft and also hard to financially justify for many companies. But if you ever get to use it, it is a very solid RDBMS, even if the rest of your stack is open source.)

Re:Advent of Code 2024 in pure SQL by bob1029 on Friday January 03, 2025 @04:35AM (#42584119)
The cost of MSSQL is largely controlled by how the system is designed and the complexity of the business.

The model I am most familiar with is a 10-20 employee B2B SaaS startup running one "big" instance on a single vm in the cloud somewhere. If this is approximately all you require, then the cost should not be a dominating factor in your decision.

I think "because Microsoft" is also really poor justification if we are being serious about the technological capabilities and pursuing high quality business outcomes. If your business is fundamentally about open source advocacy and you are operating as a non profit, I totally get it. But, this is probably not your business model.

Re:Advent of Code 2024 in pure SQL by larodi on Friday January 03, 2025 @05:49AM (#42584499)
MS SQL is one fascinating piece of software and much closer to commercial big bro offering such as Oracle and DB2, yet much more user friendly and convenient.

This sentiment against it really always comes only from people who have not used it or have never touched the enterprise version which is a very mature ecosystem with lotta features available for ages now.

Most of my career I’ve been dealing with DBs and MSSql is the easiest to admin perhaps being also tightly integrated with all scripting in the platform. It also runs Linux and is doing it better than the rest can say for running Windows.

Re:Advent of Code 2024 in pure SQL by youerbt on Friday January 03, 2025 @04:42AM (#42584159)
I do AoC in SQL, I wish it was true. With Postgres, you have lots of regex/string manipulation functions that make it easy.

For me, the biggest problem was memory. Recursive CTEs are meant to generate tables, so if you are doing some maze traversal, you have to keep every step in memory, until you are done.

Re:Advent of Code 2024 in pure SQL by xdavidliu on Friday January 03, 2025 @10:41AM (#42586579)
parsing is most difficult for probably the first third of the problems. when you get to day 19 or so, the input is still just a grid or a bunch of ints just like day 1, but the algorithms required are considerably more challenging than the parsing part. (I've done all 25 problems in all years)
Re:Advent of Code 2024 in pure SQL by brettgriffin on Thursday January 02, 2025 @02:57PM (#42578132)
I'm as equally amazed by the solutions in this post's github repo as I am with Taco Bell's new chicken nuggets.
Re:Advent of Code 2024 in pure SQL by upghost on Friday January 03, 2025 @08:57AM (#42585680)
⊂ •͡˘∠•͡˘ ⊃

Suspicious. Need to investigate if taco bell has interesting ANSI SQL flavored chicken nuggets or I've been taken for a fool!

Re:Advent of Code 2024 in pure SQL by larodi on Thursday January 02, 2025 @05:54PM (#42579914)
but why? what would make you react at human ingenuity with shame and desire? is this something about you or something about them in particular? isnt the whole of HN about human ingenuity...?

are we to feel Taco Bell menu about it all, what am I missing?

Re:Advent of Code 2024 in pure SQL by knicholes on Thursday January 02, 2025 @07:10PM (#42580538)
This is just a guess, but if the OP's reason is similar to mine, DBMSs should be reserved for managing databases and not implementing complex logic.
Re:Advent of Code 2024 in pure SQL by liontwist on Thursday January 02, 2025 @07:40PM (#42580780)
Why not? I think the main barrier is the programmers are not comfortable with recursion, rather than a technical limitation.
Re:Advent of Code 2024 in pure SQL by FridgeSeal on Thursday January 02, 2025 @08:34PM (#42581203)
I’d hazard a guess that quite a few devs have at one point, been lumped with a db where someone pushed too much logic into it. Probably great for the one person who wrote it, and awful for everyone else.

Mystery triggers, unclear or badly managed stored procedures, opaque invariants. It’s not to say that all of these things are bad, but a certain level of DB complexity will naturally set off alarm bells for some devs.

Re:Advent of Code 2024 in pure SQL by scop on Thursday January 02, 2025 @08:58PM (#42581374)
One of the most eye opening moments in my junior dev career was when I found a really nifty way to achieve an operation using SQL alone and brought it to our VP (start up, small company size). It wasn’t esoteric, but used some SQL features not well-trodden by web devs. I thought I was the bees knees and this would be a clear demonstration of my sizable brain. He complimented the thought behind it, but immediately vetoed it as too complex for the majority of developers who would have to touch the code down the road. In hindsight he was completely right.
Re:Advent of Code 2024 in pure SQL by liontwist on Friday January 03, 2025 @12:33AM (#42582817)
I don’t think this is a win. Cautioning against adding new technology to learn and maintain is one thing. But not using an effective solution in your toolset because it’s too hard is absurd.

Our Industry pretends solutions must be immediately understandable with common sense, without study or explanation. But nothing that actually matters does this (relational databases in the first place).

No other engineering discipline has this confusion. The world has complexity and it’s our job to understand it.

Re:Advent of Code 2024 in pure SQL by liontwist on Friday January 03, 2025 @12:29AM (#42582795)
SQL tends to be very dense (more thinking than typing).

It is tedious to specify all that information (access, constraints, foreign key, cascading, views), but it’s all essential information you would end up specifying in another way.

Re:Advent of Code 2024 in pure SQL by xarope on Friday January 03, 2025 @12:11AM (#42582681)
In the past (why do I feel so old when I say this?), DBMSs WERE used to implement complex logic. Not just complex business logic, but even authentication, authorization, etc were implement with stored procs, embedded sql, native DB features.

The cycle in tech will shift from one side to another; last decade was a lot of no-sql where all this logic and implementation had to reside on the application side. I'm seeing a shift back to DBs again (e.g. supabase), and the reality is that it's probably a continuum somewhere in-between, depending on your requirements (as always, the answer is "Yes, but it depends").

Remember, these are all "just" tools.

Re:Advent of Code 2024 in pure SQL by harrall on Friday January 03, 2025 @01:09AM (#42583003)
SQL is like regex.

A small fraction of people bothered to learn how they work and know how and when to use them properly.

Everyone else just perpetuates a giant in-joke of “it’s black magic!”

Re:Advent of Code 2024 in pure SQL by lelanthran on Friday January 03, 2025 @07:33AM (#42585090)
I do line of business apps right now.

It seems to me that we have the abstraction inverted when it comes to ORMs, which is why so many experienced devs dont like ORMs (but can't really articulate why).

Here's my take, in the context of business logic:

The schema represents the business rules. The ORM papers over the DB so that the code doesn't have to match the business rules. Then the dev implements those rules again in code, but poorly.

If you simply skip the programming language using something like Postgrest you end up with a declarative structure of the business rules.

The first problem is that most developers would rather hunt a bug or perform a modification on a 30k Java or C# or python or ruby program than in 5k of SQL.

The next problem is that tooling for SQL is crap. It's declarative so good luck stepping through the logic. The dialects are all different so editors aren't great at spotting errors.

The last major problem is that changing it is hard - you can't simply change the schema and redeploy because migrations have to be made on the existing data.

All of these are surmountable in some ways, but they certainly ain't easy.

Doing LoB apps have opened my eyes a lot: maybe 99% of logic is shorter when expressed in SQL. Of course, that means that the only use of an ORM in this case is connecting to the DB, sending the query, and sending back results.

Re:Advent of Code 2024 in pure SQL by PaulHoule on Thursday January 09, 2025 @12:25PM (#42647798)
What I can't stand about Taco Bell is the fake nacho cheese. The ordinary grated cheese that is on (say) a hard taco is OK even if it's not the best, but it takes me a strong act of suppression to choke down anything with Velveeta in it. Maybe their tablet interface can be drilled into to get ingredients but as it is it's a game of chance. (Funny there is a taco stands that has the best street tacos I've seen anywhere except LA a block from Taco bell but too often it's not open when I'm there)

Seriously though,

https://www.amazon.com/Joe-Celkos-SQL-Smarties-Programming-d...

is a masterclass in extreme SQLmanship.