Search in sources :

Example 1 with FunctionDataTupleCreator

use of com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator in project tis by qlangtech.

the class TestOrderCustomersSqlParse method testParse.

public void testParse() throws Exception {
    final String order_customers = "order_customers";
    TableTupleCreator task = parseSqlTaskNode(order_customers);
    ColRef colRef = task.getColsRefs();
    Assert.assertNotNull(colRef);
    ColRef.ListMap /* colName */
    colRefMap = colRef.getColRefMap();
    // {name:has_fetch=FunctionDataTuple,
    // name:customer_ids=ref:a1,entity:innertab_a1,
    // name:order_id=ref:a1,entity:innertab_a1}
    IDataTupleCreator tupleCreator = null;
    Assert.assertEquals(3, colRefMap.size());
    ColName hasFetch = new ColName("has_fetch");
    tupleCreator = colRefMap.get(hasFetch);
    Assert.assertTrue(tupleCreator instanceof FunctionDataTupleCreator);
    FunctionDataTupleCreator funcDataTuple = (FunctionDataTupleCreator) tupleCreator;
    Assert.assertEquals(1, funcDataTuple.getParams().size());
    Optional<ColName> funcParam = funcDataTuple.getParams().keySet().stream().findFirst();
    Assert.assertTrue(funcParam.isPresent());
    ColName hasFetchParam = funcParam.get();
    Assert.assertEquals("has_fetch", hasFetchParam.getAliasName());
    Assert.assertEquals("has_fetch", hasFetchParam.getName());
    Optional<IDataTupleCreator> hasFetchRef = funcDataTuple.getParams().values().stream().findFirst();
    Assert.assertTrue(hasFetchRef.isPresent());
    IDataTupleCreator asTableTuple = hasFetchRef.get();
    Assert.assertTrue(asTableTuple instanceof TableTupleCreator);
    TableTupleCreator a2Tuple = (TableTupleCreator) asTableTuple;
    // EntitiyRef entityRef = a2Tuple.getEntityRef();
    // Assert.assertNotNull(entityRef);
    Assert.assertEquals("tis.innertab_a2", a2Tuple.getEntityName().toString());
    // task = entityRef.getTaskNode();
    // Assert.assertNotNull(task);
    ColName hasFetchOfinnertab_a2 = new ColName("has_fetch");
    tupleCreator = task.getColsRefs().getColRefMap().get(hasFetchOfinnertab_a2);
    Assert.assertTrue(tupleCreator instanceof FunctionDataTupleCreator);
    funcDataTuple = (FunctionDataTupleCreator) tupleCreator;
    Assert.assertEquals(1, funcDataTuple.getParams().size());
    ColName customerIds = new ColName("customer_ids");
    tupleCreator = colRefMap.get(customerIds);
    assertA1Tuple(tupleCreator);
    ColName orderid = new ColName("order_id");
    tupleCreator = colRefMap.get(orderid);
    assertA1Tuple(tupleCreator);
    Assert.assertEquals(2, colRef.getBaseRefKeys().size());
    Assert.assertTrue(colRef.getTupleCreator("a1") != null);
    Assert.assertTrue(colRef.getTupleCreator("a2") != null);
    Assert.assertTrue(colRef.getTupleCreator("a1") instanceof TableTupleCreator);
    Assert.assertTrue(colRef.getTupleCreator("a2") instanceof TableTupleCreator);
}
Also used : ColRef(com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef) IDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator) TableTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator) FunctionDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator)

Example 2 with FunctionDataTupleCreator

use of com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator 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 3 with FunctionDataTupleCreator

use of com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator 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)

Example 4 with FunctionDataTupleCreator

use of com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator in project tis by qlangtech.

the class StreamComponentCodeGeneratorFlink method generateCreateGroupResultScript.

private void generateCreateGroupResultScript(FuncFormat rr, final PropGetter propGetter, TisGroupBy groups) {
    if (propGetter.isLastFunctInChain()) {
        rr.startLine("var result:Any = null");
    }
    final AtomicBoolean hasBreak = new AtomicBoolean();
    rr.startLine(new IBlockToString() {

        public String toString() {
            return hasBreak.get() ? "breakable {" : StringUtils.EMPTY;
        }
    });
    rr.methodBody("for ((k:GroupKey, v:GroupValues)  <- " + groups.getGroupAggrgationName() + ")", (r) -> {
        final PropGetter pgetter = propGetter;
        if (propGetter.isLastFunctInChain()) {
            rr.startLine("result = ");
            hasBreak.set(true);
            propGetter.getGroovyScript(rr, true);
            rr.startLine("break");
            rr.returnLine();
        } else {
            PropGetter prev = propGetter.getPrev();
            while (prev != null) {
                if (prev.getTupleCreator() instanceof FunctionDataTupleCreator) {
                    break;
                }
                prev = prev.getPrev();
            }
            boolean shallCallableProcess = (prev != null && prev.shallCallableProcess());
            if (shallCallableProcess) {
                rr.startLine("putMediaResult(\"" + pgetter.getOutputColName().getAliasName() + "\", //");
            } else {
                rr.startLine("v.putMediaData( // \n");
                rr.startLine("\"").append(propGetter.getOutputColName().getAliasName()).append("\" // \n");
                rr.startLine(", //\n");
            }
            // );
            propGetter.getGroovyScript(rr, true);
            if (shallCallableProcess) {
                rr.startLine(") // end putMediaResult");
                hasBreak.set(true);
                rr.startLine("break");
            } else {
                rr.startLine(") // end putMediaData\n");
            }
        }
    });
    rr.startLine(new IBlockToString() {

        public String toString() {
            return hasBreak.get() ? "} //end breakable" : StringUtils.EMPTY;
        }
    });
    if (propGetter.isLastFunctInChain()) {
        rr.startLine(" result //return");
    }
    rr.returnLine();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IBlockToString(com.qlangtech.tis.sql.parser.visitor.IBlockToString) FunctionDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator) PropGetter(com.qlangtech.tis.sql.parser.tuple.creator.impl.PropGetter) IBlockToString(com.qlangtech.tis.sql.parser.visitor.IBlockToString)

Example 5 with FunctionDataTupleCreator

use of com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator in project tis by qlangtech.

the class StreamComponentCodeGenerator method generateCreateGroupResultScript.

private void generateCreateGroupResultScript(FuncFormat rr, final PropGetter propGetter, TisGroupBy groups) {
    if (propGetter.isLastFunctInChain()) {
        rr.startLine("var result:Any = null");
    }
    final AtomicBoolean hasBreak = new AtomicBoolean();
    rr.startLine(new IBlockToString() {

        public String toString() {
            return hasBreak.get() ? "breakable {" : StringUtils.EMPTY;
        }
    });
    rr.methodBody("for ((k:GroupKey, v:GroupValues)  <- " + groups.getGroupAggrgationName() + ")", (r) -> {
        final PropGetter pgetter = propGetter;
        if (propGetter.isLastFunctInChain()) {
            rr.startLine("result = ");
            hasBreak.set(true);
            propGetter.getGroovyScript(rr, true);
            rr.startLine("break");
            rr.returnLine();
        } else {
            PropGetter prev = propGetter.getPrev();
            while (prev != null) {
                if (prev.getTupleCreator() instanceof FunctionDataTupleCreator) {
                    break;
                }
                prev = prev.getPrev();
            }
            boolean shallCallableProcess = (prev != null && prev.shallCallableProcess());
            if (shallCallableProcess) {
                rr.startLine("putMediaResult(\"" + pgetter.getOutputColName().getAliasName() + "\", //");
            } else {
                rr.startLine("v.putMediaData( // \n");
                rr.startLine("\"").append(propGetter.getOutputColName().getAliasName()).append("\" // \n");
                rr.startLine(", //\n");
            }
            // );
            propGetter.getGroovyScript(rr, true);
            if (shallCallableProcess) {
                rr.startLine(") // end putMediaResult");
                hasBreak.set(true);
                rr.startLine("break");
            } else {
                rr.startLine(") // end putMediaData\n");
            }
        }
    });
    rr.startLine(new IBlockToString() {

        public String toString() {
            return hasBreak.get() ? "} //end breakable" : StringUtils.EMPTY;
        }
    });
    if (propGetter.isLastFunctInChain()) {
        rr.startLine(" result //return");
    }
    rr.returnLine();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IBlockToString(com.qlangtech.tis.sql.parser.visitor.IBlockToString) FunctionDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator) PropGetter(com.qlangtech.tis.sql.parser.tuple.creator.impl.PropGetter) IBlockToString(com.qlangtech.tis.sql.parser.visitor.IBlockToString)

Aggregations

FunctionDataTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator)7 IDataTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator)4 ColRef (com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef)4 TableTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator)3 NodeProcessResult (com.qlangtech.tis.sql.parser.NodeProcessResult)2 TisGroupBy (com.qlangtech.tis.sql.parser.TisGroupBy)2 TisGroup (com.qlangtech.tis.sql.parser.TisGroupBy.TisGroup)2 PropGetter (com.qlangtech.tis.sql.parser.tuple.creator.impl.PropGetter)2 IBlockToString (com.qlangtech.tis.sql.parser.visitor.IBlockToString)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)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 ColName (com.qlangtech.tis.sql.parser.ColName)1 IDumpNodeMapContext (com.qlangtech.tis.sql.parser.IDumpNodeMapContext)1 TabCriteriaEntityRecognizeVisitor (com.qlangtech.tis.sql.parser.tuple.creator.impl.TabCriteriaEntityRecognizeVisitor)1 NodeUtils (com.qlangtech.tis.sql.parser.utils.NodeUtils)1 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1