--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.compile.IsNullNode\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.sql.compile;\r
+\r
+import org.apache.derby.iapi.reference.ClassName;\r
+\r
+import org.apache.derby.iapi.services.compiler.MethodBuilder;\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import org.apache.derby.iapi.sql.compile.C_NodeTypes;\r
+import org.apache.derby.iapi.sql.compile.Optimizable;\r
+\r
+import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;\r
+\r
+import org.apache.derby.iapi.store.access.ScanController;\r
+\r
+import org.apache.derby.iapi.types.DataTypeDescriptor;\r
+import org.apache.derby.iapi.types.DataValueDescriptor;\r
+import org.apache.derby.iapi.types.DataValueFactory;\r
+import org.apache.derby.iapi.types.StringDataValue;\r
+import org.apache.derby.iapi.types.TypeId;\r
+\r
+import org.apache.derby.iapi.types.Orderable;\r
+\r
+import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;\r
+\r
+import java.sql.Types;\r
+\r
+/**\r
+ * This node represents either a unary \r
+ * IS NULL or IS NOT NULL comparison operator\r
+ *\r
+ */\r
+\r
+public final class IsNullNode extends UnaryComparisonOperatorNode\r
+ implements RelationalOperator\r
+{\r
+\r
+ private DataValueDescriptor nullValue;\r
+\r
+ public void setNodeType(int nodeType)\r
+ {\r
+ String operator;\r
+ String methodName;\r
+\r
+ if (nodeType == C_NodeTypes.IS_NULL_NODE)\r
+ {\r
+ /* By convention, the method name for the is null operator is "isNull" */\r
+ operator = "is null";\r
+ methodName = "isNullOp";\r
+ }\r
+ else\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ if (nodeType != C_NodeTypes.IS_NOT_NULL_NODE)\r
+ {\r
+ SanityManager.THROWASSERT(\r
+ "Unexpected nodeType = " + nodeType);\r
+ }\r
+ }\r
+ /* By convention, the method name for the is not null operator is \r
+ * "isNotNull" \r
+ */\r
+ operator = "is not null";\r
+ methodName = "isNotNull";\r
+ }\r
+ setOperator(operator);\r
+ setMethodName(methodName);\r
+ super.setNodeType(nodeType);\r
+ }\r
+\r
+ /**\r
+ * Negate the comparison.\r
+ *\r
+ * @param operand The operand of the operator\r
+ *\r
+ * @return UnaryOperatorNode The negated expression\r
+ *\r
+ * @exception StandardException Thrown on error\r
+ */\r
+ UnaryOperatorNode getNegation(ValueNode operand)\r
+ throws StandardException\r
+ {\r
+ UnaryOperatorNode negation;\r
+\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ SanityManager.ASSERT(getTypeServices() != null,\r
+ "dataTypeServices is expected to be non-null");\r
+ }\r
+\r
+ if (isNullNode())\r
+ {\r
+ setNodeType(C_NodeTypes.IS_NOT_NULL_NODE);\r
+ }\r
+ else\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ if (! isNotNullNode())\r
+ {\r
+ SanityManager.THROWASSERT(\r
+ "Unexpected nodeType = " + getNodeType());\r
+ }\r
+ }\r
+ setNodeType(C_NodeTypes.IS_NULL_NODE);\r
+ }\r
+ return this;\r
+ }\r
+\r
+ /**\r
+ * Bind a ? parameter operand of the IS [NOT] NULL predicate.\r
+ *\r
+ * @exception StandardException Thrown on error\r
+ */\r
+\r
+ void bindParameter()\r
+ throws StandardException\r
+ {\r
+ /*\r
+ ** If IS [NOT] NULL has a ? operand, we assume\r
+ ** its type is varchar with the implementation-defined maximum length\r
+ ** for a varchar.\r
+ ** Also, for IS [NOT] NULL, it doesn't matter what is VARCHAR's \r
+ ** collation (since for NULL check, no collation sensitive processing\r
+ ** is required) and hence we will not worry about the collation setting\r
+ */\r
+\r
+ operand.setType(new DataTypeDescriptor(TypeId.getBuiltInTypeId(Types.VARCHAR), true));\r
+ }\r
+\r
+ /* RelationalOperator interface */\r
+\r
+ /** @see RelationalOperator#usefulStartKey */\r
+ public boolean usefulStartKey(Optimizable optTable)\r
+ {\r
+ // IS NULL is start/stop key, IS NOT NULL is not\r
+ return (isNullNode());\r
+ }\r
+\r
+ /** @see RelationalOperator#usefulStopKey */\r
+ public boolean usefulStopKey(Optimizable optTable)\r
+ {\r
+ // IS NULL is start/stop key, IS NOT NULL is not\r
+ return (isNullNode());\r
+ }\r
+\r
+ /** @see RelationalOperator#getStartOperator */\r
+ public int getStartOperator(Optimizable optTable)\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ if (! isNullNode())\r
+ {\r
+ SanityManager.THROWASSERT(\r
+ "getNodeType() not expected to return " + getNodeType());\r
+ }\r
+ }\r
+ return ScanController.GE;\r
+ }\r
+\r
+ /** @see RelationalOperator#getStopOperator */\r
+ public int getStopOperator(Optimizable optTable)\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ if (! isNullNode())\r
+ {\r
+ SanityManager.THROWASSERT(\r
+ "getNodeType() not expected to return " + getNodeType());\r
+ }\r
+ }\r
+ return ScanController.GT;\r
+ }\r
+\r
+ /** @see RelationalOperator#generateOperator */\r
+ public void generateOperator(MethodBuilder mb,\r
+ Optimizable optTable)\r
+ {\r
+ mb.push(Orderable.ORDER_OP_EQUALS);\r
+ }\r
+\r
+ /** @see RelationalOperator#generateNegate */\r
+ public void generateNegate(MethodBuilder mb,\r
+ Optimizable optTable)\r
+ {\r
+ mb.push(isNotNullNode());\r
+ }\r
+\r
+ /** @see RelationalOperator#getOperator */\r
+ public int getOperator()\r
+ {\r
+ int operator;\r
+ if (isNullNode())\r
+ {\r
+ operator = IS_NULL_RELOP;\r
+ }\r
+ else\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ if (! isNotNullNode())\r
+ {\r
+ SanityManager.THROWASSERT(\r
+ "Unexpected nodeType = " + getNodeType());\r
+ }\r
+ }\r
+ operator = IS_NOT_NULL_RELOP;\r
+ }\r
+\r
+ return operator;\r
+ }\r
+\r
+ /** @see RelationalOperator#compareWithKnownConstant */\r
+ public boolean compareWithKnownConstant(Optimizable optTable, boolean considerParameters)\r
+ {\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * @see RelationalOperator#getCompareValue\r
+ *\r
+ * @exception StandardException Thrown on error\r
+ */\r
+ public DataValueDescriptor getCompareValue(Optimizable optTable)\r
+ throws StandardException\r
+ {\r
+ if (nullValue == null)\r
+ {\r
+ nullValue = operand.getTypeServices().getNull();\r
+ }\r
+\r
+ return nullValue;\r
+ }\r
+\r
+ /** @see RelationalOperator#equalsComparisonWithConstantExpression */\r
+ public boolean equalsComparisonWithConstantExpression(Optimizable optTable)\r
+ {\r
+ boolean retval = false;\r
+\r
+ // Always return false for NOT NULL\r
+ if (isNotNullNode())\r
+ {\r
+ return false;\r
+ }\r
+\r
+ /*\r
+ ** Is the operand a column in the given table?\r
+ */\r
+ if (operand instanceof ColumnReference)\r
+ {\r
+ int tabNum = ((ColumnReference) operand).getTableNumber();\r
+ if (optTable.hasTableNumber() &&\r
+ (optTable.getTableNumber() == tabNum))\r
+ {\r
+ retval = true;\r
+ }\r
+ }\r
+\r
+ return retval;\r
+ }\r
+\r
+ /** \r
+ * @see RelationalOperator#getTransitiveSearchClause \r
+ *\r
+ * @exception StandardException thrown on error\r
+ */\r
+ public RelationalOperator getTransitiveSearchClause(ColumnReference otherCR)\r
+ throws StandardException\r
+ {\r
+ return (RelationalOperator) getNodeFactory().getNode(\r
+ getNodeType(),\r
+ otherCR,\r
+ getContextManager());\r
+ }\r
+\r
+ /**\r
+ * null operators are defined on DataValueDescriptor.\r
+ * Overrides method in UnaryOperatorNode for code generation purposes.\r
+ */\r
+ public String getReceiverInterfaceName() {\r
+ return ClassName.DataValueDescriptor;\r
+ }\r
+\r
+ /** IS NULL is like =, so should have the same selectivity */\r
+ public double selectivity(Optimizable optTable) \r
+ {\r
+ if (isNullNode())\r
+ {\r
+ return 0.1d;\r
+ }\r
+ else\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ if (! isNotNullNode())\r
+ {\r
+ SanityManager.THROWASSERT(\r
+ "Unexpected nodeType = " + getNodeType());\r
+ }\r
+ }\r
+ /* IS NOT NULL is like <>, so should have same selectivity */\r
+ return 0.9d;\r
+ }\r
+ }\r
+\r
+ private boolean isNullNode()\r
+ {\r
+ return getNodeType() == C_NodeTypes.IS_NULL_NODE;\r
+ }\r
+\r
+ private boolean isNotNullNode()\r
+ {\r
+ return getNodeType() == C_NodeTypes.IS_NOT_NULL_NODE;\r
+ }\r
+ \r
+ /** @see ValueNode#isRelationalOperator */\r
+ public boolean isRelationalOperator()\r
+ {\r
+ return true;\r
+ }\r
+\r
+ /** @see ValueNode#optimizableEqualityNode */\r
+ public boolean optimizableEqualityNode(Optimizable optTable, \r
+ int columnNumber, \r
+ boolean isNullOkay)\r
+ {\r
+ if (!isNullNode() || !isNullOkay)\r
+ return false;\r
+ \r
+ ColumnReference cr = getColumnOperand(optTable,\r
+ columnNumber);\r
+ if (cr == null)\r
+ {\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+}\r