Search in sources :

Example 41 with Expression

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

the class MetaDataProcessor method createProjectedSymbolMetadata.

private Map<Integer, Object>[] createProjectedSymbolMetadata(Command originalCommand) throws TeiidComponentException {
    Map<Integer, Object>[] columnMetadata;
    // Allow command to use temporary metadata
    TempMetadataStore tempMetadata = originalCommand.getTemporaryMetadata();
    if (tempMetadata != null && tempMetadata.getData().size() > 0) {
        TempMetadataAdapter tempFacade = new TempMetadataAdapter(this.metadata, tempMetadata);
        this.metadata = tempFacade;
    }
    List<Expression> projectedSymbols = originalCommand.getProjectedSymbols();
    columnMetadata = new Map[projectedSymbols.size()];
    Iterator<Expression> symbolIter = projectedSymbols.iterator();
    for (int i = 0; symbolIter.hasNext(); i++) {
        Expression symbol = symbolIter.next();
        String shortColumnName = Symbol.getShortName(Symbol.getOutputName(symbol));
        if (symbol instanceof AliasSymbol) {
            symbol = ((AliasSymbol) symbol).getSymbol();
        }
        try {
            columnMetadata[i] = createColumnMetadata(shortColumnName, symbol);
        } catch (QueryMetadataException e) {
            throw new TeiidComponentException(QueryPlugin.Event.TEIID30559, e);
        }
    }
    return columnMetadata;
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Expression(org.teiid.query.sql.symbol.Expression) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) HashMap(java.util.HashMap) Map(java.util.Map) SymbolMap(org.teiid.query.sql.util.SymbolMap) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore)

Example 42 with Expression

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

the class LanguageBridgeFactory method translate.

public org.teiid.language.OrderBy translate(OrderBy orderBy, boolean set) {
    if (orderBy == null) {
        return null;
    }
    List<OrderByItem> items = orderBy.getOrderByItems();
    List<SortSpecification> translatedItems = new ArrayList<SortSpecification>();
    for (int i = 0; i < items.size(); i++) {
        Expression symbol = items.get(i).getSymbol();
        Ordering direction = items.get(i).isAscending() ? Ordering.ASC : Ordering.DESC;
        SortSpecification orderByItem = null;
        if (!set && (items.get(i).isUnrelated() || symbol instanceof ElementSymbol)) {
            orderByItem = new SortSpecification(direction, translate(symbol));
        } else {
            orderByItem = new SortSpecification(direction, new ColumnReference(null, Symbol.getShortName(((Symbol) symbol).getOutputName()), null, symbol.getType()));
        }
        orderByItem.setNullOrdering(items.get(i).getNullOrdering());
        translatedItems.add(orderByItem);
    }
    return new org.teiid.language.OrderBy(translatedItems);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) OrderBy(org.teiid.query.sql.lang.OrderBy) ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) Symbol(org.teiid.query.sql.symbol.Symbol) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Expression(org.teiid.query.sql.symbol.Expression) Ordering(org.teiid.language.SortSpecification.Ordering)

Example 43 with Expression

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

the class LanguageBridgeFactory method translate.

/* Query */
Select translate(Query query) {
    With with = translate(query.getWith());
    List<Expression> symbols = query.getSelect().getSymbols();
    List<DerivedColumn> translatedSymbols = new ArrayList<DerivedColumn>(symbols.size());
    for (Iterator<Expression> i = symbols.iterator(); i.hasNext(); ) {
        Expression symbol = i.next();
        String alias = null;
        if (symbol instanceof AliasSymbol) {
            alias = ((AliasSymbol) symbol).getOutputName();
            symbol = ((AliasSymbol) symbol).getSymbol();
        }
        org.teiid.language.Expression iExp = translate(symbol);
        DerivedColumn selectSymbol = new DerivedColumn(alias, iExp);
        translatedSymbols.add(selectSymbol);
    }
    List<TableReference> items = null;
    if (query.getFrom() != null) {
        List<FromClause> clauses = query.getFrom().getClauses();
        items = new ArrayList<TableReference>(clauses.size());
        for (Iterator<FromClause> i = clauses.iterator(); i.hasNext(); ) {
            items.add(translate(i.next()));
        }
    }
    Select q = new Select(translatedSymbols, query.getSelect().isDistinct(), items, translate(query.getCriteria()), translate(query.getGroupBy()), translate(query.getHaving()), translate(query.getOrderBy(), false));
    q.setLimit(translate(query.getLimit()));
    q.setWith(with);
    return q;
}
Also used : org.teiid.language(org.teiid.language) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Expression(org.teiid.query.sql.symbol.Expression) Select(org.teiid.language.Select) DerivedColumn(org.teiid.language.DerivedColumn)

Example 44 with Expression

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

the class LanguageBridgeFactory method translate.

org.teiid.language.Expression translate(Function function) {
    Expression[] args = function.getArgs();
    List<org.teiid.language.Expression> params = new ArrayList<org.teiid.language.Expression>(args.length);
    for (int i = 0; i < args.length; i++) {
        params.add(translate(args[i]));
    }
    String name = function.getName();
    if (function.getFunctionDescriptor() != null) {
        name = function.getFunctionDescriptor().getName();
        if (!supportsConcat2 && function.getFunctionDescriptor().getMethod().getParent() == null && name.equalsIgnoreCase(SourceSystemFunctions.CONCAT2)) {
            Expression[] newArgs = new Expression[args.length];
            boolean useCase = true;
            for (int i = 0; i < args.length; i++) {
                if (args[i] instanceof Constant) {
                    newArgs[i] = args[i];
                    useCase = false;
                } else {
                    // $NON-NLS-1$
                    Function f = new Function(SourceSystemFunctions.IFNULL, new Expression[] { args[i], new Constant("") });
                    newArgs[i] = f;
                    f.setType(args[i].getType());
                    FunctionDescriptor descriptor = metadataFactory.getMetadata().getFunctionLibrary().findFunction(SourceSystemFunctions.IFNULL, new Class[] { args[i].getType(), DataTypeManager.DefaultDataClasses.STRING });
                    f.setFunctionDescriptor(descriptor);
                }
            }
            Function concat = new Function(SourceSystemFunctions.CONCAT, newArgs);
            concat.setType(DataTypeManager.DefaultDataClasses.STRING);
            if (!useCase) {
                return translate(concat);
            }
            FunctionDescriptor descriptor = metadataFactory.getMetadata().getFunctionLibrary().findFunction(SourceSystemFunctions.CONCAT, new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING });
            concat.setFunctionDescriptor(descriptor);
            List<CompoundCriteria> when = Arrays.asList(new CompoundCriteria(CompoundCriteria.AND, new IsNullCriteria(args[0]), new IsNullCriteria(args[1])));
            Constant nullConstant = new Constant(null, DataTypeManager.DefaultDataClasses.STRING);
            List<Constant> then = Arrays.asList(nullConstant);
            SearchedCaseExpression caseExpr = new SearchedCaseExpression(when, then);
            caseExpr.setElseExpression(concat);
            caseExpr.setType(DataTypeManager.DefaultDataClasses.STRING);
            return translate(caseExpr);
        }
        // check for translator pushdown functions, and use the name in source if possible
        if (function.getFunctionDescriptor().getMethod().getNameInSource() != null && (CoreConstants.SYSTEM_MODEL.equals(function.getFunctionDescriptor().getSchema()) || (function.getFunctionDescriptor().getMethod().getParent() != null && function.getFunctionDescriptor().getMethod().getParent().isPhysical()))) {
            name = function.getFunctionDescriptor().getMethod().getNameInSource();
        }
    } else {
        name = Symbol.getShortName(name);
    }
    // if there is any ambiguity in the function name it will be up to the translator logic to check the
    // metadata
    org.teiid.language.Function result = new org.teiid.language.Function(name, params, function.getType());
    if (function.getFunctionDescriptor() != null) {
        result.setMetadataObject(function.getFunctionDescriptor().getMethod());
    }
    return result;
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) org.teiid.language(org.teiid.language) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) WindowFunction(org.teiid.query.sql.symbol.WindowFunction) Function(org.teiid.query.sql.symbol.Function) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Expression(org.teiid.query.sql.symbol.Expression)

Example 45 with Expression

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

the class LanguageBridgeFactory method translate.

org.teiid.language.WindowFunction translate(WindowFunction windowFunction) {
    org.teiid.language.WindowFunction result = new org.teiid.language.WindowFunction();
    result.setFunction(translate(windowFunction.getFunction()));
    WindowSpecification ws = new WindowSpecification();
    ws.setOrderBy(translate(windowFunction.getWindowSpecification().getOrderBy(), false));
    List<Expression> partition = windowFunction.getWindowSpecification().getPartition();
    if (partition != null) {
        ArrayList<org.teiid.language.Expression> partitionList = translateExpressionList(partition);
        ws.setPartition(partitionList);
    }
    result.setWindowSpecification(ws);
    return result;
}
Also used : WindowFunction(org.teiid.query.sql.symbol.WindowFunction) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Expression(org.teiid.query.sql.symbol.Expression) WindowSpecification(org.teiid.language.WindowSpecification) org.teiid.language(org.teiid.language)

Aggregations

Expression (org.teiid.query.sql.symbol.Expression)257 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)104 ArrayList (java.util.ArrayList)74 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)54 Test (org.junit.Test)53 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)50 List (java.util.List)49 Constant (org.teiid.query.sql.symbol.Constant)38 SymbolMap (org.teiid.query.sql.util.SymbolMap)37 HashMap (java.util.HashMap)22 LinkedList (java.util.LinkedList)22 Criteria (org.teiid.query.sql.lang.Criteria)22 Map (java.util.Map)21 ClobType (org.teiid.core.types.ClobType)16 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)16 HashSet (java.util.HashSet)13 LinkedHashSet (java.util.LinkedHashSet)13 AliasSymbol (org.teiid.query.sql.symbol.AliasSymbol)13 SearchedCaseExpression (org.teiid.query.sql.symbol.SearchedCaseExpression)13 OrderBy (org.teiid.query.sql.lang.OrderBy)12