/** * Adds a {@code GROUP BY} clause to this statement. * * <p>Note: support for {@code GROUP BY} clause is only available from Cassandra 3.10 onwards. * * @param columns the columns to group by. * @return the {@code SELECT} statement this {@code WHERE} clause is part of. * @throws IllegalStateException if a {@code GROUP BY} clause has already been provided. */ public Select groupBy(Object... columns) { return statement.groupBy(columns); }
assertThat(select().all().from("foo").groupBy("c1", column("c2"), raw("c3")).toString()) .isEqualTo("SELECT * FROM foo GROUP BY c1,c2,c3;"); assertThat( .all() .from("foo") .groupBy("c1", column("c2"), raw("c3")) .orderBy(asc("c1")) .toString()) .isEqualTo("SELECT * FROM foo WHERE x=42 GROUP BY c1,c2,c3 ORDER BY c1 ASC;"); try { select().all().from("foo").groupBy("foo").groupBy("bar"); fail("Should not allow GROUP BY twice"); } catch (IllegalStateException e) {
select("a", "b", "e", count("b"), max("e")) .from(table) .groupBy("a", "b") .limit(2))) .containsExactly(row(1, 2, 6, 2L, 12), row(1, 4, 12, 2L, 24)); select("a", "b", "e", count("b"), max("e")) .from(table) .groupBy("a", "b") .perPartitionLimit(1))) .containsExactly(row(1, 2, 6, 2L, 12), row(2, 2, 6, 1L, 6), row(4, 8, 24, 1L, 24)); select("a", "b", "e", count("b"), max("e")) .from(table) .groupBy("a", "b") .perPartitionLimit(1) .limit(2))) assertThat(session().execute(select("a", count("a")).distinct().from(table).groupBy("a"))) .containsExactly(row(1, 1L), row(2, 1L), row(4, 1L)); session().execute(select("a", count("a")).distinct().from(table).groupBy("a").limit(2))) .containsExactly(row(1, 1L), row(2, 1L)); session().execute(select().column("a").column("b").max("d").from(table).groupBy("a")); fail("Expecting IQE"); } catch (InvalidQueryException e) { assertThat(session().execute(select("a", "b", max("d")).from(table).groupBy("a", "b")))
assertThat(select().all().from("foo").groupBy("c1", column("c2"), raw("c3")).toString()) .isEqualTo("SELECT * FROM foo GROUP BY c1,c2,c3;"); assertThat( .all() .from("foo") .groupBy("c1", column("c2"), raw("c3")) .orderBy(asc("c1")) .toString()) .isEqualTo("SELECT * FROM foo WHERE x=42 GROUP BY c1,c2,c3 ORDER BY c1 ASC;"); try { select().all().from("foo").groupBy("foo").groupBy("bar"); fail("Should not allow GROUP BY twice"); } catch (IllegalStateException e) {
select("a", "b", "e", count("b"), max("e")) .from(table) .groupBy("a", "b") .limit(2))) .containsExactly(row(1, 2, 6, 2L, 12), row(1, 4, 12, 2L, 24)); select("a", "b", "e", count("b"), max("e")) .from(table) .groupBy("a", "b") .perPartitionLimit(1))) .containsExactly(row(1, 2, 6, 2L, 12), row(2, 2, 6, 1L, 6), row(4, 8, 24, 1L, 24)); select("a", "b", "e", count("b"), max("e")) .from(table) .groupBy("a", "b") .perPartitionLimit(1) .limit(2))) assertThat(session().execute(select("a", count("a")).distinct().from(table).groupBy("a"))) .containsExactly(row(1, 1L), row(2, 1L), row(4, 1L)); session().execute(select("a", count("a")).distinct().from(table).groupBy("a").limit(2))) .containsExactly(row(1, 1L), row(2, 1L)); session().execute(select().column("a").column("b").max("d").from(table).groupBy("a")); fail("Expecting IQE"); } catch (InvalidQueryException e) { assertThat(session().execute(select("a", "b", max("d")).from(table).groupBy("a", "b")))