Search in sources :

Example 1 with TextLine

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

the class GroupingNode method initAccumulator.

static AggregateFunction initAccumulator(AggregateSymbol aggSymbol, RelationalNode node, LinkedHashMap<Expression, Integer> expressionIndexes) {
    int[] argIndexes = new int[aggSymbol.getArgs().length];
    AggregateFunction result = null;
    Expression[] args = aggSymbol.getArgs();
    Class<?>[] inputTypes = new Class[args.length];
    for (int j = 0; j < args.length; j++) {
        inputTypes[j] = args[j].getType();
        argIndexes[j] = getIndex(args[j], expressionIndexes);
    }
    Type function = aggSymbol.getAggregateFunction();
    switch(function) {
        case RANK:
        case DENSE_RANK:
            result = new RankingFunction(function);
            break;
        // same as count(*)
        case ROW_NUMBER:
        case COUNT:
            result = new Count();
            break;
        case SUM:
            result = new Sum();
            break;
        case AVG:
            result = new Avg();
            break;
        case MIN:
            result = new Min();
            break;
        case MAX:
            result = new Max();
            break;
        case XMLAGG:
            result = new XMLAgg();
            break;
        case ARRAY_AGG:
            result = new ArrayAgg();
            break;
        case JSONARRAY_AGG:
            result = new JSONArrayAgg();
            break;
        case TEXTAGG:
            result = new TextAgg((TextLine) args[0]);
            break;
        case STRING_AGG:
            result = new StringAgg(aggSymbol.getType() == DataTypeManager.DefaultDataClasses.BLOB);
            break;
        case FIRST_VALUE:
            result = new FirstLastValue(aggSymbol.getType(), true);
            break;
        case LAST_VALUE:
            result = new FirstLastValue(aggSymbol.getType(), false);
            break;
        case LEAD:
        case LAG:
            result = new LeadLagValue();
            break;
        case USER_DEFINED:
            try {
                result = new UserDefined(aggSymbol.getFunctionDescriptor());
            } catch (FunctionExecutionException e) {
                throw new TeiidRuntimeException(e);
            }
            break;
        default:
            result = new StatsFunction(function);
    }
    if (aggSymbol.getOrderBy() != null) {
        int numOrderByItems = aggSymbol.getOrderBy().getOrderByItems().size();
        List<OrderByItem> orderByItems = new ArrayList<OrderByItem>(numOrderByItems);
        List<ElementSymbol> schema = createSortSchema(result, inputTypes);
        argIndexes = Arrays.copyOf(argIndexes, argIndexes.length + numOrderByItems);
        for (ListIterator<OrderByItem> iterator = aggSymbol.getOrderBy().getOrderByItems().listIterator(); iterator.hasNext(); ) {
            OrderByItem item = iterator.next();
            argIndexes[args.length + iterator.previousIndex()] = getIndex(item.getSymbol(), expressionIndexes);
            ElementSymbol element = new ElementSymbol(String.valueOf(iterator.previousIndex()));
            element.setType(item.getSymbol().getType());
            schema.add(element);
            OrderByItem newItem = item.clone();
            newItem.setSymbol(element);
            orderByItems.add(newItem);
        }
        SortingFilter filter = new SortingFilter(result, node.getBufferManager(), node.getConnectionID(), aggSymbol.isDistinct());
        filter.setElements(schema);
        filter.setSortItems(orderByItems);
        result = filter;
    } else if (aggSymbol.isDistinct()) {
        SortingFilter filter = new SortingFilter(result, node.getBufferManager(), node.getConnectionID(), true);
        List<ElementSymbol> elements = createSortSchema(result, inputTypes);
        filter.setElements(elements);
        result = filter;
    }
    result.setArgIndexes(argIndexes);
    if (aggSymbol.getCondition() != null) {
        result.setConditionIndex(getIndex(aggSymbol.getCondition(), expressionIndexes));
    }
    result.initialize(aggSymbol.getType(), inputTypes);
    return result;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ArrayList(java.util.ArrayList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) OrderByItem(org.teiid.query.sql.lang.OrderByItem) ArrayList(java.util.ArrayList) List(java.util.List) TextLine(org.teiid.query.sql.symbol.TextLine) Type(org.teiid.query.sql.symbol.AggregateSymbol.Type) Expression(org.teiid.query.sql.symbol.Expression)

Example 2 with TextLine

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

the class TextAgg method buildResult.

private FileStoreInputStreamFactory buildResult(CommandContext context) throws TeiidProcessingException {
    try {
        // $NON-NLS-1$
        FileStore fs = context.getBufferManager().createFileStore("textagg");
        FileStoreInputStreamFactory fisf = new FileStoreInputStreamFactory(fs, textLine.getEncoding() == null ? Streamable.ENCODING : textLine.getEncoding());
        Writer w = fisf.getWriter();
        if (textLine.isIncludeHeader()) {
            Object[] header = TextLine.evaluate(textLine.getExpressions(), new TextLine.ValueExtractor<DerivedColumn>() {

                public Object getValue(DerivedColumn t) {
                    if (t.getAlias() == null && t.getExpression() instanceof ElementSymbol) {
                        return ((ElementSymbol) t.getExpression()).getShortName();
                    }
                    return t.getAlias();
                }
            }, textLine);
            writeList(w, header);
        }
        w.flush();
        return fisf;
    } catch (IOException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30420, e);
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) FileStore(org.teiid.common.buffer.FileStore) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) TextLine(org.teiid.query.sql.symbol.TextLine) IOException(java.io.IOException) DerivedColumn(org.teiid.query.sql.symbol.DerivedColumn) Writer(java.io.Writer) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Aggregations

ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)2 TextLine (org.teiid.query.sql.symbol.TextLine)2 IOException (java.io.IOException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)1 FileStore (org.teiid.common.buffer.FileStore)1 FileStoreInputStreamFactory (org.teiid.common.buffer.FileStoreInputStreamFactory)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)1 OrderByItem (org.teiid.query.sql.lang.OrderByItem)1 Type (org.teiid.query.sql.symbol.AggregateSymbol.Type)1 DerivedColumn (org.teiid.query.sql.symbol.DerivedColumn)1 Expression (org.teiid.query.sql.symbol.Expression)1