Search in sources :

Example 1 with SqmExpressible

use of org.hibernate.query.sqm.SqmExpressible in project hibernate-orm by hibernate.

the class TypeConfiguration method resolveTupleType.

public SqmExpressible<?> resolveTupleType(List<? extends SqmTypedNode<?>> typedNodes) {
    final SqmExpressible<?>[] components = new SqmExpressible<?>[typedNodes.size()];
    for (int i = 0; i < typedNodes.size(); i++) {
        final SqmExpressible<?> sqmExpressible = typedNodes.get(i).getNodeType();
        components[i] = sqmExpressible != null ? sqmExpressible : getBasicTypeForJavaType(Object.class);
    }
    return arrayTuples.computeIfAbsent(new ArrayCacheKey(components), key -> new ArrayTupleType(key.components));
}
Also used : SqmExpressible(org.hibernate.query.sqm.SqmExpressible) ArrayTupleType(org.hibernate.metamodel.model.domain.internal.ArrayTupleType)

Example 2 with SqmExpressible

use of org.hibernate.query.sqm.SqmExpressible in project hibernate-orm by hibernate.

the class SqlTuple method createDomainResult.

@Override
public DomainResult createDomainResult(String resultVariable, DomainResultCreationState creationState) {
    final JavaType javaType = ((SqmExpressible<?>) valueMapping).getExpressibleJavaType();
    final int[] valuesArrayPositions = new int[expressions.size()];
    for (int i = 0; i < expressions.size(); i++) {
        valuesArrayPositions[i] = creationState.getSqlAstCreationState().getSqlExpressionResolver().resolveSqlSelection(expressions.get(i), javaType, creationState.getSqlAstCreationState().getCreationContext().getMappingMetamodel().getTypeConfiguration()).getValuesArrayPosition();
    }
    return new TupleResult(valuesArrayPositions, resultVariable, javaType);
}
Also used : JavaType(org.hibernate.type.descriptor.java.JavaType) SqmExpressible(org.hibernate.query.sqm.SqmExpressible) TupleResult(org.hibernate.sql.results.graph.tuple.TupleResult)

Example 3 with SqmExpressible

use of org.hibernate.query.sqm.SqmExpressible in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method transformDurationArithmetic.

private Object transformDurationArithmetic(SqmBinaryArithmetic<?> expression) {
    BinaryArithmeticOperator operator = expression.getOperator();
    // the expression tree
    switch(operator) {
        case ADD:
        case SUBTRACT:
            // the only legal binary operations involving
            // a duration with a date or timestamp are
            // addition and subtraction with the duration
            // on the right and the date or timestamp on
            // the left, producing a date or timestamp
            // 
            // ts + d or ts - d
            // 
            // the only legal binary operations involving
            // two durations are addition and subtraction,
            // producing a duration
            // 
            // d1 + d2
            // re-express addition of non-leaf duration
            // expressions to a date or timestamp as
            // addition of leaf durations to a date or
            // timestamp
            // ts + x * (d1 + d2) => (ts + x * d1) + x * d2
            // ts - x * (d1 + d2) => (ts - x * d1) - x * d2
            // ts + x * (d1 - d2) => (ts + x * d1) - x * d2
            // ts - x * (d1 - d2) => (ts - x * d1) + x * d2
            Expression timestamp = adjustedTimestamp;
            SqmExpressible<?> timestampType = adjustedTimestampType;
            adjustedTimestamp = toSqlExpression(expression.getLeftHandOperand().accept(this));
            JdbcMappingContainer type = adjustedTimestamp.getExpressionType();
            if (type instanceof SqmExpressible) {
                adjustedTimestampType = (SqmExpressible<?>) type;
            } else if (type instanceof AttributeMapping) {
                adjustedTimestampType = (SqmExpressible<?>) ((AttributeMapping) type).getMappedType();
            } else {
                // else we know it has not been transformed
                adjustedTimestampType = expression.getLeftHandOperand().getNodeType();
            }
            if (operator == SUBTRACT) {
                negativeAdjustment = !negativeAdjustment;
            }
            try {
                return expression.getRightHandOperand().accept(this);
            } finally {
                if (operator == SUBTRACT) {
                    negativeAdjustment = !negativeAdjustment;
                }
                adjustedTimestamp = timestamp;
                adjustedTimestampType = timestampType;
            }
        case MULTIPLY:
            // finally, we can multiply a duration on the
            // right by a scalar value on the left
            // scalar multiplication produces a duration
            // x * d
            // distribute scalar multiplication over the
            // terms, not forgetting the propagated scale
            // x * (d1 + d2) => x * d1 + x * d2
            // x * (d1 - d2) => x * d1 - x * d2
            // -x * (d1 + d2) => - x * d1 - x * d2
            // -x * (d1 - d2) => - x * d1 + x * d2
            Expression duration = toSqlExpression(expression.getLeftHandOperand().accept(this));
            Expression scale = adjustmentScale;
            boolean negate = negativeAdjustment;
            adjustmentScale = applyScale(duration);
            // was sucked into the scale
            negativeAdjustment = false;
            try {
                return expression.getRightHandOperand().accept(this);
            } finally {
                adjustmentScale = scale;
                negativeAdjustment = negate;
            }
        default:
            throw new SemanticException("illegal operator for a duration " + operator);
    }
}
Also used : JdbcMappingContainer(org.hibernate.metamodel.mapping.JdbcMappingContainer) SqmExpressible(org.hibernate.query.sqm.SqmExpressible) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SqmModifiedSubQueryExpression(org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) BinaryArithmeticOperator(org.hibernate.query.sqm.BinaryArithmeticOperator) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) SemanticException(org.hibernate.query.SemanticException)

Example 4 with SqmExpressible

use of org.hibernate.query.sqm.SqmExpressible in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method determineValueMapping.

private MappingModelExpressible<?> determineValueMapping(SqmExpression<?> sqmExpression, FromClauseIndex fromClauseIndex) {
    if (sqmExpression instanceof SqmParameter) {
        return determineValueMapping((SqmParameter<?>) sqmExpression);
    } else if (sqmExpression instanceof SqmPath) {
        log.debugf("Determining mapping-model type for SqmPath : %s ", sqmExpression);
        final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
        return SqmMappingModelHelper.resolveMappingModelExpressible(sqmExpression, domainModel, fromClauseIndex::findTableGroup);
    } else // The model type of an enum literal is always inferred
    if (sqmExpression instanceof SqmEnumLiteral<?>) {
        final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
        if (mappingModelExpressible != null) {
            return mappingModelExpressible;
        }
    } else if (sqmExpression instanceof SqmSubQuery<?>) {
        final SqmSubQuery<?> subQuery = (SqmSubQuery<?>) sqmExpression;
        final SqmSelectClause selectClause = subQuery.getQuerySpec().getSelectClause();
        if (selectClause.getSelections().size() == 1) {
            final SqmSelection<?> subQuerySelection = selectClause.getSelections().get(0);
            final SqmSelectableNode<?> selectableNode = subQuerySelection.getSelectableNode();
            if (selectableNode instanceof SqmExpression<?>) {
                return determineValueMapping((SqmExpression<?>) selectableNode, fromClauseIndex);
            }
            final SqmExpressible<?> selectionNodeType = subQuerySelection.getNodeType();
            if (selectionNodeType != null) {
                final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
                final MappingModelExpressible<?> expressible = domainModel.resolveMappingExpressible(selectionNodeType, this::findTableGroupByPath);
                if (expressible != null) {
                    return expressible;
                }
                try {
                    final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
                    if (mappingModelExpressible != null) {
                        return mappingModelExpressible;
                    }
                } catch (Exception ignore) {
                    return null;
                }
            }
        }
    }
    log.debugf("Determining mapping-model type for generalized SqmExpression : %s", sqmExpression);
    final SqmExpressible<?> nodeType = sqmExpression.getNodeType();
    if (nodeType == null) {
        // We can't determine the type of the expression
        return null;
    }
    final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final MappingModelExpressible<?> valueMapping = domainModel.resolveMappingExpressible(nodeType, fromClauseIndex::getTableGroup);
    if (valueMapping == null) {
        final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
        if (mappingModelExpressible != null) {
            return mappingModelExpressible;
        }
    }
    if (valueMapping == null) {
        // For literals it is totally possible that we can't figure out a mapping type
        if (sqmExpression instanceof SqmLiteral<?>) {
            return null;
        }
        throw new ConversionException("Could not determine ValueMapping for SqmExpression: " + sqmExpression);
    }
    return valueMapping;
}
Also used : SqmSubQuery(org.hibernate.query.sqm.tree.select.SqmSubQuery) SqmSelectableNode(org.hibernate.query.sqm.tree.select.SqmSelectableNode) MappingModelExpressible(org.hibernate.metamodel.mapping.MappingModelExpressible) SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmSelectClause(org.hibernate.query.sqm.tree.select.SqmSelectClause) SelfInterpretingSqmPath(org.hibernate.query.sqm.sql.internal.SelfInterpretingSqmPath) SqmPath(org.hibernate.query.sqm.tree.domain.SqmPath) DiscriminatorSqmPath(org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath) SqmEnumLiteral(org.hibernate.query.sqm.tree.expression.SqmEnumLiteral) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException) SemanticException(org.hibernate.query.SemanticException) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) SqlTreeCreationException(org.hibernate.sql.ast.SqlTreeCreationException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) InterpretationException(org.hibernate.query.sqm.InterpretationException) SQLException(java.sql.SQLException) SqmExpressible(org.hibernate.query.sqm.SqmExpressible) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) SqmParameter(org.hibernate.query.sqm.tree.expression.SqmParameter) SqmLiteral(org.hibernate.query.sqm.tree.expression.SqmLiteral)

Aggregations

SqmExpressible (org.hibernate.query.sqm.SqmExpressible)4 SemanticException (org.hibernate.query.SemanticException)2 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)2 SQLException (java.sql.SQLException)1 HibernateException (org.hibernate.HibernateException)1 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)1 QueryException (org.hibernate.QueryException)1 MultipleBagFetchException (org.hibernate.loader.MultipleBagFetchException)1 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)1 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)1 JdbcMappingContainer (org.hibernate.metamodel.mapping.JdbcMappingContainer)1 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)1 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)1 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)1 ArrayTupleType (org.hibernate.metamodel.model.domain.internal.ArrayTupleType)1 DiscriminatorSqmPath (org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath)1 BinaryArithmeticOperator (org.hibernate.query.sqm.BinaryArithmeticOperator)1 InterpretationException (org.hibernate.query.sqm.InterpretationException)1 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)1 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)1