Package com.foundationdb.server.types

Examples of com.foundationdb.server.types.TInstance


            int nparams = instancesMap.lastDefinedIndex();
            for (int i = 0; i < nparams; i++) {
                if (!instancesMap.isDefined(i)) continue;
                List<ExpressionNode> siblings = instancesMap.get(i);
                TPreptimeValue sharedTpv = siblings.get(0).getPreptimeValue();
                TInstance type = sharedTpv.type();
                DataTypeDescriptor sqlType = null;
                if (type == null) {
                    sqlType = siblings.get(0).getSQLtype();
                    if (sqlType != null)
                        type = typesTranslator.typeForSQLType(sqlType);
                    else
                        type = typesTranslator.typeClassForString().instance(true);
                    sharedTpv.type(type);
                }
                if (sqlType == null)
                    sqlType = type.dataTypeDescriptor();
                for (ExpressionNode param : siblings) {
                    param.setSQLtype(sqlType);
                    if (param.getSQLsource() != null) {
                        try {
                            param.getSQLsource().setType(sqlType);
View Full Code Here


        }
        return result;
    }

    private static ExpressionNode boolExpr(ExpressionNode expression, boolean nullable) {
        TInstance type = AkBool.INSTANCE.instance(nullable);
        ValueSource value = null;
        if (expression.getPreptimeValue() != null) {
            if (type.equals(expression.getPreptimeValue().type()))
                return expression;
            value = expression.getPreptimeValue().value();
        }
        expression.setPreptimeValue(new TPreptimeValue(type, value));
        return expression;
View Full Code Here

            ColumnSource source = columnNode.getTable();
            if (source instanceof ExpressionsSource) {
                ExpressionsSource expressionsTable = (ExpressionsSource) source;
                List<List<ExpressionNode>> rows = expressionsTable.getExpressions();
                int pos = columnNode.getPosition();
                TInstance castType = castNode.getType();
                for (int i = 0, nrows = rows.size(); i < nrows; ++i) {
                    List<ExpressionNode> row = rows.get(i);
                    ExpressionNode targetColumn = row.get(pos);
                    targetColumn = castTo(targetColumn, castType, folder, parametersSync);
                    row.set(pos, targetColumn);
View Full Code Here

        if (field.getAIScolumn() != null) {
            pgType = PostgresType.fromAIS(field.getAIScolumn());
        }
        else if (field.getSQLtype() != null) {
            DataTypeDescriptor sqlType = field.getSQLtype();
            TInstance type = field.getType();
            if (type == null)
                type = getTypesTranslator().typeForSQLType(sqlType);
            pgType = PostgresType.fromDerby(sqlType, type);
        }
        else {
View Full Code Here

            n = folder.foldConstants(n);
            // Set nullability of TInstance if it hasn't been given explicitly
            // At the same time, update the node's DataTypeDescriptor to match its TInstance
            TPreptimeValue tpv = n.getPreptimeValue();
            if (tpv != null) {
                TInstance type = tpv.type();
                if ((n.getSQLtype() != null) &&
                    (n.getSQLtype().getCharacterAttributes() != null) &&
                    (n.getSQLtype().getCharacterAttributes().getCollationDerivation() ==
                        CharacterTypeAttributes.CollationDerivation.EXPLICIT)) {
                    // Apply result of explicit COLLATE, which will otherwise get lost.
                    // No way to mutate the existing instance, so replace entire tpv.
                    type = StringAttribute.copyWithCollation(type, n.getSQLtype().getCharacterAttributes());
                    tpv = new TPreptimeValue(type, tpv.value());
                    n.setPreptimeValue(tpv);
                }
                if (type != null) {
                    DataTypeDescriptor newDtd = type.dataTypeDescriptor();
                    n.setSQLtype(newDtd);
                }
            }
            return n;
        }
View Full Code Here

            // also happen if an ExpressionNode is a constant NULL.
            for (int rownum = 0, expressionsSize = rows.size(); rownum < expressionsSize; rownum++) {
                List<ExpressionNode> row = rows.get(rownum);
                assert row.size() == nfields : "jagged rows: " + node;
                for (int field = 0; field < nfields; ++field) {
                    TInstance botInstance = type(row.get(field));
                    special:
                    if (botInstance == null) {
                        // type is unknown (parameter or literal NULL), so it doesn't participate in determining a
                        // type, but a cast is needed.
                        needCasts.set(field);
                        // If it is a parameter, it needs to be allowed to be wider than
                        // any of the existing values, while still consistent with them.
                        if (row.get(field) instanceof ParameterExpression) {
                            // Force use of commonTClass(existing,null) below to widen.
                            if (!widened.get(field)) {
                                widened.set(field);
                                break special;
                            }
                        }
                        continue;
                    }
                    else if (instances[field] == null) {
                        // Take type from first non-NULL, unless we have to widen,
                        // which commonTClass(null,expr) will take care of.
                        if (widened.get(field)) {
                            break special;
                        }
                        instances[field] = botInstance;
                        continue;
                    }

                    // If the two are the same, we know we don't need to cast them.
                    // This logic also handles the case where both are null, which is not a valid argument
                    // to resolver.commonTClass.
                    if (Objects.equal(instances[field], botInstance))
                        continue;
                   
                    TClass topClass = tclass(instances[field]);
                    TClass botClass = tclass(botInstance);
                    TClass commonTClass = registry.getCastsResolver().commonTClass(topClass, botClass);
                    if (commonTClass == null) {
                        throw new AkibanInternalException("no common type found found between row " + (rownum-1)
                        + " and " + rownum + " at field " + field);
                    }
                    // The two rows have different TClasses at this index, so we'll need at least one of them to
                    // be casted. Also the common class will be the widest comparable.
                    needCasts.set(field);
                    widened.set(field);

                    boolean eitherIsNullable;
                    if (botInstance == null)
                        eitherIsNullable = true;
                    else
                        eitherIsNullable = botInstance.nullability();
                    if ( (!eitherIsNullable) && (instances[field] != null)) {
                        // bottom is not nullable, and there is a top. See if it's nullable
                        eitherIsNullable = instances[field].nullability();
                    }
                   
                    // need to set a new instances[field]. Rules:
                    // - if topClass and botClass are the same as common, use picking algorithm
                    // - else, if one of them == commonTClass, use topInstance or botInstance (whichever is == common)
                    // - else, use commonTClass.instance()
                    boolean topIsCommon = (topClass == commonTClass);
                    boolean botIsCommon = (botClass == commonTClass);
                    if (topIsCommon && botIsCommon) {
                        // TODO: The special case here for TClass VARCHAR with mismatched charsets
                        // is a limitation of the TClass#pickInstance, as there is no current way
                        // to create a common TInstance for TString with difference charsets.
                        if (commonTClass instanceof TString &&
                            botInstance.attribute(StringAttribute.CHARSET) != instances[field].attribute(StringAttribute.CHARSET)) {
                            ;
                        }
                        else {   
                            instances[field] = topClass.pickInstance(instances[field], botInstance);
                        }
View Full Code Here

            node.setTInstances(instances);
        }

        ExpressionNode handleCastExpression(CastExpression expression) {
            DataTypeDescriptor dtd = expression.getSQLtype();
            TInstance type = typesTranslator.typeForSQLType(dtd);
            expression.setPreptimeValue(new TPreptimeValue(type));
            if (expression.getOperand() instanceof ParameterExpression) {
                parametersSync.set(expression.getOperand(), type);
            }
            return finishCast(expression, folder, parametersSync);
View Full Code Here

            OverloadResult<V> resolutionResult = resolver.get(expression.getFunction(), operandClasses);

            // cast operands
            for (int i = 0, operandsSize = operands.size(); i < operandsSize; i++) {
                TInstance targetType = resolutionResult.getTypeClass(i);
                if (targetType != null) {
                    ExpressionNode operand = castTo(operands.get(i), targetType, folder, parametersSync);
                    operands.set(i, operand);
                }
            }

            V overload = resolutionResult.getOverload();
            expression.setResolved(overload);

            final List<TPreptimeValue> operandValues = new ArrayList<>(operands.size());
            List<TInstance> operandInstances = new ArrayList<>(operands.size());
            boolean anyOperandsNullable = false;
            for (ExpressionNode operand : operands) {
                TPreptimeValue preptimeValue = operand.getPreptimeValue();
                operandValues.add(preptimeValue);
                operandInstances.add(preptimeValue.type());
                if (Boolean.TRUE.equals(preptimeValue.isNullable()))
                    anyOperandsNullable = true;
            }

            TOverloadResult overloadResultStrategy = overload.resultStrategy();
            TInstance resultInstance;
            TInstance castTo;

            TPreptimeContext context;
            if (createPreptimeContext) {
                context = new TPreptimeContext(operandInstances, queryContext);
                expression.setPreptimeContext(context);
            }
            else {
                context = null;
            }
            switch (overloadResultStrategy.category()) {
            case CUSTOM:
                TInstance castSource = overloadResultStrategy.customRuleCastSource(anyOperandsNullable);
                if (context == null)
                    context = new TPreptimeContext(operandInstances, queryContext);
                expression.setPreptimeContext(context);
                if (castSource == null) {
                    castTo = null;
View Full Code Here

                    boolean conditionMet = conditionVal.getBoolean(false);
                    return conditionMet ? thenExpr : elseExpr;
                }
            }

            TInstance commonInstance = commonInstance(registry.getCastsResolver(), type(thenExpr), type(elseExpr));
            if (commonInstance == null)
                return ConstantExpression.typedNull(null, null, null);

            thenExpr = castTo(thenExpr, commonInstance, folder, parametersSync);
            elseExpr = castTo(elseExpr, commonInstance, folder, parametersSync);
View Full Code Here

        }

        ExpressionNode handleComparisonCondition(ComparisonCondition expression) {
            ExpressionNode left = expression.getLeft();
            ExpressionNode right = expression.getRight();
            TInstance leftTInst = type(left);
            TInstance rightTInst = type(right);
            boolean nullable = isNullable(left) || isNullable(right);
            TKeyComparable keyComparable = registry.getKeyComparable(tclass(leftTInst), tclass(rightTInst));
            if (keyComparable != null) {
                expression.setKeyComparable(keyComparable);
            }
            else if (TClass.comparisonNeedsCasting(leftTInst, rightTInst)) {
                boolean needCasts = true;
                TCastResolver casts = registry.getCastsResolver();
                if ( (left.getClass() == ColumnExpression.class)&& (right.getClass() == ConstantExpression.class)) {
                    // Left is a Column, right is a Constant. Ideally, we'd like to keep the Column as a Column,
                    // and not a CAST(Column AS _) -- otherwise, we can't use it in an index lookup.
                    // So, try to cast the const to the column's type. To do this, CAST(Const -> Column) must be
                    // indexFriendly, *and* casting this result back to the original Const type must equal the same
                    // const.
                    if (rightTInst == null) {
                        // literal null, so a comparison always returns UNKNOWN
                        return new BooleanConstantExpression(null);
                    }
                    if (casts.isIndexFriendly(tclass(leftTInst), tclass(rightTInst))) {
                        TInstance columnType = type(left);
                        TInstance constType = type(right);
                        TCast constToCol = casts.cast(constType, columnType);
                        if (constToCol != null) {
                            TCast colToConst = casts.cast(columnType, constType);
                            if (colToConst != null) {
                                TPreptimeValue constValue = right.getPreptimeValue();
                                ValueSource asColType = castValue(constToCol, constValue, columnType);
                                TPreptimeValue asColTypeTpv = (asColType == null)
                                        ? null
                                        : new TPreptimeValue(columnType, asColType);
                                ValueSource backToConstType = castValue(colToConst, asColTypeTpv, constType);
                                if (ValueSources.areEqual(constValue.value(), backToConstType)) {
                                    TPreptimeValue constTpv = new TPreptimeValue(columnType, asColType);
                                    ConstantExpression constCasted = new ConstantExpression(constTpv);
                                    expression.setRight(constCasted);
                                    assert columnType.equals(type(expression.getRight()));
                                    needCasts = false;
                                }
                            }
                        }
                    }
                }
                if (needCasts) {
                    TInstance common = commonInstance(casts, left, right);
                    if (common == null) {
                        // TODO this means we have something like '? = ?' or '? = NULL'. What to do? Varchar for now?
                        common = typesTranslator.typeForString();
                    }
                    left = castTo(left, common, folder, parametersSync);
View Full Code Here

TOP

Related Classes of com.foundationdb.server.types.TInstance

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.