Search in sources :

Example 1 with Literal

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

the class AccumuloUpdateExecution method getRowId.

private byte[] getRowId(List<ColumnReference> columns, List<Expression> values) throws TranslatorException {
    for (int i = 0; i < columns.size(); i++) {
        Column column = columns.get(i).getMetadataObject();
        String rowId = SQLStringVisitor.getRecordName(column);
        if (rowId.equalsIgnoreCase(AccumuloMetadataProcessor.ROWID) || AccumuloQueryVisitor.isPartOfPrimaryKey(column)) {
            Object value = values.get(i);
            if (value instanceof Literal) {
                return AccumuloDataTypeManager.serialize(((Literal) value).getValue());
            }
            throw new TranslatorException(AccumuloPlugin.Event.TEIID19006, AccumuloPlugin.Util.gs(AccumuloPlugin.Event.TEIID19006));
        }
    }
    return null;
}
Also used : Column(org.teiid.metadata.Column) Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException)

Example 2 with Literal

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

the class AccumuloUpdateExecution method writeMutation.

private void writeMutation(BatchWriter writer, List<ColumnReference> columns, List<Expression> values) throws MutationsRejectedException, TranslatorException {
    byte[] rowId = getRowId(columns, values);
    Mutation mutation = new Mutation(rowId);
    for (int i = 0; i < columns.size(); i++) {
        Column column = columns.get(i).getMetadataObject();
        if (SQLStringVisitor.getRecordName(column).equalsIgnoreCase(AccumuloMetadataProcessor.ROWID)) {
            continue;
        }
        Object value = values.get(i);
        if (value instanceof Literal) {
            buildMutation(mutation, column, ((Literal) value).getValue());
        } else {
            buildMutation(mutation, column, value);
        }
    }
    writer.addMutation(mutation);
}
Also used : Column(org.teiid.metadata.Column) Literal(org.teiid.language.Literal) Mutation(org.apache.accumulo.core.data.Mutation)

Example 3 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);
    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 4 with Literal

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

the class S3ProcedureExecution method invokeHTTP.

protected BinaryWSProcedureExecution invokeHTTP(String method, String uri, Object payload, Map<String, String> headers) throws TranslatorException {
    Map<String, List<String>> targetHeaders = new HashMap<String, List<String>>();
    headers.forEach((k, v) -> targetHeaders.put(k, Arrays.asList(v)));
    if (LogManager.isMessageToBeRecorded(LogConstants.CTX_WS, MessageLevel.DETAIL)) {
        try {
            LogManager.logDetail(LogConstants.CTX_WS, "Source-URL=", // $NON-NLS-1$ //$NON-NLS-2$
            URLDecoder.decode(uri, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
        }
    }
    List<Argument> parameters = new ArrayList<Argument>();
    parameters.add(new Argument(Direction.IN, new Literal(method, TypeFacility.RUNTIME_TYPES.STRING), null));
    parameters.add(new Argument(Direction.IN, new Literal(payload, TypeFacility.RUNTIME_TYPES.OBJECT), null));
    parameters.add(new Argument(Direction.IN, new Literal(uri, TypeFacility.RUNTIME_TYPES.STRING), null));
    parameters.add(new Argument(Direction.IN, new Literal(true, TypeFacility.RUNTIME_TYPES.BOOLEAN), null));
    // the engine currently always associates out params at resolve time even if the
    // values are not directly read by the call
    parameters.add(new Argument(Direction.OUT, TypeFacility.RUNTIME_TYPES.STRING, null));
    Call call = this.ef.getLanguageFactory().createCall("invokeHttp", parameters, null);
    BinaryWSProcedureExecution execution = new BinaryWSProcedureExecution(call, this.metadata, this.ec, null, this.conn);
    execution.setUseResponseContext(true);
    execution.setCustomHeaders(targetHeaders);
    return execution;
}
Also used : Call(org.teiid.language.Call) Argument(org.teiid.language.Argument) HashMap(java.util.HashMap) Literal(org.teiid.language.Literal) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with Literal

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

the class SimpleDBUpdateVisitor method visit.

@Override
public void visit(SetClause obj) {
    Column column = obj.getSymbol().getMetadataObject();
    if (obj.getValue() instanceof Literal) {
        try {
            Literal l = (Literal) obj.getValue();
            this.attributes.put(SimpleDBMetadataProcessor.getName(column), SimpleDBDataTypeManager.convertToSimpleDBType(l.getValue(), column.getJavaType()));
        } catch (TranslatorException e) {
            this.exceptions.add(e);
        }
    } else if (obj.getValue() instanceof Array) {
        try {
            Array array = (Array) obj.getValue();
            String[] result = SimpleDBInsertVisitor.getValuesArray(array);
            this.attributes.put(SimpleDBMetadataProcessor.getName(column), result);
        } catch (TranslatorException e) {
            this.exceptions.add(e);
        }
    } else {
        this.exceptions.add(new TranslatorException(SimpleDBPlugin.Event.TEIID24001, SimpleDBPlugin.Util.gs(SimpleDBPlugin.Event.TEIID24001)));
    }
}
Also used : Array(org.teiid.language.Array) Column(org.teiid.metadata.Column) Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException)

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