Skip to content

Commit

Permalink
IGNITE-23112 SQL Calcite: Allow grouping by alias and ordinal value - F…
Browse files Browse the repository at this point in the history
…ixes #11500.

Signed-off-by: Aleksey Plekhanov <plehanov.alex@gmail.com>
  • Loading branch information
Vladsz83 authored and alex-plekhanov committed Sep 24, 2024
1 parent 3a789b6 commit 6fa2be7
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ public class IgniteSqlConformance extends SqlAbstractConformance {
@Override public boolean isBangEqualAllowed() {
return true;
}

/** {@inheritDoc} */
@Override public boolean isGroupByAlias() {
return true;
}

/** {@inheritDoc} */
@Override public boolean isGroupByOrdinal() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.ignite.internal.processors.query.calcite.integration;

import java.util.List;
import org.apache.calcite.sql.validate.SqlConformance;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.cache.CacheMode;
Expand Down Expand Up @@ -140,6 +141,32 @@ public void testCountIndexedField() {
assertQuery("SELECT COUNT(b) FROM tbl").returns(100L).check();
}

/**
* Tests grouping result by an alias and an ordinal value.
*
* @see SqlConformance#isGroupByAlias()
* @see SqlConformance#isGroupByOrdinal()
*/
@Test
public void testGroupingByAlias() {
executeSql("CREATE TABLE t1(id INT, val_int INT, val_char VARCHAR, PRIMARY KEY(id))");

for (int i = 0; i < 10; i++)
executeSql("INSERT INTO t1 VALUES (?, ?, ?)", i, i % 3, "val" + i % 3);

assertQuery("SELECT val_char as ALS, count(val_int) FROM t1 GROUP BY ALS")
.returns("val0", 4L)
.returns("val1", 3L)
.returns("val2", 3L)
.check();

assertQuery("SELECT val_char, count(val_int) FROM t1 GROUP BY 1")
.returns("val0", 4L)
.returns("val1", 3L)
.returns("val2", 3L)
.check();
}

/** */
@Test
public void testCountOfNonNumericField() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.sql.SqlExplainLevel;
import org.apache.calcite.sql.fun.SqlAvgAggFunction;
import org.apache.calcite.sql.validate.SqlConformance;
import org.apache.ignite.internal.processors.query.calcite.rel.IgniteAggregate;
import org.apache.ignite.internal.processors.query.calcite.rel.IgniteIndexScan;
import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel;
Expand Down Expand Up @@ -326,6 +327,38 @@ public void singleSumTypes() throws Exception {
assertEquals(tf.createJavaType(Double.class), rowTypes.getFieldList().get(7).getType());
}

/**
* Tests that grouping plan works with an alias and an ordinal value.
*
* @see SqlConformance#isGroupByAlias()
* @see SqlConformance#isGroupByOrdinal()
*/
@Test
public void groupingByAliasAndOrdinal() throws Exception {
IgniteSchema schema = createSchema(
createTable(
"TEST", IgniteDistributions.broadcast(),
"ID", Integer.class,
"GRP", Integer.class,
"VAL_INT", Integer.class
)
);

assertPlan(
"SELECT GRP AS ALS, SUM(VAL_INT) FROM test GROUP BY ALS",
schema,
nodeOrAnyChild(isInstanceOf(algo.colocated)),
algo.rulesToDisable
);

assertPlan(
"SELECT GRP, SUM(VAL_INT) FROM test GROUP BY 1",
schema,
nodeOrAnyChild(isInstanceOf(algo.colocated)),
algo.rulesToDisable
);
}

/**
* @throws Exception If failed.
*/
Expand Down
34 changes: 30 additions & 4 deletions modules/calcite/src/test/sql/aggregate/group/test_group_by.test
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ SELECT b, SUM(a), COUNT(*), SUM(a+2) FROM test WHERE a <= 12 GROUP BY b ORDER BY
statement error
SELECT b % 2 AS f, COUNT(SUM(a)) FROM test GROUP BY f;

# group by alias
query IR
SELECT b % 2 AS f, SUM(a) FROM test GROUP BY f ORDER By f;
----
0 24.000000
1 12.000000

statement ok
INSERT INTO test VALUES (12, 21), (12, 21), (12, 21)

Expand Down Expand Up @@ -113,13 +120,32 @@ SELECT i, SUM(j), ANY_VALUE(j) FROM integers GROUP BY i ORDER BY i
2 4.000000 4
3 8.000000 4

# use an alias that is identical to a column name (should prioritize column name)
# group by constant alias
query IR
SELECT 1 AS i, SUM(i) FROM integers GROUP BY i ORDER BY 2;
SELECT 1 AS k, SUM(i) FROM integers GROUP BY k ORDER BY 2;
----
1 8.000000

# use an alias that is identical to a column name and the table reference
query IR
SELECT 1 AS i, SUM(t.i) FROM integers t GROUP BY t.i ORDER BY 2;
----
1 2.000000
1 6.000000

# use an alias that is identical to a column name
query IR
SELECT 1 AS i, SUM(i) FROM integers GROUP BY i ORDER BY 2;
----
1 8.000000

# refer to the same alias twice
query IR
SELECT i % 2 AS k, SUM(i) FROM integers GROUP BY k, k ORDER BY 1;
----
0 2.000000
1 6.000000

statement ok
DROP TABLE integers;

Expand All @@ -138,9 +164,9 @@ NULL NULL
2 2.000000
3 3.000000

# column reference should have preference over alias reference in grouping
# column reference have to be specified when an alias with the same name exists
query IIR
SELECT i, i % 2 AS i, SUM(i) FROM integers GROUP BY i ORDER BY 1 NULLS FIRST;
SELECT i, i % 2 AS i, SUM(i) FROM integers t GROUP BY t.i ORDER BY 1 NULLS FIRST;
----
NULL NULL NULL
1 1 1.000000
Expand Down
182 changes: 0 additions & 182 deletions modules/calcite/src/test/sql/aggregate/group/test_group_by.test_ignore

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ CREATE TABLE integers(i INTEGER)
statement ok
INSERT INTO integers VALUES (1), (2), (3), (NULL)

# an alias in grouping + HAVING
# CONTROVERSIAL: this query does not work in MonetDB
query IR
SELECT i % 2 AS k, SUM(i) FROM integers WHERE i IS NOT NULL GROUP BY k HAVING i%2>0;
----
1 4.000000

# select groups by constant (similar to order by constant)
query IR
SELECT i % 2 AS k, SUM(i) FROM integers WHERE i IS NOT NULL GROUP BY 1 HAVING i%2>0;
----
1 4.000000

# constant out of range
statement error
SELECT i % 2 AS k, SUM(i) FROM integers WHERE i IS NOT NULL GROUP BY 42 HAVING i%2>0;

# this now orders by the actual grouping column
query IIR
SELECT i, i % 2 AS k, SUM(i) FROM integers GROUP BY i ORDER BY i;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# name: test/sql/aggregate/group/test_group_by_alias.test
# description: Test aliases in group by/aggregation
# group: [group]
# Ignored: https://issues.apache.org/jira/browse/IGNITE-14885

statement ok
PRAGMA enable_verification
Expand Down
Loading

0 comments on commit 6fa2be7

Please sign in to comment.