Search in sources :

Example 1 with Expression

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

the class AccumuloUpdateExecution method performInsert.

private void performInsert(Insert insert) throws TranslatorException, TableNotFoundException, MutationsRejectedException {
    Table table = insert.getTable().getMetadataObject();
    this.updateCount = 0;
    Connector connector = this.connection.getInstance();
    BatchWriter writer = createBatchWriter(table, connector);
    List<ColumnReference> columns = insert.getColumns();
    if (insert.getParameterValues() == null) {
        List<Expression> values = ((ExpressionValueSource) insert.getValueSource()).getValues();
        writeMutation(writer, columns, values);
        this.updateCount++;
    } else {
        int batchSize = this.executionContext.getBatchSize();
        // bulk insert; should help
        Iterator<? extends List<Expression>> args = (Iterator<? extends List<Expression>>) insert.getParameterValues();
        while (args.hasNext()) {
            List<Expression> values = args.next();
            writeMutation(writer, columns, values);
            this.updateCount++;
            if ((this.updateCount % batchSize) == 0) {
                writer.close();
                writer = createBatchWriter(table, connector);
            }
        }
    }
    // write the mutation
    writer.close();
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Table(org.teiid.metadata.Table) Expression(org.teiid.language.Expression) Iterator(java.util.Iterator) List(java.util.List) BatchWriter(org.apache.accumulo.core.client.BatchWriter) ColumnReference(org.teiid.language.ColumnReference) ExpressionValueSource(org.teiid.language.ExpressionValueSource)

Example 2 with Expression

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

the class SubstringFunctionModifier method translate.

@Override
public List<?> translate(Function function) {
    this.modify(function);
    Expression from = function.getParameters().get(1);
    Boolean isFromNegative = isNegative(from);
    Function length = new Function(SourceSystemFunctions.LENGTH, Arrays.asList(function.getParameters().get(0)), TypeFacility.RUNTIME_TYPES.INTEGER);
    if (function.getParameters().size() == 2 && (isFromNegative == null || isFromNegative)) {
        // couchbase does not handle default length with a negative from index
        function.getParameters().add(length);
    }
    if (function.getParameters().size() == 3) {
        // 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 = isNegative(forLength);
        Expression adjustedFrom = from;
        if (isFromNegative == null || isFromNegative) {
            adjustedFrom = new SearchedCase(Arrays.asList(new SearchedWhenClause(new Comparison(from, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.LT), new Function(SourceSystemFunctions.ADD_OP, Arrays.asList(new Function(SourceSystemFunctions.ADD_OP, Arrays.asList(length, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER), from), TypeFacility.RUNTIME_TYPES.INTEGER))), from, TypeFacility.RUNTIME_TYPES.INTEGER);
        }
        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);
    }
    Expression adjustedFrom = function.getParameters().get(1);
    if (isFromNegative == null) {
        // case when start > 0 then start - 1 else start end
        SearchedCase sc = new SearchedCase(Arrays.asList(new SearchedWhenClause(new Comparison(adjustedFrom, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.GT), new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER))), from, TypeFacility.RUNTIME_TYPES.INTEGER);
        function.getParameters().set(1, sc);
    } else if (!isFromNegative) {
        function.getParameters().set(1, new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER));
    }
    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 3 with Expression

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

the class TestInsertImpl method testGetValues.

public void testGetValues() throws Exception {
    // $NON-NLS-1$
    Insert insert = example("a.b");
    assertNotNull(insert.getValueSource());
    assertEquals(4, ((ExpressionValueSource) insert.getValueSource()).getValues().size());
    for (Iterator i = ((ExpressionValueSource) insert.getValueSource()).getValues().iterator(); i.hasNext(); ) {
        assertTrue(i.next() instanceof Expression);
    }
}
Also used : Expression(org.teiid.language.Expression) Iterator(java.util.Iterator) Insert(org.teiid.language.Insert) ExpressionValueSource(org.teiid.language.ExpressionValueSource)

Example 4 with Expression

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

the class ODataUpdateVisitor method visit.

@Override
public void visit(Insert obj) {
    this.operationType = OperationType.INSERT;
    visitNode(obj.getTable());
    try {
        // read the properties
        int elementCount = obj.getColumns().size();
        for (int i = 0; i < elementCount; i++) {
            Column column = obj.getColumns().get(i).getMetadataObject();
            List<Expression> values = ((ExpressionValueSource) obj.getValueSource()).getValues();
            Expression expr = values.get(i);
            Object value = resolveExpressionValue(expr);
            this.odataQuery.addInsertProperty(column, ODataMetadataProcessor.getNativeType(column), value);
        }
    } catch (TranslatorException e) {
        this.exceptions.add(e);
    }
}
Also used : Column(org.teiid.metadata.Column) Expression(org.teiid.language.Expression) TranslatorException(org.teiid.translator.TranslatorException) ExpressionValueSource(org.teiid.language.ExpressionValueSource)

Example 5 with Expression

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

the class InsertExecutionImpl method buildBulkRowPayload.

protected List<com.sforce.async.SObject> buildBulkRowPayload(Insert insert, Iterator<? extends List<?>> it, int rowCount) throws TranslatorException {
    List<com.sforce.async.SObject> rows = new ArrayList<com.sforce.async.SObject>();
    List<ColumnReference> columns = insert.getColumns();
    int boundCount = 0;
    List<Expression> literalValues = ((ExpressionValueSource) insert.getValueSource()).getValues();
    while (it.hasNext()) {
        if (boundCount >= rowCount) {
            break;
        }
        boundCount++;
        List<?> values = it.next();
        com.sforce.async.SObject sobj = new com.sforce.async.SObject();
        for (int i = 0; i < columns.size(); i++) {
            Expression ex = literalValues.get(i);
            ColumnReference element = columns.get(i);
            Column column = element.getMetadataObject();
            Class<?> type = ex.getType();
            Object value = null;
            if (ex instanceof Parameter) {
                value = values.get(((Parameter) ex).getValueIndex());
            } else if (!(ex instanceof Literal)) {
                throw new TranslatorException(SalesForcePlugin.Util.gs(SalesForcePlugin.Event.TEIID13007));
            } else {
                value = ((Literal) ex).getValue();
            }
            sobj.setField(column.getSourceName(), getStringValue(value, type));
        }
        rows.add(sobj);
    }
    return rows;
}
Also used : ArrayList(java.util.ArrayList) Expression(org.teiid.language.Expression) Column(org.teiid.metadata.Column) Literal(org.teiid.language.Literal) SObject(com.sforce.async.SObject) Parameter(org.teiid.language.Parameter) SObject(com.sforce.async.SObject) TranslatorException(org.teiid.translator.TranslatorException) SObject(com.sforce.async.SObject) ColumnReference(org.teiid.language.ColumnReference) ExpressionValueSource(org.teiid.language.ExpressionValueSource)

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