prql icon indicating copy to clipboard operation
prql copied to clipboard

Dialect-specific SQL

Open max-sixty opened this issue 4 years ago • 16 comments

Even with the current code, take 20 will produce TOP(20), which is incompatible with some DBs.

The original plan was to allow a db named arg to queries, which I still think works well, but open to alternatives.

max-sixty avatar Mar 20 '22 23:03 max-sixty

The original plan was to allow a db named arg to queries, which I still think works well, but open to alternatives.

What would that look like ?

qorrect avatar Mar 20 '22 23:03 qorrect

The original plan was like db:postgres in the first line: https://github.com/max-sixty/prql/blob/main/examples/functions-1.md?plain=1#L2

max-sixty avatar Mar 21 '22 00:03 max-sixty

Ahha gotcha. I had been using limit. Also should take have an optional offset parameter?

qorrect avatar Mar 22 '22 16:03 qorrect

Yes I think limit might be better if we only have one, from memory.

Good idea re offset — do you want to open an issue? We could follow SQL with that and just do offset, or we could do some sort of range syntax, like other languages... (e.g. take 5..20 / take 5-20 / something better!)

max-sixty avatar Mar 22 '22 16:03 max-sixty

Oh I like the range, I"m cool with .. I think most people are familiar with that to mean range , I'll make an issue

qorrect avatar Mar 22 '22 18:03 qorrect

@qorrect

Not to stray from the point of this issue but:

Ahha gotcha. I had been using limit. Also should take have an optional offset parameter?

echo "from albums | limit 10" | prql compile

returns

Expected a known transformation; got limit

How are you utilizing limit?

Otherwise specifying the dialect in the PRQL preamble or in the CLI itself (as a flag) would be a great addition. (even though implementing dialect based sql generation seems like a huge undertaking).

Cheers.

Akaame avatar Apr 03 '22 20:04 Akaame

@Akaame the PRQL keyword is actually take

echo "from albums | take 10" | prql compile

Should work. I was actually talking about under the hood in PyPrql , we use LIMIT instead of TOP in the generated SQL

qorrect avatar Apr 03 '22 20:04 qorrect

The problem I had is with 'take' I got TOP (10) instead of LIMIT 10 and thought there was a 'limit' directive.

we use LIMIT instead of TOP in the generated SQL

What is the cause of the different behavior I am getting?

Akaame avatar Apr 03 '22 21:04 Akaame

I think we could switch the default to LIMIT, given that's more prevalent.

And then we can do dialect-specific soon, and provide the correct one.

max-sixty avatar Apr 03 '22 22:04 max-sixty

We now use LIMIT by default

max-sixty avatar Apr 04 '22 01:04 max-sixty

We now use LIMIT by default, but switch to TOP if query starts with prql dialect:ms or prql dialect:microsoft or prql dialect:ms-sql-server.

prql dialect:ms_sql_server
from Employees
select [FirstName]
take 3
SELECT
  TOP (3) FirstName
FROM
  Employees

Currently there is no other effect of dialect, but I'm sure there should be.

aljazerzen avatar Apr 12 '22 07:04 aljazerzen

@aljazerzen should we have a single name for each dialect to start? I think that might be less confusing for people confronted by an autocomplete, or sending scripts to others.

(I trust your judgement tbc, your call)

max-sixty avatar Apr 12 '22 15:04 max-sixty

Although I do think that its a bit confusing, it can get tedious to always write dialect: ms-sql-server. That was my rationale.

In reference, we should use longer names, but I do think its quite useful to have abbreviations.

On a similar topic, do you think we should have ms-sql-server or sql-server? postgresql / postgres / postgre?

aljazerzen avatar Apr 12 '22 16:04 aljazerzen

Half a p for postgres, l <--

qorrect avatar Apr 12 '22 17:04 qorrect

Yeah I think it's fine to be somewhat abbreviated — mssql, postgres, mysql — etc

max-sixty avatar Apr 12 '22 18:04 max-sixty

I think we can say this is complete, and the next part can be in https://github.com/prql/prql/issues/367, including any renames of the abbreviations.

max-sixty avatar Apr 17 '22 04:04 max-sixty