Search in sources :

Example 21 with Expression

use of org.teiid.language.Expression in project teiid by teiid.

the class TestModFunctionModifier method testOneIntElemOneIntConst2.

/**
 * Test {@link ModFunctionModifier#modify(Function)} to validate a call to
 * MOD(e1,y) using a {@link Integer} element and a {@link Integer} constant
 * for parameters returns MOD(e1,y).  {@link ModFunctionModifier} will be
 * constructed with a function name of "MOD" but without a supported type
 * list.
 *
 * @throws Exception
 */
public void testOneIntElemOneIntConst2() throws Exception {
    Expression[] args = new Expression[] { // $NON-NLS-1$
    LANG_FACTORY.createColumnReference("e1", null, null, Integer.class), LANG_FACTORY.createLiteral(new Integer(6), Integer.class) };
    // mod / default
    // $NON-NLS-1$ //$NON-NLS-2$
    helpTestMod("MOD", args, "MOD(e1, 6)");
}
Also used : BigInteger(java.math.BigInteger) Expression(org.teiid.language.Expression)

Example 22 with Expression

use of org.teiid.language.Expression in project teiid by teiid.

the class TestModFunctionModifier method testTwoBigIntConst.

/**
 * Test {@link ModFunctionModifier#modify(Function)} to validate a call to
 * MOD(x,y) using {@link BigInteger} constants for both parameters returns
 * (x - (TRUNC((x / y), 0) * y)).  {@link ModFunctionModifier} will be
 * constructed without specifying a function name or a supported type list.
 *
 * @throws Exception
 */
public void testTwoBigIntConst() throws Exception {
    Expression[] args = new Expression[] { // $NON-NLS-1$
    LANG_FACTORY.createLiteral(new BigInteger("10"), BigInteger.class), // $NON-NLS-1$
    LANG_FACTORY.createLiteral(new BigInteger("6"), BigInteger.class) };
    // $NON-NLS-1$
    helpTestMod(args, "(10 - (sign(10) * floor(abs((10 / 6))) * abs(6)))");
}
Also used : Expression(org.teiid.language.Expression) BigInteger(java.math.BigInteger)

Example 23 with Expression

use of org.teiid.language.Expression in project teiid by teiid.

the class TestLog10FunctionModifier method testModifier.

public void testModifier() {
    Literal arg = LANG_FACTORY.createLiteral(new Double(5.2), Double.class);
    // $NON-NLS-1$
    Function func = LANG_FACTORY.createFunction("log10", Arrays.asList(arg), Double.class);
    Log10FunctionModifier modifier = new Log10FunctionModifier(LANG_FACTORY);
    modifier.translate(func);
    // $NON-NLS-1$
    assertEquals("log", func.getName());
    assertEquals(Double.class, func.getType());
    List<Expression> outArgs = func.getParameters();
    assertEquals(2, outArgs.size());
    assertEquals(arg, outArgs.get(1));
    assertTrue(outArgs.get(1) instanceof Literal);
    Literal newArg = (Literal) outArgs.get(0);
    assertEquals(Integer.class, newArg.getType());
    assertEquals(new Integer(10), newArg.getValue());
    // $NON-NLS-1$
    assertEquals("log(10, 5.2)", SQLStringVisitor.getSQLString(func));
}
Also used : Function(org.teiid.language.Function) Log10FunctionModifier(org.teiid.translator.jdbc.hana.Log10FunctionModifier) Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal)

Example 24 with Expression

use of org.teiid.language.Expression in project teiid by teiid.

the class TestLog10FunctionModifier method testModifier.

public void testModifier() {
    Literal arg = LANG_FACTORY.createLiteral(new Double(5.2), Double.class);
    // $NON-NLS-1$
    Function func = LANG_FACTORY.createFunction("log10", Arrays.asList(arg), Double.class);
    Log10FunctionModifier modifier = new Log10FunctionModifier(LANG_FACTORY);
    modifier.translate(func);
    // $NON-NLS-1$
    assertEquals("log", func.getName());
    assertEquals(Double.class, func.getType());
    List<Expression> outArgs = func.getParameters();
    assertEquals(2, outArgs.size());
    assertEquals(arg, outArgs.get(1));
    assertTrue(outArgs.get(1) instanceof Literal);
    Literal newArg = (Literal) outArgs.get(0);
    assertEquals(Integer.class, newArg.getType());
    assertEquals(new Integer(10), newArg.getValue());
    // $NON-NLS-1$
    assertEquals("log(10, 5.2)", SQLStringVisitor.getSQLString(func));
}
Also used : Function(org.teiid.language.Function) Log10FunctionModifier(org.teiid.translator.jdbc.oracle.Log10FunctionModifier) Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal)

Example 25 with Expression

use of org.teiid.language.Expression in project teiid by teiid.

the class SAPIQExecutionFactory method start.

public void start() throws TranslatorException {
    super.start();
    registerFunctionModifier(SourceSystemFunctions.CONCAT, new ConcatFunctionModifier(getLanguageFactory()) {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$
            function.setName("||");
            return super.translate(function);
        }
    });
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CONCAT2, new AliasModifier("STRING"));
    registerFunctionModifier(SourceSystemFunctions.DAYOFWEEK, new EscapeSyntaxModifier());
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier(SourceSystemFunctions.DAYOFYEAR, new TemplateFunctionModifier("DATEPART(dy,", 0, ")"));
    registerFunctionModifier(SourceSystemFunctions.DAYOFMONTH, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.WEEK, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPADD, new AddDiffModifier(true, this.getLanguageFactory()).supportsQuarter(true));
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPDIFF, new AddDiffModifier(false, this.getLanguageFactory()).supportsQuarter(true));
    registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier(SourceSystemFunctions.COALESCE));
    registerFunctionModifier(SourceSystemFunctions.LOCATE, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            List<Expression> params = function.getParameters();
            Expression param1 = params.get(0);
            Expression param2 = params.set(1, param1);
            params.set(0, param2);
            return null;
        }
    });
    // add in type conversion
    ConvertModifier convertModifier = new ConvertModifier();
    convertModifier.setBooleanNullable(booleanNullable());
    convertModifier.addNumericBooleanConversions();
    // boolean isn't treated as bit, since it doesn't support null
    // byte is treated as smallint, since tinyint is unsigned
    // $NON-NLS-1$
    convertModifier.addTypeMapping("smallint", FunctionModifier.BYTE, FunctionModifier.SHORT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("bigint", FunctionModifier.LONG);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("int", FunctionModifier.INTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("double", FunctionModifier.DOUBLE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("real", FunctionModifier.FLOAT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(38, 0)", FunctionModifier.BIGINTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(38, 19)", FunctionModifier.BIGDECIMAL);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("char(1)", FunctionModifier.CHAR);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("varchar(4000)", FunctionModifier.STRING);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("varbinary", FunctionModifier.VARBINARY);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("date", FunctionModifier.DATE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("time", FunctionModifier.TIME);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("timestamp", FunctionModifier.TIMESTAMP);
    convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            return convertTimeToString(function);
        }
    });
    convertModifier.addConvert(FunctionModifier.DATE, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            return convertDateToString(function);
        }
    });
    convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            return convertTimestampToString(function);
        }
    });
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convertModifier);
}
Also used : Function(org.teiid.language.Function) TemplateFunctionModifier(org.teiid.translator.jdbc.TemplateFunctionModifier) Expression(org.teiid.language.Expression) TemplateFunctionModifier(org.teiid.translator.jdbc.TemplateFunctionModifier) ConcatFunctionModifier(org.teiid.translator.jdbc.oracle.ConcatFunctionModifier) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) EscapeSyntaxModifier(org.teiid.translator.jdbc.EscapeSyntaxModifier) ConcatFunctionModifier(org.teiid.translator.jdbc.oracle.ConcatFunctionModifier) AddDiffModifier(org.teiid.translator.jdbc.hsql.AddDiffModifier) ConvertModifier(org.teiid.translator.jdbc.ConvertModifier)

Aggregations

Expression (org.teiid.language.Expression)61 ArrayList (java.util.ArrayList)18 ExpressionValueSource (org.teiid.language.ExpressionValueSource)18 Literal (org.teiid.language.Literal)17 TranslatorException (org.teiid.translator.TranslatorException)16 ColumnReference (org.teiid.language.ColumnReference)14 Function (org.teiid.language.Function)13 Column (org.teiid.metadata.Column)12 Insert (org.teiid.language.Insert)11 List (java.util.List)10 Table (org.teiid.metadata.Table)9 BigInteger (java.math.BigInteger)7 Test (org.junit.Test)7 Parameter (org.teiid.language.Parameter)7 Iterator (java.util.Iterator)5 Comparison (org.teiid.language.Comparison)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 FakeExecutionContextImpl (org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl)4 NamedTable (org.teiid.language.NamedTable)4