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

[jdbc] JDBC persistence ignores operator set in FilterQuery #9906

Closed
splatch opened this issue Jan 21, 2021 · 4 comments · Fixed by #13734, #13843 or #13850
Closed

[jdbc] JDBC persistence ignores operator set in FilterQuery #9906

splatch opened this issue Jan 21, 2021 · 4 comments · Fixed by #13734, #13843 or #13850
Labels
bug An unexpected problem or unintended behavior of an add-on

Comments

@splatch
Copy link
Contributor

splatch commented Jan 21, 2021

Given that FilterQuery makes it possible to specify additional operator I am convinced that ignoring it is a bug. There is currently no way to tell JDBC persistence to be inclusive.

The generated query always uses > for begin date and < for end date. There are cases where these should be => or <=.

@splatch splatch added the bug An unexpected problem or unintended behavior of an add-on label Jan 21, 2021
@splatch splatch changed the title JDBC persistence ignores operator set in FilterQuery [jdbc] JDBC persistence ignores operator set in FilterQuery May 4, 2021
@jlaur
Copy link
Contributor

jlaur commented Nov 16, 2022

Probably this is the logic in question:

protected String resolveTimeFilter(FilterCriteria filter, ZoneId timeZone) {
String filterString = "";
if (filter.getBeginDate() != null) {
filterString += filterString.isEmpty() ? " WHERE" : " AND";
filterString += " TIME>'" + JDBC_DATE_FORMAT.format(filter.getBeginDate().withZoneSameInstant(timeZone))
+ "'";
}
if (filter.getEndDate() != null) {
filterString += filterString.isEmpty() ? " WHERE" : " AND";
filterString += " TIME<'" + JDBC_DATE_FORMAT.format(filter.getEndDate().withZoneSameInstant(timeZone))
+ "'";
}
return filterString;
}

and this test:

@Test
public void testHistItemFilterQueryProviderWithStartAndEndDateReturnsDeleteQueryWithWhereClauseDescendingOrder() {
filter.setBeginDate(parseDateTimeString("2022-01-10T15:01:44"));
filter.setEndDate(parseDateTimeString("2022-01-15T15:01:44"));
String sql = jdbcBaseDAO.histItemFilterQueryProvider(filter, 0, DB_TABLE_NAME, "TEST", UTC_ZONE_ID);
assertThat(sql, is("SELECT time, value FROM " + DB_TABLE_NAME + " WHERE TIME>'" //
+ JdbcBaseDAO.JDBC_DATE_FORMAT.format(filter.getBeginDate()) + "'" //
+ " AND TIME<'" + JdbcBaseDAO.JDBC_DATE_FORMAT.format(filter.getEndDate()) + "' ORDER BY time DESC"));
}

@splatch
Copy link
Contributor Author

splatch commented Nov 16, 2022

Yup, these are places I been patching in order to get boundaries.

@jlaur
Copy link
Contributor

jlaur commented Nov 16, 2022

But unfortunately the current implementation seems correct, at least according to these comments in core implementation:
https://github.com/openhab/openhab-core/blob/06c0e905357a2478ef4fdccae3a6fcff14c7a7be/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/FilterCriteria.java#L70-L74

These are only comments, though, not official documentation. @cweitkamp - do you know if these comments are to be interpreted literally?

The operator is for comparing state, not dates:
https://www.openhab.org/javadoc/latest/org/openhab/core/persistence/filtercriteria

beginDate and endDate seem to be interpreted differently here:

if (!Double.isNaN(value) && (((ts >= start) && (ts <= end)) || (start == end))) {
RRD4jItem rrd4jItem = new RRD4jItem(itemName, mapToState(value, item, unit),
ZonedDateTime.ofInstant(Instant.ofEpochSecond(ts), ZoneId.systemDefault()));
items.add(rrd4jItem);
}

And here:

boolean hasBeginDate = false;
boolean hasEndDate = false;
String queryString = "SELECT n FROM " + JpaPersistentItem.class.getSimpleName()
+ " n WHERE n.realName = :itemName";
if (filter.getBeginDate() != null) {
queryString += " AND n.timestamp >= :beginDate";
hasBeginDate = true;
}
if (filter.getEndDate() != null) {
queryString += " AND n.timestamp <= :endDate";
hasEndDate = true;
}

And here:

BasicDBObject dateQueries = new BasicDBObject();
if (filter.getBeginDate() != null) {
dateQueries.put("$gte", Date.from(filter.getBeginDate().toInstant()));
}
if (filter.getEndDate() != null) {
dateQueries.put("$lte", Date.from(filter.getEndDate().toInstant()));
}
if (!dateQueries.isEmpty()) {
query.put(FIELD_TIMESTAMP, dateQueries);
}

So I would agree that we can change to "TIME>=" and "TIME<=" in lines 519 and 524, is that also how you patched it?

If this would be the outcome, I would also open a PR towards core to change those comments.

jlaur added a commit to jlaur/openhab-addons that referenced this issue Nov 16, 2022
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
@jlaur
Copy link
Contributor

jlaur commented Nov 16, 2022

@splatch - you could have a look at this draft pull request: #13734.

cweitkamp pushed a commit that referenced this issue Nov 22, 2022
Fixes #9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
jlaur added a commit to jlaur/openhab-addons that referenced this issue Dec 4, 2022
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
lolodomo pushed a commit that referenced this issue Dec 4, 2022
Fixes #9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
jlaur added a commit to jlaur/openhab-addons that referenced this issue Dec 5, 2022
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
wborn pushed a commit that referenced this issue Dec 5, 2022
Fixes #9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
morph166955 pushed a commit to morph166955/openhab-addons that referenced this issue Dec 18, 2022
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Ben Rosenblum <rosenblumb@gmail.com>
morph166955 pushed a commit to morph166955/openhab-addons that referenced this issue Dec 18, 2022
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Ben Rosenblum <rosenblumb@gmail.com>
andrasU pushed a commit to andrasU/openhab-addons that referenced this issue Dec 24, 2022
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
andrasU pushed a commit to andrasU/openhab-addons that referenced this issue Dec 24, 2022
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
andrasU pushed a commit to andrasU/openhab-addons that referenced this issue Dec 24, 2022
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
borazslo pushed a commit to borazslo/openhab-mideaac-addon that referenced this issue Jan 8, 2023
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
borazslo pushed a commit to borazslo/openhab-mideaac-addon that referenced this issue Jan 8, 2023
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
borazslo pushed a commit to borazslo/openhab-mideaac-addon that referenced this issue Jan 8, 2023
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
psmedley pushed a commit to psmedley/openhab-addons that referenced this issue Feb 23, 2023
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
psmedley pushed a commit to psmedley/openhab-addons that referenced this issue Feb 23, 2023
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
psmedley pushed a commit to psmedley/openhab-addons that referenced this issue Feb 23, 2023
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
nemerdaud pushed a commit to nemerdaud/openhab-addons that referenced this issue Feb 28, 2023
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
nemerdaud pushed a commit to nemerdaud/openhab-addons that referenced this issue Feb 28, 2023
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
nemerdaud pushed a commit to nemerdaud/openhab-addons that referenced this issue Feb 28, 2023
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
andrasU pushed a commit to andrasU/openhab-addons that referenced this issue Jan 6, 2024
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
andrasU pushed a commit to andrasU/openhab-addons that referenced this issue Jan 6, 2024
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
andrasU pushed a commit to andrasU/openhab-addons that referenced this issue Jan 6, 2024
Fixes openhab#9906

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or unintended behavior of an add-on
Projects
None yet
2 participants