[teiid-commits] teiid SVN: r2875 - in trunk/connectors/translator-jdbc/src: test/java/org/teiid/translator/jdbc/db2 and 1 other directory.

teiid-commits at lists.jboss.org teiid-commits at lists.jboss.org
Tue Jan 25 15:19:08 EST 2011


Author: shawkins
Date: 2011-01-25 15:19:07 -0500 (Tue, 25 Jan 2011)
New Revision: 2875

Modified:
   trunk/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/db2/SubstringFunctionModifier.java
   trunk/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/db2/TestDB2SqlTranslator.java
Log:
TEIID-1281 correcting the max length

Modified: trunk/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/db2/SubstringFunctionModifier.java
===================================================================
--- trunk/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/db2/SubstringFunctionModifier.java	2011-01-24 22:33:44 UTC (rev 2874)
+++ trunk/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/db2/SubstringFunctionModifier.java	2011-01-25 20:19:07 UTC (rev 2875)
@@ -49,7 +49,7 @@
 		if (function.getParameters().size() != 3) {
 			return null;
 		}
-		//case when length < 0 then null when length < LENGTH(string) - start + 1 then exp else LENGTH(string) - start + 1
+		//case when length > LENGTH(string) - start + 1 then LENGTH(string) - start + 1 case when length > 0 then length end
 		Expression length = function.getParameters().get(2);
 		List<SearchedWhenClause> clauses = new ArrayList<SearchedWhenClause>(2);
 		Boolean isNegative = null;
@@ -60,13 +60,6 @@
 				isNegative = value < 0;
 			}
 		}
-		if (isNegative == null) {
-			clauses.add(new SearchedWhenClause(new Comparison(length, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.LT), new Literal(null, TypeFacility.RUNTIME_TYPES.INTEGER)));
-		} else if (isNegative) {
-			//TODO: could be done in the rewriter
-			function.getParameters().set(2, null);
-			return null;
-		} 
 		Expression maxLength = new Function(
 				SourceSystemFunctions.SUBTRACT_OP,
 				Arrays.asList(new Function(
@@ -74,15 +67,25 @@
 								Arrays.asList(function.getParameters().get(0)),
 								TypeFacility.RUNTIME_TYPES.INTEGER),
 							new Function(
-								SourceSystemFunctions.ADD_OP,
+								SourceSystemFunctions.SUBTRACT_OP,
 								Arrays.asList(
 										function.getParameters().get(1),
 										new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)),
 							    TypeFacility.RUNTIME_TYPES.INTEGER)),
 				TypeFacility.RUNTIME_TYPES.INTEGER);
-		clauses.add(new SearchedWhenClause(new Comparison(length, maxLength, Operator.LE), length));
+		clauses.add(new SearchedWhenClause(new Comparison(length, maxLength, Operator.GT), maxLength));
+		Expression defaultExpr = null;
+		if (isNegative == null) {
+			clauses.add(new SearchedWhenClause(new Comparison(length, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.GT), length));
+		} else if (isNegative) {
+			//TODO: could be done in the rewriter
+			function.getParameters().set(2, null);
+			return null;
+		} else {
+			defaultExpr = length;
+		}
 		SearchedCase sc = new SearchedCase(clauses, 
-				maxLength, TypeFacility.RUNTIME_TYPES.INTEGER);
+				defaultExpr, TypeFacility.RUNTIME_TYPES.INTEGER);
 		function.getParameters().set(2, sc);
 		return null;
 	}

Modified: trunk/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/db2/TestDB2SqlTranslator.java
===================================================================
--- trunk/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/db2/TestDB2SqlTranslator.java	2011-01-24 22:33:44 UTC (rev 2874)
+++ trunk/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/db2/TestDB2SqlTranslator.java	2011-01-25 20:19:07 UTC (rev 2875)
@@ -245,7 +245,7 @@
     
     @Test public void testSubstring() throws Exception {
         String input = "SELECT substring(STRINGNUM, 2, 10) FROM BQT1.SMALLA"; //$NON-NLS-1$
-        String output = "SELECT substr(SmallA.StringNum, 2, CASE WHEN 10 <= (length(SmallA.StringNum) - (2 + 1)) THEN 10 ELSE (length(SmallA.StringNum) - (2 + 1)) END) FROM SmallA";  //$NON-NLS-1$
+        String output = "SELECT substr(SmallA.StringNum, 2, CASE WHEN 10 > (length(SmallA.StringNum) - (2 - 1)) THEN (length(SmallA.StringNum) - (2 - 1)) ELSE 10 END) FROM SmallA";  //$NON-NLS-1$
 
         TranslationHelper.helpTestVisitor(TranslationHelper.BQT_VDB,
                 input, output, 
@@ -254,7 +254,7 @@
     
     @Test public void testSubstring1() throws Exception {
         String input = "SELECT substring(STRINGNUM, 2, intnum) FROM BQT1.SMALLA"; //$NON-NLS-1$
-        String output = "SELECT substr(SmallA.StringNum, 2, CASE WHEN SmallA.IntNum < 0 THEN NULL WHEN SmallA.IntNum <= (length(SmallA.StringNum) - (2 + 1)) THEN SmallA.IntNum ELSE (length(SmallA.StringNum) - (2 + 1)) END) FROM SmallA";  //$NON-NLS-1$
+        String output = "SELECT substr(SmallA.StringNum, 2, CASE WHEN SmallA.IntNum > (length(SmallA.StringNum) - (2 - 1)) THEN (length(SmallA.StringNum) - (2 - 1)) WHEN SmallA.IntNum > 0 THEN SmallA.IntNum END) FROM SmallA";  //$NON-NLS-1$
 
         TranslationHelper.helpTestVisitor(TranslationHelper.BQT_VDB,
                 input, output, 



More information about the teiid-commits mailing list