Fix literal related test issues

This commit is contained in:
Christian Beikov 2023-08-17 12:15:58 +02:00
parent 0f3548d586
commit 09abefefc1
8 changed files with 52 additions and 19 deletions

View File

@ -45,7 +45,6 @@ import org.hibernate.query.TypedParameterValue;
import org.hibernate.query.criteria.JpaExpression;
import org.hibernate.query.internal.QueryOptionsImpl;
import org.hibernate.query.sqm.SqmExpressible;
import org.hibernate.query.sqm.tree.expression.SqmHqlNumericLiteral;
import org.hibernate.query.sqm.tree.expression.SqmLiteral;
import org.hibernate.query.sqm.tree.expression.SqmParameter;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
@ -133,10 +132,6 @@ public abstract class AbstractCommonQueryContract implements CommonQueryContract
if ( expression instanceof SqmLiteral<?> ) {
fetchValue = ( (SqmLiteral<Number>) expression ).getLiteralValue();
}
else if ( expression instanceof SqmHqlNumericLiteral ) {
final SqmHqlNumericLiteral<Number> hqlNumericLiteral = (SqmHqlNumericLiteral<Number>) expression;
fetchValue = hqlNumericLiteral.getTypeCategory().parseLiteralValue( hqlNumericLiteral.getLiteralValue() );
}
else if ( expression instanceof SqmParameter<?> ) {
fetchValue = getParameterValue( (Parameter<Number>) expression );
if ( fetchValue == null ) {

View File

@ -5525,7 +5525,11 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
return handleConvertedUnparsedNumericLiteral( numericLiteral, expressible );
}
return new UnparsedNumericLiteral<>( numericLiteral.getLiteralValue(), jdbcMapping );
return new UnparsedNumericLiteral<>(
numericLiteral.getUnparsedLiteralValue(),
numericLiteral.getTypeCategory(),
jdbcMapping
);
}
private <N extends Number> Expression handleConvertedUnparsedNumericLiteral(
@ -5535,7 +5539,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
final BasicValueConverter valueConverter = expressible.getJdbcMapping().getValueConverter();
assert valueConverter != null;
final Number parsedValue = numericLiteral.getTypeCategory().parseLiteralValue( numericLiteral.getLiteralValue() );
final Number parsedValue = numericLiteral.getTypeCategory().parseLiteralValue( numericLiteral.getUnparsedLiteralValue() );
final Object sqlLiteralValue;
if ( valueConverter.getDomainJavaType().isInstance( parsedValue ) ) {
//noinspection unchecked
@ -6663,7 +6667,9 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
//TODO: do we need to temporarily set appliedByUnit
// to null before we recurse down the tree?
// and what about scale?
inferrableTypeAccessStack.push( () -> null );
Expression magnitude = toSqlExpression( toDuration.getMagnitude().accept( this ) );
inferrableTypeAccessStack.pop();
DurationUnit unit = (DurationUnit) toDuration.getUnit().accept( this );
// let's start by applying the propagated scale

View File

@ -31,7 +31,7 @@ import org.hibernate.type.descriptor.java.JavaType;
*
* @author Steve Ebersole
*/
public class SqmHqlNumericLiteral<N extends Number> extends AbstractSqmExpression<N> {
public class SqmHqlNumericLiteral<N extends Number> extends SqmLiteral<N> {
private final String literalValue;
private final NumericTypeCategory typeCategory;
@ -52,10 +52,15 @@ public class SqmHqlNumericLiteral<N extends Number> extends AbstractSqmExpressio
this.typeCategory = typeCategory;
}
public String getLiteralValue() {
public String getUnparsedLiteralValue() {
return literalValue;
}
@Override
public N getLiteralValue() {
return typeCategory.parseLiteralValue( literalValue );
}
public NumericTypeCategory getTypeCategory() {
return typeCategory;
}

View File

@ -67,12 +67,12 @@ public class SqmLiteral<T> extends AbstractSqmExpression<T> {
@Override
public String asLoggableText() {
return "Literal( " + value + ")";
return "Literal( " + getLiteralValue() + ")";
}
@Override
public void appendHqlString(StringBuilder sb) {
appendHqlString( sb, getJavaTypeDescriptor(), value );
appendHqlString( sb, getJavaTypeDescriptor(), getLiteralValue() );
}
public static <T> void appendHqlString(StringBuilder sb, JavaType<T> javaType, T value) {

View File

@ -6678,7 +6678,7 @@ public abstract class AbstractSqlAstTranslator<T extends JdbcOperation> implemen
@Override
public <N extends Number> void visitUnparsedNumericLiteral(UnparsedNumericLiteral<N> literal) {
appendSql( literal.getLiteralValue() );
appendSql( literal.getUnparsedLiteralValue() );
}
private void visitLiteral(Literal literal) {

View File

@ -6,12 +6,18 @@
*/
package org.hibernate.sql.ast.tree.expression;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.JdbcMappingContainer;
import org.hibernate.query.sqm.sql.internal.DomainResultProducer;
import org.hibernate.query.sqm.tree.expression.NumericTypeCategory;
import org.hibernate.sql.ast.SqlAstWalker;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.exec.spi.ExecutionContext;
import org.hibernate.sql.exec.spi.JdbcParameterBindings;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.basic.BasicResult;
@ -24,16 +30,38 @@ import org.hibernate.type.spi.TypeConfiguration;
*
* @author Steve Ebersole
*/
public class UnparsedNumericLiteral<N extends Number> implements Expression, DomainResultProducer<N> {
public class UnparsedNumericLiteral<N extends Number> implements Literal, DomainResultProducer<N> {
private final String literalValue;
private final NumericTypeCategory typeCategory;
private final JdbcMapping jdbcMapping;
public UnparsedNumericLiteral(String literalValue, JdbcMapping jdbcMapping) {
public UnparsedNumericLiteral(String literalValue, NumericTypeCategory typeCategory, JdbcMapping jdbcMapping) {
this.literalValue = literalValue;
this.typeCategory = typeCategory;
this.jdbcMapping = jdbcMapping;
}
public String getLiteralValue() {
@Override
public N getLiteralValue() {
return typeCategory.parseLiteralValue( literalValue );
}
@Override
public void bindParameterValue(
PreparedStatement statement,
int startPosition,
JdbcParameterBindings jdbcParameterBindings,
ExecutionContext executionContext) throws SQLException {
//noinspection unchecked
jdbcMapping.getJdbcValueBinder().bind(
statement,
getLiteralValue(),
startPosition,
executionContext.getSession()
);
}
public String getUnparsedLiteralValue() {
return literalValue;
}

View File

@ -12,7 +12,6 @@ import org.hibernate.query.sqm.ComparisonOperator;
import org.hibernate.query.sqm.tree.domain.SqmPath;
import org.hibernate.query.sqm.tree.expression.SqmCollectionSize;
import org.hibernate.query.sqm.tree.expression.SqmHqlNumericLiteral;
import org.hibernate.query.sqm.tree.expression.SqmLiteral;
import org.hibernate.query.sqm.tree.predicate.SqmNullnessPredicate;
import org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate;
import org.hibernate.query.sqm.tree.predicate.SqmPredicate;
@ -82,7 +81,7 @@ public class WhereClauseTests extends BaseSqmUnitTest {
assertThat( relationalPredicate.getSqmOperator(), is( ComparisonOperator.EQUAL ) );
assertThat( relationalPredicate.getRightHandExpression(), instanceOf( SqmHqlNumericLiteral.class ) );
assertThat( ( (SqmHqlNumericLiteral<?>) relationalPredicate.getRightHandExpression() ).getLiteralValue(), is( "311" ) );
assertThat( ( (SqmHqlNumericLiteral<?>) relationalPredicate.getRightHandExpression() ).getUnparsedLiteralValue(), is( "311" ) );
assertThat( relationalPredicate.getLeftHandExpression(), instanceOf( SqmCollectionSize.class ) );
@ -102,7 +101,7 @@ public class WhereClauseTests extends BaseSqmUnitTest {
assertThat( relationalPredicate.getSqmOperator(), is( ComparisonOperator.GREATER_THAN ) );
assertThat( relationalPredicate.getRightHandExpression(), instanceOf( SqmHqlNumericLiteral.class ) );
assertThat( ( (SqmHqlNumericLiteral<?>) relationalPredicate.getRightHandExpression() ).getLiteralValue(), is( "2" ) );
assertThat( ( (SqmHqlNumericLiteral<?>) relationalPredicate.getRightHandExpression() ).getUnparsedLiteralValue(), is( "2" ) );
assertThat( relationalPredicate.getLeftHandExpression(), instanceOf( SqmPath.class ) );
final SqmPath<?> indexPath = (SqmPath<?>) relationalPredicate.getLeftHandExpression();

View File

@ -61,7 +61,7 @@ public class SubqueryTest extends BaseSessionFactoryFunctionalTest {
public void render(
SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, SqlAstTranslator<?> walker) {
sqlAstArguments.get( 0 ).accept( walker );
sqlAppender.appendSql( " limit " + ( (UnparsedNumericLiteral<?>) sqlAstArguments.get( 1 ) ).getLiteralValue() );
sqlAppender.appendSql( " limit " + ( (UnparsedNumericLiteral<?>) sqlAstArguments.get( 1 ) ).getUnparsedLiteralValue() );
}
}