Skip to content

Commit

Permalink
improve or-condition join duplicates bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Sep 26, 2023
1 parent 3ccdb7a commit c581256
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 12 deletions.
19 changes: 13 additions & 6 deletions lib/DboBuilder/QueryBuilder/getJoinQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type GetJoinQueryResult = {
resultAlias: string;
queryLines: string[];
firstJoinTableJoinFields: string[];
isOrJoin: boolean;
}

/**
Expand Down Expand Up @@ -79,6 +80,7 @@ export const getJoinQuery = (viewHandler: ViewHandler, { q1, q2, depth }: Args):

const joinType = q2.isLeftJoin? "LEFT" : "INNER";

const isOrJoin = firstJoinTablePath.on.length > 1;
const joinCondition = getJoinOnCondition({
on: firstJoinTablePath.on,
leftAlias: asName(q1.tableAlias || q1.table),
Expand All @@ -94,33 +96,38 @@ export const getJoinQuery = (viewHandler: ViewHandler, { q1, q2, depth }: Args):

const { innerQuery } = getInnerJoinQuery({ paths, q1, q2, rootSelectItems, targetTableAliasRaw });

// const requiredJoinFields = joinFields.map(field => getJoinCol(field).alias);
const requiredJoinFields = joinFields.map(field => getJoinCol(field).alias);
/**
* Used to prevent duplicates in case of OR filters
*/
const rootTableIdField = `${ROOT_TABLE_ALIAS}.${ROOT_TABLE_ROW_NUM_ID}`;
const wrappingQuery = [
`SELECT `,
// ...indentLines([...requiredJoinFields, jsonAgg, ...rootNestedSort.map(d => d.nested!.wrapperQuerySortItem)], { appendCommas: true }),
...indentLines([rootTableIdField, jsonAgg, ...rootNestedSort.map(d => d.nested!.wrapperQuerySortItem)], { appendCommas: true }),
...indentLines([
...(isOrJoin? [rootTableIdField]: requiredJoinFields),
jsonAgg,
...rootNestedSort.map(d => d.nested!.wrapperQuerySortItem)
], { appendCommas: true }),
`FROM (`,
...indentLines(innerQuery),
`) ${targetTableAlias}`,
`WHERE ${joinCondition}`,
`GROUP BY ${rootTableIdField}`
`GROUP BY ${isOrJoin? rootTableIdField : requiredJoinFields}`,
];

const queryLines = [
`${joinType} JOIN LATERAL (`,
...indentLines(wrappingQuery),
`) ${targetTableAlias}`,
// `ON ${joinCondition}`
`ON ${targetTableAlias}.${ROOT_TABLE_ROW_NUM_ID} = ${rootTableIdField}`
isOrJoin?
`ON ${targetTableAlias}.${ROOT_TABLE_ROW_NUM_ID} = ${rootTableIdField}` :
`ON ${joinCondition}`
];

return {
resultAlias: JSON_AGG_FIELD_NAME,
queryLines,
isOrJoin,
firstJoinTableJoinFields: firstJoinTableJoinFields,
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/DboBuilder/QueryBuilder/getSelectQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function getSelectQuery(
`SELECT`
,...indentLines(selectItems, { appendCommas: true })
, `FROM ( `
, ` SELECT * ${parsedJoins.length? `, ROW_NUMBER() OVER() as ${ROOT_TABLE_ROW_NUM_ID}` : ""}`
, ` SELECT * ${parsedJoins.some(j => j.isOrJoin)? `, ROW_NUMBER() OVER() as ${ROOT_TABLE_ROW_NUM_ID}` : ""}`
, ` FROM ${q.table}`
, ` ${q.where}`
, `) ${ROOT_TABLE_ALIAS}`
Expand Down
2 changes: 2 additions & 0 deletions lib/Prostgles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,8 @@ export class Prostgles {
const isAllowed = await canRunSQL(this, localParams);
if(isAllowed){
localParams.returnQuery = (param3 as LocalParams).returnQuery;
} else {
throw "Must be allowed to run sql to use returnQuery"
}
}
const res = await this.dbo[tableName]![command]!(param1, param2, param3, valid_table_command_rules, localParams);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.1.63",
"version": "4.1.64",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tests/client/PID.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
157499
284153
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c581256

Please sign in to comment.