Skip to content

Commit

Permalink
Feat/cast optimize (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
eXigentCoder authored Oct 1, 2024
2 parents 8b72d9a + e6268b1 commit 8e1ce72
Show file tree
Hide file tree
Showing 4 changed files with 432 additions and 8 deletions.
72 changes: 66 additions & 6 deletions lib/make/optimize-join-and-where.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,12 +593,26 @@ function convertLookupToPipeline(lookup) {
* @param {MatchType} parentMatchType
*/
function getMatches(matches, sourceName, destinationName, parentMatchType) {
const sourceMatches = matches.filter((m) =>
Object.keys(m)[0].startsWith(`${sourceName}.`)
);
const destinationMatches = matches.filter((m) =>
Object.keys(m)[0].startsWith(`${destinationName}.`)
);
const sourceMatches = matches.filter((m) => {
if (m.$expr) {
return checkExpressionForString(
m.$expr,
`${sourceName}.`,
`${destinationName}.`
);
}
return Object.keys(m)[0].startsWith(`${sourceName}.`);
});
const destinationMatches = matches.filter((m) => {
if (m.$expr) {
return checkExpressionForString(
m.$expr,
`${destinationName}.`,
`${sourceName}.`
);
}
return Object.keys(m)[0].startsWith(`${destinationName}.`);
});
const miscMatches = matches.filter((m) => {
const key = Object.keys(m)[0];
return (
Expand All @@ -622,6 +636,52 @@ function getMatches(matches, sourceName, destinationName, parentMatchType) {
};
}

/**
*
* @param expression
* @param mustContain
* @param mustNotContain
*/
function checkExpressionForString(expression, mustContain, mustNotContain) {
let expressionArray;
if (expression.$and) {
expressionArray = expression.$and;
} else if (expression.$or) {
expressionArray = expression.$or;
} else {
return false;
}
const containsRequired = expressionContains(expressionArray, mustContain);
const containsNotAllowed = expressionContains(
expressionArray,
mustNotContain
);
if (containsNotAllowed) {
return false;
}
return containsRequired;
}

/**
*
* @param {Record<string,unknown>[]} expression
* @param {string} searchString
* @returns {boolean}
*/
function expressionContains(expression, searchString) {
return expression.some((e) => {
if (typeof e === 'string') {
return (
e.startsWith(searchString) || e.startsWith(`$${searchString}`)
);
}
if ($check.object(e)) {
const values = Object.values(e).flat();
return expressionContains(values, searchString);
}
});
}

/**
*
* @param match
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@synatic/noql",
"version": "4.1.9",
"version": "4.1.10",
"description": "Convert SQL statements to mongo queries or aggregates",
"main": "index.js",
"files": [
Expand Down
3 changes: 2 additions & 1 deletion test/optimizations/optimizations.json
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,7 @@
}
},
"use-cases": {
"case-1": {}
"case-1": {},
"case-2": {}
}
}
Loading

0 comments on commit 8e1ce72

Please sign in to comment.