From f2e3131f4d6377320c324e68f6100f7b28c8502d Mon Sep 17 00:00:00 2001 From: Benoit Orihuela Date: Wed, 20 Sep 2023 18:44:59 +0200 Subject: [PATCH] fix: minor deduplication --- .../egm/stellio/search/scope/ScopeService.kt | 5 +--- .../service/AttributeInstanceService.kt | 7 ++--- .../stellio/search/util/DBAggregationUtils.kt | 30 +++++++++++-------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/scope/ScopeService.kt b/search-service/src/main/kotlin/com/egm/stellio/search/scope/ScopeService.kt index 6760d3195..95d3753be 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/scope/ScopeService.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/scope/ScopeService.kt @@ -140,10 +140,7 @@ class ScopeService( temporalEntitiesQuery.withAggregatedValues -> { val temporalQuery = temporalEntitiesQuery.temporalQuery val aggrPeriodDuration = temporalQuery.aggrPeriodDuration - val allAggregates = temporalQuery.aggrMethods?.joinToString(",") { - val sqlAggregateExpression = aggrMethodToSqlAggregate(it, AttributeValueType.ARRAY) - "$sqlAggregateExpression as ${it.method}_value" - } + val allAggregates = temporalQuery.aggrMethods?.composeAggregationSelectClause(AttributeValueType.ARRAY) // if retrieving a temporal entity, origin is calculated beforehand as timeAt is optional in this case // if querying temporal entities, timeAt is mandatory and will be used if origin is null if (aggrPeriodDuration != WHOLE_TIME_RANGE_DURATION) { diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/service/AttributeInstanceService.kt b/search-service/src/main/kotlin/com/egm/stellio/search/service/AttributeInstanceService.kt index d72aa6afb..10cd1582c 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/service/AttributeInstanceService.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/service/AttributeInstanceService.kt @@ -203,11 +203,8 @@ class AttributeInstanceService( ) = when { temporalQuery.aggrPeriodDuration != null -> { val aggrPeriodDuration = temporalQuery.aggrPeriodDuration - val allAggregates = temporalQuery.aggrMethods?.joinToString(",") { - val sqlAggregateExpression = - aggrMethodToSqlAggregate(it, temporalEntityAttributes[0].attributeValueType) - "$sqlAggregateExpression as ${it.method}_value" - } + val allAggregates = temporalQuery.aggrMethods + ?.composeAggregationSelectClause(temporalEntityAttributes[0].attributeValueType) // if retrieving a temporal entity, origin is calculated beforehand as timeAt is optional in this case // if querying temporal entities, timeAt is mandatory and will be used if origin is null if (aggrPeriodDuration != WHOLE_TIME_RANGE_DURATION) { diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/util/DBAggregationUtils.kt b/search-service/src/main/kotlin/com/egm/stellio/search/util/DBAggregationUtils.kt index 23d9f9d32..fbe6e4f56 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/util/DBAggregationUtils.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/util/DBAggregationUtils.kt @@ -1,22 +1,22 @@ package com.egm.stellio.search.util -import com.egm.stellio.search.model.TemporalEntityAttribute +import com.egm.stellio.search.model.TemporalEntityAttribute.AttributeValueType import com.egm.stellio.search.model.TemporalQuery fun aggrMethodToSqlAggregate( aggregate: TemporalQuery.Aggregate, - attributeValueType: TemporalEntityAttribute.AttributeValueType + attributeValueType: AttributeValueType ): String = when (attributeValueType) { - TemporalEntityAttribute.AttributeValueType.STRING -> sqlAggregationForJsonString(aggregate) - TemporalEntityAttribute.AttributeValueType.NUMBER -> sqlAggregateForJsonNumber(aggregate) - TemporalEntityAttribute.AttributeValueType.OBJECT -> sqlAggregateForJsonObject(aggregate) - TemporalEntityAttribute.AttributeValueType.ARRAY -> sqlAggregateForJsonArray(aggregate) - TemporalEntityAttribute.AttributeValueType.BOOLEAN -> sqlAggregateForJsonBoolean(aggregate) - TemporalEntityAttribute.AttributeValueType.DATETIME -> sqlAggregateForDateTime(aggregate) - TemporalEntityAttribute.AttributeValueType.DATE -> sqlAggregateForDate(aggregate) - TemporalEntityAttribute.AttributeValueType.TIME -> sqlAggregateForTime(aggregate) - TemporalEntityAttribute.AttributeValueType.URI -> sqlAggregateForURI(aggregate) - TemporalEntityAttribute.AttributeValueType.GEOMETRY -> "null" + AttributeValueType.STRING -> sqlAggregationForJsonString(aggregate) + AttributeValueType.NUMBER -> sqlAggregateForJsonNumber(aggregate) + AttributeValueType.OBJECT -> sqlAggregateForJsonObject(aggregate) + AttributeValueType.ARRAY -> sqlAggregateForJsonArray(aggregate) + AttributeValueType.BOOLEAN -> sqlAggregateForJsonBoolean(aggregate) + AttributeValueType.DATETIME -> sqlAggregateForDateTime(aggregate) + AttributeValueType.DATE -> sqlAggregateForDate(aggregate) + AttributeValueType.TIME -> sqlAggregateForTime(aggregate) + AttributeValueType.URI -> sqlAggregateForURI(aggregate) + AttributeValueType.GEOMETRY -> "null" } fun sqlAggregationForJsonString(aggregate: TemporalQuery.Aggregate): String = when (aggregate) { @@ -95,3 +95,9 @@ fun sqlAggregateForURI(aggregate: TemporalQuery.Aggregate): String = when (aggre TemporalQuery.Aggregate.DISTINCT_COUNT -> "count(distinct(value))" else -> "null" } + +fun List?.composeAggregationSelectClause(attributeValueType: AttributeValueType): String? = + this?.joinToString(",") { + val sqlAggregateExpression = aggrMethodToSqlAggregate(it, attributeValueType) + "$sqlAggregateExpression as ${it.method}_value" + }