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, <> for = ALL, with <> 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;
}
use of org.apache.derby.iapi.services.context.ContextManager in project derby by apache.
the class ValueNode method genSQLJavaSQLTree.
/**
* Generate a SQL->Java->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;
}
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);
}
}
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
}
}
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);
}
}
Aggregations