Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.function.AvgWithArgumentCastFunction;
import org.hibernate.dialect.function.NoArgSQLFunction;
import org.hibernate.dialect.function.RoundFunction;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.dialect.function.VarArgsSQLFunction;
import org.hibernate.dialect.hint.IndexQueryHintHandler;
Expand Down Expand Up @@ -163,7 +164,7 @@ public H2Dialect() {
registerFunction( "power", new StandardSQLFunction( "power", StandardBasicTypes.DOUBLE ) );
registerFunction( "radians", new StandardSQLFunction( "radians", StandardBasicTypes.DOUBLE ) );
registerFunction( "rand", new NoArgSQLFunction( "rand", StandardBasicTypes.DOUBLE ) );
registerFunction( "round", new StandardSQLFunction( "round", StandardBasicTypes.DOUBLE ) );
registerFunction( "round", new RoundFunction( "round" ) );
registerFunction( "roundmagic", new StandardSQLFunction( "roundmagic", StandardBasicTypes.DOUBLE ) );
registerFunction( "sign", new StandardSQLFunction( "sign", StandardBasicTypes.INTEGER ) );
registerFunction( "sin", new StandardSQLFunction( "sin", StandardBasicTypes.DOUBLE ) );
Expand Down Expand Up @@ -425,12 +426,12 @@ public boolean doesReadCommittedCauseWritersToBlockReaders() {
// see http://groups.google.com/group/h2-database/browse_thread/thread/562d8a49e2dabe99?hl=en
return true;
}

@Override
public boolean supportsTuplesInSubqueries() {
return false;
}

@Override
public boolean dropConstraints() {
// We don't need to drop constraints before dropping tables, that just leads to error
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/

package org.hibernate.dialect.function;

import org.hibernate.engine.spi.Mapping;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;

/**
* When there is need to use round function with other function (i.e.sum()),
* its return type becomes Double by default if it is registered as a StandardSQLFunction.
* This function provides the feasibility to return BigDecimal as return type if argument is
* BigDecimal
*
* @author Mayur Bhindi
*/
public class RoundFunction extends StandardSQLFunction {

/**
* Constructs RoundFunction
*
* @param name
*/
public RoundFunction(String name) {
super( name );
}

@Override
public Type getReturnType(Type firstArgumentType, Mapping mapping) {
return firstArgumentType == StandardBasicTypes.BIG_DECIMAL ? firstArgumentType : StandardBasicTypes.DOUBLE;
}

}
Copy link
Contributor

@NathanQingyangXu NathanQingyangXu May 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a general rule, PR without testing case has slim chance to be approved (with the exception of doc PR), let alone merged.
Please consider creating a compelling testing case(s) to prove your code changes. It would help both reviewer and reviewee at the same time.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @NathanQingyangXu I have added unit tests for same.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.hibernate.dialect;

import java.util.Map;

import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.type.StandardBasicTypes;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Unit test of the behavior of the H2Dialect
*
* @author Mayur Bhindi
*/
public class H2DialectTest {

private H2Dialect dialect;

@Before
public void setup() {
dialect = new H2Dialect();
}

@After
public void tearDown() {
dialect = null;
}

@Test
@TestForIssue(jiraKey = "HHH-15228")
public void testRoundFunction() {
Map<String, SQLFunction> functions = dialect.getFunctions();
SQLFunction sqlFunction = functions.get( "round" );
assertEquals( StandardBasicTypes.DOUBLE, sqlFunction.getReturnType( null, null ) );
assertEquals( StandardBasicTypes.DOUBLE, sqlFunction.getReturnType( StandardBasicTypes.INTEGER, null ) );
assertEquals(
StandardBasicTypes.BIG_DECIMAL,
sqlFunction.getReturnType( StandardBasicTypes.BIG_DECIMAL, null )
);

}
}