Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,12 @@ private SqlQuerySet createQuerySet(
queryTemplates.getValueQueryTemplates().stream()
.map(
valueQueryTemplate ->
// single-shot reads matching rows in one pass: no offset and no key-range
// window (which would otherwise constrain the result set to the meta
// query's minKey/maxKey); an optional per-sub-query maximum caps each
// sub-query (0 = no limit)
unpaged
// single-shot reads, and reads without a meta query (single feature),
// read matching rows in one pass: no offset and no key-range window
// (which would otherwise constrain the result set to the meta query's
// minKey/maxKey); an optional per-sub-query maximum caps each sub-query
// (0 = no limit)
unpaged || skipMetaQuery
? valueQueryTemplate.generateValueQuery(
query instanceof MultiFeatureQuery
? ((MultiFeatureQuery) query).getMaxFeaturesPerSubQuery()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.google.common.collect.ImmutableList;
import de.ii.xtraplatform.cql.domain.And;
import de.ii.xtraplatform.cql.domain.Cql2Expression;
import de.ii.xtraplatform.cql.domain.In;
import de.ii.xtraplatform.features.domain.SortKey;
import de.ii.xtraplatform.features.domain.SortKey.Direction;
import de.ii.xtraplatform.features.domain.Tuple;
Expand Down Expand Up @@ -166,17 +165,14 @@ ValueQueryTemplate createValueQueryTemplate(SqlQuerySchema schema, SqlQueryMappi
forceSimpleFeatures,
minMaxKeys,
virtualTables) -> {
boolean isIdFilter = filter.filter(SqlQueryTemplatesDeriver::containsIdFilter).isPresent();
List<String> aliases = AliasGenerator.getAliases(schema);

SqlQueryTable main = schema.getRelations().isEmpty() ? schema : schema.getRelations().get(0);

Optional<String> sqlFilter = getFilter(main, mapping, filter);
Optional<String> whereClause =
isIdFilter
? sqlFilter
: toWhereClause(
aliases.get(0), main.getSortKey(), additionalSortKeys, minMaxKeys, sqlFilter);
toWhereClause(
aliases.get(0), main.getSortKey(), additionalSortKeys, minMaxKeys, sqlFilter);
boolean useRangePaging = additionalSortKeys.isEmpty() && minMaxKeys.isPresent();
Optional<String> pagingClause =
useRangePaging || (limit == 0 && offset == 0)
Expand Down Expand Up @@ -282,24 +278,6 @@ private String getTableQuery(
columns, mainTable, join.isEmpty() ? "" : " ", join, where, orderBy, paging);
}

/**
* Recognises an id-bounded filter even when it is buried inside conjunctions, e.g. {@code
* In(_ID_, [...])} on its own or {@code And(In(_ID_, [...]), <other>)} — both treat the row-set
* as constrained by the id list and let the SQL generator skip the surrogate-key range guard.
*/
private static boolean containsIdFilter(Cql2Expression expr) {
if (expr instanceof In && ((In) expr).isIdFilter()) {
return true;
}
if (expr instanceof And) {
return ((And) expr)
.getArgs().stream()
.anyMatch(
arg -> arg instanceof Cql2Expression && containsIdFilter((Cql2Expression) arg));
}
return false;
}

private Optional<String> toWhereClause(
String alias,
String keyField,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class SqlQueryTemplatesDeriverSpec extends Specification {
"self joins with nested duplicate join" | td | 0 | 0 | [] | null | "self_joins_with_nested_duplicate_join" | "self_joins_with_nested_duplicate_join" || "self_joins_with_nested_duplicate_join"
"object without sourcePath" | td | 0 | 0 | [] | null | "object_without_sourcePath" | "object_without_sourcePath" || "object_without_sourcePath"
"paging" | td | 10 | 10 | [] | null | "object_array" | "object_array" || "object_array_paging"
"ids" | td | 0 | 0 | [] | In.of(ScalarLiteral.of("1"), ScalarLiteral.of("2")) | "object_array" | "object_array" || "object_array_ids"
"ids + paging" | td | 10 | 10 | [] | In.of(ScalarLiteral.of("1"), ScalarLiteral.of("2")) | "object_array" | "object_array" || "object_array_ids_paging"
"sortBy" | td | 0 | 0 | [SortKey.of("created")] | null | "object_array" | "object_array" || "object_array_sortby"
"sortBy + filter" | td | 0 | 0 | [SortKey.of("created")] | Eq.of(Property.of("task.title"), ScalarLiteral.of("foo")) | "object_array" | "object_array" || "object_array_sortby_filter"
"sortBy + paging" | td | 10 | 10 | [SortKey.of("created")] | null | "object_array" | "object_array" || "object_array_sortby_paging"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- >-
SELECT A.id AS SKEY, A.id
FROM explorationsite A
WHERE (A.id IN ('1','2'))
ORDER BY 1
- >-
SELECT A.id AS SKEY, C.id AS SKEY_1, C.id, C.projectname, C.id
FROM explorationsite A
JOIN explorationsite_task B ON (A.id=B.explorationsite_fk)
JOIN task C ON (B.task_fk=C.id)
WHERE (A.id IN ('1','2'))
ORDER BY 1,2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- >-
SELECT A.id AS SKEY, A.id
FROM explorationsite A
WHERE (A.id >= 10 AND A.id <= 19) AND (A.id IN ('1','2'))
ORDER BY 1
- >-
SELECT A.id AS SKEY, C.id AS SKEY_1, C.id, C.projectname, C.id
FROM explorationsite A
JOIN explorationsite_task B ON (A.id=B.explorationsite_fk)
JOIN task C ON (B.task_fk=C.id)
WHERE (A.id >= 10 AND A.id <= 19) AND (A.id IN ('1','2'))
ORDER BY 1,2
Loading