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 Order nulls in PostgreSQL #4989

Merged
merged 3 commits into from
Feb 1, 2022
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
5 changes: 5 additions & 0 deletions lib/dialects/cockroachdb/crdb-querycompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class QueryCompiler_CRDB extends QueryCompiler_PG {
return body === '' ? '' : sql + body;
}

_groupOrder(item, type) {
// CockroachDB don't support PostgreSQL order nulls first/last syntax, we take the generic one.
return this._basicGroupOrder(item, type);
}

whereJsonPath(statement) {
let castValue = '';
if (parseInt(statement.value)) {
Expand Down
17 changes: 1 addition & 16 deletions lib/dialects/oracledb/query/oracledb-querycompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ const BlobHelper = require('../utils').BlobHelper;
const { isString } = require('../../../util/is');
const {
columnize: columnize_,
direction: direction_,
} = require('../../../formatter/wrappingFormatter');
const Raw = require('../../../raw');

class Oracledb_Compiler extends Oracle_Compiler {
// Compiles an "insert" query, allowing for multiple
Expand Down Expand Up @@ -318,20 +316,7 @@ class Oracledb_Compiler extends Oracle_Compiler {
}

_groupOrder(item, type) {
const column = super._formatGroupsItemValue(item.value);
const direction =
type === 'order' && item.type !== 'orderByRaw'
? ` ${direction_(
item.direction,
this.builder,
this.client,
this.bindingsHolder
)}`
: '';
if (item.nulls && !(item.value instanceof Raw)) {
return `${column}${direction ? direction : ''} nulls ${item.nulls}`;
}
return column + direction;
return super._groupOrderNulls(item, type);
}

update() {
Expand Down
4 changes: 4 additions & 0 deletions lib/dialects/postgres/query/pg-querycompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ class QueryCompiler_PG extends QueryCompiler {
return lockMode + (tables.length ? ' of ' + this._tableNames(tables) : '');
}

_groupOrder(item, type) {
return super._groupOrderNulls(item, type);
}

forUpdate() {
return this._lockingClause('for update');
}
Expand Down
23 changes: 22 additions & 1 deletion lib/query/querycompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ class QueryCompiler {
return groupOrder;
}

_groupOrder(item, type) {
_basicGroupOrder(item, type) {
const column = this._formatGroupsItemValue(item.value, item.nulls);
const direction =
type === 'order' && item.type !== 'orderByRaw'
Expand All @@ -1353,6 +1353,27 @@ class QueryCompiler {
return column + direction;
}

_groupOrder(item, type) {
return this._basicGroupOrder(item, type);
}

_groupOrderNulls(item, type) {
const column = this._formatGroupsItemValue(item.value);
const direction =
type === 'order' && item.type !== 'orderByRaw'
? ` ${direction_(
item.direction,
this.builder,
this.client,
this.bindingsHolder
)}`
: '';
if (item.nulls && !(item.value instanceof Raw)) {
return `${column}${direction ? direction : ''} nulls ${item.nulls}`;
}
return column + direction;
}

// Compiles the `order by` statements.
_groupsOrders(type) {
const items = this.grouped[type];
Expand Down
12 changes: 6 additions & 6 deletions test/integration2/query/select/selects.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,19 +610,19 @@ describe('Selects', function () {
);
tester(
'pg',
'select "id" from "OrderByNullTest" order by ("null_col" is not null) asc, "string_col" asc',
'select "id" from "OrderByNullTest" order by "null_col" asc nulls first, "string_col" asc',
[],
[2, 4, 1, 3]
);
tester(
'pgnative',
'select "id" from "OrderByNullTest" order by ("null_col" is not null) asc, "string_col" asc',
'select "id" from "OrderByNullTest" order by "null_col" asc nulls first, "string_col" asc',
[],
[2, 4, 1, 3]
);
tester(
'pg-redshift',
'select "id" from "OrderByNullTest" order by ("null_col" is not null) asc, "string_col" asc',
'select "id" from "OrderByNullTest" order by "null_col" asc nulls first, "string_col" asc',
[],
['2', '4', '1', '3']
);
Expand Down Expand Up @@ -661,19 +661,19 @@ describe('Selects', function () {
);
tester(
'pg',
'select "id" from "OrderByNullTest" order by ("null_col" is null) asc, "string_col" asc',
'select "id" from "OrderByNullTest" order by "null_col" asc nulls last, "string_col" asc',
[],
[1, 3, 2, 4]
);
tester(
'pgnative',
'select "id" from "OrderByNullTest" order by ("null_col" is null) asc, "string_col" asc',
'select "id" from "OrderByNullTest" order by "null_col" asc nulls last, "string_col" asc',
[],
[1, 3, 2, 4]
);
tester(
'pg-redshift',
'select "id" from "OrderByNullTest" order by ("null_col" is null) asc, "string_col" asc',
'select "id" from "OrderByNullTest" order by "null_col" asc nulls last, "string_col" asc',
[],
['1', '3', '2', '4']
);
Expand Down
16 changes: 8 additions & 8 deletions test/unit/query/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5800,10 +5800,10 @@ describe('QueryBuilder', () => {
sql: 'select * from [users] order by IIF([foo] is null,0,1) desc',
},
pg: {
sql: 'select * from "users" order by ("foo" is not null) desc',
sql: 'select * from "users" order by "foo" desc nulls first',
},
'pg-redshift': {
sql: 'select * from "users" order by ("foo" is not null) desc',
sql: 'select * from "users" order by "foo" desc nulls first',
},
});
});
Expand All @@ -5821,10 +5821,10 @@ describe('QueryBuilder', () => {
sql: 'select * from [users] order by IIF([foo] is null,0,1) desc',
},
pg: {
sql: 'select * from "users" order by ("foo" is not null) desc',
sql: 'select * from "users" order by "foo" desc nulls first',
},
'pg-redshift': {
sql: 'select * from "users" order by ("foo" is not null) desc',
sql: 'select * from "users" order by "foo" desc nulls first',
},
}
);
Expand All @@ -5839,10 +5839,10 @@ describe('QueryBuilder', () => {
sql: 'select * from [users] order by IIF([foo] is null,1,0) desc',
},
pg: {
sql: 'select * from "users" order by ("foo" is null) desc',
sql: 'select * from "users" order by "foo" desc nulls last',
},
'pg-redshift': {
sql: 'select * from "users" order by ("foo" is null) desc',
sql: 'select * from "users" order by "foo" desc nulls last',
},
});
});
Expand All @@ -5860,10 +5860,10 @@ describe('QueryBuilder', () => {
sql: 'select * from [users] order by IIF([foo] is null,1,0) desc',
},
pg: {
sql: 'select * from "users" order by ("foo" is null) desc',
sql: 'select * from "users" order by "foo" desc nulls last',
},
'pg-redshift': {
sql: 'select * from "users" order by ("foo" is null) desc',
sql: 'select * from "users" order by "foo" desc nulls last',
},
}
);
Expand Down