Search in sources :

Example 21 with DataTypeDescriptor

use of org.apache.derby.iapi.types.DataTypeDescriptor in project derby by apache.

the class ParameterNode method generateExpression.

// //////////////////////////////////////////////////////////////////
// 
// CODE GENERATOR
// 
// //////////////////////////////////////////////////////////////////
/**
 * For a ParameterNode, we generate for the return value:
 *
 *		(<java type name>)
 *			( (BaseActivation) this.getParameter(parameterNumber) )
 *
 * @param acb	The ExpressionClassBuilder for the class being built
 * @param mb	The method the expression will go into
 *
 * @exception StandardException		Thrown on error
 */
@Override
void generateExpression(ExpressionClassBuilder acb, MethodBuilder mb) throws StandardException {
    /* If we were given a specific ValueNode to generate then
		 * just use that.  See, in particular, the preprocess method
		 * of InListOperatorNode.
		 */
    if (valToGenerate != null) {
        valToGenerate.generateExpression(acb, mb);
        return;
    }
    DataTypeDescriptor dtd = getTypeServices();
    if ((dtd != null) && dtd.getTypeId().isXMLTypeId()) {
        // "setType" do we figure out the type).
        throw StandardException.newException(SQLState.LANG_ATTEMPT_TO_BIND_XML);
    }
    /* Generate the return value */
    mb.pushThis();
    // arg
    mb.push(parameterNumber);
    mb.callMethod(VMOpcode.INVOKEVIRTUAL, ClassName.BaseActivation, "getParameter", ClassName.DataValueDescriptor, 1);
    switch(dtd.getJDBCTypeId()) {
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
        case Types.BLOB:
            mb.dup();
            mb.push(dtd.getMaximumWidth());
            mb.callMethod(VMOpcode.INVOKEINTERFACE, (String) null, "checkHostVariable", "void", 1);
            break;
        default:
            break;
    }
    /* Cast the result to its specific interface */
    mb.cast(getTypeCompiler().interfaceName());
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 22 with DataTypeDescriptor

use of org.apache.derby.iapi.types.DataTypeDescriptor in project derby by apache.

the class JoinNode method deferredBindExpressions.

private void deferredBindExpressions(FromList fromListParam) throws StandardException {
    ContextManager cm = getContextManager();
    CompilerContext cc = getCompilerContext();
    /* Bind the expressions in the join clause */
    subqueryList = new SubqueryList(cm);
    aggregates = new ArrayList<AggregateNode>();
    /* ON clause */
    if (joinClause != null) {
        joinClause = bindExpression(joinClause, true, true, "ON");
    } else /* USING clause */
    if (usingClause != null) {
        /* Build a join clause from the usingClause, using the
			 * exposed names in the left and right RSNs.
			 * For each column in the list, we generate 2 ColumnReferences,
			 * 1 for the left and 1 for the right.  We bind each of these
			 * to the appropriate side and build an equality predicate
			 * between the 2.  We bind the = and AND nodes by hand because
			 * we have to bind the ColumnReferences a side at a time.
			 * We need to bind the CRs a side at a time to ensure that
			 * we don't find an bogus ambiguous column reference. (Bug 377)
			 */
        joinClause = new BooleanConstantNode(true, cm);
        for (ResultColumn rc : usingClause) {
            BinaryComparisonOperatorNode equalsNode;
            ColumnReference leftCR;
            ColumnReference rightCR;
            /* Create and bind the left CR */
            fromListParam.insertElementAt(leftResultSet, 0);
            leftCR = new ColumnReference(rc.getName(), ((FromTable) leftResultSet).getTableName(), cm);
            leftCR = (ColumnReference) leftCR.bindExpression(fromListParam, subqueryList, aggregates);
            fromListParam.removeElementAt(0);
            /* Create and bind the right CR */
            fromListParam.insertElementAt(rightResultSet, 0);
            rightCR = new ColumnReference(rc.getName(), ((FromTable) rightResultSet).getTableName(), cm);
            rightCR = (ColumnReference) rightCR.bindExpression(fromListParam, subqueryList, aggregates);
            fromListParam.removeElementAt(0);
            /* Create and insert the new = condition */
            equalsNode = new BinaryRelationalOperatorNode(BinaryRelationalOperatorNode.K_EQUALS, leftCR, rightCR, false, cm);
            equalsNode.bindComparisonOperator();
            // Create a new join clause by ANDing the new = condition and
            // the old join clause.
            AndNode newJoinClause = new AndNode(equalsNode, joinClause, cm);
            newJoinClause.postBindFixup();
            joinClause = newJoinClause;
        }
    }
    if (joinClause != null) {
        /* If joinClause is a parameter, (where ?), then we assume
			 * it will be a nullable boolean.
			 */
        if (joinClause.requiresTypeFromContext()) {
            joinClause.setType(new DataTypeDescriptor(TypeId.BOOLEAN_ID, true));
        }
        /*
			** Is the datatype of the JOIN clause BOOLEAN?
			**
			** NOTE: This test is not necessary in SQL92 entry level, because
			** it is syntactically impossible to have a non-Boolean JOIN clause
			** in that level of the standard.  But we intend to extend the
			** language to allow Boolean user functions in the JOIN clause,
			** so we need to test for the error condition.
			*/
        TypeId joinTypeId = joinClause.getTypeId();
        /* If the where clause is not a built-in type, then generate a bound 
			 * conversion tree to a built-in type.
			 */
        if (joinTypeId.userType()) {
            joinClause = joinClause.genSQLJavaSQLTree();
        }
        if (!joinClause.getTypeServices().getTypeId().equals(TypeId.BOOLEAN_ID)) {
            throw StandardException.newException(SQLState.LANG_NON_BOOLEAN_JOIN_CLAUSE, joinClause.getTypeServices().getTypeId().getSQLTypeName());
        }
    }
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) ContextManager(org.apache.derby.iapi.services.context.ContextManager)

Example 23 with DataTypeDescriptor

use of org.apache.derby.iapi.types.DataTypeDescriptor in project derby by apache.

the class MaxMinAggregateDefinition method getAggregator.

/**
 * Determines the result datatype.  Accept NumberDataValues
 * only.
 * <P>
 * <I>Note</I>: In the future you should be able to do
 * a sum user data types.  One option would be to run
 * sum on anything that implements divide().
 *
 * @param inputType	the input type, either a user type or a java.lang object
 *
 * @return the output Class (null if cannot operate on
 *	value expression of this type.
 */
public final DataTypeDescriptor getAggregator(DataTypeDescriptor inputType, StringBuffer aggregatorClass) {
    LanguageConnectionContext lcc = (LanguageConnectionContext) QueryTreeNode.getContext(LanguageConnectionContext.CONTEXT_ID);
    /*
			** MIN and MAX may return null
			*/
    DataTypeDescriptor dts = inputType.getNullabilityType(true);
    TypeId compType = dts.getTypeId();
    /*
		** If the class implements NumberDataValue, then we
		** are in business.  Return type is same as input
		** type.
		*/
    if (compType.orderable(lcc.getLanguageConnectionFactory().getClassFactory())) {
        aggregatorClass.append(ClassName.MaxMinAggregator);
        return dts;
    }
    return null;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 24 with DataTypeDescriptor

use of org.apache.derby.iapi.types.DataTypeDescriptor in project derby by apache.

the class ResultColumnList method setUnionResultExpression.

/**
 * Set up the result expressions for a UNION, INTERSECT, or EXCEPT:
 *	o Verify union type compatiblity
 *	o Get dominant type for result (type + max length + nullability)
 *  o Create a new ColumnReference with dominant type and name of from this
 *    RCL and make that the new expression.
 *  o Set the type info for in the ResultColumn to the dominant type
 *
 * NOTE - We are assuming that caller has generated a new RCL for the UNION
 * with the same names as the left side's RCL and copies of the expressions.
 *
 * @param otherRCL	RCL from other side of the UNION.
 * @param tableNumber	The tableNumber for the UNION.
 * @param level		The nesting level for the UNION.
 * @param operatorName "UNION", "INTERSECT", or "EXCEPT"
 *
 * @exception StandardException			Thrown on error
 */
void setUnionResultExpression(ResultColumnList otherRCL, int tableNumber, int level, String operatorName) throws StandardException {
    TableName dummyTN;
    if (SanityManager.DEBUG) {
        if (visibleSize() != otherRCL.visibleSize()) {
            SanityManager.THROWASSERT("visibleSize() = (" + visibleSize() + ") is expected to equal otherRCL.visibleSize (" + otherRCL.visibleSize() + ")");
        }
        // Generated grouping columns and unselected ORDER BY columns
        // should have been removed for the RCL of a SetOperatorNode, so
        // that size and visible size are equal (DERBY-3764).
        SanityManager.ASSERT(size() == visibleSize(), "size() and visibleSize() should be equal");
    }
    /* Make a dummy TableName to be shared by all new CRs */
    dummyTN = new TableName(null, null, getContextManager());
    int size = visibleSize();
    for (int index = 0; index < size; index++) {
        ColumnReference newCR;
        ResultColumn thisRC = elementAt(index);
        ResultColumn otherRC = otherRCL.elementAt(index);
        ValueNode thisExpr = thisRC.getExpression();
        ValueNode otherExpr = otherRC.getExpression();
        // not be 'autoincrement'.
        if (!otherRC.isAutoincrementGenerated() && thisRC.isAutoincrementGenerated()) {
            thisRC.resetAutoincrementGenerated();
        }
        /*
			** If there are ? parameters in the ResultColumnList of a row
			** in a table constructor, their types will not be set.  Just skip
			** these - their types will be set later.  Each ? parameter will
			** get the type of the first non-? in its column, so it can't
			** affect the final dominant type.  It's possible that all the
			** rows for a particular column will have ? parameters - this is
			** an error condition that will be caught later.
			*/
        TypeId thisTypeId = thisExpr.getTypeId();
        if (thisTypeId == null)
            continue;
        TypeId otherTypeId = otherExpr.getTypeId();
        if (otherTypeId == null)
            continue;
        /* 
			** Check type compatability.
			*/
        ClassFactory cf = getClassFactory();
        if (!unionCompatible(thisExpr, otherExpr)) {
            throw StandardException.newException(SQLState.LANG_NOT_UNION_COMPATIBLE, thisTypeId.getSQLTypeName(), otherTypeId.getSQLTypeName(), operatorName);
        }
        DataTypeDescriptor resultType = thisExpr.getTypeServices().getDominantType(otherExpr.getTypeServices(), cf);
        newCR = new ColumnReference(thisRC.getName(), dummyTN, getContextManager());
        newCR.setType(resultType);
        /* Set the tableNumber and nesting levels in newCR.
			 * If thisExpr is not a CR, then newCR cannot be
			 * correlated, hence source and nesting levels are
			 * the same.
			 */
        if (thisExpr instanceof ColumnReference) {
            newCR.copyFields((ColumnReference) thisExpr);
        } else {
            newCR.setNestingLevel(level);
            newCR.setSourceLevel(level);
        }
        newCR.setTableNumber(tableNumber);
        thisRC.setExpression(newCR);
        thisRC.setType(thisRC.getTypeServices().getDominantType(otherRC.getTypeServices(), cf));
        /* DB2 requires both sides of union to have same name for the result to
			 * have that name. Otherwise, leave it or set it to a generated name */
        if (thisRC.getName() != null && !thisRC.isNameGenerated() && otherRC.getName() != null) {
            /* Result name needs to be changed */
            if (otherRC.isNameGenerated()) {
                thisRC.setName(otherRC.getName());
                thisRC.setNameGenerated(true);
            } else if (!thisRC.getName().equals(otherRC.getName())) {
                /* Both sides have user specified names that don't match */
                thisRC.setName(null);
                thisRC.guaranteeColumnName();
                thisRC.setNameGenerated(true);
            }
        }
    }
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 25 with DataTypeDescriptor

use of org.apache.derby.iapi.types.DataTypeDescriptor in project derby by apache.

the class FromTable method getPerRowUsage.

private double getPerRowUsage() throws StandardException {
    if (perRowUsage < 0) {
        // Do not use getRefCols() because the cached refCols may no longer be valid.
        FormatableBitSet refCols = getResultColumns().getReferencedFormatableBitSet(cursorTargetTable(), true, false);
        perRowUsage = 0.0;
        /* Add up the memory usage for each referenced column */
        for (int i = 0; i < refCols.size(); i++) {
            if (refCols.isSet(i)) {
                ResultColumn rc = getResultColumns().elementAt(i);
                DataTypeDescriptor expressionType = rc.getExpression().getTypeServices();
                if (expressionType != null)
                    perRowUsage += expressionType.estimatedMemoryUsage();
            }
        }
        /*
            ** If the proposed conglomerate is a non-covering index, add the 
            ** size of the RowLocation column to the total.
            **
            ** NOTE: We don't have a DataTypeDescriptor representing a
            ** REF column here, so just add a constant here.
            */
        ConglomerateDescriptor cd = getCurrentAccessPath().getConglomerateDescriptor();
        if (cd != null) {
            if (cd.isIndex() && (!isCoveringIndex(cd))) {
                perRowUsage += 12.0;
            }
        }
    }
    return perRowUsage;
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet)

Aggregations

DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)99 TypeId (org.apache.derby.iapi.types.TypeId)32 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)14 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)14 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)9 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)8 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)8 TypeDescriptor (org.apache.derby.catalog.TypeDescriptor)7 UUID (org.apache.derby.catalog.UUID)5 ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)5 ResultColumnDescriptor (org.apache.derby.iapi.sql.ResultColumnDescriptor)5 TypeCompiler (org.apache.derby.iapi.sql.compile.TypeCompiler)5 StandardException (org.apache.derby.shared.common.error.StandardException)5 Properties (java.util.Properties)4 UserDefinedTypeIdImpl (org.apache.derby.catalog.types.UserDefinedTypeIdImpl)4 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)4 AliasDescriptor (org.apache.derby.iapi.sql.dictionary.AliasDescriptor)3 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)3 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)3 ExecIndexRow (org.apache.derby.iapi.sql.execute.ExecIndexRow)3