Search in sources :

Example 1 with TisGroupBy

use of com.qlangtech.tis.sql.parser.TisGroupBy in project tis by qlangtech.

the class StreamTransformVisitor method visitGroupBy.

@Override
protected NodeProcessResult<?> visitGroupBy(GroupBy node, StackableAstVisitorContext<Integer> context) {
    // System.out.println(
    // "GROUP BY " + (node.isDistinct() ? " DISTINCT " : "") +
    // formatGroupBy(node.getGroupingElements()));
    TisGroupBy groupBy = new TisGroupBy();
    for (GroupingElement groupingElement : node.getGroupingElements()) {
        if (groupingElement instanceof SimpleGroupBy) {
            Set<Expression> columns = ImmutableSet.copyOf(((SimpleGroupBy) groupingElement).getColumnExpressions());
            for (Expression g : columns) {
                if (g instanceof DereferenceExpression) {
                    DereferenceExpression group = (DereferenceExpression) g;
                    if (!(group.getBase() instanceof Identifier)) {
                        faild(group.getBase());
                    }
                    groupBy.add(new TisGroup(((Identifier) group.getBase()).getValue(), group.getField().getValue()));
                } else {
                    faild("groupby cols shall col must has tab reference", g);
                }
            }
        // this.groupBy = Optional.of(groupBy);
        } else {
            faild(node);
        }
    // else if (groupingElement instanceof GroupingSets) {
    // result = format("GROUPING SETS (%s)", Joiner.on(", ").join(
    // ((GroupingSets) groupingElement).getSets().stream()
    // .map(ExpressionFormatter::formatGroupingSet)
    // .iterator()));
    // }
    // else if (groupingElement instanceof Cube) {
    // result = format("CUBE %s", formatGroupingSet(((Cube)
    // groupingElement).getColumns()));
    // }
    // else if (groupingElement instanceof Rollup) {
    // result = format("ROLLUP %s", formatGroupingSet(((Rollup)
    // groupingElement).getColumns()));
    // }
    // resultStrings.add(result);
    }
    if (groupBy.getGroups().size() > 0) {
        this.colsRef.getColRefMap().entrySet().forEach((r) -> {
            if (r.getValue() instanceof FunctionDataTupleCreator) {
                ((FunctionDataTupleCreator) r.getValue()).setGroupBy(groupBy);
            }
        // System.out.println(r.getKey() + ":" + r.getValue().hashCode());
        });
    }
    return null;
}
Also used : FunctionDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator) TisGroup(com.qlangtech.tis.sql.parser.TisGroupBy.TisGroup) TisGroupBy(com.qlangtech.tis.sql.parser.TisGroupBy)

Example 2 with TisGroupBy

use of com.qlangtech.tis.sql.parser.TisGroupBy in project tis by qlangtech.

the class TableReferenceVisitor method setFuncGroupByTuple.

private void setFuncGroupByTuple(IDataTupleCreator tupleCreator, final String tabRefName) {
    Stream<FunctionDataTupleCreator> funcs = this.colRef.getColRefMap().values().stream().filter((r) -> r instanceof FunctionDataTupleCreator).map((r) -> (FunctionDataTupleCreator) r);
    funcs.forEach((f) -> {
        Optional<TisGroupBy> g = f.getGroupBy();
        if (g.isPresent()) {
            g.get().getGroups().forEach((group) -> {
                TableTupleCreator tabTuple = null;
                if (!group.isTupleSetted() && StringUtils.equals(group.getTabRef(), tabRefName)) {
                    if (!(tupleCreator instanceof TableTupleCreator)) {
                        throw new IllegalStateException("tupleCreator type shall be TableTupleCterator");
                    }
                    tabTuple = (TableTupleCreator) tupleCreator;
                    if (!tabRefName.equals(tabTuple.getMediaTabRef())) {
                        throw new IllegalStateException("tabRefName:" + tabRefName + ",tabTuple.getMediaTabRef:" + tabTuple.getMediaTabRef() + " shall be equal");
                    }
                    group.setTabTuple(tabTuple);
                }
            });
        }
    });
}
Also used : IDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator) FunctionDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator) ColRef(com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef) StringUtils(org.apache.commons.lang.StringUtils) com.facebook.presto.sql.tree(com.facebook.presto.sql.tree) TisGroupBy(com.qlangtech.tis.sql.parser.TisGroupBy) TableTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator) TabCriteriaEntityRecognizeVisitor(com.qlangtech.tis.sql.parser.tuple.creator.impl.TabCriteriaEntityRecognizeVisitor) Stream(java.util.stream.Stream) IDumpNodeMapContext(com.qlangtech.tis.sql.parser.IDumpNodeMapContext) StackableAstVisitorContext(com.facebook.presto.sql.tree.TISStackableAstVisitor.StackableAstVisitorContext) Optional(java.util.Optional) Type(com.facebook.presto.sql.tree.Join.Type) NodeProcessResult(com.qlangtech.tis.sql.parser.NodeProcessResult) Joiner(com.google.common.base.Joiner) NodeUtils(com.qlangtech.tis.sql.parser.utils.NodeUtils) TableTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator) FunctionDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator) TisGroupBy(com.qlangtech.tis.sql.parser.TisGroupBy)

Aggregations

TisGroupBy (com.qlangtech.tis.sql.parser.TisGroupBy)2 FunctionDataTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator)2 com.facebook.presto.sql.tree (com.facebook.presto.sql.tree)1 Type (com.facebook.presto.sql.tree.Join.Type)1 StackableAstVisitorContext (com.facebook.presto.sql.tree.TISStackableAstVisitor.StackableAstVisitorContext)1 Joiner (com.google.common.base.Joiner)1 IDumpNodeMapContext (com.qlangtech.tis.sql.parser.IDumpNodeMapContext)1 NodeProcessResult (com.qlangtech.tis.sql.parser.NodeProcessResult)1 TisGroup (com.qlangtech.tis.sql.parser.TisGroupBy.TisGroup)1 IDataTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator)1 ColRef (com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef)1 TabCriteriaEntityRecognizeVisitor (com.qlangtech.tis.sql.parser.tuple.creator.impl.TabCriteriaEntityRecognizeVisitor)1 TableTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator)1 NodeUtils (com.qlangtech.tis.sql.parser.utils.NodeUtils)1 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1 StringUtils (org.apache.commons.lang.StringUtils)1