question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getCountQuery doesn't respect distinct flag

See original GitHub issue

It seems our getCountQuery implementation doesn’t respect the distinct flag i.e. it should construct a count(distinct ...) for the select list in that case. Also, it can’t work when group by and distinct are combined.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
beikovcommented, Oct 15, 2020

Thanks for testing! I think these cases are covered by the following testsuite methods:

  • testCountWithHavingClauseThatCannotBeExpressedAsWhereClause is covered by com.blazebit.persistence.testsuite.CountQueryTest#countQueryFromHavingQuery
  • testCountWithGroupByClauseThatCannotBeRemoved is covered by com.blazebit.persistence.testsuite.CountQueryTest#countQueryWithGroupBy
  • testCountDistinctWithGroupByClauseThatCannotBeRemoved is covered by com.blazebit.persistence.testsuite.CountQueryTest#countQueryWithDistinct
0reactions
jwgmeligmeylingcommented, Oct 15, 2020

I’ve just done a couple of tests in CountQueryTest against the current master, and they all worked flawlessly.

The scenario’s I’ve tested:

  • Having clause that cannot be expressed as a where clause, and thus requires a subquery
  • Group by clause that is cardinality increasing
  • Count distinct on multiple columns

My scenario’s were as follows, if you’re interested in any of them let me know, then I’ll provide them as patch. 👍 I wasn’t sure for which of these scenarios tests already exist.

@Test
public void testCountWithHavingClauseThatCannotBeExpressedAsWhereClause() {
    CriteriaBuilder<Tuple> crit = cbf.create(em, Tuple.class)
            .from(Person.class, "owner")
            .leftJoin("owner.ownedDocuments", "d")
            .select("owner.id")
            .select("count(d.id)")
            .groupBy("owner.id")
            .having("count(d.id)").gtExpression("3");

    final Long expected = (long) crit.getResultList().size();
    final Long actual = crit.getCountQuery().getSingleResult();
    assertEquals(expected, actual);
}

@Test
public void testCountWithGroupByClauseThatCannotBeRemoved() {
    CriteriaBuilder<Tuple> crit = cbf.create(em, Tuple.class)
            .from(Person.class, "owner")
            .leftJoin("owner.ownedDocuments", "d")
            .select("count(d.id)")
            .groupBy("CASE WHEN d.id < 2 THEN TRUE ELSE FALSE END");

    final Long expected = (long) crit.getResultList().size();
    final Long actual = crit.getCountQuery().getSingleResult();
    assertEquals(expected, actual);
}


@Test
public void testCountDistinctWithGroupByClauseThatCannotBeRemoved() {
    CriteriaBuilder<Tuple> crit = cbf.create(em, Tuple.class)
            .from(Person.class, "owner")
            .leftJoin("owner.ownedDocuments", "d")
            .distinct()
            .select("owner.id")
            .select("min(d.id)");

    final Long expected = (long) crit.getResultList().size();
    final Long actual = crit.getCountQuery().getSingleResult();
    assertEquals(expected, actual);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to count distinct flags in the sql - Stack Overflow
You can use conditional aggregation. SELECT Category, COUNT(DISTINCT CASE WHEN Flag = 1 THEN PERSON END) FROM MYGROUP GROUP BY Category;.
Read more >
Overview of the SQL Count Distinct Function - SQLShack
This article explores SQL Count Distinct operator for eliminating the duplicate rows in the result set. A developer needs to get data from...
Read more >
DAX measure to find distinct count of flags set to '1' and '0'
Hi Experts, I have a table say employee which has a attendance flag say flag set to 1 or 0 stating whether they...
Read more >
HAVING clause of GROUP BY. - Ask TOM
Hi Tom,Recently I came across a query with the following text:SELECT bbl1. ... key from ttest group by key having count(distinct flag) =...
Read more >
Using Self Joins To Calculate Your Retention, Churn, And ...
This is tricky in SQL, which doesn't have explicit ways to bucket users ... user_id from events ) select this_month.month, count(distinct user_id) from ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found