Skip to content

Commit

Permalink
Merge pull request #1389 from visualize-admin/perf/remove-other-dimen…
Browse files Browse the repository at this point in the history
…sions-from-dimension-values-query

perf: Do not include other dimension iris when fetching dimension values
  • Loading branch information
bprusinowski authored Mar 7, 2024
2 parents 984e244 + e03366a commit eecf383
Showing 1 changed file with 28 additions and 46 deletions.
74 changes: 28 additions & 46 deletions app/rdf/query-dimension-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,57 +306,39 @@ export const getQueryFilters = (
return "";
}

let i = 0;
const filterDimensionIris = filtersList.map(([iri]) => iri);
// Also include other dimensions to make sure we don't return values that could
// result in no observations. Inclusion of other dimensions is necessary to
// filter out unobserved values.
const otherDimensionFilters = cube.dimensions
.filter(
(dim) =>
dim.path?.value !== dimensionIri &&
!filterDimensionIris.includes(dim.path?.value ?? "") &&
!dim.path?.equals(ns.cube.observedBy)
)
.map((dim) => `?observation <${dim.path?.value}> ?dimension${i++} .`);
const filterDimensionFilters = filtersList.map(([iri, value], j) => {
const dimension = cube.dimensions.find((d) => d.path?.value === iri);

// Ignore the current dimension
if (!dimension || dimensionIri === iri) {
return "";
}
return filtersList
.map(([iri, value], i) => {
const dimension = cube.dimensions.find((d) => d.path?.value === iri);

// Ignore filters with no value or with the special value
if (
value.type === "single" &&
(value.value === FIELD_VALUE_NONE || isDynamicMaxValue(value.value))
) {
return "";
}
// Ignore the current dimension
if (!dimension || dimensionIri === iri) {
return "";
}

// Ignore range filters for now
if (value.type === "range") {
return "";
}
// Ignore filters with no value or with the special value
if (
value.type === "single" &&
(value.value === FIELD_VALUE_NONE || isDynamicMaxValue(value.value))
) {
return "";
}

const versioned = dimension ? dimensionIsVersioned(dimension) : false;
// Ignore range filters for now
if (value.type === "range") {
return "";
}

return `${
versioned
? `?dimension${i + j} <${
ns.schema.sameAs.value
}> ?dimension_unversioned${i + j} .`
: ""
}
?observation <${iri}> ?dimension${i + j} .
${formatFilterIntoSparqlFilter(value, dimension, versioned, i + j)}`;
});
const versioned = dimension ? dimensionIsVersioned(dimension) : false;

return `
${otherDimensionFilters.join("\n")}
${filterDimensionFilters.join("\n")}
`;
return `${
versioned
? `?dimension${i} <${ns.schema.sameAs.value}> ?dimension_unversioned${i} .`
: ""
}
?observation <${iri}> ?dimension${i} .
${formatFilterIntoSparqlFilter(value, dimension, versioned, i)}`;
})
.join("\n");
};

const parseMinMax = (result: ResultRow[]) => {
Expand Down

0 comments on commit eecf383

Please sign in to comment.