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

Implement CLOSED_OPEN and TIME filters for SQL to allow proper filtering #381

Merged
merged 3 commits into from
Apr 22, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Not released

- Implement CLOSED_OPEN and TIME filters for SQL to allow proper filtering [#381](https://github.com/CartoDB/carto-react/pull/381)

## 1.3

### 1.3.0-alpha.4 (2022-04-20)
Expand Down
18 changes: 16 additions & 2 deletions packages/react-core/__tests__/filters/FilterQueryBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,24 @@ describe('Filters to SQL', () => {
owner: 'widgetId3',
values: [1, 2, 3]
}
},
// Time filter
column4: {
time: {
owner: 'widgetId3',
values: [[0, 1]]
}
},
// Closed open
column5: {
closed_open: {
owner: 'widgetId3',
values: [[0, 1]]
}
}
};
expect(filtersToSQL(customFilters)).toEqual(
"WHERE (column1 in('a','b','c')) and ((column2 >= 1 and column2 < 2)) and (column3 in(1,2,3))"
"WHERE (column1 in('a','b','c')) and ((column2 >= 1 and column2 <= 2)) and (column3 in(1,2,3)) and ((cast(column4 as timestamp) >= cast('1970-01-01T00:00:00.000Z' as timestamp) and cast(column4 as timestamp) <= cast('1970-01-01T00:00:00.001Z' as timestamp))) and ((column5 >= 0 and column5 < 1))"
);
});

Expand All @@ -60,6 +74,6 @@ describe('Filters to SQL', () => {
pow: {}
}
};
expect(() => filtersToSQL(param)).toThrow(`Not valid operator has provided: pow`);
expect(() => filtersToSQL(param)).toThrow(`Filter pow is not defined`);
});
});
118 changes: 92 additions & 26 deletions packages/react-core/src/filters/FilterQueryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,105 @@ export const getApplicableFilters = (filters = {}, owner) => {
return filtersCopy;
};

// Filters to SQL

const filterFunctions = {
[FilterTypes.IN](column, filterValues) {
const formattedValues = filterValues.map((v) => (isFinite(v) ? v : `'${v}'`));
return `${column} in(${formattedValues})`;
},
[FilterTypes.BETWEEN](column, filterValues) {
const queryFilters = filterValues.map(([left, right]) => {
const hasLeft = isFinite(left);
const hasRight = isFinite(right);

let query = '';

if (hasLeft) {
query += `${column} >= ${left}`;
}

if (hasLeft && hasRight) {
query += ' and ';
}

if (hasRight) {
query += `${column} <= ${right}`;
}

return query;
});

return joinFilters(queryFilters);
},
[FilterTypes.TIME](column, filterValues) {
const tsColumn = `cast(${column} as timestamp)`;
const queryFilters = filterValues.map(([left, right]) => {
const hasLeft = isFinite(left);
const hasRight = isFinite(right);

let query = '';
if (hasLeft) {
query += `${tsColumn} >= cast('${new Date(left).toISOString()}' as timestamp)`;
}

if (hasLeft && hasRight) {
query += ' and ';
}

if (hasRight) {
query += `${tsColumn} <= cast('${new Date(right).toISOString()}' as timestamp)`;
}

return query;
});

return joinFilters(queryFilters);
},
[FilterTypes.CLOSED_OPEN](column, filterValues) {
const queryFilters = filterValues.map(([left, right]) => {
const hasLeft = isFinite(left);
const hasRight = isFinite(right);

let query = '';

if (hasLeft) {
query += `${column} >= ${left}`;
}

if (hasLeft && hasRight) {
query += ' and ';
}

if (hasRight) {
query += `${column} < ${right}`;
}

return query;
});

return joinFilters(queryFilters);
}
};

export const filtersToSQL = (
filters = {},
filtersLogicalOperator = FiltersLogicalOperators.AND
) => {
const result = [];

Object.entries(filters).forEach(([column, filter]) => {
Object.entries(filter).forEach(([operator, params]) => {
switch (operator) {
case FilterTypes.IN:
result.push(
`${column} ${operator}(${params.values
.map((v) => (isFinite(v) ? v : `'${v}'`))
.join(',')})`
);
break;
case FilterTypes.BETWEEN:
result.push(
`(${params.values
.map(
([left, right]) =>
`${left ? `${column} >= ${left}` : ``} ${
left && right ? ' and ' : ''
} ${right ? `${column} < ${right}` : ``}`
)
.join(') OR (')})`
);
break;
default:
throw new Error(`Not valid operator has provided: ${operator}`);
}
Object.entries(filters).forEach(([column, filters]) => {
Object.entries(filters).forEach(([operator, filter]) => {
const filterFn = filterFunctions[operator];
if (!filterFn) throw new Error(`Filter ${operator} is not defined.`);
result.push(filterFn(column, filter.values, filter.params));
});
});

return result.length ? `WHERE (${result.join(`) ${filtersLogicalOperator} (`)})` : '';
return result.length ? `WHERE ${joinFilters(result, filtersLogicalOperator)}` : '';
};

// Aux
function joinFilters(queryFilters, operator = FiltersLogicalOperators.OR) {
return queryFilters.map((queryFilter) => `(${queryFilter})`).join(` ${operator} `);
}