Skip to content

Notation for creating sample data #286

Closed

Description

@max-sixty

One annoyance in SQL is that it's quite difficult to create some data in code. This is generally used for sample tables when demonstrating something, or for quick lookup tables.

IME it ends up being something like:

select tim as first_name, smith as last_name, 30 as age
union all
select mary, jones, 31
union all
...

I think we could make this much nicer. The simplest approach would be to just do from_json (or _csv or _yaml), parse the inline string, and then convert to the verbose SQL representation. As above, it's not for loading lots of data; it's just for small samples.

There's possibly a better approach which could include types / structs / arrays etc, though we're somewhat limited by the constraints of SQL. The thought was inspired by @jamii's post's, worth reading (& subscribing!): https://www.scattered-thoughts.net/writing/the-shape-of-data/

Activity

  1. added
    language-design
    on Mar 30, 2022
  2. aljazerzen commented on Apr 2, 2022

    @aljazerzen
    Member

    I would rather propose we use from for this:

    from data:[
      first_name: "tim",
      hobby: "HI",
      age: 30,
    ]
    join ...
    

    From can then be thought as "data source" either as a table, inline data or csv/json/yml

    Also there has to be quotes on strings.

  3. max-sixty commented on Apr 2, 2022

    @max-sixty
    MemberAuthor

    Great, agree with the from (/ table?) syntax.

    With the more literal style, how would be represent multiple rows succinctly? It would be convenient to have a format that people can copy from somewhere else too, as opposed to a PRQL-specific format.

    I had started making something like:

    from csv:r#"
    first_name, last_name, age
    tim, smith, 30
    mary, jones 31
    "#
    

    I had started with CSV since it doesn't need quotes (it wouldn't need the r#""# at first), and is more concise than JSON.

    WDYT? Do you still prefer the more "PRQL-native" approach?

  4. aljazerzen commented on Apr 2, 2022

    @aljazerzen
    Member

    Hmmm, why not both? PRQL has nice highlighting, CSV is copy-pastable. There are use cases for both.

    Problem with CSV is that there is no standard. comma/semicolon/tab/space, header/no header, spaces after comma are all ambiguous in different software.

    Before adding r#""# think twice about how will be include regex literals. Also, in your example string literal starts with new line...

    Ah, so much problems with such a simple feature.

  5. max-sixty commented on Apr 2, 2022

    @max-sixty
    MemberAuthor

    Before adding r#""# think twice about how will be include regex literals. Also, in your example string literal starts with new line...

    I was thinking that r"\w" would be raw text, and the rust approach of r#"foo"# was a good way of extending this to multiple lines! WDYT?

    Hmmm, why not both? PRQL has nice highlighting, CSV is copy-pastable. There are use cases for both.

    Definitely agree. I do think there can be a temptation to build too many ways of doing something — it's difficult to turn ideas down! But for new users it can be confusing. Generally I would vote to do one thing and then assess if we need another.

    Possibly in this case there is a real use for both though.

  6. qorrect commented on Apr 3, 2022

    @qorrect
    Contributor

    I do appreciate you keep things clean @max-sixty , I agree with your sentiment of only building one way to do things in the beginning. More ways will come as the language grows but in the beginning if we can keep it light I think it will go far.

    That said , I still like the prql approach of from data , but it looks like it would only allow for one row. How many rows do you need in this approach ?

  7. max-sixty commented on Apr 3, 2022

    @max-sixty
    MemberAuthor

    How many rows do you need in this approach ?

    Generally for sample data I've needed 5-10...

  8. aljazerzen commented on Apr 4, 2022

    @aljazerzen
    Member

    Ah, in this case we would need nested arrays:

    from data:[
      [first_name: "tim", hobby: "HI", age: 30],
      [first_name: "tim", hobby: "HI", age: 30],
      [first_name: "tim", hobby: "HI", age: 30],
    ]
    join ...
    

    but now, CSV looks better because it has headers at the top.

  9. max-sixty commented on Apr 10, 2022

    @max-sixty
    MemberAuthor

    Something I've noticed that I miss from SQL as I'm writing documentation is the ability to start without a table; e.g. if we want to demonstrate an s-string, we need to have something like:

    from employees
    derive db_version: s"version()"

    ...whereas in SQL, that's just SELECT version(); no need for the from.

    Hopefully as part of this we can make that easier; e.g.

    from data:""
    derive db_version: s"version()"
    

    ...might be sufficient

  10. max-sixty commented on Apr 10, 2022

    @max-sixty
    MemberAuthor

    but now, CSV looks better because it has headers at the top.

    CSV is not a great format, but it does have this one advantage! Are there nicer formats that also can have column names specified only once?

  11. aljazerzen commented on Apr 11, 2022

    @aljazerzen
    Member

    SQL has that:

    INSERT INTO employees (first_name, age) VALUES
    ("John", 15),
    ("Janez", 17),
    ("Hans", 16)
    

    Pandas has columns paramater of DataFrame constructor. In prql this would look like:

    from columns:[first_name, age] data:[
      ["John", 15],
      ["Janez", 17],
      ["Hans", 16],
    ]
    select ...
    
  12. aljazerzen commented on Apr 11, 2022

    @aljazerzen
    Member

    Also, currently this is a valid query:

    derive db_version: s'version()'
    
    SELECT
      version() AS db_version
    
  13. max-sixty commented on Apr 11, 2022

    @max-sixty
    MemberAuthor

    Also, currently this is a valid query:

    Ah, I didn't even realize that! That's great!

    SQL has that:

    INSERT INTO employees (first_name, age) VALUES
    ("John", 15),
    ("Janez", 17),
    ("Hans", 16)
    

    Yes, this is very reasonable. it's a shame that this doesn't extend to inline sample data; and requires table modifications — particularly for analytical DBs where table modifications are expensive & rare.

  14. max-sixty commented on Apr 15, 2022

    @max-sixty
    MemberAuthor
  15. 26 remaining items

  16. removed
    good first issue
    on Jan 19, 2023
  17. modified the milestones: 0.4, 0.5 on Feb 2, 2023
  18. vanillajonathan commented on Feb 10, 2023

    @vanillajonathan
    Collaborator

    Maybe the formats should be defined in modules instead of the core?
    Maybe it should be possible to augment this with new data kinds from third-party modules?

    Maybe someone want to from_tsv, from_ xml, from_bson, from_jsonb, from_excel, or from_rdf?

    If you have from_string format:csv "..." then where format is specified as parameter (format:{format_type}) then would that format be possible to argument with new formats defined in modules that gets imported?

  19. snth commented on Feb 10, 2023

    @snth
    Member

    @vanillajonathan This has actually been implemented and the following works in the playground:

    from_text format:json '[{"a": 1 , "b":2}]'

    I believe the following should work too but for some reason I keep having problems with it. Can anyone reproduce the buggy playground behaviour for the example below?

    from_text format:csv 'a,b\n1,2'
  20. vanillajonathan commented on Feb 10, 2023

    @vanillajonathan
    Collaborator

    I see. Maybe it should be possible to extend the formats used by from_text by importing formats from modules?

    I find it rather confusing that from_text has no parenthesis, it makes me not know if its a language construct or if it is a function call. It seems from_text is defined in the std.prql file so it seems it is a function.

  21. snth commented on Feb 10, 2023

    @snth
    Member

    "Language constructs" are pretty much all functions (it's functions all the way down). Function calls don't take parentheses in PRQL and PRQL is more like functional languages like Haskell in that respect. There's some more detail here: https://prql-lang.org/book_integrated/

    This could probably be documented or explained better. If you have a suggestion for something that's missing from the docs or that could be explained better, please feel free to submit a PR - this makes a great first PR!

  22. vanillajonathan commented on Feb 10, 2023

    @vanillajonathan
    Collaborator

    The link just points to the book not a particular page on the book.

    So I guess from, filter, and take are just functions too?

  23. added a commit that references this issue on Feb 11, 2023
  24. max-sixty commented on Feb 11, 2023

    @max-sixty
    MemberAuthor

    (closing as completed, feel free to continue the discussion re functions!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementlanguage-design

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions