Search in sources :

Example 36 with Expression

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

the class TestModFunctionModifier method helpTestMod.

/**
 * Create an expression containing a MOD function using a function name of
 * <code>modFunctionName</code> which supports types of <code>supportedTypes</code>
 * and uses the arguments <code>args</code> and pass it to the
 * <code>Translator</code>'s MOD function modifier and compare the resulting
 * expression to <code>expectedStr</code>.
 *
 * @param modFunctionName the name to use for the function modifier
 * @param args an array of <code>IExpression</code>'s to use as the
 *             arguments to the MOD() function
 * @param expectedStr A string representing the modified expression
 * @return On success, the modified expression.
 * @throws Exception
 */
public void helpTestMod(final String modFunctionName, Expression[] args, String expectedStr) throws Exception {
    Expression param1 = args[0];
    Expression param2 = args[1];
    Function func = LANG_FACTORY.createFunction(modFunctionName, Arrays.asList(param1, param2), param1.getType());
    JDBCExecutionFactory trans = new JDBCExecutionFactory() {

        @Override
        public void start() throws TranslatorException {
            super.start();
            registerFunctionModifier(SourceSystemFunctions.MOD, new ModFunctionModifier(modFunctionName, getLanguageFactory()));
        }
    };
    trans.start();
    SQLConversionVisitor sqlVisitor = trans.getSQLConversionVisitor();
    sqlVisitor.append(func);
    // $NON-NLS-1$
    assertEquals("Modified function does not match", expectedStr, sqlVisitor.toString());
}
Also used : Function(org.teiid.language.Function) Expression(org.teiid.language.Expression)

Example 37 with Expression

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

the class MongoDBExecutionFactory method start.

@SuppressWarnings("nls")
@Override
public void start() throws TranslatorException {
    super.start();
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier("+", new AliasModifier("$add"));
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier("-", new AliasModifier("$subtract"));
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier("*", new AliasModifier("$multiply"));
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier("/", new AliasModifier("$divide"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CONCAT, new AliasModifier("$concat"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.SUBSTRING, new AliasModifier("$substr"));
    registerFunctionModifier(SourceSystemFunctions.SUBSTRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$
            function.setName("$substr");
            ArrayList<Expression> params = new ArrayList<Expression>();
            params.add(function.getParameters().get(0));
            // MongoDB is zero base index; Teiid is 1 based;
            params.add(LanguageFactory.INSTANCE.createFunction("-", new Expression[] { function.getParameters().get(1), LanguageFactory.INSTANCE.createLiteral(1, TypeFacility.RUNTIME_TYPES.INTEGER) }, TypeFacility.RUNTIME_TYPES.INTEGER));
            if (function.getParameters().size() == 2) {
                function.getParameters().add(LanguageFactory.INSTANCE.createLiteral(DataTypeManager.MAX_STRING_LENGTH, TypeFacility.RUNTIME_TYPES.INTEGER));
            }
            params.add(function.getParameters().get(2));
            function.getParameters().clear();
            function.getParameters().addAll(params);
            return null;
        }
    });
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("$toLower"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("$toUpper"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYOFYEAR, new AliasModifier("$dayOfYear"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYOFMONTH, new AliasModifier("$dayOfMonth"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYOFWEEK, new AliasModifier("$dayOfWeek"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.YEAR, new AliasModifier("$year"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.MONTH, new AliasModifier("$month"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.WEEK, new AliasModifier("$week"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.HOUR, new AliasModifier("$hour"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.MINUTE, new AliasModifier("$minute"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.SECOND, new AliasModifier("$second"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier("$ifNull"));
    FunctionMethod method = null;
    method = addPushDownFunction(MONGO, FUNC_GEO_INTERSECTS, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING, // $NON-NLS-1$
    TypeFacility.RUNTIME_NAMES.DOUBLE + "[][]");
    method.setProperty(AVOID_PROJECTION, "true");
    method = addPushDownFunction(MONGO, FUNC_GEO_INTERSECTS, TypeFacility.RUNTIME_NAMES.BOOLEAN, // $NON-NLS-1$
    TypeFacility.RUNTIME_NAMES.GEOMETRY);
    method.setProperty(AVOID_PROJECTION, "true");
    method = addPushDownFunction(MONGO, FUNC_GEO_WITHIN, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING, // $NON-NLS-1$
    TypeFacility.RUNTIME_NAMES.DOUBLE + "[][]");
    method.setProperty(AVOID_PROJECTION, "true");
    method = addPushDownFunction(MONGO, FUNC_GEO_WITHIN, TypeFacility.RUNTIME_NAMES.BOOLEAN, // $NON-NLS-1$
    TypeFacility.RUNTIME_NAMES.GEOMETRY);
    method.setProperty(AVOID_PROJECTION, "true");
    if (getVersion().compareTo(MongoDBExecutionFactory.TWO_6) >= 0) {
        method = addPushDownFunction(MONGO, FUNC_GEO_NEAR, TypeFacility.RUNTIME_NAMES.BOOLEAN, // $NON-NLS-1$
        TypeFacility.RUNTIME_NAMES.STRING, // $NON-NLS-1$
        TypeFacility.RUNTIME_NAMES.DOUBLE + "[]", TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.INTEGER);
        method.setProperty(AVOID_PROJECTION, "true");
        method = addPushDownFunction(MONGO, FUNC_GEO_NEAR, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.GEOMETRY, TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.INTEGER);
        method.setProperty(AVOID_PROJECTION, "true");
        method = addPushDownFunction(MONGO, FUNC_GEO_NEAR_SPHERE, TypeFacility.RUNTIME_NAMES.BOOLEAN, // $NON-NLS-1$
        TypeFacility.RUNTIME_NAMES.STRING, // $NON-NLS-1$
        TypeFacility.RUNTIME_NAMES.DOUBLE + "[]", TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.INTEGER);
        method.setProperty(AVOID_PROJECTION, "true");
        method = addPushDownFunction(MONGO, FUNC_GEO_NEAR_SPHERE, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.GEOMETRY, TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.INTEGER);
        method.setProperty(AVOID_PROJECTION, "true");
    } else {
        method = addPushDownFunction(MONGO, FUNC_GEO_NEAR, TypeFacility.RUNTIME_NAMES.BOOLEAN, // $NON-NLS-1$
        TypeFacility.RUNTIME_NAMES.STRING, // $NON-NLS-1$
        TypeFacility.RUNTIME_NAMES.DOUBLE + "[]", TypeFacility.RUNTIME_NAMES.INTEGER);
        method.setProperty(AVOID_PROJECTION, "true");
        method = addPushDownFunction(MONGO, FUNC_GEO_NEAR_SPHERE, TypeFacility.RUNTIME_NAMES.BOOLEAN, // $NON-NLS-1$
        TypeFacility.RUNTIME_NAMES.STRING, // $NON-NLS-1$
        TypeFacility.RUNTIME_NAMES.DOUBLE + "[]", TypeFacility.RUNTIME_NAMES.INTEGER);
        method.setProperty(AVOID_PROJECTION, "true");
    }
    method = addPushDownFunction(MONGO, FUNC_GEO_POLYGON_INTERSECTS, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.DOUBLE, TypeFacility.RUNTIME_NAMES.DOUBLE, TypeFacility.RUNTIME_NAMES.DOUBLE, TypeFacility.RUNTIME_NAMES.DOUBLE);
    method.setProperty(AVOID_PROJECTION, "true");
    method = addPushDownFunction(MONGO, FUNC_GEO_POLYGON_INTERSECTS, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.GEOMETRY);
    method.setProperty(AVOID_PROJECTION, "true");
    method = addPushDownFunction(MONGO, FUNC_GEO_POLYGON_WITHIN, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.DOUBLE, TypeFacility.RUNTIME_NAMES.DOUBLE, TypeFacility.RUNTIME_NAMES.DOUBLE, TypeFacility.RUNTIME_NAMES.DOUBLE);
    method.setProperty(AVOID_PROJECTION, "true");
    method = addPushDownFunction(MONGO, FUNC_GEO_POLYGON_WITHIN, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.GEOMETRY);
    method.setProperty(AVOID_PROJECTION, "true");
    // $NON-NLS-1$
    registerFunctionModifier(FUNC_GEO_NEAR, new AliasModifier("$near"));
    // $NON-NLS-1$
    registerFunctionModifier(FUNC_GEO_NEAR_SPHERE, new AliasModifier("$nearSphere"));
    // $NON-NLS-1$
    registerFunctionModifier(FUNC_GEO_WITHIN, new AliasModifier("$geoWithin"));
    // $NON-NLS-1$
    registerFunctionModifier(FUNC_GEO_INTERSECTS, new AliasModifier("$geoIntersects"));
    // $NON-NLS-1$
    registerFunctionModifier(FUNC_GEO_POLYGON_INTERSECTS, new GeoPolygonFunctionModifier("$geoIntersects"));
    // $NON-NLS-1$
    registerFunctionModifier(FUNC_GEO_POLYGON_WITHIN, new GeoPolygonFunctionModifier("$geoWithin"));
}
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) FunctionMethod(org.teiid.metadata.FunctionMethod) BasicDBList(com.mongodb.BasicDBList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 38 with Expression

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

the class ODataUpdateVisitor method visit.

@Override
public void visit(Update obj) {
    this.operationType = OperationType.UPDATE;
    visitNode(obj.getTable());
    this.odataQuery.setCondition(obj.getWhere());
    try {
        // read the properties
        int elementCount = obj.getChanges().size();
        for (int i = 0; i < elementCount; i++) {
            Column column = obj.getChanges().get(i).getSymbol().getMetadataObject();
            String type = ODataTypeManager.odataType(column.getRuntimeType()).getFullQualifiedName().getFullQualifiedNameAsString();
            Expression expr = obj.getChanges().get(i).getValue();
            Object value = resolveExpressionValue(expr);
            this.odataQuery.addUpdateProperty(column, type, 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)

Example 39 with Expression

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

the class ODataUpdateVisitor method readProperty.

private OProperty<?> readProperty(Column column, Object value) {
    if (value instanceof Array) {
        EdmType componentType = ODataTypeManager.odataType(column.getRuntimeType());
        if (componentType instanceof EdmCollectionType) {
            componentType = ((EdmCollectionType) componentType).getItemType();
        }
        OCollection.Builder<OObject> b = OCollections.newBuilder(componentType);
        List<Expression> values = ((Array) value).getExpressions();
        for (int i = 0; i < values.size(); i++) {
            Literal literal = (Literal) values.get(i);
            b.add(OSimpleObjects.create((EdmSimpleType<?>) componentType, literal.getValue()));
        }
        return OProperties.collection(column.getName(), new EdmCollectionType(CollectionKind.Collection, componentType), b.build());
    } else {
        Literal literal = (Literal) value;
        return OProperties.simple(column.getName(), literal.getValue());
    }
}
Also used : Array(org.teiid.language.Array) EdmType(org.odata4j.edm.EdmType) Expression(org.teiid.language.Expression) OCollection(org.odata4j.core.OCollection) Literal(org.teiid.language.Literal) EdmCollectionType(org.odata4j.edm.EdmCollectionType) EdmSimpleType(org.odata4j.edm.EdmSimpleType) OObject(org.odata4j.core.OObject)

Example 40 with Expression

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

the class JsonSerializer method writeDocument.

private void writeDocument(Document doc, String name, JsonGenerator json, boolean writeName) throws IOException, SQLException {
    if (doc.getProperties().isEmpty() && doc.getChildren().isEmpty()) {
        return;
    }
    if (writeName) {
        json.writeObjectFieldStart(name);
    } else {
        json.writeStartObject();
    }
    for (Map.Entry<String, Object> entry : doc.getProperties().entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if (key.startsWith(name + "/")) {
            key = key.substring(name.length() + 1);
        }
        if (value instanceof org.teiid.language.Array) {
            json.writeArrayFieldStart(key);
            org.teiid.language.Array array = (org.teiid.language.Array) value;
            for (Expression expr : array.getExpressions()) {
                writeProperty(json, ((Literal) expr).getValue());
            }
            json.writeEndArray();
        } else {
            writeProperty(json, key, value);
        }
    }
    if (!doc.getChildren().isEmpty()) {
        for (Map.Entry<String, List<Document>> entry : doc.getChildren().entrySet()) {
            String docName = entry.getKey();
            List<Document> children = entry.getValue();
            boolean array = children.get(0).isArray();
            if (array) {
                json.writeArrayFieldStart(docName);
                for (Document child : children) {
                    writeDocument(child, docName, json, false);
                }
                json.writeEndArray();
            } else {
                writeDocument(children.get(0), docName, json, true);
            }
        }
    }
    json.writeEndObject();
}
Also used : Document(org.teiid.translator.document.Document) Expression(org.teiid.language.Expression) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

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