prql icon indicating copy to clipboard operation
prql copied to clipboard

Underscores in numbers — `5_000`

Open max-sixty opened this issue 2 years ago • 1 comments

As suggested by @snth on Discord.

I like this feature a lot in Python, I think it makes sense to add — I don't really see any downside.

Here's the python spec, with references to other langs https://peps.python.org/pep-0515/

I had thought it could only be permitted every three digits (i.e. 5_000 or 50_000, but not 5_00_000), but that makes it more difficult to edit numbers, and we can autoformat to every three digits.

If folks agree, I think this could be a good first issue — it would be:

  • editing the pest file to allow underscores (but ensuring that a single underscore isn't a number) at https://github.com/PRQL/prql/blob/a138cdaa246adc043b36783cf872477be82046ea/prql-compiler/src/prql.pest#L93 to allow underscores
  • removing the underscores in parser.rs when parsing to numbers: https://github.com/PRQL/prql/blob/a138cdaa246adc043b36783cf872477be82046ea/prql-compiler/src/parser.rs#L251

A follow-up would be editing the grammar files, but that wouldn't need to be coupled (indeed, could even happen prior if anyone is more comfortable editing those)

max-sixty avatar Jan 07 '23 04:01 max-sixty

Thanks for creating the issue @max-sixty . I got the idea for it from the recent Hackernews thread: https://news.ycombinator.com/item?id=34266078

One of the comments mentioned that the reason for allowing the underscores in arbitrary positions in numbers is that other countries have different conventions for grouping, e.g. India might group every two digits or something like that.

Therefore I think it might be good to allow underscores in arbitrary positions in numbers.

snth avatar Jan 07 '23 06:01 snth