Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler crash on from_text with zero rows #4421

Open
2 tasks done
kgutwin opened this issue Apr 25, 2024 · 4 comments
Open
2 tasks done

Compiler crash on from_text with zero rows #4421

kgutwin opened this issue Apr 25, 2024 · 4 comments
Labels
bug Invalid compiler output or panic

Comments

@kgutwin
Copy link

kgutwin commented Apr 25, 2024

What happened?

It should be possible (not very useful, perhaps, but still possible) to define a relation using from_text where it has known columns but no rows. At a minimum, such a definition ought not to crash...

PRQL input

from_text "col_a,col_b"
# or, in JSON format
from_text format:json '{"columns": ["col_a","col_b"], "data": []}'

SQL output

n/a, crashes

Expected SQL output

-- could be something like this
WITH table_0 AS (
  SELECT
    NULL AS col_a,
    NULL AS col_b
  WHERE 1 = 0
)
SELECT
  col_a,
  col_b
FROM
  table_0

MVCE confirmation

  • Minimal example
  • New issue

Anything else?

The message on crash is:

The application panicked (crashed).
Message:  removal index (is 0) should be < len (is 0)
Location: prqlc/prqlc/src/sql/gen_query.rs:446

Tested with latest code, commit d642a30

@kgutwin kgutwin added the bug Invalid compiler output or panic label Apr 25, 2024
@m-span
Copy link
Contributor

m-span commented Apr 26, 2024

I'm not sure that the provided SQL output is the best choice moving forward, but I'm making a PR that at least provides an error message for this situation and prevents a crash

@snth
Copy link
Member

snth commented Apr 26, 2024

@kgutwin What is your use case for this?

  1. Is this as a kind of schema definition/declaration, or
  2. you are processing files which sometimes don't have any data?

For case 2. @KaeporaGaebora should help (and is greatly appreciated 🙏).

For case 1., this kind of thing should be handled by the type system once implemented.

Please note that the following works:

from_text "col_a,col_b\n,"

You can produce the SQL you provided with the following:

from [{col_a=null, col_b=null}]

HTH

@kgutwin
Copy link
Author

kgutwin commented Apr 26, 2024

Thanks - the use case might be a little uncommon, but our team is working on code that takes user input and generates PRQL documents. As part of that process, it's typical for users to generate empty relations with known columns. It's not very useful to them -- they will typically immediately begin adding rows -- but since our app generates and executes the PRQL "live", having a compiler error on an empty relation isn't something we want the users to need to deal with.

We can, of course, catch the compiler error and smoothly handle it, so the solution proposed by @KaeporaGaebora is indeed plausible; but I do wonder whether an error is the right solution, given that it is definitely not a SQL error to have an empty relation. As of now, we don't have a specific need to be able to create an empty relation, but I suppose at some point in the future it might come up.

I did want to note that the PRQL snippets you suggested produce subtly different output than what I had expected; in particular, the last one is missing the necessary WHERE 1 = 0 clause which prevents a row from being generated by the inner SELECT. For what it's worth, it appears that WHERE 1 = 0 is the most portable way of generating zero rows, see StackOverflow: https://stackoverflow.com/questions/18021915/is-there-a-sql-query-that-will-always-return-zero-results

max-sixty added a commit that referenced this issue Apr 26, 2024
Co-authored-by: Michael Spanier <michael.spanier@gmail.com>
Co-authored-by: Maximilian Roos <5635139+max-sixty@users.noreply.github.com>
@max-sixty
Copy link
Member

I definitely think it should be possible to produce from_text with zero rows of data.

I'm not sure whether it should be possible to produce from_text with zero data, which implies zero columns. I don't see a compelling reason why not, but not confident / open to thoughts!

Thanks for adding the error @KaeporaGaebora , that's already better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Invalid compiler output or panic
Projects
None yet
Development

No branches or pull requests

4 participants