Skip to content

Commit 81d0c36

Browse files
committed
emulate var_pop and var_samp on Informix
1 parent 2ddf360 commit 81d0c36

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/InformixDialect.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
340340
functionFactory.stddev();
341341
functionFactory.variance();
342342
functionFactory.bitLength_pattern( "length(?1)*8" );
343+
functionFactory.varPop_sumCount();
344+
functionContributions.getFunctionRegistry()
345+
.registerAlternateKey( "var_samp", "variance" );
343346

344347
if ( getVersion().isSameOrAfter( 12 ) ) {
345348
functionFactory.locate_charindex();

hibernate-core/src/main/java/org/hibernate/dialect/function/CommonFunctionFactory.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ public void stddev() {
421421
/**
422422
* Warning: the semantics of this function are inconsistent between DBs.
423423
* <ul>
424-
* <li>On Postgres it means {@code var_samp()}
425-
* <li>On Oracle, DB2, MySQL it means {@code var_pop()}
424+
* <li>On Postgres and Informix it means {@code var_samp()} (the unbiased estimator)
425+
* <li>On Oracle, DB2, MySQL it means {@code var_pop()} (the MLE)
426426
* </ul>
427427
*/
428428
public void variance() {
@@ -505,8 +505,17 @@ public void varianceSamp() {
505505
.register();
506506
}
507507

508+
private static final String VAR_POP_SUM_COUNT_PATTERN = "(sum(power(?1,2))-(power(sum(?1),2)/count(?1)))/nullif(count(?1),0)";
508509
private static final String VAR_SAMP_SUM_COUNT_PATTERN = "(sum(power(?1,2))-(power(sum(?1),2)/count(?1)))/nullif(count(?1)-1,0)";
509510

511+
public void varPop_sumCount() {
512+
functionRegistry.patternAggregateDescriptorBuilder( "var_pop", VAR_POP_SUM_COUNT_PATTERN )
513+
.setInvariantType( doubleType )
514+
.setExactArgumentCount( 1 )
515+
.setParameterTypes( NUMERIC )
516+
.register();
517+
}
518+
510519
/**
511520
* DB2 before 11
512521
*/

0 commit comments

Comments
 (0)