Search in sources :

Example 1 with AliasSymbol

use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.

the class ODataSQLBuilder method processOrderBy.

private OrderBy processOrderBy(OrderBy orderBy, List<OrderByItem> orderByItems, DocumentNode resource) throws TeiidException {
    for (OrderByItem obitem : orderByItems) {
        ODataExpressionToSQLVisitor visitor = new ODataExpressionToSQLVisitor(resource, false, getUriInfo(), this.metadata, this.odata, this.nameGenerator, this.params, this.parseService);
        Expression expr = visitor.getExpression(obitem.getExpression());
        if (expr instanceof ElementSymbol) {
            orderBy.addVariable(expr, !obitem.isDescending());
        } else {
            AliasSymbol alias = new AliasSymbol("_orderByAlias", expr);
            orderBy.addVariable(alias, !obitem.isDescending());
            visitor.getEntityResource().addProjectedColumn(alias, EdmInt32.getInstance(), null, false);
        }
    }
    return orderBy;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) OrderByItem(org.apache.olingo.server.api.uri.queryoption.OrderByItem) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Expression(org.teiid.query.sql.symbol.Expression)

Example 2 with AliasSymbol

use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.

the class ODataSQLBuilder method selectQuery.

public Query selectQuery() throws TeiidException {
    if (!this.exceptions.isEmpty()) {
        throw this.exceptions.get(0);
    }
    Query query = this.context.buildQuery();
    if (this.countQuery) {
        AggregateSymbol aggregateSymbol = new AggregateSymbol(AggregateSymbol.Type.COUNT.name(), false, null);
        Select select = new Select(Arrays.asList(aggregateSymbol));
        query.setSelect(select);
    } else if (this.orderBy != null) {
        if (this.context.getIterator() != null) {
            // currently this doesn't matter as the ordering can only be based upon the parent entity
            ((AggregateSymbol) ((AliasSymbol) query.getSelect().getSymbol(query.getSelect().getProjectedSymbols().size() - 1)).getSymbol()).setOrderBy(this.orderBy);
        } else {
            query.setOrderBy(this.orderBy);
        }
    }
    if (this.expandOption != null) {
        processExpandOption(this.expandOption, this.context, query, 1, null);
    }
    return query;
}
Also used : AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol)

Example 3 with AliasSymbol

use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.

the class ODataExpressionToSQLVisitor method visit.

@Override
public void visit(UriResourceIt info) {
    if (info.getType() instanceof SingletonPrimitiveType) {
        org.teiid.query.sql.symbol.Expression ex = null;
        if (this.ctxQuery.getIterator() == null) {
            String group = this.nameGenerator.getNextGroup();
            GroupSymbol groupSymbol = new GroupSymbol(group);
            StoredProcedure procedure = new StoredProcedure();
            procedure.setProcedureName("arrayiterate");
            // the projected should only be the collection property at this point
            // we may need more checks here to ensure that is valid
            Collection<ProjectedColumn> values = this.ctxQuery.getProjectedColumns().values();
            Assertion.assertTrue(values.size() == 1);
            ProjectedColumn projectedColumn = values.iterator().next();
            ElementSymbol projectedEs = (ElementSymbol) projectedColumn.getExpression();
            List<SPParameter> params = new ArrayList<SPParameter>();
            SPParameter param = new SPParameter(1, SPParameter.IN, "val");
            param.setExpression(projectedEs);
            params.add(param);
            procedure.setParameter(param);
            SubqueryFromClause fromClause = new SubqueryFromClause(group, procedure);
            fromClause.setLateral(true);
            ElementSymbol es = new ElementSymbol("col", groupSymbol);
            String type = ODataTypeManager.teiidType((SingletonPrimitiveType) info.getType(), false);
            Function castFunction = new Function(CAST, new org.teiid.query.sql.symbol.Expression[] { es, new Constant(type) });
            DocumentNode itResource = new DocumentNode();
            org.teiid.query.sql.symbol.Expression clone = (org.teiid.query.sql.symbol.Expression) castFunction.clone();
            AggregateSymbol symbol = new AggregateSymbol(AggregateSymbol.Type.ARRAY_AGG.name(), false, clone);
            AliasSymbol expression = new AliasSymbol(projectedEs.getShortName(), symbol);
            itResource.setFromClause(fromClause);
            itResource.setGroupSymbol(groupSymbol);
            itResource.addProjectedColumn(expression, info.getType(), projectedColumn.getProperty(), true);
            this.ctxQuery.getProjectedColumns().remove(projectedColumn.getExpression());
            this.ctxQuery.setIterator(itResource);
            ex = castFunction;
        } else {
            GroupSymbol groupSymbol = this.ctxQuery.getIterator().getGroupSymbol();
            ElementSymbol es = new ElementSymbol("col", groupSymbol);
            String type = ODataTypeManager.teiidType((SingletonPrimitiveType) info.getType(), false);
            ex = new Function(CAST, new org.teiid.query.sql.symbol.Expression[] { es, new Constant(type) });
        }
        this.stack.push(ex);
    } else {
        boolean ex = true;
        if (this.ctxQuery instanceof ExpandDocumentNode) {
            ExpandDocumentNode node = (ExpandDocumentNode) this.ctxQuery;
            DocumentNode parent = node.getCollectionContext();
            if (parent != null) {
                this.ctxExpression = parent;
                ex = false;
            }
        }
        if (ex) {
            throw new TeiidRuntimeException(new TeiidNotImplementedException(ODataPlugin.Event.TEIID16010, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16010)));
        }
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) ProjectedColumn(org.teiid.olingo.ProjectedColumn) Function(org.teiid.query.sql.symbol.Function) SingletonPrimitiveType(org.apache.olingo.commons.core.edm.primitivetype.SingletonPrimitiveType) ScalarSubquery(org.teiid.query.sql.symbol.ScalarSubquery) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol)

Example 4 with AliasSymbol

use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.

the class TestAliasSymbol method testClone.

@Test
public void testClone() {
    // $NON-NLS-1$ //$NON-NLS-2$
    AliasSymbol a1 = new AliasSymbol("X", new ExpressionSymbol("x", new Constant(1)));
    // $NON-NLS-1$
    a1.setOutputName("foo");
    AliasSymbol clone = (AliasSymbol) a1.clone();
    assertEquals(a1, clone);
    assertEquals(a1.getOutputName(), clone.getOutputName());
}
Also used : AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Constant(org.teiid.query.sql.symbol.Constant) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) Test(org.junit.Test)

Example 5 with AliasSymbol

use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.

the class TestSelect method sample2.

public static final Select sample2() {
    Select select = new Select();
    // $NON-NLS-1$
    select.addSymbol(new ElementSymbol("a"));
    // $NON-NLS-1$
    select.addSymbol(new ElementSymbol("b"));
    // $NON-NLS-1$
    select.addSymbol(new ElementSymbol("c"));
    // $NON-NLS-1$ //$NON-NLS-2$
    select.addSymbol(new AliasSymbol("Z", new ElementSymbol("ZZ 9 Plural Z Alpha")));
    return select;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Select(org.teiid.query.sql.lang.Select)

Aggregations

AliasSymbol (org.teiid.query.sql.symbol.AliasSymbol)19 Expression (org.teiid.query.sql.symbol.Expression)12 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)9 ArrayList (java.util.ArrayList)7 ExpressionSymbol (org.teiid.query.sql.symbol.ExpressionSymbol)5 List (java.util.List)3 Map (java.util.Map)3 OrderBy (org.teiid.query.sql.lang.OrderBy)3 AggregateSymbol (org.teiid.query.sql.symbol.AggregateSymbol)3 Constant (org.teiid.query.sql.symbol.Constant)3 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)3 SymbolMap (org.teiid.query.sql.util.SymbolMap)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 Test (org.junit.Test)2 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)2 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)2 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)2 Reference (org.teiid.query.sql.symbol.Reference)2