⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inlistoperatornode.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*   Derby - Class org.apache.derby.impl.sql.compile.InListOperatorNode   Copyright 1997, 2004 The Apache Software Foundation or its licensors, as applicable.   Licensed under the Apache License, Version 2.0 (the "License");   you may not use this file except in compliance with the License.   You may obtain a copy of the License at      http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License. */package	org.apache.derby.impl.sql.compile;import org.apache.derby.iapi.sql.compile.C_NodeTypes;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;import org.apache.derby.iapi.sql.dictionary.DataDictionary;import org.apache.derby.iapi.reference.ClassName;import org.apache.derby.iapi.types.TypeId;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.services.compiler.MethodBuilder;import org.apache.derby.iapi.services.compiler.LocalField;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.sql.compile.Optimizable;import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;import org.apache.derby.iapi.util.JBitSet;import org.apache.derby.iapi.services.classfile.VMOpcode;import java.lang.reflect.Modifier;/** * An InListOperatorNode represents an IN list. * * @author Jerry Brenner */public final class InListOperatorNode extends BinaryListOperatorNode{	private boolean isOrdered;	/**	 * Initializer for a InListOperatorNode	 *	 * @param leftOperand		The left operand of the node	 * @param rightOperandList	The right operand list of the node	 */	public void init(Object leftOperand, Object rightOperandList)	{		init(leftOperand, rightOperandList, "IN", "in");	}	/**	 * Convert this object to a String.  See comments in QueryTreeNode.java	 * for how this should be done for tree printing.	 *	 * @return	This object as a String	 */	public String toString()	{		if (SanityManager.DEBUG)		{			return "isOrdered: " + isOrdered + "\n" +				super.toString();		}		else		{			return "";		}	}	/**	 * Preprocess an expression tree.  We do a number of transformations	 * here (including subqueries, IN lists, LIKE and BETWEEN) plus	 * subquery flattening.	 * NOTE: This is done before the outer ResultSetNode is preprocessed.	 *	 * @param	numTables			Number of tables in the DML Statement	 * @param	outerFromList		FromList from outer query block	 * @param	outerSubqueryList	SubqueryList from outer query block	 * @param	outerPredicateList	PredicateList from outer query block	 *	 * @return		The modified expression	 *	 * @exception StandardException		Thrown on error	 */	public ValueNode preprocess(int numTables,								FromList outerFromList,								SubqueryList outerSubqueryList,								PredicateList outerPredicateList) 					throws StandardException	{		super.preprocess(numTables,						 outerFromList, outerSubqueryList,						 outerPredicateList);		/* Check for the degenerate case of a single element in the IN list.		 * If found, then convert to "=".		 */		if (rightOperandList.size() == 1)		{			BinaryComparisonOperatorNode equal = 				(BinaryComparisonOperatorNode) getNodeFactory().getNode(						C_NodeTypes.BINARY_EQUALS_OPERATOR_NODE,						leftOperand, 						(ValueNode) rightOperandList.elementAt(0),						getContextManager());			/* Set type info for the operator node */			equal.bindComparisonOperator();			return equal;		}		else if ((leftOperand instanceof ColumnReference) &&				 rightOperandList.containsAllConstantNodes())		{			/* When sorting or choosing min/max in the list, if types are not an exact			 * match, we use the left operand's type as the "judge", assuming that they			 * are compatible, as also the case with DB2.			 */			TypeId judgeTypeId = leftOperand.getTypeServices().getTypeId();			DataValueDescriptor judgeODV = null;  //no judge, no argument			if (! rightOperandList.allSamePrecendence(judgeTypeId.typePrecedence()))				judgeODV = (DataValueDescriptor) judgeTypeId.getNull();			//Sort the list in ascending order			rightOperandList.sortInAscendingOrder(judgeODV);			isOrdered = true;			/* If the leftOperand is a ColumnReference			 * and the IN list is all constants, then we generate			 * an additional BETWEEN clause of the form:			 *	CRClone BETWEEN minValue and maxValue			 */			ValueNode leftClone = leftOperand.getClone();			ValueNode minValue = (ValueNode) rightOperandList.elementAt(0);  //already sorted			ValueNode maxValue = (ValueNode) rightOperandList.elementAt(rightOperandList.size() - 1);			/* Handle the degenerate case where 			 * the min and the max are the same value.			 */			DataValueDescriptor minODV =				 ((ConstantNode) minValue).getValue();			DataValueDescriptor maxODV =				 ((ConstantNode) maxValue).getValue();			if ((judgeODV == null && minODV.compare(maxODV) == 0) ||				(judgeODV != null && judgeODV.equals(minODV, maxODV).equals(true)))			{				BinaryComparisonOperatorNode equal = 					(BinaryComparisonOperatorNode) getNodeFactory().getNode(						C_NodeTypes.BINARY_EQUALS_OPERATOR_NODE,						leftOperand, 						minValue,						getContextManager());				/* Set type info for the operator node */				equal.bindComparisonOperator();				return equal;			}			// Build the Between			ValueNodeList vnl = (ValueNodeList) getNodeFactory().getNode(													C_NodeTypes.VALUE_NODE_LIST,													getContextManager());			vnl.addValueNode(minValue);			vnl.addValueNode(maxValue);			BetweenOperatorNode bon = 				(BetweenOperatorNode) getNodeFactory().getNode(									C_NodeTypes.BETWEEN_OPERATOR_NODE,									leftClone,									vnl,									getContextManager());			/* The transformed tree has to be normalized:			 *				AND			 *			   /   \			 *		IN LIST    AND			 *				   /   \			 *				  >=	AND			 *						/   \			 *					   <=	TRUE			 */			/* Create the AND */			AndNode newAnd;			newAnd = (AndNode) getNodeFactory().getNode(									C_NodeTypes.AND_NODE,									this,									bon.preprocess(numTables,												   outerFromList,												   outerSubqueryList,												   outerPredicateList),									getContextManager());			newAnd.postBindFixup();			/* Mark this node as transformed so that we don't get			 * calculated into the selectivity mulitple times.			 */			setTransformed();			// Return new AndNode			return newAnd;		}		else		{			return this;		}	}	/**	 * Eliminate NotNodes in the current query block.  We traverse the tree, 	 * inverting ANDs and ORs and eliminating NOTs as we go.  We stop at 	 * ComparisonOperators and boolean expressions.  We invert 	 * ComparisonOperators and replace boolean expressions with 	 * boolean expression = false.	 * NOTE: Since we do not recurse under ComparisonOperators, there	 * still could be NotNodes left in the tree.	 *	 * @param	underNotNode		Whether or not we are under a NotNode.	 *								 *	 * @return		The modified expression	 *	 * @exception StandardException		Thrown on error	 */	ValueNode eliminateNots(boolean underNotNode) 					throws StandardException	{		AndNode						 newAnd = null;		BinaryComparisonOperatorNode leftBCO;		BinaryComparisonOperatorNode rightBCO;		int							 listSize = rightOperandList.size();		ValueNode					 leftSide;		if (SanityManager.DEBUG)		SanityManager.ASSERT(listSize > 0,			"rightOperandList.size() is expected to be > 0");		if (! underNotNode)		{			return this;		}		/* we want to convert the IN List into = OR = ... as		 * described below.  		 */		/* Convert:		 *		leftO IN rightOList.elementAt(0) , rightOList.elementAt(1) ...		 * to:		 *		leftO <> rightOList.elementAt(0) AND leftO <> rightOList.elementAt(1) ...		 * NOTE - We do the conversion here since the single table clauses		 * can be pushed down and the optimizer may eventually have a filter factor		 * for <>.		 */		/* leftO <> rightOList.at(0) */		/* If leftOperand is a ColumnReference, it may be remapped during optimization, and that		 * requires each <> node to have a separate object.		 */		ValueNode leftClone = (leftOperand instanceof ColumnReference) ? leftOperand.getClone() : leftOperand;		leftBCO = (BinaryComparisonOperatorNode) 					getNodeFactory().getNode(						C_NodeTypes.BINARY_NOT_EQUALS_OPERATOR_NODE,						leftClone,						(ValueNode) rightOperandList.elementAt(0),						getContextManager());		/* Set type info for the operator node */		leftBCO.bindComparisonOperator();		leftSide = leftBCO;		for (int elemsDone = 1; elemsDone < listSize; elemsDone++)		{			/* leftO <> rightOList.elementAt(elemsDone) */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -