📄 binaryrelationaloperatornode.java
字号:
/* Derby - Class org.apache.derby.impl.sql.compile.BinaryRelationalOperatorNode 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.reference.ClassName;import org.apache.derby.iapi.reference.JDBC30Translation;import org.apache.derby.iapi.util.JBitSet;import org.apache.derby.iapi.services.loader.GeneratedMethod;import org.apache.derby.iapi.services.compiler.MethodBuilder;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.sql.compile.C_NodeTypes;import org.apache.derby.iapi.sql.compile.Optimizable;import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;import org.apache.derby.iapi.store.access.Qualifier;import org.apache.derby.iapi.store.access.ScanController;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.types.TypeId;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.types.Orderable;import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;import java.sql.Types;/** * This class represents the 6 binary operators: LessThan, LessThanEquals, * Equals, NotEquals, GreaterThan and GreaterThanEquals. * * @author Manish Khettry */public class BinaryRelationalOperatorNode extends BinaryComparisonOperatorNode implements RelationalOperator{ private int operatorType; /* RelationalOperator Interface */ public void init(Object leftOperand, Object rightOperand) { String methodName = ""; String operatorName = ""; switch (getNodeType()) { case C_NodeTypes.BINARY_EQUALS_OPERATOR_NODE: methodName = "equals"; operatorName = "="; operatorType = RelationalOperator.EQUALS_RELOP; break; case C_NodeTypes.BINARY_GREATER_EQUALS_OPERATOR_NODE: methodName = "greaterOrEquals"; operatorName = ">="; operatorType = RelationalOperator.GREATER_EQUALS_RELOP; break; case C_NodeTypes.BINARY_GREATER_THAN_OPERATOR_NODE: methodName = "greaterThan"; operatorName = ">"; operatorType = RelationalOperator.GREATER_THAN_RELOP; break; case C_NodeTypes.BINARY_LESS_EQUALS_OPERATOR_NODE: methodName = "lessOrEquals"; operatorName = "<="; operatorType = RelationalOperator.LESS_EQUALS_RELOP; break; case C_NodeTypes.BINARY_LESS_THAN_OPERATOR_NODE: methodName = "lessThan"; operatorName = "<"; operatorType = RelationalOperator.LESS_THAN_RELOP; break; case C_NodeTypes.BINARY_NOT_EQUALS_OPERATOR_NODE: methodName = "notEquals"; operatorName = "<>"; operatorType = RelationalOperator.NOT_EQUALS_RELOP; break; default: if (SanityManager.DEBUG) { SanityManager.THROWASSERT("init for BinaryRelationalOperator called with wrong nodeType = " + getNodeType()); } break; } super.init(leftOperand, rightOperand, operatorName, methodName); } /** @see RelationalOperator#getColumnOperand */ public ColumnReference getColumnOperand( Optimizable optTable, int columnPosition) { FromTable ft = (FromTable) optTable; return getColumnOperand(ft.getTableNumber(), columnPosition); } /** @see RelationalOperator#getColumnOperand */ public ColumnReference getColumnOperand( int tableNumber, int columnPosition) { ColumnReference cr; if (leftOperand instanceof ColumnReference) { /* ** The left operand is a column reference. ** Is it the correct column? */ cr = (ColumnReference) leftOperand; if (cr.getTableNumber() == tableNumber) { /* ** The table is correct, how about the column position? */ if (cr.getSource().getColumnPosition() == columnPosition) { /* We've found the correct column - return it */ return cr; } } } if (rightOperand instanceof ColumnReference) { /* ** The right operand is a column reference. ** Is it the correct column? */ cr = (ColumnReference) rightOperand; if (cr.getTableNumber() == tableNumber) { /* ** The table is correct, how about the column position? */ if (cr.getSource().getColumnPosition() == columnPosition) { /* We've found the correct column - return it */ return cr; } } } /* Neither side is the column we're looking for */ return null; } /** @see RelationalOperator#getColumnOperand */ public ColumnReference getColumnOperand(Optimizable optTable) { ColumnReference cr; if (leftOperand instanceof ColumnReference) { /* ** The left operand is a column reference. ** Is it the correct column? */ cr = (ColumnReference) leftOperand; if (cr.getTableNumber() == optTable.getTableNumber()) { /* ** The table is correct. */ return cr; } } if (rightOperand instanceof ColumnReference) { /* ** The right operand is a column reference. ** Is it the correct column? */ cr = (ColumnReference) rightOperand; if (cr.getTableNumber() == optTable.getTableNumber()) { /* ** The table is correct */ return cr; } } /* Neither side is the column we're looking for */ return null; } /** * @see RelationalOperator#getExpressionOperand */ public ValueNode getExpressionOperand( int tableNumber, int columnPosition) { ColumnReference cr; if (leftOperand instanceof ColumnReference) { /* ** The left operand is a column reference. ** Is it the correct column? */ cr = (ColumnReference) leftOperand; if (cr.getTableNumber() == tableNumber) { /* ** The table is correct, how about the column position? */ if (cr.getSource().getColumnPosition() == columnPosition) { /* ** We've found the correct column - ** return the other side */ return rightOperand; } } } if (rightOperand instanceof ColumnReference) { /* ** The right operand is a column reference. ** Is it the correct column? */ cr = (ColumnReference) rightOperand; if (cr.getTableNumber() == tableNumber) { /* ** The table is correct, how about the column position? */ if (cr.getSource().getColumnPosition() == columnPosition) { /* ** We've found the correct column - ** return the other side */ return leftOperand; } } } return null; } /** * @see RelationalOperator#generateExpressionOperand * * @exception StandardException Thrown on error */ public void generateExpressionOperand( Optimizable optTable, int columnPosition, ExpressionClassBuilder acb, MethodBuilder mb) throws StandardException { ColumnReference cr; FromBaseTable ft; if (SanityManager.DEBUG) { SanityManager.ASSERT(optTable instanceof FromBaseTable); } ft = (FromBaseTable) optTable; ValueNode exprOp = getExpressionOperand(ft.getTableNumber(), columnPosition); if (SanityManager.DEBUG) { if (exprOp == null) { SanityManager.THROWASSERT( "ColumnReference for correct column (columnPosition = " + columnPosition + ", exposed table name = " + ft.getExposedName() + ") not found on either side of BinaryRelationalOperator"); } } exprOp.generateExpression(acb, mb); } /** @see RelationalOperator#selfComparison * * @exception StandardException Thrown on error */ public boolean selfComparison(ColumnReference cr) throws StandardException { ValueNode otherSide; JBitSet tablesReferenced; /* ** Figure out which side the given ColumnReference is on, ** and look for the same table on the other side. */ if (leftOperand == cr) { otherSide = rightOperand; } else if (rightOperand == cr) { otherSide = leftOperand; } else { otherSide = null; if (SanityManager.DEBUG) { SanityManager.THROWASSERT( "ColumnReference not found on either side of binary comparison."); } } tablesReferenced = otherSide.getTablesReferenced(); /* Return true if the table we're looking for is in the bit map */ return tablesReferenced.get(cr.getTableNumber()); } /** @see RelationalOperator#usefulStartKey */ public boolean usefulStartKey(Optimizable optTable) { /* ** Determine whether this operator is a useful start operator ** with knowledge of whether the key column is on the left or right. */ int columnSide = columnOnOneSide(optTable); if (columnSide == NEITHER) return false; else return usefulStartKey(columnSide == LEFT); } /** * Return true if a key column for the given table is found on the * left side of this operator, false if it is found on the right * side of this operator. * * NOTE: This method assumes that a key column will be found on one * side or the other. If you don't know whether a key column exists, * use the columnOnOneSide() method (below). * * @param optTable The Optimizable table that we're looking for a key * column on. * * @return true if a key column for the given table is on the left * side of this operator, false if one is found on the right * side of this operator. */ protected boolean keyColumnOnLeft(Optimizable optTable) { ColumnReference cr; FromTable ft; boolean left = false; ft = (FromTable) optTable; /* Is the key column on the left or the right? */ if (leftOperand instanceof ColumnReference) { /* ** The left operand is a column reference. ** Is it the correct column? */ cr = (ColumnReference) leftOperand; if (cr.getTableNumber() == ft.getTableNumber()) { /* The left operand is the key column */ left = true; } } else { if (SanityManager.DEBUG) { SanityManager.ASSERT((rightOperand instanceof ColumnReference) && (((ColumnReference) rightOperand).getTableNumber() == ft.getTableNumber()), "Key column not found on either side."); } /* The right operand is the key column */ left = false; } return left; } /* Return values for columnOnOneSide */ protected static final int LEFT = -1; protected static final int NEITHER = 0; protected static final int RIGHT = 1; /** * Determine whether there is a column from the given table on one side * of this operator, and if so, which side is it on? * * @param optTable The Optimizable table that we're looking for a key * column on. * * @return LEFT if there is a column on the left, RIGHT if there is * a column on the right, NEITHER if no column found on either * side.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -