Search in sources :

Example 21 with ContextManager

use of org.apache.derby.iapi.services.context.ContextManager in project derby by apache.

the class SubqueryNode method pushNewPredicate.

/**
 * Transform:
 *      expression QuantifiedOperator (select x from ...)
 * into
 *		(select true from .. where expression <BinaryComparisonOperator> x ...)
 *		IS [NOT] NULL
 *
 * or, if we have an aggregate:
 *		(select true from
 *			(select AGG(x) from ...)
 *		where expression <BinaryComparisonOperator> x ...)
 *		IS [NOT] NULL
 *
 * For ANY and IN subqueries:
 *		o  We generate an IS NULL above the SubqueryNode and return the top of
 *		   the new tree to the caller.
 *		o  The operator in the new predicate that is added to the subquery
 *		   will correspond to the operator that modifies the ANY.
 *		   (eg, = for = ANY, with = for IN.)
 * For ALL and NOT IN subqueries:
 *		o  We generate an IS NOT NULL above the SubqueryNode and return the top of
 *		   the new tree to the caller.
 *		o  The operator in the new predicate that is added to the subquery
 *		   will be a BinaryAllOperatorNode whose bcoNodeType corresponds to
 *		   the negation of the operator that modifies the ALL.
 *		   (eg, &lt;&gt; for = ALL, with &lt;&gt; for NOT IN.)
 *
 * NOTE: This method is called after the underlying subquery has been
 * preprocessed, so we build a new Predicate, not just a new expression.
 *
 * @param numTables			Number of tables in DML Statement
 *
 * @return UnaryComparisonOperatorNode	An IS [NOT] NULL above the
 *										transformed subquery.
 *
 * @exception StandardException		Thrown on error
 */
private UnaryComparisonOperatorNode pushNewPredicate(int numTables) throws StandardException {
    AndNode andNode;
    JBitSet tableMap;
    Predicate predicate;
    ResultColumn firstRC;
    ResultColumnList resultColumns;
    UnaryComparisonOperatorNode ucoNode = null;
    ValueNode rightOperand;
    ContextManager cm = getContextManager();
    /* We have to ensure that the resultSet immediately under us has
		 * a PredicateList, otherwise we can't push the predicate down.
		 */
    resultSet = resultSet.ensurePredicateList(numTables);
    /* RESOLVE - once we understand how correlated columns will work, 
		 * we probably want to mark leftOperand as a correlated column
		 */
    resultColumns = resultSet.getResultColumns();
    /*
		** Create a new PR node.  Put it over the original subquery.  resulSet
		** is now the new PR.  We give the chance that things under the PR node
		** can be materialized.  See beetle 4373.
		*/
    ResultColumnList newRCL = resultColumns.copyListAndObjects();
    newRCL.genVirtualColumnNodes(resultSet, resultColumns);
    resultSet = new ProjectRestrictNode(// child
    resultSet, // result columns
    newRCL, // restriction
    null, // restriction list
    null, // project subqueries
    null, // restrict subqueries
    null, null, cm);
    resultColumns = newRCL;
    firstRC = resultColumns.elementAt(0);
    rightOperand = firstRC.getExpression();
    BinaryComparisonOperatorNode bcoNode = getNewJoinCondition(leftOperand, rightOperand);
    ValueNode andLeft = bcoNode;
    /* For NOT IN or ALL, and if either side of the comparison is nullable, and the
		 * subquery can not be flattened (because of that), we need to add IS NULL node
		 * on top of the nullables, such that the behavior is (beetle 5173):
		 *
		 *    (1) If we have nulls in right operand, no row is returned.
		 *    (2) If subquery result is empty before applying join predicate, every
		 *		  left row (including NULLs) is returned.
		 *	  (3) Otherwise, return {all left row} - {NULLs}
		 */
    if (isNOT_IN() || isALL()) {
        boolean leftNullable = leftOperand.getTypeServices().isNullable();
        boolean rightNullable = rightOperand.getTypeServices().isNullable();
        if (leftNullable || rightNullable) {
            /* Create a normalized structure.
				 */
            BooleanConstantNode falseNode = new BooleanConstantNode(false, cm);
            OrNode newOr = new OrNode(bcoNode, falseNode, cm);
            newOr.postBindFixup();
            andLeft = newOr;
            if (leftNullable) {
                UnaryComparisonOperatorNode leftIsNull = new IsNullNode(leftOperand, false, cm);
                leftIsNull.bindComparisonOperator();
                newOr = new OrNode(leftIsNull, andLeft, cm);
                newOr.postBindFixup();
                andLeft = newOr;
            }
            if (rightNullable) {
                UnaryComparisonOperatorNode rightIsNull = new IsNullNode(rightOperand, false, cm);
                rightIsNull.bindComparisonOperator();
                newOr = new OrNode(rightIsNull, andLeft, cm);
                newOr.postBindFixup();
                andLeft = newOr;
            }
        }
    }
    /* Place an AndNode above the <BinaryComparisonOperator> */
    andNode = new AndNode(andLeft, getTrueNode(), cm);
    /* Build the referenced table map for the new predicate */
    tableMap = new JBitSet(numTables);
    andNode.postBindFixup();
    /* Put the AndNode under a Predicate */
    predicate = new Predicate(andNode, tableMap, cm);
    predicate.categorize();
    /* Push the new Predicate to the subquery's list */
    resultSet = resultSet.addNewPredicate(predicate);
    /* Clean up the leftOperand and subquery ResultColumn */
    leftOperand = null;
    firstRC.setType(getTypeServices());
    firstRC.setExpression(getTrueNode());
    /* Add the IS [NOT] NULL above the SubqueryNode */
    switch(subqueryType) {
        case IN_SUBQUERY:
        case EQ_ANY_SUBQUERY:
        case NE_ANY_SUBQUERY:
        case LE_ANY_SUBQUERY:
        case LT_ANY_SUBQUERY:
        case GE_ANY_SUBQUERY:
        case GT_ANY_SUBQUERY:
            ucoNode = new IsNullNode(this, true, cm);
            break;
        case NOT_IN_SUBQUERY:
        case EQ_ALL_SUBQUERY:
        case NE_ALL_SUBQUERY:
        case LE_ALL_SUBQUERY:
        case LT_ALL_SUBQUERY:
        case GE_ALL_SUBQUERY:
        case GT_ALL_SUBQUERY:
            ucoNode = new IsNullNode(this, false, cm);
            break;
        default:
            if (SanityManager.DEBUG) {
                SanityManager.NOTREACHED();
            }
    }
    ucoNode.bindComparisonOperator();
    return ucoNode;
}
Also used : JBitSet(org.apache.derby.iapi.util.JBitSet) ContextManager(org.apache.derby.iapi.services.context.ContextManager)

Example 22 with ContextManager

use of org.apache.derby.iapi.services.context.ContextManager in project derby by apache.

the class ValueNode method genSQLJavaSQLTree.

/**
 * Generate a SQL-&gt;Java-&gt;SQL conversion tree above the current node
 * and bind the new nodes individually.
 * This is useful when doing comparisons, built-in functions, etc. on
 * java types which have a direct mapping to system built-in types.
 *
 * @return ValueNode	The new tree.
 *
 * @exception StandardException	Thrown on error
 */
ValueNode genSQLJavaSQLTree() throws StandardException {
    if (SanityManager.DEBUG) {
        SanityManager.ASSERT(getTypeId() != null, "genSQLJavaSQLTree() only expected to be called on a bound node");
        SanityManager.ASSERT(getTypeId().userType(), "genSQLJavaSQLTree() only expected to be called on user types");
    }
    final ContextManager cm = getContextManager();
    JavaValueNode stjvn = new SQLToJavaValueNode(this, cm);
    ValueNode jtsvn = new JavaToSQLValueNode(stjvn, cm);
    DataTypeDescriptor resultType;
    if ((getTypeServices() != null) && getTypeId().userType()) {
        resultType = getTypeServices();
    } else {
        resultType = DataTypeDescriptor.getSQLDataTypeDescriptor(stjvn.getJavaTypeName());
    }
    jtsvn.setType(resultType);
    return jtsvn;
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ContextManager(org.apache.derby.iapi.services.context.ContextManager)

Example 23 with ContextManager

use of org.apache.derby.iapi.services.context.ContextManager in project derby by apache.

the class DDLConstantAction method adjustUDTDependencies.

/**
 * Add and drop dependencies of an object on UDTs.
 *
 * @param lcc Interpreter's state variable for this session.
 * @param dd Metadata
 * @param dependent Object which depends on UDT
 * @param addUdtMap Map of UDTs for which dependencies should be added
 * @param dropUdtMap Map of UDT for which dependencies should be dropped
 */
private void adjustUDTDependencies(LanguageConnectionContext lcc, DataDictionary dd, Dependent dependent, HashMap<String, AliasDescriptor> addUdtMap, HashMap<String, AliasDescriptor> dropUdtMap) throws StandardException {
    // again, nothing to do if there are no columns of udt type
    if ((addUdtMap.isEmpty()) && (dropUdtMap.isEmpty())) {
        return;
    }
    TransactionController tc = lcc.getTransactionExecute();
    DependencyManager dm = dd.getDependencyManager();
    ContextManager cm = lcc.getContextManager();
    // add new dependencies
    for (AliasDescriptor ad : addUdtMap.values()) {
        dm.addDependency(dependent, ad, cm);
    }
    // drop dependencies that are orphaned
    for (AliasDescriptor ad : dropUdtMap.values()) {
        DependencyDescriptor dep = new DependencyDescriptor(dependent, ad);
        dd.dropStoredDependency(dep, tc);
    }
}
Also used : DependencyDescriptor(org.apache.derby.iapi.sql.dictionary.DependencyDescriptor) ContextManager(org.apache.derby.iapi.services.context.ContextManager) AliasDescriptor(org.apache.derby.iapi.sql.dictionary.AliasDescriptor) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Example 24 with ContextManager

use of org.apache.derby.iapi.services.context.ContextManager in project derby by apache.

the class DDLConstantAction method addColumnDependencies.

/**
 * Add dependencies of a column on providers. These can arise if a generated column depends
 * on a user created function.
 */
protected void addColumnDependencies(LanguageConnectionContext lcc, DataDictionary dd, TableDescriptor td, ColumnInfo ci) throws StandardException {
    ProviderInfo[] providers = ci.providers;
    if (providers != null) {
        DependencyManager dm = dd.getDependencyManager();
        ContextManager cm = lcc.getContextManager();
        int providerCount = providers.length;
        ColumnDescriptor cd = td.getColumnDescriptor(ci.name);
        DefaultDescriptor defDesc = cd.getDefaultDescriptor(dd);
        for (int px = 0; px < providerCount; px++) {
            ProviderInfo pi = providers[px];
            DependableFinder finder = pi.getDependableFinder();
            UUID providerID = pi.getObjectId();
            Provider provider = (Provider) finder.getDependable(dd, providerID);
            dm.addDependency(defDesc, provider, cm);
        }
    // end loop through providers
    }
}
Also used : DependableFinder(org.apache.derby.catalog.DependableFinder) ProviderInfo(org.apache.derby.iapi.sql.depend.ProviderInfo) ContextManager(org.apache.derby.iapi.services.context.ContextManager) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) DefaultDescriptor(org.apache.derby.iapi.sql.dictionary.DefaultDescriptor) UUID(org.apache.derby.catalog.UUID) Provider(org.apache.derby.iapi.sql.depend.Provider)

Example 25 with ContextManager

use of org.apache.derby.iapi.services.context.ContextManager in project derby by apache.

the class T_FileSystemData method runTestSet.

/**
 *	  run the test
 *
 *	  @exception T_Fail Unexpected behaviour from the API
 */
protected void runTestSet() throws T_Fail {
    // get a utility helper
    ContextManager cm1 = contextService.newContextManager();
    contextService.setCurrentContextManager(cm1);
    try {
        runCostEstimationTests();
        runAllocationTests();
    } catch (StandardException se) {
        // Assume database is not active. DERBY-4856 thread dump
        cm1.cleanupOnError(se, false);
        throw T_Fail.exceptionFail(se);
    } finally {
        contextService.resetCurrentContextManager(cm1);
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) ContextManager(org.apache.derby.iapi.services.context.ContextManager)

Aggregations

ContextManager (org.apache.derby.iapi.services.context.ContextManager)61 StandardException (org.apache.derby.shared.common.error.StandardException)22 RawTransaction (org.apache.derby.iapi.store.raw.xact.RawTransaction)8 ContextService (org.apache.derby.iapi.services.context.ContextService)7 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)7 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)6 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)5 TransactionController (org.apache.derby.iapi.store.access.TransactionController)5 XAException (javax.transaction.xa.XAException)4 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)4 XAResourceManager (org.apache.derby.iapi.store.access.xa.XAResourceManager)4 XAXactId (org.apache.derby.iapi.store.access.xa.XAXactId)4 SQLException (java.sql.SQLException)3 Xid (javax.transaction.xa.Xid)3 Serviceable (org.apache.derby.iapi.services.daemon.Serviceable)3 ExecPreparedStatement (org.apache.derby.iapi.sql.execute.ExecPreparedStatement)3 RowLocation (org.apache.derby.iapi.types.RowLocation)3 Date (java.util.Date)2 HashSet (java.util.HashSet)2 Properties (java.util.Properties)2