Repository navigation
Run an aggregate on the right of a data service @Where comparison as a subquery #17153
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
Open
borinquenkid
wants to merge
4
commits into
8.0.x
Choose a base branch
from
fix/service-where-aggregate-8.0.x
base: 8.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d908a35
fix(datamapping): run an aggregate on the right of a data service @Wh…
borinquenkid 8301b0a
fix(datamapping): an aggregate function inside the block of an embedd…
borinquenkid d862d7f
docs(upgrading): an aggregate function inside the block of an embedde…
borinquenkid aaba3bc
Merge 8.0.x into fix/service-where-aggregate-8.0.x
borinquenkid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
...5/core/src/test/groovy/org/grails/orm/hibernate/services/WhereServiceAggregateSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.grails.orm.hibernate.services | ||
|
|
||
| import spock.lang.AutoCleanup | ||
| import spock.lang.Shared | ||
| import spock.lang.Specification | ||
|
|
||
| import grails.gorm.DetachedCriteria | ||
| import grails.gorm.annotation.Entity | ||
| import grails.gorm.services.Service | ||
| import grails.gorm.services.Where | ||
|
|
||
| import org.grails.orm.hibernate.HibernateDatastore | ||
|
|
||
| /** | ||
| * An aggregate on the right of a comparison in the {@code @Where} of a data service is run as a subquery over the | ||
| * entity of the service, as it is in a {@code where} query: the comparisons with the average, the maximum and the | ||
| * minimum give the same rows as the query written with {@code where}. | ||
| */ | ||
| class WhereServiceAggregateSpec extends Specification { | ||
|
|
||
| @Shared @AutoCleanup HibernateDatastore datastore = new HibernateDatastore(WsaReading) | ||
|
|
||
| @Shared WsaReadingService service | ||
|
|
||
| void setupSpec() { | ||
| service = datastore.getService(WsaReadingService) | ||
| } | ||
|
|
||
| void setup() { | ||
| WsaReading.withNewTransaction { | ||
| WsaReading.executeUpdate('delete from WsaReading') | ||
| [1, 2, 3, 4, 10].each { new WsaReading(qty: it).save(failOnError: true, flush: true) } | ||
| } | ||
| } | ||
|
|
||
| void "a data service @Where compares with the average of the entity like the same where query"() { | ||
| expect: 'the quantities are 1, 2, 3, 4 and 10, so the average is 4' | ||
| found(service.aboveAverage()) == [10] | ||
| found(listOf(aboveAverageQuery())) == [10] | ||
| } | ||
|
|
||
| void "a data service @Where compares with the average of the entity or equal to it like the same where query"() { | ||
| expect: | ||
| found(service.atLeastAverage()) == [4, 10] | ||
| found(listOf(atLeastAverageQuery())) == [4, 10] | ||
| } | ||
|
|
||
| void "a data service @Where compares with the maximum of the entity like the same where query"() { | ||
| expect: | ||
| found(service.theMaximum()) == [10] | ||
| found(listOf(maximumQuery())) == [10] | ||
| } | ||
|
|
||
| void "a data service @Where compares with the minimum of the entity like the same where query"() { | ||
| expect: | ||
| found(service.theMinimum()) == [1] | ||
| found(listOf(minimumQuery())) == [1] | ||
| } | ||
|
|
||
| static DetachedCriteria<WsaReading> aboveAverageQuery() { | ||
| WsaReading.where { qty > avg(qty) } | ||
| } | ||
|
|
||
| static DetachedCriteria<WsaReading> atLeastAverageQuery() { | ||
| WsaReading.where { qty >= avg(qty) } | ||
| } | ||
|
|
||
| static DetachedCriteria<WsaReading> maximumQuery() { | ||
| WsaReading.where { qty == max(qty) } | ||
| } | ||
|
|
||
| static DetachedCriteria<WsaReading> minimumQuery() { | ||
| WsaReading.where { qty == min(qty) } | ||
| } | ||
|
|
||
| private static List<WsaReading> listOf(DetachedCriteria<WsaReading> query) { | ||
| WsaReading.withNewTransaction { query.list() } as List<WsaReading> | ||
| } | ||
|
|
||
| private static List<Integer> found(List<WsaReading> readings) { | ||
| readings*.qty.sort() | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class WsaReading { | ||
|
|
||
| Integer qty | ||
| } | ||
|
|
||
| @Service(WsaReading) | ||
| interface WsaReadingService { | ||
|
|
||
| @Where({ qty > avg(qty) }) | ||
| List<WsaReading> aboveAverage() | ||
|
|
||
| @Where({ qty >= avg(qty) }) | ||
| List<WsaReading> atLeastAverage() | ||
|
|
||
| @Where({ qty == max(qty) }) | ||
| List<WsaReading> theMaximum() | ||
|
|
||
| @Where({ qty == min(qty) }) | ||
| List<WsaReading> theMinimum() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stats { qty > avg(qty) } should return the reading whose quantity is above the average. It throws, because the average is built against the embedded type as if it were its own class. Build the average against the reading.