Commit fbf2d3efdbd66a3cf5f1513439fe220c1c5cdeec
Committed by
GitHub
1 parent
e618f238
Fix entities filtering by kv when search value is empty (#4515)
* Fix entities filtering by kv when search value is empty * Refactor
Showing
1 changed file
with
19 additions
and
10 deletions
@@ -243,23 +243,21 @@ public class EntityKeyMapping { | @@ -243,23 +243,21 @@ public class EntityKeyMapping { | ||
243 | } else { | 243 | } else { |
244 | entityTypeStr = "'" + entityType.name() + "'"; | 244 | entityTypeStr = "'" + entityType.name() + "'"; |
245 | } | 245 | } |
246 | - ctx.addStringParameter(alias + "_key_id", entityKey.getKey()); | 246 | + ctx.addStringParameter(getKeyId(), entityKey.getKey()); |
247 | String filterQuery = toQueries(ctx, entityFilter.getType()) | 247 | String filterQuery = toQueries(ctx, entityFilter.getType()) |
248 | .filter(StringUtils::isNotEmpty) | 248 | .filter(StringUtils::isNotEmpty) |
249 | .collect(Collectors.joining(" and ")); | 249 | .collect(Collectors.joining(" and ")); |
250 | - if (StringUtils.isEmpty(filterQuery)) { | ||
251 | - filterQuery = ""; | ||
252 | - } else { | 250 | + if (StringUtils.isNotEmpty(filterQuery)) { |
253 | filterQuery = " AND (" + filterQuery + ")"; | 251 | filterQuery = " AND (" + filterQuery + ")"; |
254 | } | 252 | } |
255 | if (entityKey.getType().equals(EntityKeyType.TIME_SERIES)) { | 253 | if (entityKey.getType().equals(EntityKeyType.TIME_SERIES)) { |
256 | - String join = hasFilter() ? "inner join" : "left join"; | 254 | + String join = (hasFilter() && hasFilterValues(ctx)) ? "inner join" : "left join"; |
257 | return String.format("%s ts_kv_latest %s ON %s.entity_id=entities.id AND %s.key = (select key_id from ts_kv_dictionary where key = :%s_key_id) %s", | 255 | return String.format("%s ts_kv_latest %s ON %s.entity_id=entities.id AND %s.key = (select key_id from ts_kv_dictionary where key = :%s_key_id) %s", |
258 | join, alias, alias, alias, alias, filterQuery); | 256 | join, alias, alias, alias, alias, filterQuery); |
259 | } else { | 257 | } else { |
260 | String query; | 258 | String query; |
261 | if (!entityKey.getType().equals(EntityKeyType.ATTRIBUTE)) { | 259 | if (!entityKey.getType().equals(EntityKeyType.ATTRIBUTE)) { |
262 | - String join = hasFilter() ? "inner join" : "left join"; | 260 | + String join = (hasFilter() && hasFilterValues(ctx)) ? "inner join" : "left join"; |
263 | query = String.format("%s attribute_kv %s ON %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id ", | 261 | query = String.format("%s attribute_kv %s ON %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id ", |
264 | join, alias, alias, alias, entityTypeStr, alias, alias); | 262 | join, alias, alias, alias, entityTypeStr, alias, alias); |
265 | String scope; | 263 | String scope; |
@@ -272,7 +270,7 @@ public class EntityKeyMapping { | @@ -272,7 +270,7 @@ public class EntityKeyMapping { | ||
272 | } | 270 | } |
273 | query = String.format("%s AND %s.attribute_type='%s' %s", query, alias, scope, filterQuery); | 271 | query = String.format("%s AND %s.attribute_type='%s' %s", query, alias, scope, filterQuery); |
274 | } else { | 272 | } else { |
275 | - String join = hasFilter() ? "join LATERAL" : "left join LATERAL"; | 273 | + String join = (hasFilter() && hasFilterValues(ctx)) ? "join LATERAL" : "left join LATERAL"; |
276 | query = String.format("%s (select * from attribute_kv %s WHERE %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id %s " + | 274 | query = String.format("%s (select * from attribute_kv %s WHERE %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id %s " + |
277 | "ORDER BY %s.last_update_ts DESC limit 1) as %s ON true", | 275 | "ORDER BY %s.last_update_ts DESC limit 1) as %s ON true", |
278 | join, alias, alias, alias, entityTypeStr, alias, alias, filterQuery, alias, alias); | 276 | join, alias, alias, alias, entityTypeStr, alias, alias, filterQuery, alias, alias); |
@@ -281,15 +279,26 @@ public class EntityKeyMapping { | @@ -281,15 +279,26 @@ public class EntityKeyMapping { | ||
281 | } | 279 | } |
282 | } | 280 | } |
283 | 281 | ||
282 | + private boolean hasFilterValues(QueryContext ctx) { | ||
283 | + return Arrays.stream(ctx.getParameterNames()).anyMatch(parameterName -> { | ||
284 | + return !parameterName.equals(getKeyId()) && parameterName.startsWith(alias); | ||
285 | + }); | ||
286 | + } | ||
287 | + | ||
288 | + private String getKeyId() { | ||
289 | + return alias + "_key_id"; | ||
290 | + } | ||
291 | + | ||
284 | public static String buildSelections(List<EntityKeyMapping> mappings, EntityFilterType filterType, EntityType entityType) { | 292 | public static String buildSelections(List<EntityKeyMapping> mappings, EntityFilterType filterType, EntityType entityType) { |
285 | return mappings.stream().map(mapping -> mapping.toSelection(filterType, entityType)).collect( | 293 | return mappings.stream().map(mapping -> mapping.toSelection(filterType, entityType)).collect( |
286 | Collectors.joining(", ")); | 294 | Collectors.joining(", ")); |
287 | } | 295 | } |
288 | 296 | ||
289 | public static String buildLatestJoins(QueryContext ctx, EntityFilter entityFilter, EntityType entityType, List<EntityKeyMapping> latestMappings, boolean countQuery) { | 297 | public static String buildLatestJoins(QueryContext ctx, EntityFilter entityFilter, EntityType entityType, List<EntityKeyMapping> latestMappings, boolean countQuery) { |
290 | - return latestMappings.stream().filter(mapping -> !countQuery || mapping.hasFilter()) | ||
291 | - .map(mapping -> mapping.toLatestJoin(ctx, entityFilter, entityType)).collect( | ||
292 | - Collectors.joining(" ")); | 298 | + return latestMappings.stream() |
299 | + .filter(mapping -> !countQuery || mapping.hasFilter()) | ||
300 | + .map(mapping -> mapping.toLatestJoin(ctx, entityFilter, entityType)) | ||
301 | + .collect(Collectors.joining(" ")); | ||
293 | } | 302 | } |
294 | 303 | ||
295 | public static String buildQuery(QueryContext ctx, List<EntityKeyMapping> mappings, EntityFilterType filterType) { | 304 | public static String buildQuery(QueryContext ctx, List<EntityKeyMapping> mappings, EntityFilterType filterType) { |