Skip to content

Commit

Permalink
MONDRIAN: Add unit test for Dialect.allowsSelectNotInGroupBy.
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//open/mondrian/": change = 12923]
  • Loading branch information
julianhyde committed Jul 9, 2009
1 parent e2d667d commit df4bfb8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
16 changes: 13 additions & 3 deletions src/main/mondrian/spi/Dialect.java
Expand Up @@ -663,9 +663,19 @@ boolean supportsResultSetConcurrency(
boolean allowsDialectSharing();

/**
* Returns whether the database currently permits queries to
* SELECT columns that are not listed in the GROUP BY clause.
* Most don't, MySQL is an example of one that does.
* Returns whether the database currently permits queries to include in the
* SELECT clause expressions that are not listed in the GROUP BY clause. The
* SQL standard allows this if the database can deduce that the expression
* is functionally dependent on columns in the GROUP BY clause.
*
* <p>For example, {@code SELECT empno, first_name || ' ' || last_name FROM
* emps GROUP BY empno} is valid because {@code empno} is the primary key of
* the {@code emps} table, and therefore all columns are dependent on it.
* For a given value of {@code empno},
* {@code first_name || ' ' || last_name} has a unique value.
*
* <p>Most databases do not, MySQL is an example of one that does (if the
* functioality is enabled).
*
* @return Whether this Dialect allows SELECT clauses to contain
* columns that are not in the GROUP BY clause
Expand Down
2 changes: 1 addition & 1 deletion src/main/mondrian/spi/impl/JdbcDialectImpl.java
Expand Up @@ -759,7 +759,7 @@ public boolean requiresUnionOrderByOrdinal() {
}

public boolean requiresUnionOrderByExprToBeInSelectClause() {
return false;
return true;
}

public boolean supportsMultiValueInExpr() {
Expand Down
52 changes: 46 additions & 6 deletions testsrc/main/mondrian/test/DialectTest.java
Expand Up @@ -369,6 +369,10 @@ public void testRequiresUnionOrderByExprToBeInSelectClause() {
+ "includes fields that are not selected by the query\\. "
+ "Only those fields requested in the first query can be "
+ "included in an ORDER BY expression\\.",
// derby (yes, lame message)
"Java exception: ': java.lang.NullPointerException'.",
// oracle
"ORA-01785: ORDER BY item must be the number of a SELECT-list expression\n",
};
assertQueryFails(sql, errs);
}
Expand All @@ -383,11 +387,11 @@ public void testSupportsGroupByExpressions() {
if (getDialect().supportsGroupByExpressions()) {
assertQuerySucceeds(sql);
} else {
assertQueryFails(
sql,
new String[] {
"'sum\\(`unit_sales` \\+ 3\\) \\+ 8' isn't in GROUP BY"
});
final String[] errs = {
// mysql
"'sum\\(`unit_sales` \\+ 3\\) \\+ 8' isn't in GROUP BY",
};
assertQueryFails(sql, errs);
}
}

Expand Down Expand Up @@ -739,7 +743,7 @@ protected void assertQueryFails(String sql, String[] patterns) {
}
throw new AssertionFailedError(
"error [" + message
+ "] did not match any of the supplied patterns");
+ "] did not match any of the supplied patterns");
}
assertTrue(resultSet.next());
Object col1 = resultSet.getObject(1);
Expand All @@ -756,6 +760,42 @@ protected void assertQueryFails(String sql, String[] patterns) {
}
}
}

/**
* Unit test for {@link Dialect#allowsSelectNotInGroupBy}.
*/
public void testAllowsSelectNotInGroupBy() throws SQLException {
Dialect dialect = getDialect();
String sql =
"select "
+ dialect.quoteIdentifier("time_id")
+ ", "
+ dialect.quoteIdentifier("the_month")
+ " from "
+ dialect.quoteIdentifier("time_by_day")
+ " group by "
+ dialect.quoteIdentifier("time_id");
if (dialect.allowsSelectNotInGroupBy()) {
final ResultSet resultSet =
getConnection().createStatement().executeQuery(sql);
assertTrue(resultSet.next());
resultSet.close();
} else {
String[] errs = {
// oracle
"ORA-00979: not a GROUP BY expression\n",
// derby
"The SELECT list of a grouped query contains at least one "
+ "invalid expression. If a SELECT list has a GROUP BY, the "
+ "list may only contain valid grouping expressions and valid "
+ "aggregate expressions. ",
// mysql (if sql_mode contains ONLY_FULL_GROUP_BY)
"ERROR 1055 (42000): 'foodmart.time_by_day.the_month' isn't in "
+ "GROUP BY",
};
assertQueryFails(sql, errs);
}
}
}

// End DialectTest.java

0 comments on commit df4bfb8

Please sign in to comment.