HHH-8864 count distinct tuples for postgres and h2
This commit is contained in:
parent
95566f772f
commit
d59387cd01
|
@ -2576,6 +2576,15 @@ public abstract class Dialect implements ConversionContext {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If {@link #supportsTupleDistinctCounts()} is true, does the Dialect require the tuple to be wrapped with parens?
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public boolean requiresParensForTupleDistinctCounts() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the limit that the underlying database places on the number elements in an {@code IN} predicate.
|
* Return the limit that the underlying database places on the number elements in an {@code IN} predicate.
|
||||||
* If the database defines no such limits, simply return zero or less-than-zero.
|
* If the database defines no such limits, simply return zero or less-than-zero.
|
||||||
|
|
|
@ -413,8 +413,8 @@ public class H2Dialect extends Dialect {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supportsTupleDistinctCounts() {
|
public boolean requiresParensForTupleDistinctCounts() {
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -361,8 +361,8 @@ public class PostgreSQL81Dialect extends Dialect {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supportsTupleDistinctCounts() {
|
public boolean requiresParensForTupleDistinctCounts() {
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -30,6 +30,7 @@ import java.util.Map;
|
||||||
|
|
||||||
import org.hibernate.MappingException;
|
import org.hibernate.MappingException;
|
||||||
import org.hibernate.QueryException;
|
import org.hibernate.QueryException;
|
||||||
|
import org.hibernate.dialect.Dialect;
|
||||||
import org.hibernate.engine.spi.Mapping;
|
import org.hibernate.engine.spi.Mapping;
|
||||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||||
import org.hibernate.type.StandardBasicTypes;
|
import org.hibernate.type.StandardBasicTypes;
|
||||||
|
@ -58,15 +59,16 @@ public class StandardAnsiSqlAggregationFunctions {
|
||||||
public String render(Type firstArgumentType, List arguments, SessionFactoryImplementor factory) {
|
public String render(Type firstArgumentType, List arguments, SessionFactoryImplementor factory) {
|
||||||
if ( arguments.size() > 1 ) {
|
if ( arguments.size() > 1 ) {
|
||||||
if ( "distinct".equalsIgnoreCase( arguments.get( 0 ).toString() ) ) {
|
if ( "distinct".equalsIgnoreCase( arguments.get( 0 ).toString() ) ) {
|
||||||
return renderCountDistinct( arguments );
|
return renderCountDistinct( arguments, factory.getDialect() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return super.render( firstArgumentType, arguments, factory );
|
return super.render( firstArgumentType, arguments, factory );
|
||||||
}
|
}
|
||||||
|
|
||||||
private String renderCountDistinct(List arguments) {
|
private String renderCountDistinct(List arguments, Dialect dialect) {
|
||||||
final StringBuilder buffer = new StringBuilder();
|
final StringBuilder buffer = new StringBuilder();
|
||||||
buffer.append( "count(distinct " );
|
buffer.append( "count(distinct " );
|
||||||
|
if (dialect.requiresParensForTupleDistinctCounts()) buffer.append("(");
|
||||||
String sep = "";
|
String sep = "";
|
||||||
final Iterator itr = arguments.iterator();
|
final Iterator itr = arguments.iterator();
|
||||||
// intentionally skip first
|
// intentionally skip first
|
||||||
|
@ -75,6 +77,7 @@ public class StandardAnsiSqlAggregationFunctions {
|
||||||
buffer.append( sep ).append( itr.next() );
|
buffer.append( sep ).append( itr.next() );
|
||||||
sep = ", ";
|
sep = ", ";
|
||||||
}
|
}
|
||||||
|
if (dialect.requiresParensForTupleDistinctCounts()) buffer.append(")");
|
||||||
return buffer.append( ")" ).toString();
|
return buffer.append( ")" ).toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -260,7 +260,10 @@ public class DotNode extends FromReferenceNode implements DisplayableNode, Selec
|
||||||
private void initText() {
|
private void initText() {
|
||||||
String[] cols = getColumns();
|
String[] cols = getColumns();
|
||||||
String text = StringHelper.join( ", ", cols );
|
String text = StringHelper.join( ", ", cols );
|
||||||
if ( cols.length > 1 && getWalker().isComparativeExpressionClause() ) {
|
boolean countDistinct = getWalker().isInCountDistinct()
|
||||||
|
&& getWalker().getSessionFactoryHelper().getFactory().getDialect().requiresParensForTupleDistinctCounts();
|
||||||
|
if ( cols.length > 1 &&
|
||||||
|
( getWalker().isComparativeExpressionClause() || countDistinct ) ) {
|
||||||
text = "(" + text + ")";
|
text = "(" + text + ")";
|
||||||
}
|
}
|
||||||
setText( text );
|
setText( text );
|
||||||
|
|
|
@ -27,6 +27,7 @@ package org.hibernate.hql.internal.ast.tree;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.hibernate.QueryException;
|
import org.hibernate.QueryException;
|
||||||
|
import org.hibernate.dialect.Dialect;
|
||||||
import org.hibernate.dialect.function.SQLFunction;
|
import org.hibernate.dialect.function.SQLFunction;
|
||||||
import org.hibernate.hql.internal.antlr.HqlSqlTokenTypes;
|
import org.hibernate.hql.internal.antlr.HqlSqlTokenTypes;
|
||||||
import org.hibernate.hql.internal.antlr.SqlTokenTypes;
|
import org.hibernate.hql.internal.antlr.SqlTokenTypes;
|
||||||
|
@ -173,10 +174,14 @@ public class IdentNode extends FromReferenceNode implements SelectExpression {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final boolean isInNonDistinctCount = getWalker().isInCount() && ! getWalker().isInCountDistinct();
|
final Dialect dialect = getWalker().getSessionFactoryHelper().getFactory().getDialect();
|
||||||
|
final boolean isInCount = getWalker().isInCount();
|
||||||
|
final boolean isInDistinctCount = isInCount && getWalker().isInCountDistinct();
|
||||||
|
final boolean isInNonDistinctCount = isInCount && ! getWalker().isInCountDistinct();
|
||||||
final boolean isCompositeValue = columnExpressions.length > 1;
|
final boolean isCompositeValue = columnExpressions.length > 1;
|
||||||
if ( isCompositeValue ) {
|
if ( isCompositeValue ) {
|
||||||
if ( isInNonDistinctCount && ! getWalker().getSessionFactoryHelper().getFactory().getDialect().supportsTupleCounts() ) {
|
if ( isInNonDistinctCount && ! dialect.supportsTupleCounts() ) {
|
||||||
|
// TODO: #supportsTupleCounts currently false for all Dialects -- could this be cleaned up?
|
||||||
setText( columnExpressions[0] );
|
setText( columnExpressions[0] );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -184,7 +189,8 @@ public class IdentNode extends FromReferenceNode implements SelectExpression {
|
||||||
// avoid wrapping in parenthesis (explicit tuple treatment) if possible due to varied support for
|
// avoid wrapping in parenthesis (explicit tuple treatment) if possible due to varied support for
|
||||||
// tuple syntax across databases..
|
// tuple syntax across databases..
|
||||||
final boolean shouldSkipWrappingInParenthesis =
|
final boolean shouldSkipWrappingInParenthesis =
|
||||||
getWalker().isInCount()
|
(isInDistinctCount && ! dialect.requiresParensForTupleDistinctCounts())
|
||||||
|
|| isInNonDistinctCount
|
||||||
|| getWalker().getCurrentTopLevelClauseType() == HqlSqlTokenTypes.ORDER
|
|| getWalker().getCurrentTopLevelClauseType() == HqlSqlTokenTypes.ORDER
|
||||||
|| getWalker().getCurrentTopLevelClauseType() == HqlSqlTokenTypes.GROUP;
|
|| getWalker().getCurrentTopLevelClauseType() == HqlSqlTokenTypes.GROUP;
|
||||||
if ( ! shouldSkipWrappingInParenthesis ) {
|
if ( ! shouldSkipWrappingInParenthesis ) {
|
||||||
|
|
|
@ -23,24 +23,25 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.cid;
|
package org.hibernate.test.cid;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import org.hibernate.Hibernate;
|
import org.hibernate.Hibernate;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.Transaction;
|
import org.hibernate.Transaction;
|
||||||
import org.hibernate.engine.query.spi.HQLQueryPlan;
|
import org.hibernate.engine.query.spi.HQLQueryPlan;
|
||||||
|
import org.hibernate.exception.SQLGrammarException;
|
||||||
import org.hibernate.hql.spi.QueryTranslator;
|
import org.hibernate.hql.spi.QueryTranslator;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Gavin King
|
* @author Gavin King
|
||||||
|
@ -93,6 +94,45 @@ public class CompositeIdTest extends BaseCoreFunctionalTestCase {
|
||||||
final String countExpressionFragment = generatedSql.substring( countExpressionListStart+6, countExpressionListEnd+1 );
|
final String countExpressionFragment = generatedSql.substring( countExpressionListStart+6, countExpressionListEnd+1 );
|
||||||
assertTrue( countExpressionFragment.startsWith( "distinct" ) );
|
assertTrue( countExpressionFragment.startsWith( "distinct" ) );
|
||||||
assertTrue( countExpressionFragment.contains( "," ) );
|
assertTrue( countExpressionFragment.contains( "," ) );
|
||||||
|
|
||||||
|
Session s = openSession();
|
||||||
|
s.beginTransaction();
|
||||||
|
Customer c = new Customer();
|
||||||
|
c.setCustomerId( "1" );
|
||||||
|
c.setAddress("123 somewhere");
|
||||||
|
c.setName("Brett");
|
||||||
|
Order o1 = new Order( c );
|
||||||
|
o1.setOrderDate( Calendar.getInstance() );
|
||||||
|
Order o2 = new Order( c );
|
||||||
|
o2.setOrderDate( Calendar.getInstance() );
|
||||||
|
s.persist( c );
|
||||||
|
s.persist( o1 );
|
||||||
|
s.persist( o2 );
|
||||||
|
s.getTransaction().commit();
|
||||||
|
s.clear();
|
||||||
|
|
||||||
|
s.beginTransaction();
|
||||||
|
try {
|
||||||
|
long count = ( Long ) s.createQuery( "select count(distinct o) FROM Order o" ).uniqueResult();
|
||||||
|
if ( ! getDialect().supportsTupleDistinctCounts() ) {
|
||||||
|
fail( "expected SQLGrammarException" );
|
||||||
|
}
|
||||||
|
assertEquals( 2l, count );
|
||||||
|
}
|
||||||
|
catch ( SQLGrammarException e ) {
|
||||||
|
if ( getDialect().supportsTupleDistinctCounts() ) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.getTransaction().commit();
|
||||||
|
s.close();
|
||||||
|
|
||||||
|
s = openSession();
|
||||||
|
s.beginTransaction();
|
||||||
|
s.createQuery("delete from Order").executeUpdate();
|
||||||
|
s.createQuery("delete from Customer").executeUpdate();
|
||||||
|
s.getTransaction().commit();
|
||||||
|
s.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue