Skip to content

Commit

Permalink
Merge branch 'master' of github.com:teiid/teiid
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Oct 24, 2017
2 parents b1c4496 + fd3dc51 commit 254829c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 11 deletions.
Expand Up @@ -20,8 +20,11 @@

import static org.teiid.translator.TypeFacility.RUNTIME_NAMES.*;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
Expand Down Expand Up @@ -50,9 +53,6 @@
public class PIExecutionFactory extends JDBCExecutionFactory {
public static String PI = "pi"; //$NON-NLS-1$
protected ConvertModifier convert = new ConvertModifier();




public PIExecutionFactory() {
setUseBindVariables(false);
Expand All @@ -75,6 +75,13 @@ public void start() throws TranslatorException {
convert.addTypeMapping("Time", FunctionModifier.TIME); //$NON-NLS-1$
convert.addTypeMapping("Variant", FunctionModifier.OBJECT); //$NON-NLS-1$

convert.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.TIME, new FunctionModifier() {
@Override
public List<?> translate(Function function) {
return Arrays.asList("cast(format(",function.getParameters().get(0), ", 'hh:mm:ss.fff') as Time)"); //$NON-NLS-1$ //$NON-NLS-2$
}
});

registerFunctionModifier(SourceSystemFunctions.CONVERT, convert);
registerFunctionModifier(SourceSystemFunctions.MOD, new AliasModifier("%")); //$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.DAYOFYEAR, new AliasModifier("DAY")); //$NON-NLS-1$
Expand All @@ -91,6 +98,7 @@ public List<?> translate(Function function) {
registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("LOWER")); //$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("UPPER")); //$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.SUBSTRING, new AliasModifier("SUBSTR")); //$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.LENGTH, new AliasModifier("LEN")); //$NON-NLS-1$

addPushDownFunction(PI, "COSH", FLOAT, FLOAT); //$NON-NLS-1$
addPushDownFunction(PI, "TANH", FLOAT, FLOAT); //$NON-NLS-1$
Expand Down Expand Up @@ -284,5 +292,35 @@ public boolean supportsLateralJoinCondition() {
*/
public boolean supportsProcedureTable() {
return true;
}

@Override
public Object retrieveValue(ResultSet results, int columnIndex, Class<?> expectedType) throws SQLException {
Object result = results.getObject(columnIndex);
if (result == null) {
return null;
}
return super.retrieveValue(results, columnIndex, expectedType);
}

@Override
public Object retrieveValue(CallableStatement results, int parameterIndex, Class<?> expectedType)
throws SQLException {
Object result = results.getObject(parameterIndex);
if (result == null) {
return null;
}
return super.retrieveValue(results, parameterIndex, expectedType);
}

@Override
public boolean supportsConvert(int fromType, int toType) {
if (!super.supportsConvert(fromType, toType)) {
return false;
}
if (convert.hasTypeMapping(toType)) {
return true;
}
return false;
}
}
Expand Up @@ -150,14 +150,18 @@ protected void appendCallStart(Call call) {
}

public void visit(ColumnReference obj) {
ColumnSet<?> cs = obj.getMetadataObject().getParent();
if (cs.getParent() instanceof Procedure && isTVF((Procedure)cs.getParent())) {
this.shortNameOnly = true;
super.visit(obj);
this.shortNameOnly = false;
} else {
super.visit(obj);
}
if (obj.getMetadataObject() != null) {
ColumnSet<?> cs = obj.getMetadataObject().getParent();
if (cs.getParent() instanceof Procedure && isTVF((Procedure)cs.getParent())) {
this.shortNameOnly = true;
super.visit(obj);
this.shortNameOnly = false;
} else {
super.visit(obj);
}
} else {
super.visit(obj);
}
}

public void visit(DerivedTable obj) {
Expand Down
Expand Up @@ -21,12 +21,14 @@
import java.util.TimeZone;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.teiid.core.util.ObjectConverterUtil;
import org.teiid.core.util.TimestampWithTimezone;
import org.teiid.core.util.UnitTestUtil;
import org.teiid.translator.TranslatorException;
import org.teiid.translator.TypeFacility;
import org.teiid.translator.jdbc.TranslationHelper;

@SuppressWarnings("nls")
Expand Down Expand Up @@ -113,4 +115,38 @@ public void testApplyOuter() throws Exception {
String ddl = ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("pi.ddl"));
TranslationHelper.helpTestVisitor(ddl, input, output, TRANSLATOR);
}

@Test
public void testTimestamp2Time() throws TranslatorException {
String input = "select cast(timestampvalue as time) from BQT1.MediumA"; //$NON-NLS-1$
String output = "SELECT cast(format(MediumA.TimestampValue, 'hh:mm:ss.fff') as Time) FROM MediumA"; //$NON-NLS-1$
TranslationHelper.helpTestVisitor(TranslationHelper.BQT_VDB, input, output, TRANSLATOR);
}

@Test
public void testLength() throws TranslatorException {
String input = "select length(STRINGKEY) from BQT1.MediumA"; //$NON-NLS-1$
String output = "SELECT LEN(MediumA.StringKey) FROM MediumA"; //$NON-NLS-1$
TranslationHelper.helpTestVisitor(TranslationHelper.BQT_VDB, input, output, TRANSLATOR);
}

@Test
public void testOrderBy() throws TranslatorException {
String input = "SELECT FloatNum FROM BQT1.MediumA ORDER BY FloatNum ASC"; //$NON-NLS-1$
String output = "SELECT MediumA.FloatNum FROM MediumA ORDER BY MediumA.FloatNum"; //$NON-NLS-1$
TranslationHelper.helpTestVisitor(TranslationHelper.BQT_VDB, input, output, TRANSLATOR);
}

@Test
public void testConvertToBigDecimal() throws TranslatorException {
Assert.assertFalse(
TRANSLATOR.supportsConvert(TypeFacility.RUNTIME_CODES.STRING, TypeFacility.RUNTIME_CODES.BIG_DECIMAL));
Assert.assertFalse(
TRANSLATOR.supportsConvert(TypeFacility.RUNTIME_CODES.STRING, TypeFacility.RUNTIME_CODES.BIG_INTEGER));
Assert.assertFalse(
TRANSLATOR.supportsConvert(TypeFacility.RUNTIME_CODES.STRING, TypeFacility.RUNTIME_CODES.GEOMETRY));
Assert.assertTrue(
TRANSLATOR.supportsConvert(TypeFacility.RUNTIME_CODES.STRING, TypeFacility.RUNTIME_CODES.INTEGER));

}
}

0 comments on commit 254829c

Please sign in to comment.