Skip to content

Conversation

@Sylvestre67
Copy link

@Sylvestre67 Sylvestre67 commented Nov 1, 2022

Resolves https://github.com/observablehq/projects/issues/269.

SQL server syntax for LIMIT/OFFSET type query is

ORDER BY <col> <direction>
OFFSET 0 ROWS
FETCH NEXT 1000 ROWS ONLY

The table cell is currently generating the standard SQL syntax only.

LIMIT 1000
OFFSET 0

If the source is of dialect mssql it should use the right LIMIT/OFFSET syntax.

MS SQL documentation.

@Sylvestre67 Sylvestre67 changed the title Syl/ms sql query template MS SQL syntax for LIMIT/OFFSET Nov 1, 2022
@Sylvestre67 Sylvestre67 marked this pull request as ready for review November 1, 2022 15:27
@Sylvestre67 Sylvestre67 requested a review from mbostock November 1, 2022 16:43
Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind running Prettier on the parts you’ve changed? I see some formatting nits here and I’d rather not comment on them. Thanks!

src/table.mjs Outdated
if (slice.to !== null || slice.from !== null) {
if(!sort.length){
appendSql(`\nORDER BY `, args);
appendOrderBy({column: select.columns[0], direction: 'ASC'}, args);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we’ll want the escaped column name here, to match the SELECT.

Suggested change
appendOrderBy({column: select.columns[0], direction: 'ASC'}, args);
appendOrderBy({column: columns[0], direction: 'ASC'}, args);

Also, this will crash if select.columns is null, as it is with SELECT *. In practice this should never happen for a MS SQL data source because we would always materialize the selected columns in a table cell, but we should either throw an error here with a human-readable message or perhaps we can make it work with something like ORDER BY NULL.

Copy link
Author

@Sylvestre67 Sylvestre67 Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like ORDER BY NULL won't work, mssql is not happy with it. So I added a catch and throwing an error with a human-readable message as you suggested.

Regarding the escaping, it seems that columns[0] has the alias of the table (t) prepended to it (L245). The table alias is also added in the appendOrderBy method (L286), so we end up with something like ORDER BY t.t."ModifiedDate" ASC. I decided to apply the escaper to the select.columns[0].

Also after looking at it for the other operations (sort and filter) I realized these are not escaped either. So I added the escape logic for both of these as well + some test.

@Sylvestre67
Copy link
Author

Do you mind running Prettier on the parts you’ve changed? I see some formatting nits here and I’d rather not comment on them. Thanks!

I applied the styling. prettier wants to slice any line longer than the default printWidth value of 80 char . So I ignored these changes. Hopefully this is good now.

Should we change the printWidth in the .prettierrc to something like 120 ?

@Sylvestre67 Sylvestre67 force-pushed the syl/ms_sql_query_template branch from 7fe0016 to 71c0bfc Compare November 2, 2022 15:14
Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find on the need for more escaping! Thanks for adding tests, too. 🙏

@mbostock
Copy link
Member

mbostock commented Nov 4, 2022

Sent #314 for your consideration. 🙏

@Sylvestre67 Sylvestre67 force-pushed the syl/ms_sql_query_template branch from 5cd4e9e to 267af6c Compare November 4, 2022 21:11
@Sylvestre67
Copy link
Author

Sent #314 for your consideration. 🙏

Merged. Thanks a lot !

Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there…

Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍

@Sylvestre67 Sylvestre67 merged commit 7c8cab6 into main Nov 6, 2022
@Sylvestre67 Sylvestre67 deleted the syl/ms_sql_query_template branch November 6, 2022 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants