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

fix: Render LIMIT 0 and OFFSET 0 properly in SQL templates #8781

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -3217,8 +3217,8 @@ export class BaseQuery {
'{% if filter %}\nWHERE {{ filter }}{% endif %}' +
'{% if group_by %}\nGROUP BY {{ group_by }}{% endif %}' +
'{% if order_by %}\nORDER BY {{ order_by | map(attribute=\'expr\') | join(\', \') }}{% endif %}' +
'{% if limit %}\nLIMIT {{ limit }}{% endif %}' +
'{% if offset %}\nOFFSET {{ offset }}{% endif %}',
'{% if limit is not none %}\nLIMIT {{ limit }}{% endif %}' +
'{% if offset is not none %}\nOFFSET {{ offset }}{% endif %}',
group_by_exprs: '{{ group_by | map(attribute=\'index\') | join(\', \') }}',
},
expressions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ export class PrestodbQuery extends BaseQuery {
'FROM (\n {{ from }}\n) AS {{ from_alias }} \n' +
'{% if group_by %} GROUP BY {{ group_by }}{% endif %}' +
'{% if order_by %} ORDER BY {{ order_by | map(attribute=\'expr\') | join(\', \') }}{% endif %}' +
'{% if offset %}\nOFFSET {{ offset }}{% endif %}' +
'{% if limit %}\nLIMIT {{ limit }}{% endif %}';
'{% if offset is not none %}\nOFFSET {{ offset }}{% endif %}' +
'{% if limit is not none %}\nLIMIT {{ limit }}{% endif %}';
templates.expressions.extract = 'EXTRACT({{ date_part }} FROM {{ expr }})';
templates.expressions.interval_single_date_part = 'INTERVAL \'{{ num }}\' {{ date_part }}';
templates.expressions.timestamp_literal = 'from_iso8601_timestamp(\'{{ value }}\')';
Expand Down
7 changes: 7 additions & 0 deletions packages/cubejs-testing/test/smoke-cubesql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ describe('SQL API', () => {
expect(res.rows).toEqual([]);
});

test('zero limited dimension aggregated queries through wrapper', async () => {
// Attempts to trigger query generation from SQL templates, not from Cube
const query = 'SELECT MIN(t.maxval) FROM (SELECT MAX(createdAt) as maxval FROM Orders LIMIT 10) t LIMIT 0';
const res = await connection.query(query);
expect(res.rows).toEqual([]);
});

test('select dimension agg where false', async () => {
const query =
'SELECT MAX("createdAt") AS "max" FROM "BigOrders" WHERE 1 = 0';
Expand Down
4 changes: 2 additions & 2 deletions rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15455,8 +15455,8 @@ ORDER BY "source"."str0" ASC
r#"SELECT {{ select_concat | map(attribute='aliased') | join(', ') }}
FROM ({{ from }}) AS {{ from_alias }}
{% if group_by %} GROUP BY {{ group_by | map(attribute='index') | join(', ') }}{% endif %}
{% if order_by %} ORDER BY {{ order_by | map(attribute='expr') | join(', ') }}{% endif %}{% if offset %}
OFFSET {{ offset }}{% endif %}{% if limit %}
{% if order_by %} ORDER BY {{ order_by | map(attribute='expr') | join(', ') }}{% endif %}{% if offset is not none %}
OFFSET {{ offset }}{% endif %}{% if limit is not none %}
LIMIT {{ limit }}{% endif %}"#.to_string(),
),
]
Expand Down
4 changes: 2 additions & 2 deletions rust/cubesql/cubesql/src/compile/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@ FROM (
) AS {{ from_alias }} {% endif %} {% if filter %}
WHERE {{ filter }}{% endif %}{% if group_by %}
GROUP BY {{ group_by }}{% endif %}{% if order_by %}
ORDER BY {{ order_by | map(attribute='expr') | join(', ') }}{% endif %}{% if limit %}
LIMIT {{ limit }}{% endif %}{% if offset %}
ORDER BY {{ order_by | map(attribute='expr') | join(', ') }}{% endif %}{% if limit is not none %}
LIMIT {{ limit }}{% endif %}{% if offset is not none %}
OFFSET {{ offset }}{% endif %}"#.to_string(),
),
(
Expand Down
37 changes: 37 additions & 0 deletions rust/cubesql/cubesql/src/compile/test/test_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,3 +964,40 @@ async fn test_case_wrapper_escaping() {
// Expect 6 backslashes as output is JSON and it's escaped one more time
.contains("\\\\\\\\\\\\`"));
}

/// Test that WrappedSelect(... limit=Some(0) ...) will render it correctly
#[tokio::test]
async fn test_wrapper_limit_zero() {
if !Rewriter::sql_push_down_enabled() {
return;
}
init_testing_logger();

let query_plan = convert_select_to_query_plan(
// language=PostgreSQL
r#"
SELECT
MIN(t.a)
FROM (
SELECT
MAX(order_date) AS a
FROM
KibanaSampleDataEcommerce
LIMIT 10
) t LIMIT 0
"#
.to_string(),
DatabaseProtocol::PostgreSQL,
)
.await;

let logical_plan = query_plan.as_logical_plan();
assert!(logical_plan
.find_cube_scan_wrapper()
.wrapped_sql
.unwrap()
.sql
.contains("LIMIT 0"));

let _physical_plan = query_plan.as_physical_plan().await.unwrap();
}
Loading