[teiid-commits] teiid SVN: r3155 - in branches/7.4.x/connectors/translator-jdbc/src: test/java/org/teiid/translator/jdbc/teradata and 1 other directory.
teiid-commits at lists.jboss.org
teiid-commits at lists.jboss.org
Thu May 5 13:35:15 EDT 2011
Author: rareddy
Date: 2011-05-05 13:35:15 -0400 (Thu, 05 May 2011)
New Revision: 3155
Modified:
branches/7.4.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/teradata/TeradataExecutionFactory.java
branches/7.4.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/teradata/TeradataSQLConversionVisitor.java
branches/7.4.x/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/teradata/TestTeradataTranslator.java
Log:
TEIID-1495: Handle negation in the IN criteria, specify supported functions when they have function modifiers and some time conversion utilities are corrected.
Modified: branches/7.4.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/teradata/TeradataExecutionFactory.java
===================================================================
--- branches/7.4.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/teradata/TeradataExecutionFactory.java 2011-05-04 20:38:13 UTC (rev 3154)
+++ branches/7.4.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/teradata/TeradataExecutionFactory.java 2011-05-05 17:35:15 UTC (rev 3155)
@@ -30,12 +30,15 @@
import java.util.List;
import org.teiid.core.types.DataTypeManager;
+import org.teiid.language.Expression;
import org.teiid.language.Function;
+import org.teiid.language.Literal;
import org.teiid.metadata.FunctionMethod;
import org.teiid.metadata.FunctionParameter;
import org.teiid.translator.SourceSystemFunctions;
import org.teiid.translator.Translator;
import org.teiid.translator.TranslatorException;
+import org.teiid.translator.TypeFacility;
import org.teiid.translator.jdbc.AliasModifier;
import org.teiid.translator.jdbc.ConvertModifier;
import org.teiid.translator.jdbc.FunctionModifier;
@@ -74,14 +77,18 @@
convert.addConvert(FunctionModifier.SHORT, FunctionModifier.STRING, new ImplicitConvertModifier());
convert.addConvert(FunctionModifier.DOUBLE, FunctionModifier.STRING, new ImplicitConvertModifier());
convert.addConvert(FunctionModifier.BYTE, FunctionModifier.STRING, new ImplicitConvertModifier());
+ convert.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.TIME, new TimeModifier("TIME")); //$NON-NLS-1$
+ convert.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.DATE, new TimeModifier("DATE")); //$NON-NLS-1$
+ convert.addConvert(FunctionModifier.TIME, FunctionModifier.TIMESTAMP, new TimeModifier("TIMESTAMP")); //$NON-NLS-1$
+ convert.addConvert(FunctionModifier.DATE, FunctionModifier.TIMESTAMP, new TimeModifier("TIMESTAMP")); //$NON-NLS-1$
+
+
convert.addTypeMapping("varchar(4000)", FunctionModifier.STRING); //$NON-NLS-1$
convert.addNumericBooleanConversions();
registerFunctionModifier(SourceSystemFunctions.CONVERT, convert);
- registerFunctionModifier(SourceSystemFunctions.RAND, new AliasModifier("random")); //$NON-NLS-1$
- registerFunctionModifier(SourceSystemFunctions.NULLIF, new AliasModifier("NULLIFZERO")); //$NON-NLS-1$
- registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier("ZEROIFNULL")); //$NON-NLS-1$
+ registerFunctionModifier(SourceSystemFunctions.RAND, new AliasModifier("random")); //$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.LOG, new AliasModifier("LN")); //$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("LOWER")); //$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("UPPER")); //$NON-NLS-1$
@@ -104,6 +111,31 @@
});
registerFunctionModifier(SourceSystemFunctions.LEFT, new LeftOrRightFunctionModifier(getLanguageFactory()));
registerFunctionModifier(SourceSystemFunctions.RIGHT, new LeftOrRightFunctionModifier(getLanguageFactory()));
+ registerFunctionModifier(SourceSystemFunctions.COT, new FunctionModifier() {
+ @Override
+ public List<?> translate(Function function) {
+ function.setName(SourceSystemFunctions.TAN);
+ return Arrays.asList(getLanguageFactory().createFunction(SourceSystemFunctions.DIVIDE_OP, new Expression[] {new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER), function}, TypeFacility.RUNTIME_TYPES.DOUBLE));
+ }
+ });
+ registerFunctionModifier(SourceSystemFunctions.LTRIM, new FunctionModifier() {
+ @Override
+ public List<?> translate(Function function) {
+ return Arrays.asList("TRIM(LEADING FROM ", function.getParameters().get(0), ")"); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ });
+ registerFunctionModifier(SourceSystemFunctions.RTRIM, new FunctionModifier() {
+ @Override
+ public List<?> translate(Function function) {
+ return Arrays.asList("TRIM(TRAILING FROM ", function.getParameters().get(0), ")"); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ });
+ registerFunctionModifier(SourceSystemFunctions.MOD, new FunctionModifier() {
+ @Override
+ public List<?> translate(Function function) {
+ return Arrays.asList(function.getParameters().get(0), " MOD ", function.getParameters().get(1)); //$NON-NLS-1$
+ }
+ });
}
@Override
@@ -122,14 +154,37 @@
supportedFunctions.add(SourceSystemFunctions.ASIN);
supportedFunctions.add(SourceSystemFunctions.ATAN);
supportedFunctions.add(SourceSystemFunctions.ATAN2);
+ supportedFunctions.add(SourceSystemFunctions.COALESCE);
supportedFunctions.add(SourceSystemFunctions.COS);
+ supportedFunctions.add(SourceSystemFunctions.COT);
supportedFunctions.add(SourceSystemFunctions.CONVERT);
+ supportedFunctions.add(SourceSystemFunctions.CURDATE);
+ supportedFunctions.add(SourceSystemFunctions.CURTIME);
+ supportedFunctions.add(SourceSystemFunctions.DAYOFMONTH);
supportedFunctions.add(SourceSystemFunctions.EXP);
+ supportedFunctions.add(SourceSystemFunctions.HOUR);
+ supportedFunctions.add(SourceSystemFunctions.LEFT);
+ supportedFunctions.add(SourceSystemFunctions.LOCATE);
supportedFunctions.add(SourceSystemFunctions.LOG);
+ supportedFunctions.add(SourceSystemFunctions.LCASE);
+ supportedFunctions.add(SourceSystemFunctions.LTRIM);
+ supportedFunctions.add(SourceSystemFunctions.LENGTH);
+ supportedFunctions.add(SourceSystemFunctions.MINUTE);
+ supportedFunctions.add(SourceSystemFunctions.MOD);
+ supportedFunctions.add(SourceSystemFunctions.MONTH);
+ supportedFunctions.add(SourceSystemFunctions.NULLIF);
+ supportedFunctions.add(SourceSystemFunctions.RAND);
+ supportedFunctions.add(SourceSystemFunctions.RIGHT);
+ supportedFunctions.add(SourceSystemFunctions.RTRIM);
+ supportedFunctions.add(SourceSystemFunctions.SECOND);
supportedFunctions.add(SourceSystemFunctions.SIN);
supportedFunctions.add(SourceSystemFunctions.SQRT);
+ supportedFunctions.add(SourceSystemFunctions.SUBSTRING);
supportedFunctions.add(SourceSystemFunctions.TAN);
supportedFunctions.add("||"); //$NON-NLS-1$
+ supportedFunctions.add("**"); //$NON-NLS-1$
+ supportedFunctions.add(SourceSystemFunctions.UCASE);
+ supportedFunctions.add(SourceSystemFunctions.YEAR);
return supportedFunctions;
}
@@ -211,6 +266,16 @@
new FunctionParameter("String2", DataTypeManager.DefaultDataTypes.STRING, "")}, //$NON-NLS-1$ //$NON-NLS-2$
new FunctionParameter("result", DataTypeManager.DefaultDataTypes.INTEGER, "") ) ); //$NON-NLS-1$ //$NON-NLS-2$
+ pushdownFunctions.add(new FunctionMethod(TERADATA + '.' + "NULLIFZERO", "NULLIFZERO", TERADATA, //$NON-NLS-1$ //$NON-NLS-2$
+ new FunctionParameter[] {
+ new FunctionParameter("integer", DataTypeManager.DefaultDataTypes.BIG_DECIMAL, "")}, //$NON-NLS-1$ //$NON-NLS-2$
+ new FunctionParameter("result", DataTypeManager.DefaultDataTypes.BIG_DECIMAL, "") ) ); //$NON-NLS-1$ //$NON-NLS-2$
+
+ pushdownFunctions.add(new FunctionMethod(TERADATA + '.' + "ZEROIFNULL", "ZEROIFNULL", TERADATA, //$NON-NLS-1$ //$NON-NLS-2$
+ new FunctionParameter[] {
+ new FunctionParameter("integer", DataTypeManager.DefaultDataTypes.BIG_DECIMAL, "")}, //$NON-NLS-1$ //$NON-NLS-2$
+ new FunctionParameter("result", DataTypeManager.DefaultDataTypes.BIG_DECIMAL, "") ) ); //$NON-NLS-1$ //$NON-NLS-2$
+
return pushdownFunctions;
}
@@ -295,4 +360,16 @@
return Arrays.asList(function.getParameters().get(0));
}
}
+
+
+ public static class TimeModifier extends FunctionModifier {
+ private String target;
+ public TimeModifier(String target) {
+ this.target = target;
+ }
+ @Override
+ public List<?> translate(Function function) {
+ return Arrays.asList("cast("+function.getParameters().get(0), " AS "+this.target+")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ }
+ }
}
Modified: branches/7.4.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/teradata/TeradataSQLConversionVisitor.java
===================================================================
--- branches/7.4.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/teradata/TeradataSQLConversionVisitor.java 2011-05-04 20:38:13 UTC (rev 3154)
+++ branches/7.4.x/connectors/translator-jdbc/src/main/java/org/teiid/translator/jdbc/teradata/TeradataSQLConversionVisitor.java 2011-05-05 17:35:15 UTC (rev 3155)
@@ -52,16 +52,17 @@
}
if (decompose) {
+ Comparison.Operator opCode = obj.isNegated()?Comparison.Operator.NE:Comparison.Operator.EQ;
if (exprs.size() > 1) {
- Condition left = LanguageFactory.INSTANCE.createCompareCriteria(Comparison.Operator.EQ, obj.getLeftExpression(), exprs.get(0));
+ Condition left = LanguageFactory.INSTANCE.createCompareCriteria(opCode, obj.getLeftExpression(), exprs.get(0));
for (int i = 1; i < exprs.size(); i++) {
- AndOr replace = LanguageFactory.INSTANCE.createAndOr(Operator.OR, left, LanguageFactory.INSTANCE.createCompareCriteria(Comparison.Operator.EQ, obj.getLeftExpression(), exprs.get(i)));
+ AndOr replace = LanguageFactory.INSTANCE.createAndOr(obj.isNegated()?Operator.AND:Operator.OR, left, LanguageFactory.INSTANCE.createCompareCriteria(opCode, obj.getLeftExpression(), exprs.get(i)));
left = replace;
}
super.visit((AndOr)left);
}
else {
- super.visit(LanguageFactory.INSTANCE.createCompareCriteria(Comparison.Operator.EQ, obj.getLeftExpression(), exprs.get(0)));
+ super.visit(LanguageFactory.INSTANCE.createCompareCriteria(opCode, obj.getLeftExpression(), exprs.get(0)));
}
}
else {
Modified: branches/7.4.x/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/teradata/TestTeradataTranslator.java
===================================================================
--- branches/7.4.x/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/teradata/TestTeradataTranslator.java 2011-05-04 20:38:13 UTC (rev 3154)
+++ branches/7.4.x/connectors/translator-jdbc/src/test/java/org/teiid/translator/jdbc/teradata/TestTeradataTranslator.java 2011-05-05 17:35:15 UTC (rev 3155)
@@ -24,6 +24,7 @@
import static org.junit.Assert.assertEquals;
import java.sql.Date;
+import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -74,6 +75,10 @@
TranslationHelper.helpTestVisitor(TranslationHelper.BQT_VDB, input, output, TRANSLATOR);
}
+ @Test public void testTimestampToTime() throws Exception {
+ helpTest(LANG_FACTORY.createLiteral(new Timestamp(1304604994220L), Timestamp.class), "time", "cast({ts '2011-05-05 09:16:34.22'} AS TIME)");
+ }
+
@Test public void testByteToString() throws Exception {
helpTest(LANG_FACTORY.createLiteral(new Byte((byte)1), Byte.class), "string", "1");
}
@@ -118,6 +123,17 @@
assertEquals("'1' = func() OR '1' = '3'", helpGetString(expr));
}
+ @Test public void testNegatedInDecomposeNonLiterals() throws Exception {
+ Expression left = LANG_FACTORY.createLiteral("1", String.class);
+ List<Expression> right = new ArrayList<Expression>();
+ right.add(LANG_FACTORY.createFunction("func", new Expression[] {}, Date.class));
+ right.add(LANG_FACTORY.createLiteral("3", String.class));
+
+ In expr = LANG_FACTORY.createIn(left,right, true);
+
+ assertEquals("'1' <> func() AND '1' <> '3'", helpGetString(expr));
+ }
+
@Test public void testsingleInDecomposeNonLiterals() throws Exception {
Expression left = LANG_FACTORY.createLiteral("1", String.class);
List<Expression> right = new ArrayList<Expression>();
@@ -131,12 +147,18 @@
@Test public void testNullComapreNull() throws Exception {
String input = "SELECT INTKEY, STRINGKEY, DOUBLENUM FROM bqt1.smalla WHERE NULL <> NULL";
String out = "SELECT SmallA.IntKey, SmallA.StringKey, SmallA.DoubleNum FROM SmallA WHERE 1 = 0";
- TranslationHelper.helpTestVisitor(TranslationHelper.BQT_VDB, null, input, out, new TeradataExecutionFactory());
+ TranslationHelper.helpTestVisitor(TranslationHelper.BQT_VDB, null, input, out, TRANSLATOR);
}
@Test public void testPushDownFunction() throws Exception {
String input = "SELECT teradata.HASHBAKAMP(STRINGKEY) DOUBLENUM FROM bqt1.smalla";
String out = "SELECT HASHBAKAMP(SmallA.StringKey) AS DOUBLENUM FROM SmallA";
- TranslationHelper.helpTestVisitor(TranslationHelper.BQT_VDB, null, input, out, new TeradataExecutionFactory());
+ TranslationHelper.helpTestVisitor(TranslationHelper.BQT_VDB, null, input, out, TRANSLATOR);
}
+
+ @Test public void testRightFunction() throws Exception {
+ String input = "SELECT INTKEY, FLOATNUM FROM BQT1.SmallA WHERE right(FLOATNUM, 2) <> 0 ORDER BY INTKEY";
+ String out = "SELECT SmallA.IntKey, SmallA.FloatNum FROM SmallA WHERE SUBSTR(SmallA.FloatNum, (-1 * 2)) <> '0' ORDER BY SmallA.IntKey";
+ TranslationHelper.helpTestVisitor(TranslationHelper.BQT_VDB, null, input, out, TRANSLATOR);
+ }
}
More information about the teiid-commits
mailing list