Search in sources :

Example 51 with Expression

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

the class AccumuloUpdateExecution method performUpdate.

private void performUpdate(Update update) throws TranslatorException, TableNotFoundException, MutationsRejectedException {
    Table table = update.getTable().getMetadataObject();
    if (update.getParameterValues() != null) {
        throw new TranslatorException(AccumuloPlugin.Event.TEIID19005, AccumuloPlugin.Util.gs(AccumuloPlugin.Event.TEIID19005));
    }
    AccumuloQueryVisitor visitor = new AccumuloQueryVisitor(this.aef);
    visitor.visitNode(update.getWhere());
    if (!visitor.exceptions.isEmpty()) {
        throw visitor.exceptions.get(0);
    }
    Connector connector = this.connection.getInstance();
    BatchWriter writer = createBatchWriter(table, connector);
    Text prevRow = null;
    Iterator<Entry<Key, Value>> results = AccumuloQueryExecution.runQuery(this.aef, this.connection.getInstance(), this.connection.getAuthorizations(), visitor.getRanges(), table, visitor.scanIterators());
    while (results.hasNext()) {
        Key key = results.next().getKey();
        Text rowId = key.getRow();
        if (prevRow == null || !prevRow.equals(rowId)) {
            prevRow = rowId;
            Mutation mutation = new Mutation(rowId);
            List<SetClause> changes = update.getChanges();
            for (SetClause clause : changes) {
                Column column = clause.getSymbol().getMetadataObject();
                if (SQLStringVisitor.getRecordName(column).equalsIgnoreCase(AccumuloMetadataProcessor.ROWID)) {
                    throw new TranslatorException(AccumuloPlugin.Event.TEIID19002, AccumuloPlugin.Util.gs(AccumuloPlugin.Event.TEIID19002, table.getName()));
                }
                Expression value = clause.getValue();
                if (value instanceof Literal) {
                    buildMutation(mutation, column, ((Literal) value).getValue());
                } else {
                    throw new TranslatorException(AccumuloPlugin.Event.TEIID19001, AccumuloPlugin.Util.gs(AccumuloPlugin.Event.TEIID19001));
                }
            }
            writer.addMutation(mutation);
            this.updateCount++;
        }
    }
    writer.close();
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Table(org.teiid.metadata.Table) Text(org.apache.hadoop.io.Text) Entry(java.util.Map.Entry) Column(org.teiid.metadata.Column) Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) Key(org.apache.accumulo.core.data.Key) SetClause(org.teiid.language.SetClause)

Example 52 with Expression

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

the class CouchbaseExecutionFactory method start.

@Override
public void start() throws TranslatorException {
    super.start();
    registerFunctionModifier(SourceSystemFunctions.SUBSTRING, new SubstringFunctionModifier());
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CEILING, new AliasModifier("CEIL"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LOG, new AliasModifier("LN"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LOG10, new AliasModifier("LOG"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.RAND, new AliasModifier("RANDOM"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("LOWER"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("UPPER"));
    registerFunctionModifier(SourceSystemFunctions.CONVERT, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            Expression param = function.getParameters().get(0);
            int targetCode = getCode(function.getType());
            if (targetCode == BYTE || targetCode == SHORT || targetCode == INTEGER || targetCode == LONG || targetCode == FLOAT || targetCode == DOUBLE || targetCode == BIGINTEGER || targetCode == BIGDECIMAL) {
                if ((Number.class.isAssignableFrom(param.getType()))) {
                    return Arrays.asList(param);
                }
                // $NON-NLS-1$
                return Arrays.asList("TONUMBER" + Tokens.LPAREN, param, Tokens.RPAREN);
            } else if (targetCode == STRING || targetCode == CHAR) {
                // $NON-NLS-1$
                return Arrays.asList("TOSTRING" + Tokens.LPAREN, param, Tokens.RPAREN);
            } else if (targetCode == BOOLEAN) {
                // $NON-NLS-1$
                return Arrays.asList("TOBOOLEAN" + Tokens.LPAREN, param, Tokens.RPAREN);
            } else {
                return Arrays.asList(param);
            }
        }
    });
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "CONTAINS", TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "TITLE", TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "LTRIM", TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "TRIM", TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "RTRIM", TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "POSITION", TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "CLOCK_MILLIS", TypeFacility.RUNTIME_NAMES.DOUBLE);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "CLOCK_STR", TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "CLOCK_STR", TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "DATE_ADD_MILLIS", TypeFacility.RUNTIME_NAMES.LONG, TypeFacility.RUNTIME_NAMES.LONG, TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "DATE_ADD_STR", TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "DATE_DIFF_MILLIS", TypeFacility.RUNTIME_NAMES.LONG, TypeFacility.RUNTIME_NAMES.LONG, TypeFacility.RUNTIME_NAMES.LONG, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "DATE_DIFF_STR", TypeFacility.RUNTIME_NAMES.LONG, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "DATE_PART_MILLIS", TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.LONG, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "DATE_PART_STR", TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "NOW_MILLIS", TypeFacility.RUNTIME_NAMES.DOUBLE);
    // $NON-NLS-1$
    addPushDownFunction(COUCHBASE, "NOW_STR", TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING);
}
Also used : Function(org.teiid.language.Function) Expression(org.teiid.language.Expression) QueryExpression(org.teiid.language.QueryExpression) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) List(java.util.List)

Example 53 with Expression

use of org.teiid.language.Expression 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 54 with Expression

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

the class TestSQLtoSpreadsheetQuery method helpTestExpression.

private void helpTestExpression(String expression, String expected) throws QueryParserException {
    LanguageBridgeFactory lbf = new LanguageBridgeFactory(RealMetadataFactory.example1Cached());
    Expression ex = lbf.translate(QueryParser.getQueryParser().parseExpression(expression));
    SpreadsheetSQLVisitor spreadsheetVisitor = new SpreadsheetSQLVisitor(people);
    spreadsheetVisitor.translateSQL(ex);
    assertEquals(expected, spreadsheetVisitor.getTranslatedSQL());
}
Also used : Expression(org.teiid.language.Expression) SpreadsheetSQLVisitor(org.teiid.translator.google.visitor.SpreadsheetSQLVisitor) LanguageBridgeFactory(org.teiid.dqp.internal.datamgr.LanguageBridgeFactory)

Example 55 with Expression

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

the class InfinispanUpdateVisitor method buildInsertPayload.

private InfinispanDocument buildInsertPayload(Insert obj, List<Expression> values) {
    InfinispanDocument targetDocument = null;
    try {
        // table that insert issued for
        Table table = obj.getTable().getMetadataObject();
        Column pkColumn = getPrimaryKey();
        // create the top table parent document, where insert is actually being done at
        targetDocument = buildTargetDocument(table, true);
        // build the payload object from insert
        int elementCount = obj.getColumns().size();
        for (int i = 0; i < elementCount; i++) {
            ColumnReference columnReference = obj.getColumns().get(i);
            Column column = columnReference.getMetadataObject();
            Object value = null;
            if (values.get(i) instanceof Expression) {
                Expression expr = values.get(i);
                value = resolveExpressionValue(expr);
            } else {
                value = values.get(i);
            }
            updateDocument(targetDocument, column, value);
            if (column.equals(pkColumn) || pkColumn.equals(normalizePseudoColumn(column))) {
                targetDocument.setIdentifier(value);
            }
        }
        if (targetDocument.getIdentifier() == null) {
            this.exceptions.add(new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25004, getParentTable().getName())));
        }
    } catch (NumberFormatException e) {
        this.exceptions.add(new TranslatorException(e));
    } catch (TranslatorException e) {
        this.exceptions.add(new TranslatorException(e));
    }
    return targetDocument;
}
Also used : Table(org.teiid.metadata.Table) InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) Column(org.teiid.metadata.Column) Expression(org.teiid.language.Expression) TranslatorException(org.teiid.translator.TranslatorException) ColumnReference(org.teiid.language.ColumnReference)

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