mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1619 - Support aggregation formula on fetch clause e.g. query.fetch("machineUsage", "sum(totalKms)")
This commit is contained in:
@@ -61,7 +61,7 @@ public class FormulaPropertyPathTest extends BaseTestCase {
|
||||
|
||||
private void assertFormula(String input, String funcName, String expression, String cast, String alias) {
|
||||
|
||||
FormulaPropertyPath propertyPath = new FormulaPropertyPath(customerDesc, input);
|
||||
FormulaPropertyPath propertyPath = new FormulaPropertyPath(customerDesc, input, null);
|
||||
|
||||
assertThat(propertyPath.internalExpression()).isEqualTo(expression);
|
||||
assertThat(propertyPath.outerFunction()).isEqualTo(funcName);
|
||||
|
||||
@@ -4,6 +4,7 @@ import io.ebean.Model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Version;
|
||||
import java.util.List;
|
||||
@@ -12,20 +13,32 @@ import java.util.List;
|
||||
public class DMachine extends Model {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
private long id;
|
||||
|
||||
String name;
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
private final DOrg organisation;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
private long version;
|
||||
|
||||
@OneToMany(mappedBy = "machine")
|
||||
List<DMachineStats> machineStats;
|
||||
private List<DMachineStats> machineStats;
|
||||
|
||||
public DMachine(String name) {
|
||||
@OneToMany(mappedBy = "machine")
|
||||
private List<DMachineAuxUseAgg> auxUseAggs;
|
||||
|
||||
public DMachine(DOrg organisation, String name) {
|
||||
this.organisation = organisation;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -42,6 +55,10 @@ public class DMachine extends Model {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public DOrg getOrganisation() {
|
||||
return organisation;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
@@ -57,4 +74,12 @@ public class DMachine extends Model {
|
||||
public void setMachineStats(List<DMachineStats> machineStats) {
|
||||
this.machineStats = machineStats;
|
||||
}
|
||||
|
||||
public List<DMachineAuxUseAgg> getAuxUseAggs() {
|
||||
return auxUseAggs;
|
||||
}
|
||||
|
||||
public void setAuxUseAggs(List<DMachineAuxUseAgg> auxUseAggs) {
|
||||
this.auxUseAggs = auxUseAggs;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package org.tests.model.aggregation;
|
||||
|
||||
import io.ebean.Model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Entity
|
||||
@Table(name = "d_machine_aux_use")
|
||||
public class DMachineAuxUse extends Model {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
@ManyToOne(optional = false)
|
||||
private DMachine machine;
|
||||
|
||||
private String name;
|
||||
|
||||
private LocalDate date;
|
||||
|
||||
private long useSecs;
|
||||
|
||||
private BigDecimal fuel;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public DMachineAuxUse(DMachine machine, LocalDate date, String name) {
|
||||
this.machine = machine;
|
||||
this.date = date;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public DMachine getMachine() {
|
||||
return machine;
|
||||
}
|
||||
|
||||
public void setMachine(DMachine machine) {
|
||||
this.machine = machine;
|
||||
}
|
||||
|
||||
public LocalDate getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(LocalDate date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public long getUseSecs() {
|
||||
return useSecs;
|
||||
}
|
||||
|
||||
public void setUseSecs(long useSecs) {
|
||||
this.useSecs = useSecs;
|
||||
}
|
||||
|
||||
public BigDecimal getFuel() {
|
||||
return fuel;
|
||||
}
|
||||
|
||||
public void setFuel(BigDecimal fuel) {
|
||||
this.fuel = fuel;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package org.tests.model.aggregation;
|
||||
|
||||
import io.ebean.annotation.Sum;
|
||||
import io.ebean.annotation.View;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.ManyToOne;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Entity
|
||||
@View(name = "d_machine_aux_use")
|
||||
public class DMachineAuxUseAgg {
|
||||
|
||||
@ManyToOne
|
||||
private DMachine machine;
|
||||
|
||||
private String name;
|
||||
|
||||
private LocalDate date;
|
||||
|
||||
@Sum
|
||||
private long useSecs;
|
||||
|
||||
@Sum
|
||||
private BigDecimal fuel;
|
||||
|
||||
public DMachine getMachine() {
|
||||
return machine;
|
||||
}
|
||||
|
||||
public void setMachine(DMachine machine) {
|
||||
this.machine = machine;
|
||||
}
|
||||
|
||||
public LocalDate getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(LocalDate date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public long getUseSecs() {
|
||||
return useSecs;
|
||||
}
|
||||
|
||||
public void setUseSecs(long useSecs) {
|
||||
this.useSecs = useSecs;
|
||||
}
|
||||
|
||||
public BigDecimal getFuel() {
|
||||
return fuel;
|
||||
}
|
||||
|
||||
public void setFuel(BigDecimal fuel) {
|
||||
this.fuel = fuel;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package org.tests.model.aggregation;
|
||||
|
||||
import io.ebean.Model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Version;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Entity
|
||||
@Table(name = "d_machine_use")
|
||||
public class DMachineUse extends Model {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
@ManyToOne(optional = false)
|
||||
private DMachine machine;
|
||||
|
||||
private LocalDate date;
|
||||
|
||||
private long distanceKms;
|
||||
|
||||
private long timeSecs;
|
||||
|
||||
private BigDecimal fuel;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
public DMachineUse(DMachine machine, LocalDate date) {
|
||||
this.machine = machine;
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public DMachine getMachine() {
|
||||
return machine;
|
||||
}
|
||||
|
||||
public void setMachine(DMachine machine) {
|
||||
this.machine = machine;
|
||||
}
|
||||
|
||||
public LocalDate getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(LocalDate date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public long getDistanceKms() {
|
||||
return distanceKms;
|
||||
}
|
||||
|
||||
public void setDistanceKms(long distanceKms) {
|
||||
this.distanceKms = distanceKms;
|
||||
}
|
||||
|
||||
public long getTimeSecs() {
|
||||
return timeSecs;
|
||||
}
|
||||
|
||||
public void setTimeSecs(long timeSecs) {
|
||||
this.timeSecs = timeSecs;
|
||||
}
|
||||
|
||||
public BigDecimal getFuel() {
|
||||
return fuel;
|
||||
}
|
||||
|
||||
public void setFuel(BigDecimal fuel) {
|
||||
this.fuel = fuel;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.tests.model.aggregation;
|
||||
|
||||
import io.ebean.Model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class DOrg extends Model {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public DOrg(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.tests.model.aggregation;
|
||||
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class MachineUseData {
|
||||
|
||||
public static DOrg load() {
|
||||
|
||||
int count = Ebean.find(DMachineUse.class).findCount();
|
||||
if (count == 0) {
|
||||
return new MachineUseData().loadData();
|
||||
} else {
|
||||
return Ebean.find(DOrg.class).where().eq("name", "org0").findOne();
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(batchSize = 50)
|
||||
private DOrg loadData() {
|
||||
|
||||
DOrg org = new DOrg("org0");
|
||||
org.save();
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
DMachine machine = new DMachine(org, "org0-m" + i);
|
||||
machine.save();
|
||||
|
||||
LocalDate startDate = LocalDate.of(2018, 1, 1);
|
||||
|
||||
for (int j = 0; j < 4; j++) {
|
||||
startDate = startDate.plusDays(5);
|
||||
createUseFor(machine, startDate, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
return org;
|
||||
}
|
||||
|
||||
private void createUseFor(DMachine machine, LocalDate date, int machineIndex, int dayIndex) {
|
||||
|
||||
DMachineUse use = new DMachineUse(machine, date);
|
||||
int kms = 100 * machineIndex + dayIndex;
|
||||
use.setDistanceKms(kms);
|
||||
use.setFuel(BigDecimal.valueOf(kms).multiply(new BigDecimal(0.1), new MathContext(4)));
|
||||
use.setTimeSecs(10000 * machineIndex);
|
||||
use.save();
|
||||
|
||||
String[] aux = {"Aux1", "Aux2"};
|
||||
if (machineIndex % 2 != 0) {
|
||||
aux = new String[]{"Aux3"};
|
||||
}
|
||||
|
||||
int count = 1;
|
||||
for (String auxName : aux) {
|
||||
DMachineAuxUse auxUse = new DMachineAuxUse(machine, date, auxName);
|
||||
auxUse.setUseSecs(10 * machineIndex * 10 + (dayIndex + count++));
|
||||
auxUse.setFuel(BigDecimal.valueOf((dayIndex + machineIndex * 7) + count));
|
||||
auxUse.save();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package org.tests.model.aggregation;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestAggregationMany extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void fetchQuery_toAggregate() {
|
||||
|
||||
DOrg org = MachineUseData.load();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<DMachine> machines = Ebean.find(DMachine.class)
|
||||
.setDisableLazyLoading(true)
|
||||
.fetchQuery("auxUseAggs", "name, useSecs, fuel")
|
||||
.where().eq("organisation", org)
|
||||
.findList();
|
||||
|
||||
for (DMachine machine : machines) {
|
||||
assertThat(machine.getAuxUseAggs()).isNotEmpty();
|
||||
assertThat(machine.getMachineStats()).isEmpty();
|
||||
}
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(2);
|
||||
assertThat(sql.get(0)).contains("select t0.id, t0.name, t0.version, t0.organisation_id from dmachine t0 where t0.organisation_id = ?");
|
||||
|
||||
assertThat(machines).hasSize(5);
|
||||
if (isH2()) {
|
||||
assertThat(sql.get(1)).contains("select t0.machine_id, t0.name, sum(t0.use_secs), sum(t0.fuel) from d_machine_aux_use t0 where (t0.machine_id) in (?, ?, ?, ?, ? ) group by t0.machine_id, t0.name;");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void fetch_toAggregate() {
|
||||
|
||||
DOrg org = MachineUseData.load();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<DMachine> machines = Ebean.find(DMachine.class)
|
||||
.setDisableLazyLoading(true)
|
||||
.select("name")
|
||||
.fetch("auxUseAggs", "name, useSecs, fuel")
|
||||
.where().eq("organisation", org)
|
||||
.findList();
|
||||
|
||||
for (DMachine machine : machines) {
|
||||
assertThat(machine.getAuxUseAggs()).isNotEmpty();
|
||||
assertThat(machine.getMachineStats()).isEmpty();
|
||||
}
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(sql.get(0)).contains("select t0.id, t0.name, t1.name, sum(t1.use_secs), sum(t1.fuel) from dmachine t0 left join d_machine_aux_use t1 on t1.machine_id = t0.id where t0.organisation_id = ? group by t0.id, t0.name, t1.name order by t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fetch_toAggregate_onlyAggColumnsInFetchProperties() {
|
||||
|
||||
DOrg org = MachineUseData.load();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<DMachine> machines = Ebean.find(DMachine.class)
|
||||
.setDisableLazyLoading(true)
|
||||
.select("name")
|
||||
.fetch("auxUseAggs", "useSecs, fuel")
|
||||
.where().eq("organisation", org)
|
||||
.findList();
|
||||
|
||||
for (DMachine machine : machines) {
|
||||
assertThat(machine.getAuxUseAggs()).isNotEmpty();
|
||||
System.out.println(machine);
|
||||
assertThat(machine.getMachineStats()).isEmpty();
|
||||
}
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(sql.get(0)).contains("select t0.id, t0.name, sum(t1.use_secs), sum(t1.fuel) from dmachine t0 left join d_machine_aux_use t1 on t1.machine_id = t0.id where t0.organisation_id = ? group by t0.id, t0.name order by t0.id");
|
||||
}
|
||||
}
|
||||
@@ -200,27 +200,62 @@ public class TestAggregationTopLevel extends BaseTestCase {
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void groupBy_in_fetchClause() {
|
||||
//
|
||||
// Query<DMachine> query = Ebean.find(DMachine.class)
|
||||
// .select("name")
|
||||
// .fetch("machineStats", "date, max(rate), sum(totalKms)")
|
||||
// .where().gt("machineStats.date", LocalDate.now().minusDays(10))
|
||||
// .having().gt("sum(machineStats.totalKms)", 1)
|
||||
// .query();
|
||||
//
|
||||
// List<DMachine> result = query.findList();
|
||||
// assertThat(sqlOf(query)).contains("select distinct t0.id, t0.name, t1.date, max(t1.rate), sum(t1.total_kms) from dmachine t0 left join d_machine_stats t1 on t1.machine_id = t0.id where date > ? group by t0.id, t0.name, t1.date having sum(t1.total_kms) > ? order by t0.id");
|
||||
// assertThat(result).isNotEmpty();
|
||||
// }
|
||||
@Test
|
||||
public void groupBy_in_fetchClause_singleRowInMany() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Query<DMachine> query = Ebean.find(DMachine.class)
|
||||
.select("name")
|
||||
.fetch("machineStats", "sum(totalKms)")
|
||||
.where().eq("name", "Machine0")
|
||||
.query();
|
||||
|
||||
List<DMachine> result = query.findList();
|
||||
assertThat(result).isNotEmpty();
|
||||
assertThat(result.get(0).getMachineStats().get(0).getTotalKms()).isNotNull();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
|
||||
assertThat(sqlOf(query)).contains("select t0.id, t0.name, sum(t1.total_kms) from dmachine t0 left join d_machine_stats t1 on t1.machine_id = t0.id where t0.name = ? group by t0.id, t0.name order by t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupBy_in_fetchClause_multipleRowsInMany() {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
Query<DMachine> query = Ebean.find(DMachine.class)
|
||||
.select("name")
|
||||
.fetch("machineStats", "date, max(rate), sum(totalKms)")
|
||||
.where().eq("name", "Machine0")
|
||||
.query();
|
||||
|
||||
List<DMachine> result = query.findList();
|
||||
assertThat(result).isNotEmpty();
|
||||
assertThat(result.get(0).getMachineStats().size()).isGreaterThan(1); // expect 8 as grouped by date
|
||||
|
||||
DMachineStats firstStat = result.get(0).getMachineStats().get(0);
|
||||
assertThat(firstStat.getTotalKms()).isNotNull();
|
||||
assertThat(firstStat.getRate()).isNotNull();
|
||||
assertThat(firstStat.getDate()).isNotNull();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
|
||||
assertThat(sqlOf(query)).contains("select t0.id, t0.name, t1.date, max(t1.rate), sum(t1.total_kms) from dmachine t0 left join d_machine_stats t1 on t1.machine_id = t0.id where t0.name = ? group by t0.id, t0.name, t1.date order by t0.id");
|
||||
}
|
||||
|
||||
|
||||
public static void loadData() {
|
||||
|
||||
List<DMachine> machines = new ArrayList<>();
|
||||
DOrg org = new DOrg("other");
|
||||
org.save();
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
machines.add(new DMachine("Machine"+i));
|
||||
machines.add(new DMachine(org, "Machine" + i));
|
||||
}
|
||||
|
||||
Ebean.saveAll(machines);
|
||||
|
||||
Reference in New Issue
Block a user