Search in sources :

Example 76 with Literal

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

the class TestFileExecutionFactory method testGetTextFiles.

@Test
public void testGetTextFiles() throws Exception {
    FileExecutionFactory fef = new FileExecutionFactory();
    MetadataFactory mf = new MetadataFactory("vdb", 1, "text", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
    fef.getMetadata(mf, null);
    Procedure p = mf.getSchema().getProcedure("getTextFiles");
    FileConnection fc = Mockito.mock(FileConnection.class);
    Mockito.stub(fc.getFile("*.txt")).toReturn(new File(UnitTestUtil.getTestDataPath(), "*.txt"));
    Call call = fef.getLanguageFactory().createCall("getTextFiles", Arrays.asList(new Argument(Direction.IN, new Literal("*.txt", TypeFacility.RUNTIME_TYPES.STRING), TypeFacility.RUNTIME_TYPES.STRING, null)), p);
    ProcedureExecution pe = fef.createProcedureExecution(call, null, null, fc);
    pe.execute();
    int count = 0;
    while (true) {
        List<?> val = pe.next();
        if (val == null) {
            break;
        }
        assertEquals(5, val.size());
        assertTrue(val.get(3) instanceof Timestamp);
        assertEquals(Long.valueOf(0), val.get(4));
        count++;
    }
    assertEquals(2, count);
    call = fef.getLanguageFactory().createCall("getTextFiles", Arrays.asList(new Argument(Direction.IN, new Literal("*1*", TypeFacility.RUNTIME_TYPES.STRING), TypeFacility.RUNTIME_TYPES.STRING, null)), p);
    pe = fef.createProcedureExecution(call, null, null, fc);
    Mockito.stub(fc.getFile("*1*")).toReturn(new File(UnitTestUtil.getTestDataPath(), "*1*"));
    pe.execute();
    count = 0;
    while (true) {
        if (pe.next() == null) {
            break;
        }
        count++;
    }
    assertEquals(1, count);
}
Also used : Call(org.teiid.language.Call) Argument(org.teiid.language.Argument) Properties(java.util.Properties) Timestamp(java.sql.Timestamp) MetadataFactory(org.teiid.metadata.MetadataFactory) ProcedureExecution(org.teiid.translator.ProcedureExecution) Literal(org.teiid.language.Literal) Procedure(org.teiid.metadata.Procedure) File(java.io.File) FileConnection(org.teiid.translator.FileConnection) Test(org.junit.Test)

Example 77 with Literal

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

the class SubstringFunctionModifier method isNegative.

private Boolean isNegative(Expression ex) {
    Boolean isNegative = null;
    if (ex instanceof Literal) {
        Literal l = (Literal) ex;
        int value = (Integer) l.getValue();
        isNegative = value < 0;
    }
    return isNegative;
}
Also used : Literal(org.teiid.language.Literal)

Example 78 with Literal

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

the class SpreadsheetInsertVisitor method visit.

public void visit(Insert obj) {
    worksheetTitle = obj.getTable().getName();
    if (obj.getTable().getMetadataObject().getNameInSource() != null) {
        worksheetTitle = obj.getTable().getMetadataObject().getNameInSource();
    }
    worksheetKey = info.getWorksheetByName(worksheetTitle).getId();
    ExpressionValueSource evs = (ExpressionValueSource) obj.getValueSource();
    for (int i = 0; i < evs.getValues().size(); i++) {
        Expression e = evs.getValues().get(i);
        if (!(e instanceof Literal)) {
            throw new SpreadsheetOperationException("Only literals are allowed in the values section");
        }
        Literal l = (Literal) e;
        if (l.getValue() == null) {
            continue;
        }
        ColumnReference columnReference = obj.getColumns().get(i);
        columnNameValuePair.put(columnReference.getMetadataObject().getSourceName(), l.getValue());
    }
}
Also used : Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) ExpressionValueSource(org.teiid.language.ExpressionValueSource) ColumnReference(org.teiid.language.ColumnReference)

Example 79 with Literal

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

the class SubstringFunctionModifier method translate.

@Override
public List<?> translate(Function function) {
    this.modify(function);
    if (function.getParameters().size() != 3) {
        return null;
    }
    // case when length > LENGTH(string) - start + 1 then LENGTH(string) - start + 1 case when length > 0 then length end
    Expression forLength = function.getParameters().get(2);
    List<SearchedWhenClause> clauses = new ArrayList<SearchedWhenClause>(2);
    Boolean isNegative = null;
    if (forLength instanceof Literal) {
        Literal l = (Literal) forLength;
        int value = (Integer) l.getValue();
        isNegative = value < 0;
    }
    Function length = new Function(SourceSystemFunctions.LENGTH, Arrays.asList(function.getParameters().get(0)), TypeFacility.RUNTIME_TYPES.INTEGER);
    Expression from = function.getParameters().get(1);
    SearchedCase adjustedFrom = new SearchedCase(Arrays.asList(new SearchedWhenClause(new Comparison(from, length, Operator.GT), new Function(SourceSystemFunctions.ADD_OP, Arrays.asList(length, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER))), from, TypeFacility.RUNTIME_TYPES.INTEGER);
    function.getParameters().set(1, adjustedFrom);
    Expression maxLength = new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(length, new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER);
    clauses.add(new SearchedWhenClause(new Comparison(forLength, maxLength, Operator.GT), maxLength));
    Expression defaultExpr = null;
    if (isNegative == null) {
        clauses.add(new SearchedWhenClause(new Comparison(forLength, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.GT), forLength));
    } else if (isNegative) {
        // TODO: could be done in the rewriter
        return Arrays.asList(new Literal(null, TypeFacility.RUNTIME_TYPES.STRING));
    } else {
        defaultExpr = forLength;
    }
    SearchedCase sc = new SearchedCase(clauses, defaultExpr, TypeFacility.RUNTIME_TYPES.INTEGER);
    function.getParameters().set(2, sc);
    return null;
}
Also used : SearchedCase(org.teiid.language.SearchedCase) Function(org.teiid.language.Function) SearchedWhenClause(org.teiid.language.SearchedWhenClause) Expression(org.teiid.language.Expression) Comparison(org.teiid.language.Comparison) Literal(org.teiid.language.Literal) ArrayList(java.util.ArrayList)

Example 80 with Literal

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

the class TestSelectSymbolImpl method testGetColumnDataTypes.

public void testGetColumnDataTypes() {
    Class<?>[] expectedResults = new Class[2];
    List<DerivedColumn> symbols = new ArrayList<DerivedColumn>();
    // $NON-NLS-1$//$NON-NLS-2$
    symbols.add(new DerivedColumn("c1", new Literal("3", DataTypeManager.DefaultDataClasses.STRING)));
    expectedResults[0] = DataTypeManager.DefaultDataClasses.STRING;
    // $NON-NLS-1$
    symbols.add(new DerivedColumn("c2", new Literal(new Integer(5), DataTypeManager.DefaultDataClasses.INTEGER)));
    expectedResults[1] = DataTypeManager.DefaultDataClasses.INTEGER;
    Select query = new Select(symbols, false, null, null, null, null, null);
    Class<?>[] results = query.getColumnTypes();
    assertEquals(results[0], expectedResults[0]);
    assertEquals(results[1], expectedResults[1]);
}
Also used : Literal(org.teiid.language.Literal) ArrayList(java.util.ArrayList) Select(org.teiid.language.Select) DerivedColumn(org.teiid.language.DerivedColumn)

Aggregations

Literal (org.teiid.language.Literal)82 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)18 Expression (org.teiid.language.Expression)17 TranslatorException (org.teiid.translator.TranslatorException)16 Function (org.teiid.language.Function)15 Argument (org.teiid.language.Argument)12 ColumnReference (org.teiid.language.ColumnReference)10 Column (org.teiid.metadata.Column)10 Comparison (org.teiid.language.Comparison)9 ExpressionValueSource (org.teiid.language.ExpressionValueSource)7 List (java.util.List)5 Call (org.teiid.language.Call)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 BinaryWSProcedureExecution (org.teiid.translator.ws.BinaryWSProcedureExecution)4 DBCollection (com.mongodb.DBCollection)3 Timestamp (java.sql.Timestamp)3 Array (org.teiid.language.Array)3 Command (org.teiid.language.Command)3 Insert (org.teiid.language.Insert)3