News for hackers. Stuff that matters.
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)