[teiid-commits] teiid SVN: r813 - in trunk/engine/src: main/java/com/metamatrix/query/processor/proc and 7 other directories.

teiid-commits at lists.jboss.org teiid-commits at lists.jboss.org
Tue Apr 21 10:47:32 EDT 2009


Author: shawkins
Date: 2009-04-21 10:47:32 -0400 (Tue, 21 Apr 2009)
New Revision: 813

Modified:
   trunk/engine/src/main/java/com/metamatrix/dqp/service/CustomizableTrackingService.java
   trunk/engine/src/main/java/com/metamatrix/query/processor/proc/ExecDynamicSqlInstruction.java
   trunk/engine/src/main/java/com/metamatrix/query/processor/proc/ExecSqlInstruction.java
   trunk/engine/src/main/java/com/metamatrix/query/processor/relational/SelectNode.java
   trunk/engine/src/main/java/com/metamatrix/query/resolver/util/ResolverUtil.java
   trunk/engine/src/main/java/com/metamatrix/query/resolver/util/ResolverVisitorUtil.java
   trunk/engine/src/main/java/com/metamatrix/query/rewriter/QueryRewriter.java
   trunk/engine/src/test/java/com/metamatrix/query/optimizer/TestOptimizer.java
   trunk/engine/src/test/java/com/metamatrix/query/processor/proc/TestProcedureProcessor.java
   trunk/engine/src/test/java/com/metamatrix/query/resolver/TestResolver.java
   trunk/engine/src/test/java/com/metamatrix/query/rewriter/TestQueryRewriter.java
Log:
TEIID-252 generalizing resolver handling of constant type conversion so that the rewriter can filter invalid comparisons.

Modified: trunk/engine/src/main/java/com/metamatrix/dqp/service/CustomizableTrackingService.java
===================================================================
--- trunk/engine/src/main/java/com/metamatrix/dqp/service/CustomizableTrackingService.java	2009-04-21 02:41:33 UTC (rev 812)
+++ trunk/engine/src/main/java/com/metamatrix/dqp/service/CustomizableTrackingService.java	2009-04-21 14:47:32 UTC (rev 813)
@@ -38,7 +38,6 @@
 import com.metamatrix.common.queue.WorkerPool;
 import com.metamatrix.common.queue.WorkerPoolFactory;
 import com.metamatrix.common.util.LogCommonConstants;
-import com.metamatrix.common.util.PropertiesUtils;
 import com.metamatrix.core.util.StringUtil;
 import com.metamatrix.dqp.DQPPlugin;
 import com.metamatrix.dqp.spi.CommandLoggerSPI;

Modified: trunk/engine/src/main/java/com/metamatrix/query/processor/proc/ExecDynamicSqlInstruction.java
===================================================================
--- trunk/engine/src/main/java/com/metamatrix/query/processor/proc/ExecDynamicSqlInstruction.java	2009-04-21 02:41:33 UTC (rev 812)
+++ trunk/engine/src/main/java/com/metamatrix/query/processor/proc/ExecDynamicSqlInstruction.java	2009-04-21 14:47:32 UTC (rev 813)
@@ -149,11 +149,6 @@
 	public void process(ProgramEnvironment env) throws BlockedException,
 			MetaMatrixComponentException, MetaMatrixProcessingException {
 
-		LogManager
-				.logTrace(
-						LogConstants.CTX_DQP,
-						"Processing ExecDynamicSqlInstruction as part of processing the update procedure"); //$NON-NLS-1$
-
 		ProcedureEnvironment procEnv = (ProcedureEnvironment) env;
         
 		VariableContext localContext = procEnv.getCurrentVariableContext();

Modified: trunk/engine/src/main/java/com/metamatrix/query/processor/proc/ExecSqlInstruction.java
===================================================================
--- trunk/engine/src/main/java/com/metamatrix/query/processor/proc/ExecSqlInstruction.java	2009-04-21 02:41:33 UTC (rev 812)
+++ trunk/engine/src/main/java/com/metamatrix/query/processor/proc/ExecSqlInstruction.java	2009-04-21 14:47:32 UTC (rev 813)
@@ -31,8 +31,6 @@
 import com.metamatrix.api.exception.MetaMatrixComponentException;
 import com.metamatrix.api.exception.MetaMatrixProcessingException;
 import com.metamatrix.common.buffer.BlockedException;
-import com.metamatrix.common.log.LogManager;
-import com.metamatrix.dqp.util.LogConstants;
 import com.metamatrix.query.processor.ProcessorPlan;
 import com.metamatrix.query.processor.program.ProgramEnvironment;
 import com.metamatrix.query.sql.symbol.GroupSymbol;
@@ -73,8 +71,6 @@
     public void process(ProgramEnvironment env)
         throws BlockedException, MetaMatrixComponentException, MetaMatrixProcessingException {
 
-        LogManager.logTrace(LogConstants.CTX_DQP, new Object[]{"Processing ExecSqlInstruction as part of processing the update procedure"}); //$NON-NLS-1$
-
         ProcedureEnvironment procEnv = (ProcedureEnvironment) env;
 
         if(intoGroup != null && intoGroup.isTempGroupSymbol()){

Modified: trunk/engine/src/main/java/com/metamatrix/query/processor/relational/SelectNode.java
===================================================================
--- trunk/engine/src/main/java/com/metamatrix/query/processor/relational/SelectNode.java	2009-04-21 02:41:33 UTC (rev 812)
+++ trunk/engine/src/main/java/com/metamatrix/query/processor/relational/SelectNode.java	2009-04-21 14:47:32 UTC (rev 813)
@@ -22,7 +22,6 @@
 
 package com.metamatrix.query.processor.relational;
 
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -58,6 +57,7 @@
         blockedOnPrepare = false;
         blockedBatch = null;
         blockedRow = 0;
+        evaluator = null;
     }
 
 	public void setCriteria(Criteria criteria) { 

Modified: trunk/engine/src/main/java/com/metamatrix/query/resolver/util/ResolverUtil.java
===================================================================
--- trunk/engine/src/main/java/com/metamatrix/query/resolver/util/ResolverUtil.java	2009-04-21 02:41:33 UTC (rev 812)
+++ trunk/engine/src/main/java/com/metamatrix/query/resolver/util/ResolverUtil.java	2009-04-21 14:47:32 UTC (rev 813)
@@ -175,7 +175,7 @@
         }
         
         if(canImplicitlyConvert(sourceTypeName, targetTypeName) 
-                        || (sourceExpression instanceof Constant && canConvertConstant(sourceTypeName, targetTypeName, (Constant)sourceExpression))) {
+                        || (sourceExpression instanceof Constant && convertConstant(sourceTypeName, targetTypeName, (Constant)sourceExpression) != null)) {
             return getConversion(sourceExpression, sourceTypeName, targetTypeName);
         }
 
@@ -183,29 +183,31 @@
         throw new QueryResolverException(ErrorMessageKeys.RESOLVER_0041, QueryPlugin.Util.getString(ErrorMessageKeys.RESOLVER_0041, new Object[] {targetTypeName, sourceExpression, sourceTypeName}));
     }
 
-    private static boolean canConvertConstant(String sourceTypeName,
+    public static Constant convertConstant(String sourceTypeName,
                                            String targetTypeName,
                                            Constant constant) throws QueryResolverException {
-        if (DataTypeManager.isTransformable(sourceTypeName, targetTypeName)) {
+        if (!DataTypeManager.isTransformable(sourceTypeName, targetTypeName)) {
+        	return null;
+        }
             
-            //try to get the converted constant, if this fails then it is not in a valid format
-            Constant result = getProperlyTypedConstant(constant.getValue(), DataTypeManager.getDataTypeClass(targetTypeName));
-            
-            if (DataTypeManager.DefaultDataTypes.STRING.equals(sourceTypeName)) {
-                return true;
-            }
-            
-            //for non-strings, ensure that the conversion is consistent
-            if (DataTypeManager.isTransformable(targetTypeName, sourceTypeName)) {
-                Constant reverse = getProperlyTypedConstant(result.getValue(), constant.getType());
-                
-                if (constant.equals(reverse)) {
-                    return true;
-                }
-            }
+        //try to get the converted constant, if this fails then it is not in a valid format
+        Constant result = getProperlyTypedConstant(constant.getValue(), DataTypeManager.getDataTypeClass(targetTypeName));
+        
+        if (DataTypeManager.DefaultDataTypes.STRING.equals(sourceTypeName)) {
+            return result;
         }
+        
+        //for non-strings, ensure that the conversion is consistent
+        if (!DataTypeManager.isTransformable(targetTypeName, sourceTypeName)) {
+        	return null;
+        }
+        Constant reverse = getProperlyTypedConstant(result.getValue(), constant.getType());
+        
+        if (constant.equals(reverse)) {
+            return result;
+        }
             
-        return false;
+        return null;
     }
 
     private static Expression getConversion(Expression sourceExpression,

Modified: trunk/engine/src/main/java/com/metamatrix/query/resolver/util/ResolverVisitorUtil.java
===================================================================
--- trunk/engine/src/main/java/com/metamatrix/query/resolver/util/ResolverVisitorUtil.java	2009-04-21 02:41:33 UTC (rev 812)
+++ trunk/engine/src/main/java/com/metamatrix/query/resolver/util/ResolverVisitorUtil.java	2009-04-21 14:47:32 UTC (rev 813)
@@ -45,7 +45,6 @@
 import com.metamatrix.query.metadata.QueryMetadataInterface;
 import com.metamatrix.query.metadata.StoredProcedureInfo;
 import com.metamatrix.query.metadata.TempMetadataID;
-import com.metamatrix.query.sql.ReservedWords;
 import com.metamatrix.query.sql.lang.BetweenCriteria;
 import com.metamatrix.query.sql.lang.CompareCriteria;
 import com.metamatrix.query.sql.lang.IsNullCriteria;
@@ -146,96 +145,60 @@
     	Expression leftExpression = ccrit.getLeftExpression();
     	Expression rightExpression = ccrit.getRightExpression();
     
-    	boolean leftIsAggregate = (leftExpression instanceof AggregateSymbol);
-    	boolean rightIsAggregate = (rightExpression instanceof AggregateSymbol);
-    
     	// Check typing between expressions
         ResolverUtil.setTypeIfReference(leftExpression, rightExpression.getType(), ccrit);
         ResolverUtil.setTypeIfReference(rightExpression, leftExpression.getType(), ccrit);
     
-    	if(! leftExpression.getType().equals(rightExpression.getType()) ) {
+    	if(leftExpression.getType().equals(rightExpression.getType()) ) {
+    		return;
+    	}
     
-    		boolean converted = false;
-    
-    		// Try to apply an implicit conversion from one side to the other
-    		String leftTypeName = DataTypeManager.getDataTypeName(leftExpression.getType());
-    		String rightTypeName = DataTypeManager.getDataTypeName(rightExpression.getType());
-    
-            // Special cases when right expression is a constant
-            if(rightExpression instanceof Constant) {
-                // Auto-convert constant string on right to expected type on left
-                try {
-                    ccrit.setRightExpression(ResolverUtil.convertExpression(rightExpression, rightTypeName, leftTypeName));
-                    converted = true;                                           
-                } catch (QueryResolverException qre) {
-                    //ignore
-                }
-            } 
-            
-            // Special cases when left expression is a constant
-            if(!converted && leftExpression instanceof Constant) {
-                // Auto-convert constant string on left to expected type on right
-                try {
-                    ccrit.setLeftExpression(ResolverUtil.convertExpression(leftExpression, leftTypeName, rightTypeName));
-                    converted = true;                                           
-                } catch (QueryResolverException qre) {
-                    //ignore
-                }
+		// Try to apply an implicit conversion from one side to the other
+		String leftTypeName = DataTypeManager.getDataTypeName(leftExpression.getType());
+		String rightTypeName = DataTypeManager.getDataTypeName(rightExpression.getType());
+
+        // Special cases when right expression is a constant
+        if(rightExpression instanceof Constant) {
+            // Auto-convert constant string on right to expected type on left
+            try {
+                ccrit.setRightExpression(ResolverUtil.convertExpression(rightExpression, rightTypeName, leftTypeName));
+                return;
+            } catch (QueryResolverException qre) {
+                //ignore
             }
-    
-            // Try to apply a conversion generically (except when aggregate involved)
-            if(!converted) {
-    			// Try to apply conversion to left if not aggregate
-    			if(! leftIsAggregate && ResolverUtil.canImplicitlyConvert(leftTypeName, rightTypeName)) {
-    				ccrit.setLeftExpression(ResolverUtil.convertExpression(leftExpression, leftTypeName, rightTypeName) );
-    				converted = true;
-    			}
-    
-    			// Try to apply conversion to right if left not converted and right not aggregate
-    			if(! converted && ! rightIsAggregate && ResolverUtil.canImplicitlyConvert(rightTypeName, leftTypeName)) {
-    				ccrit.setRightExpression(ResolverUtil.convertExpression(rightExpression, rightTypeName, leftTypeName) );
-    				converted = true;
-    			}
-    	    }
-    
-    	    // Could not resolve - try last ditch effort to convert agg expression or throw exception
-    		if(! converted) {
-                if(rightIsAggregate) {
-                    if(leftIsAggregate) {
-                        // both are aggs of differing types - can't reconcile
-                        throw new QueryResolverException(ErrorMessageKeys.RESOLVER_0064, QueryPlugin.Util.getString(ErrorMessageKeys.RESOLVER_0064));
-                    }
-                    AggregateSymbol aggRight = (AggregateSymbol) rightExpression;
-                    String aggFunc = aggRight.getAggregateFunction();
-                    // Convert expression inside MIN/MAX function rather than converting aggregate
-                    if((aggFunc.equals(ReservedWords.MIN) || aggFunc.equals(ReservedWords.MAX)) &&
-                        ResolverUtil.canImplicitlyConvert(rightTypeName, leftTypeName)) {
-                        aggRight.setExpression(ResolverUtil.convertExpression(aggRight.getExpression(), rightTypeName, leftTypeName) );
-                    } else {
-                        throw new QueryResolverException(ErrorMessageKeys.RESOLVER_0028, QueryPlugin.Util.getString(ErrorMessageKeys.RESOLVER_0028));
-                    }
-                } else if(leftIsAggregate) {
-                    AggregateSymbol aggLeft = (AggregateSymbol) leftExpression;
-                    String aggFunc = aggLeft.getAggregateFunction();
-                    // Convert expression inside MIN/MAX function rather than converting aggregate
-                    if((aggFunc.equals(ReservedWords.MIN) || aggFunc.equals(ReservedWords.MAX)) &&
-                        ResolverUtil.canImplicitlyConvert(leftTypeName, rightTypeName)) {
-                            aggLeft.setExpression(ResolverUtil.convertExpression(aggLeft.getExpression(), leftTypeName, rightTypeName) );
-                    } else {
-                        throw new QueryResolverException(ErrorMessageKeys.RESOLVER_0028, QueryPlugin.Util.getString(ErrorMessageKeys.RESOLVER_0028));
-                    }
-                } else {
-                	String commonType = ResolverUtil.getCommonType(new String[] {leftTypeName, rightTypeName});
-                	
-                	if (commonType == null) {
-	                    // Neither are aggs, but types can't be reconciled
-	                    throw new QueryResolverException(ErrorMessageKeys.RESOLVER_0027, QueryPlugin.Util.getString(ErrorMessageKeys.RESOLVER_0027, new Object[] { leftTypeName, rightTypeName, ccrit }));
-                	}
-                	ccrit.setLeftExpression(ResolverUtil.convertExpression(leftExpression, leftTypeName, commonType) );
-    				ccrit.setRightExpression(ResolverUtil.convertExpression(rightExpression, rightTypeName, commonType) );
-                }
-    		}
+        } 
+        
+        // Special cases when left expression is a constant
+        if(leftExpression instanceof Constant) {
+            // Auto-convert constant string on left to expected type on right
+            try {
+                ccrit.setLeftExpression(ResolverUtil.convertExpression(leftExpression, leftTypeName, rightTypeName));
+                return;                                           
+            } catch (QueryResolverException qre) {
+                //ignore
+            }
+        }
+
+        // Try to apply a conversion generically
+		
+        if(ResolverUtil.canImplicitlyConvert(leftTypeName, rightTypeName)) {
+			ccrit.setLeftExpression(ResolverUtil.convertExpression(leftExpression, leftTypeName, rightTypeName) );
+			return;
+		}
+
+		if(ResolverUtil.canImplicitlyConvert(rightTypeName, leftTypeName)) {
+			ccrit.setRightExpression(ResolverUtil.convertExpression(rightExpression, rightTypeName, leftTypeName) );
+			return;
+	    }
+
+    	String commonType = ResolverUtil.getCommonType(new String[] {leftTypeName, rightTypeName});
+    	
+    	if (commonType == null) {
+            // Neither are aggs, but types can't be reconciled
+            throw new QueryResolverException(ErrorMessageKeys.RESOLVER_0027, QueryPlugin.Util.getString(ErrorMessageKeys.RESOLVER_0027, new Object[] { leftTypeName, rightTypeName, ccrit }));
     	}
+    	ccrit.setLeftExpression(ResolverUtil.convertExpression(leftExpression, leftTypeName, commonType) );
+		ccrit.setRightExpression(ResolverUtil.convertExpression(rightExpression, rightTypeName, commonType) );
     }
 
     static void resolveMatchCriteria(MatchCriteria mcrit)

Modified: trunk/engine/src/main/java/com/metamatrix/query/rewriter/QueryRewriter.java
===================================================================
--- trunk/engine/src/main/java/com/metamatrix/query/rewriter/QueryRewriter.java	2009-04-21 02:41:33 UTC (rev 812)
+++ trunk/engine/src/main/java/com/metamatrix/query/rewriter/QueryRewriter.java	2009-04-21 14:47:32 UTC (rev 813)
@@ -54,7 +54,6 @@
 import com.metamatrix.api.exception.query.QueryValidatorException;
 import com.metamatrix.common.buffer.BlockedException;
 import com.metamatrix.common.types.DataTypeManager;
-import com.metamatrix.common.types.Transform;
 import com.metamatrix.common.util.TimestampWithTimezone;
 import com.metamatrix.core.MetaMatrixRuntimeException;
 import com.metamatrix.core.util.Assertion;
@@ -144,7 +143,6 @@
 import com.metamatrix.query.sql.visitor.EvaluateExpressionVisitor;
 import com.metamatrix.query.sql.visitor.ExpressionMappingVisitor;
 import com.metamatrix.query.sql.visitor.PredicateCollectorVisitor;
-import com.metamatrix.query.sql.visitor.ValueIteratorProviderCollectorVisitor;
 import com.metamatrix.query.util.CommandContext;
 import com.metamatrix.query.util.ErrorMessageKeys;
 
@@ -1458,9 +1456,7 @@
      * <code>typedColumn = 5 </code> 
      * 
      * Right expression has already been checked to be a Constant, left expression to be
-     * a function.  Function is known to be "convert" or "cast".  The scope of this change
-     * will be limited to the case where the left expression is attempting to convert to
-     * 'string'.  
+     * a function.  Function is known to be "convert" or "cast".
      * 
      * @param crit CompareCriteria
      * @return same Criteria instance (possibly optimized)
@@ -1474,44 +1470,31 @@
         
         String leftExprTypeName = DataTypeManager.getDataTypeName(leftExpr.getType());
         
-        if (leftExpr.getType() == DataTypeManager.DefaultDataClasses.NULL || rightConstant.getType() != DataTypeManager.DefaultDataClasses.STRING) {
+        if (leftExpr.getType() == DataTypeManager.DefaultDataClasses.NULL) {
             return crit;
         }
         
-        Transform transform = DataTypeManager.getTransform(leftExprTypeName, DataTypeManager.DefaultDataTypes.STRING);
-        
-        if (transform.isNarrowing()) {
-            return crit;
+        Constant result = null;
+        try {
+            result = ResolverUtil.convertConstant(DataTypeManager.getDataTypeName(rightConstant.getType()), leftExprTypeName, rightConstant);
+        } catch(QueryResolverException e) {
+            
         }
-                
-        FunctionLibrary funcLib = FunctionLibraryManager.getFunctionLibrary();
         
-        FunctionDescriptor descriptor = funcLib.findTypedConversionFunction(rightConstant.getType(), leftExpr.getType());
-            
-        if(descriptor == null){
-            return crit;
-        }            
-        
-        try {
-            Object result = funcLib.invokeFunction(
-                descriptor, new Object[] { rightConstant.getValue(), leftExprTypeName } );
-            crit.setRightExpression(new Constant(result, descriptor.getReturnType()));
-            crit.setLeftExpression(leftExpr);
-        } catch(InvalidFunctionException e) {
-            String errorMsg = QueryExecPlugin.Util.getString("QueryRewriter.criteriaError", crit); //$NON-NLS-1$
-            throw new QueryValidatorException(e, errorMsg);
-        } catch(FunctionExecutionException e) {
-            if (crit.getOperator() == CompareCriteria.EQ) {
+        if (result == null) {
+        	if (crit.getOperator() == CompareCriteria.EQ) {
                 return FALSE_CRITERIA;
             }
             return TRUE_CRITERIA;
         }
+        
+        crit.setRightExpression(result);
+        crit.setLeftExpression(leftExpr);
 
         if (leftExpr instanceof Function) {
             return simplifyWithInverse(crit);
         }
         
-        // Fall through and return original criteria
         return crit;
     }
 
@@ -1541,61 +1524,49 @@
      * @throws QueryValidatorException
      * @since 4.2
      */
-    private static SetCriteria simplifyConvertFunction(SetCriteria crit) throws QueryValidatorException {
-
-
+    private static Criteria simplifyConvertFunction(SetCriteria crit, Command procCommand, CommandContext context, QueryMetadataInterface metadata) throws QueryValidatorException {
         Function leftFunction = (Function) crit.getExpression();
         Expression leftExpr = leftFunction.getArgs()[0];
-        Expression targetType = leftFunction.getArgs()[1];
         String leftExprTypeName = DataTypeManager.getDataTypeName(leftExpr.getType());
         
         Iterator i = crit.getValues().iterator();
         Collection newValues = new ArrayList(crit.getNumberOfValues());
-        boolean success = true;
         
-        try {
-            while (i.hasNext()) {
-                Object next = i.next();
-                if (next instanceof Constant) {
-    
-                    Constant rightConstant = (Constant) next;
-                    Class rightConstantType = rightConstant.getType();
-                    
-                    
-                    if(targetType.getType().equals(rightConstantType) && targetType.getType().equals(DataTypeManager.DefaultDataClasses.STRING)) {
-                        FunctionLibrary funcLib = FunctionLibraryManager.getFunctionLibrary();
-                        FunctionDescriptor descriptor = funcLib.findTypedConversionFunction(rightConstantType, leftExpr.getType());
-                        
-                        if(descriptor != null){
-                            Object result = funcLib.invokeFunction(
-                                descriptor, new Object[] { rightConstant.getValue(), leftExprTypeName } );
-                            newValues.add(new Constant(result, descriptor.getReturnType()));
-                        } else {
-                            success = false;
-                            break;
-                        }
-                    } else {
-                        success = false;
-                        break;
-                    }
-                    
-                } else {
-                    success = false;
-                    break;
-                }
+        boolean convertedAll = true;
+        boolean removedSome = false;
+        while (i.hasNext()) {
+            Object next = i.next();
+            if (!(next instanceof Constant)) {
+            	convertedAll = false;
+            	continue;
             }
-        } catch(InvalidFunctionException e) {
-            String errorMsg = QueryExecPlugin.Util.getString("QueryRewriter.criteriaError", crit); //$NON-NLS-1$
-            throw new QueryValidatorException(e, errorMsg);
-        } catch(FunctionExecutionException e) {
-            return crit;
+
+            Constant rightConstant = (Constant) next;
+            
+            Constant result = null;
+            try {
+                result = ResolverUtil.convertConstant(DataTypeManager.getDataTypeName(rightConstant.getType()), leftExprTypeName, rightConstant);
+            } catch(QueryResolverException e) {
+                
+            }
+            
+            if (result != null) {
+            	newValues.add(result);
+            } else {
+            	removedSome = true;
+            	i.remove();
+            }
         }
         
-        if (success) {
-            crit.setExpression(leftExpr);
-            crit.setValues(newValues);
+        if (!convertedAll) {
+        	if (!removedSome) {
+        		return crit; //just return as is
+        	}
+        	return rewriteCriteria(crit, procCommand, context, metadata);
         }
-        return crit;
+        crit.setExpression(leftExpr);
+        crit.setValues(newValues);
+        return rewriteCriteria(crit, procCommand, context, metadata);
     }
         
     private static CompareCriteria simplifyParseFormatFunction(CompareCriteria crit) throws QueryValidatorException {
@@ -1943,9 +1914,7 @@
         
         if (newVals.size() == 1) {
             Expression value = (Expression)newVals.iterator().next();
-            if (ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(value).size() == 0) {
-                return rewriteCriteria(new CompareCriteria(criteria.getExpression(), criteria.isNegated()?CompareCriteria.NE:CompareCriteria.EQ, value), procCommand, context, metadata);
-            }
+            return rewriteCriteria(new CompareCriteria(criteria.getExpression(), criteria.isNegated()?CompareCriteria.NE:CompareCriteria.EQ, value), procCommand, context, metadata);
         } else if (newVals.size() == 0) {
             return FALSE_CRITERIA;
         }
@@ -1954,13 +1923,7 @@
             
             Function leftFunction = (Function)criteria.getExpression();
             if(FunctionLibrary.isConvert(leftFunction)) {
-                Iterator i = criteria.getValues().iterator();
-                while (i.hasNext()) {
-                    if (!(i.next() instanceof Constant)) {
-                        return criteria;
-                    }
-                }
-                criteria = simplifyConvertFunction(criteria);        
+                return simplifyConvertFunction(criteria, procCommand, context, metadata);        
             }
         }
 

Modified: trunk/engine/src/test/java/com/metamatrix/query/optimizer/TestOptimizer.java
===================================================================
--- trunk/engine/src/test/java/com/metamatrix/query/optimizer/TestOptimizer.java	2009-04-21 02:41:33 UTC (rev 812)
+++ trunk/engine/src/test/java/com/metamatrix/query/optimizer/TestOptimizer.java	2009-04-21 14:47:32 UTC (rev 813)
@@ -6976,7 +6976,7 @@
         FakeMetadataFacade metadata = FakeMetadataFactory.exampleBQTCached();
          
         ProcessorPlan plan = helpPlan(
-            "SELECT ClobValue from LOB.LobTbl WHERE convert(ClobValue, string) = '1'",  //$NON-NLS-1$
+            "SELECT ClobValue from LOB.LobTbl WHERE convert(ClobValue, string) = ?",  //$NON-NLS-1$
             metadata,
             null, capFinder,
             new String[] {"SELECT ClobValue FROM LOB.LobTbl"}, //$NON-NLS-1$

Modified: trunk/engine/src/test/java/com/metamatrix/query/processor/proc/TestProcedureProcessor.java
===================================================================
--- trunk/engine/src/test/java/com/metamatrix/query/processor/proc/TestProcedureProcessor.java	2009-04-21 02:41:33 UTC (rev 812)
+++ trunk/engine/src/test/java/com/metamatrix/query/processor/proc/TestProcedureProcessor.java	2009-04-21 14:47:32 UTC (rev 813)
@@ -262,9 +262,9 @@
             elementSymbols,
             
             new List[] { 
-                Arrays.asList( new Object[] { "First", new Integer(5), new Boolean(true), new Double(1.003)} ), //$NON-NLS-1$
-                Arrays.asList( new Object[] { "Second", new Integer(15), new Boolean(true), new Double(2.003)} ), //$NON-NLS-1$
-                Arrays.asList( new Object[] { "Third", new Integer(51), new Boolean(true), new Double(3.003)} ) //$NON-NLS-1$
+                Arrays.asList( new Object[] { "First", new Short((short)5), new Boolean(true), new Double(1.003)} ), //$NON-NLS-1$
+                Arrays.asList( new Object[] { "Second", new Short((short)15), new Boolean(true), new Double(2.003)} ), //$NON-NLS-1$
+                Arrays.asList( new Object[] { "Third", new Short((short)51), new Boolean(true), new Double(3.003)} ) //$NON-NLS-1$
                 } );
         
         return dataMgr;
@@ -1821,7 +1821,7 @@
         ProcessorPlan plan = getProcedurePlan(userUpdateStr, metadata);
     	
         // Create expected results
-        List[] expected = new List[] { Arrays.asList(new Object[] { "First", new Integer(5)})};           //$NON-NLS-1$      
+        List[] expected = new List[] { Arrays.asList(new Object[] { "First", new Short((short)5)})};           //$NON-NLS-1$      
         
         helpTestProcess(plan, expected, dataMgr);
      }

Modified: trunk/engine/src/test/java/com/metamatrix/query/resolver/TestResolver.java
===================================================================
--- trunk/engine/src/test/java/com/metamatrix/query/resolver/TestResolver.java	2009-04-21 02:41:33 UTC (rev 812)
+++ trunk/engine/src/test/java/com/metamatrix/query/resolver/TestResolver.java	2009-04-21 14:47:32 UTC (rev 813)
@@ -59,7 +59,6 @@
 import com.metamatrix.query.parser.QueryParser;
 import com.metamatrix.query.resolver.util.BindVariableVisitor;
 import com.metamatrix.query.resolver.util.ResolverUtil;
-import com.metamatrix.query.resolver.util.ResolverVisitorUtil;
 import com.metamatrix.query.sql.LanguageObject;
 import com.metamatrix.query.sql.ProcedureReservedWords;
 import com.metamatrix.query.sql.lang.BatchedUpdateCommand;
@@ -876,7 +875,7 @@
     }
 
     public void testHavingWithAggsOfDifferentTypes() {
-        helpResolveException("SELECT * FROM pm1.g1 GROUP BY e4 HAVING MIN(e1) = MIN(e2)"); //$NON-NLS-1$
+        helpResolve("SELECT * FROM pm1.g1 GROUP BY e4 HAVING MIN(e1) = MIN(e2)"); //$NON-NLS-1$
     }
     
     public void testCaseInGroupBy() {

Modified: trunk/engine/src/test/java/com/metamatrix/query/rewriter/TestQueryRewriter.java
===================================================================
--- trunk/engine/src/test/java/com/metamatrix/query/rewriter/TestQueryRewriter.java	2009-04-21 02:41:33 UTC (rev 812)
+++ trunk/engine/src/test/java/com/metamatrix/query/rewriter/TestQueryRewriter.java	2009-04-21 14:47:32 UTC (rev 813)
@@ -22,8 +22,6 @@
 
 package com.metamatrix.query.rewriter;
 
-import java.math.BigDecimal;
-import java.math.BigInteger;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Iterator;
@@ -62,7 +60,6 @@
 import com.metamatrix.query.sql.symbol.Constant;
 import com.metamatrix.query.sql.symbol.ElementSymbol;
 import com.metamatrix.query.sql.symbol.ExpressionSymbol;
-import com.metamatrix.query.sql.symbol.Function;
 import com.metamatrix.query.sql.symbol.GroupSymbol;
 import com.metamatrix.query.sql.symbol.SingleElementSymbol;
 import com.metamatrix.query.sql.visitor.CorrelatedReferenceCollectorVisitor;
@@ -268,7 +265,7 @@
     }
     
     public void testRewriteCrit6() {
-        helpTestRewriteCriteria("1 = convert(pm1.g1.e1,integer) + 10", "convert(pm1.g1.e1, integer) = -9"); //$NON-NLS-1$ //$NON-NLS-2$
+        helpTestRewriteCriteria("1 = convert(pm1.g1.e1,integer) + 10", "pm1.g1.e1 = '-9'"); //$NON-NLS-1$ //$NON-NLS-2$
     } 
     
     public void testRewriteCrit7() {
@@ -525,7 +522,12 @@
 
     public void testRewriteCrit_formatLong() {
         helpTestRewriteCriteria("formatLong(convert(pm1.g1.e2, long), '#,##0') = '1,234,567,890,123'", //$NON-NLS-1$
-                                "convert(pm1.g1.e2, long) = 1234567890123" );         //$NON-NLS-1$
+                                "1 = 0" );         //$NON-NLS-1$
+    }
+    
+    public void testRewriteCrit_formatLong1() {
+        helpTestRewriteCriteria("formatLong(convert(pm1.g1.e2, long), '#,##0') = '1,234,567,890'", //$NON-NLS-1$
+                                "pm1.g1.e2 = 1234567890" );         //$NON-NLS-1$
     }
     
     public void testRewriteCrit_formatTimestampInvert() { 
@@ -542,28 +544,22 @@
         helpTestRewriteCriteria(original, expected);
     } 
 
-    public void testRewriteCrit_formatBigInteger() {
+    public void testRewriteCrit_formatBigInteger() throws Exception {
         String original = "formatBigInteger(convert(pm1.g1.e2, biginteger), '#,##0') = '1,234,567,890'"; //$NON-NLS-1$
-        String expected = "convert(pm1.g1.e2, biginteger) = 1234567890"; //$NON-NLS-1$
+        String expected = "pm1.g1.e2 = 1234567890"; //$NON-NLS-1$
         
         FakeMetadataFacade metadata = FakeMetadataFactory.example1Cached(); 
         Criteria origCrit = parseCriteria(original, metadata);
         Criteria expectedCrit = parseCriteria(expected, metadata);
-        ((CompareCriteria)expectedCrit).setRightExpression(new Constant(new BigInteger("1234567890"))); //$NON-NLS-1$
         
         // rewrite
-        try { 
-            Criteria actual = QueryRewriter.rewriteCriteria(origCrit, null, null, null);
-            assertEquals("Did not rewrite correctly: ", expectedCrit, actual); //$NON-NLS-1$
-        } catch(QueryValidatorException e) { 
-            e.printStackTrace();
-            fail("Exception during rewriting (" + e.getClass().getName() + "): " + e.getMessage());     //$NON-NLS-1$ //$NON-NLS-2$
-        }
+        Criteria actual = QueryRewriter.rewriteCriteria(origCrit, null, null, null);
+        assertEquals("Did not rewrite correctly: ", expectedCrit, actual); //$NON-NLS-1$
     }
 
     public void testRewriteCrit_formatFloat() throws Exception {
-        String original = "formatFloat(convert(pm1.g1.e2, float), '#,##0.###') = '1,234.123'"; //$NON-NLS-1$
-        String expected = "convert(pm1.g1.e2, float) = 1234.123"; //$NON-NLS-1$
+        String original = "formatFloat(convert(pm1.g1.e4, float), '#,##0.###') = '1,234.123'"; //$NON-NLS-1$
+        String expected = "pm1.g1.e4 = 1234.123046875"; //$NON-NLS-1$
         
         FakeMetadataFacade metadata = FakeMetadataFactory.example1Cached(); 
         Criteria origCrit = parseCriteria(original, metadata);
@@ -571,15 +567,11 @@
         // rewrite
         Criteria actual = QueryRewriter.rewriteCriteria(origCrit, null, null, null);
         assertEquals("Did not rewrite correctly: ", expected, actual.toString()); //$NON-NLS-1$
-        
-        Function left = (Function)((CompareCriteria)actual).getLeftExpression();
-        
-        assertFalse(left.isImplicit());
     }
 
-    public void testRewriteCrit_formatDouble() {
-        String original = "formatDouble(convert(pm1.g1.e2, double), '$#,##0.00') = '$1,234.50'"; //$NON-NLS-1$
-        String expected = "convert(pm1.g1.e2, double) = 1234.5"; //$NON-NLS-1$
+    public void testRewriteCrit_formatDouble() throws Exception {
+        String original = "formatDouble(convert(pm1.g1.e4, double), '$#,##0.00') = '$1,234.50'"; //$NON-NLS-1$
+        String expected = "pm1.g1.e4 = '1234.5'"; //$NON-NLS-1$
         
         FakeMetadataFacade metadata = FakeMetadataFactory.example1Cached(); 
         Criteria origCrit = parseCriteria(original, metadata);
@@ -587,32 +579,21 @@
         ((CompareCriteria)expectedCrit).setRightExpression(new Constant(new Double(1234.5)));
         
         // rewrite
-        try { 
-            Criteria actual = QueryRewriter.rewriteCriteria(origCrit, null, null, null);
-            assertEquals("Did not rewrite correctly: ", expectedCrit, actual); //$NON-NLS-1$
-        } catch(QueryValidatorException e) { 
-            e.printStackTrace();
-            fail("Exception during rewriting (" + e.getClass().getName() + "): " + e.getMessage());     //$NON-NLS-1$ //$NON-NLS-2$
-        }
+        Criteria actual = QueryRewriter.rewriteCriteria(origCrit, null, null, null);
+        assertEquals("Did not rewrite correctly: ", expectedCrit, actual); //$NON-NLS-1$
     }
 
-    public void testRewriteCrit_formatBigDecimal() {
-        String original = "formatBigDecimal(convert(pm1.g1.e2, bigdecimal), '#,##0.###') = convert(1234.5, bigdecimal)"; //$NON-NLS-1$
-        String expected = "convert(pm1.g1.e2, bigdecimal) = 1234.5"; //$NON-NLS-1$
+    public void testRewriteCrit_formatBigDecimal() throws Exception {
+        String original = "formatBigDecimal(convert(pm1.g1.e4, bigdecimal), '#,##0.###') = convert(1234.5, bigdecimal)"; //$NON-NLS-1$
+        String expected = "pm1.g1.e4 = 1234.5"; //$NON-NLS-1$
         
         FakeMetadataFacade metadata = FakeMetadataFactory.example1Cached(); 
         Criteria origCrit = parseCriteria(original, metadata);
         Criteria expectedCrit = parseCriteria(expected, metadata);
-        ((CompareCriteria)expectedCrit).setRightExpression(new Constant(new BigDecimal("1234.5"))); //$NON-NLS-1$
         
         // rewrite
-        try { 
-            Criteria actual = QueryRewriter.rewriteCriteria(origCrit, null, null, null);
-            assertEquals("Did not rewrite correctly: ", expectedCrit, actual); //$NON-NLS-1$
-        } catch(QueryValidatorException e) { 
-            e.printStackTrace();
-            fail("Exception during rewriting (" + e.getClass().getName() + "): " + e.getMessage());     //$NON-NLS-1$ //$NON-NLS-2$
-        }
+        Criteria actual = QueryRewriter.rewriteCriteria(origCrit, null, null, null);
+        assertEquals("Did not rewrite correctly: ", expectedCrit, actual); //$NON-NLS-1$
     }
     
     public void testRewriteCritTimestampDiffDate1() {
@@ -1000,7 +981,7 @@
 		
 		String rewritProc = "CREATE PROCEDURE\n"; //$NON-NLS-1$
 		rewritProc = rewritProc + "BEGIN\n"; //$NON-NLS-1$
-		rewritProc = rewritProc + "SELECT e2 FROM pm1.g1 WHERE convert(sqrt(pm1.g1.e2), integer) = 10;\n"; //$NON-NLS-1$
+		rewritProc = rewritProc + "SELECT e2 FROM pm1.g1 WHERE sqrt(pm1.g1.e2) = 10.0;\n"; //$NON-NLS-1$
 		rewritProc = rewritProc + "END"; //$NON-NLS-1$
 		
 		String procReturned = this.getReWrittenProcedure(procedure, userQuery, 
@@ -1118,7 +1099,7 @@
 		String rewritProc = "CREATE PROCEDURE\n"; //$NON-NLS-1$
 		rewritProc = rewritProc + "BEGIN\n";		 //$NON-NLS-1$
 		rewritProc = rewritProc + "DECLARE integer var1;\n"; //$NON-NLS-1$
-		rewritProc = rewritProc + "SELECT pm1.g1.e2 FROM pm1.g1 WHERE (CONCAT(e1, 'z') = CONCAT(CONCAT(e1, 'z'), 'y')) AND (convert(CONCAT(e1, 'k'), integer) = 1);\n"; //$NON-NLS-1$
+		rewritProc = rewritProc + "SELECT pm1.g1.e2 FROM pm1.g1 WHERE (CONCAT(e1, 'z') = CONCAT(CONCAT(e1, 'z'), 'y')) AND (CONCAT(e1, 'k') = '1');\n"; //$NON-NLS-1$
 		rewritProc = rewritProc + "END"; //$NON-NLS-1$
 
 		String procReturned = this.getReWrittenProcedure(procedure, userQuery, 
@@ -1602,14 +1583,12 @@
         helpTestRewriteCriteria("convert(pm1.g1.e1, string) = 'x'", "pm1.g1.e1 = 'x'"); //$NON-NLS-1$ //$NON-NLS-2$
     }    
 
-    /** Test some other type besides 'string' has no effect */
     public void testRewriteCase1954d() {
-        helpTestRewriteCriteria("convert(pm1.g1.e1, timestamp) = {ts '2005-01-03 00:00:00.0'}", "convert(pm1.g1.e1, timestamp) = {ts '2005-01-03 00:00:00.0'}"); //$NON-NLS-1$ //$NON-NLS-2$
+        helpTestRewriteCriteria("convert(pm1.g1.e1, timestamp) = {ts '2005-01-03 00:00:00.0'}", "pm1.g1.e1 = '2005-01-03 00:00:00.0'"); //$NON-NLS-1$ //$NON-NLS-2$
     }
 
-    /** Test some other type besides 'string' has no effect */
     public void testRewriteCase1954e() {
-        helpTestRewriteCriteria("convert(pm1.g1.e4, integer) = 2", "convert(pm1.g1.e4, integer) = 2"); //$NON-NLS-1$ //$NON-NLS-2$
+        helpTestRewriteCriteria("convert(pm1.g1.e4, integer) = 2", "pm1.g1.e4 = 2.0"); //$NON-NLS-1$ //$NON-NLS-2$
     }
 
     /** Check that this fails, x is not convertable to an int */
@@ -1627,7 +1606,7 @@
     }    
 
     public void testRewriteCase1954SetA() {
-        helpTestRewriteCriteria("convert(pm1.g1.e2, string) in ('2', 'x')", "convert(pm1.g1.e2, string) in ('2', 'x')"); //$NON-NLS-1$ //$NON-NLS-2$
+        helpTestRewriteCriteria("convert(pm1.g1.e2, string) in ('2', 'x')", "pm1.g1.e2 = 2"); //$NON-NLS-1$ //$NON-NLS-2$
     }    
     
     public void testRewriteCase1954SetB() {




More information about the teiid-commits mailing list