News for hackers. Stuff that matters.
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by gavinray on Monday February 05, 2024 @11:38AM (#39263145)
What makes you think that SQL doesn't have "for loops"?

Ever heard of LATERAL joins/CROSS APPLY?

  SELECT loop.value, x.squared
  FROM generate_series(1,5) AS loop(value)
  CROSS JOIN LATERAL (SELECT loop.value * loop.value AS squared) AS x;
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by fifilura on Monday February 05, 2024 @11:49AM (#39263310)
This is not what I call a for loop but more akin to a list comprehension.

And this is the point I was trying to make.

Instead of start with the "how", learn to do the "what".

Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by neuronexmachina on Monday February 05, 2024 @11:42AM (#39263201)
TIL
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by gavinray on Monday February 05, 2024 @11:49AM (#39263305)
They're incredibly useful -- any time that you want to both:

- Reference data from the previous part of the query (the "left-hand side")

- Return multiple columns

The only way you can achieve it is with LATERAL/CROSS APPLY.

Regular correlated subqueries can only return a single column, so something like this doesn't work:

  SELECT
    loop.val, (SELECT loop.val * loop.val, 'second column') AS squared
  FROM
    (SELECT loop.val FROM generate_series(1,5) AS loop(val)) as loop
You'd get:

   error: subquery must return only one column
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by ttfkam on Monday February 05, 2024 @11:59AM (#39263445)
I love LATERALs, but this still fits within set theory and a bulk application rather than an iterative for-loop. It may even be implemented as a for-loop within the engine, but SQL being declarative abstracts that away from the query interface.

It's sets all the way down. A set of f(x) is still a set.

Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by gavinray on Monday February 05, 2024 @12:07PM (#39263559)
Lets get even more cursed then:

    CREATE TEMP TABLE temp_results(value int, value_squared int);

    DO $$
    DECLARE
        r int;
    BEGIN
        FOR r IN SELECT generate_series FROM generate_series(1,5)
        LOOP
            INSERT INTO temp_results VALUES (r, r * r);
        END LOOP;
    END$$;

    SELECT * FROM temp_results;
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by fifilura on Monday February 05, 2024 @12:27PM (#39263805)
Uw you win, I guess. Already started to think what I would write in that code review comment ;)
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by koolba on Monday February 05, 2024 @03:12PM (#39266452)
At the very least mention that re-running the SELECT in the same connection would include the prior results as well because they are preserved across commands within the same connection.
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by ttfkam on Monday February 05, 2024 @03:05PM (#39266361)
Ha! plpgsql's seemingly sole purpose is to inject imperative code into a set-based environment. Probably does it more smoothly than most pl languages, but that's at the cost of imperative clarity.

But you're right. Postgres does allow for-loops like this. (They're also slower than the equivalent set-oriented approach.)

Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by samstave on Monday February 05, 2024 @12:00PM (#39263455)
If anyone is interested, I had gpt explain this SQL to me - and it was really helpful, as I couldnt parse that with my level of SQL...

https://chat.openai.com/share/931b1778-6393-4e86-94b4-b3b5a5...

Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by 3rd3 on Monday February 05, 2024 @01:02PM (#39264360)
What is the point of the cross join? This would work as well:

   SELECT loop.value, loop.value * loop.value
   FROM generate_series(1,5) AS loop(value)
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by ComputerGuru on Monday February 05, 2024 @02:36PM (#39265926)
For this example, nothing. It would be useful where neither of the two SELECT queries is a subset/superset of the other. (Not saying you didn't know that.)
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by 3rd3 on Monday February 05, 2024 @02:42PM (#39266026)
Could you give an example?
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by ako on Monday February 05, 2024 @03:13PM (#39266464)
This will be useful if you have a table with some related history records, e.g., products with product price history, and you want to get the latest price. The lateral join would get all prices for the current product, sort them by date and then pick the top row.
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by 3rd3 on Monday February 05, 2024 @03:36PM (#39266790)
How does a lateral (cross) join compare to a window function in your example?
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by ses1984 on Monday February 05, 2024 @09:40PM (#39270113)
Is a lateral join more efficient than just saying where product = current sort by date desc ?
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by beefield on Monday February 05, 2024 @02:28PM (#39265812)
I don't think that the point was that there are no for loops in SQL. I think the point was that almost always using for loops is wrong and super inefficient and there is a much more efficient way to just use joins instead.
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by fijiaarone on Monday February 05, 2024 @07:37PM (#39269251)
I wonder if anyone realizes that selects and joins in every database are implemented literally with for loops written in c.

The reason people use a relational database is because it has loops that are faster, safer, and more efficient than anything you can write.

Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by fifilura on Tuesday February 06, 2024 @01:55AM (#39271550)
What does it matter? Yeah, at the very bottom of it all there will be a loop. I wouldn't attribute that to C but to the von Neumann architecture.

The point is that by getting rid of loops you remove one way of telling the computer "How" to do it. Start here, do it in this order, combine the data this way.

When "How" is a solved problem, it is a waste of your brain to think about that again. "What" is a better use of your brain cycles.

Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by ako on Tuesday February 06, 2024 @02:10AM (#39271647)
Exactly, SQL is like a very specific natural language used to tell a database what data you need. The database contains AI (the query optimizer) that will use statistics to determine the fastest way to retrieve the data requested. Depending on your query and the data you have the fastest way to get your data will change. It’s hard to manually write this yourself.
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by riku_iki on Tuesday February 06, 2024 @05:23PM (#39281624)
> joins in every database are implemented literally with for loops written in c.

different kind of loops can be different, e.g. 2 nested loop with quadratic time:

for i in t1: for j in t2:

vs sort + merge join with n log n time.

Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by ComputerGuru on Monday February 05, 2024 @02:34PM (#39265904)
LATERAL first available in PostgreSQL 9.3 (2013), but still not available in SQLite.
Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by fifilura on Monday February 05, 2024 @02:40PM (#39265998)
SQLite has RECURSIVE, so you can generate a table with all numbers using something like:

WITH RECURSIVE cnt(x) AS ( SELECT 1 UNION ALL SELECT x+1 FROM cnt LIMIT 5 ) SELECT x FROM cnt;

And then do a regular CROSS JOIN on that table.

Re:Show HN: Natural-SQL-7B, a strong text-to-SQL model by ComputerGuru on Monday February 05, 2024 @02:59PM (#39266278)
But generating a table with all numbers was just a throwaway example of the LATERAL JOIN syntax/use, and (hopefully) not what you'd actually use it for in the real world.

It's not clear to me that (mathematically) a lateral join can be reduced to a recursive cte (and if the performance of a recursive cte would be acceptable for the cases where it does work as a substitute).