Search in sources :

Example 6 with IDataTupleCreator

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

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

the class ColRef method createBaseRefIfNull.

public IDataTupleCreator createBaseRefIfNull(String baseRef) {
    IDataTupleCreator tupleCreator = null;
    if ((tupleCreator = baseRefMap.get(baseRef)) == null) {
        tupleCreator = new TableTupleCreator(baseRef, NodeType.JOINER_SQL);
        baseRefMap.put(baseRef, tupleCreator);
    }
    return tupleCreator;
}
Also used : IDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator)

Example 8 with IDataTupleCreator

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

the class StreamTransformVisitor method visitSelect.

@Override
protected NodeProcessResult<?> visitSelect(Select node, StackableAstVisitorContext<Integer> context) {
    context.processSelect = true;
    try {
        SingleColumn single = null;
        Expression express = null;
        DereferenceExpression dref = null;
        ColName colName = null;
        ColRef colRef = new ColRef();
        // Map<ColName /* colName */, IDataTupleCreator> colRefMap = Maps.newHashMap();
        // Map<String/* ref */, IDataTupleCreator> baseRefMap = Maps.newHashMap();
        NodeProcessResult<ColRef> result = new NodeProcessResult<ColRef>(colRef);
        IDataTupleCreator tupleCreator = null;
        for (SelectItem item : node.getSelectItems()) {
            if (item instanceof SingleColumn) {
                single = (SingleColumn) item;
                express = single.getExpression();
                if (express instanceof DereferenceExpression) {
                    dref = ((DereferenceExpression) express);
                    if (dref.getBase() instanceof Identifier) {
                        if (single.getAlias().isPresent()) {
                            colName = new ColName(dref.getField().getValue(), single.getAlias().get().getValue());
                        } else {
                            colName = new ColName(dref.getField().getValue());
                        }
                        tupleCreator = createTableTupleCreator(dref, colRef);
                        colRef.getColRefMap().put(colName, tupleCreator);
                    } else {
                        // this.process(dref.getBase(), context);
                        faild(dref.getBase());
                    }
                } else {
                    if (single.getAlias().isPresent()) {
                        String name = single.getAlias().get().getValue();
                        colName = new ColName(name);
                        if (express instanceof SearchedCaseExpression) {
                            colRef.getColRefMap().put(colName, new FunctionDataTupleCreator(express, colRef));
                            continue;
                        } else if (express instanceof CoalesceExpression) {
                            // COALESCE(a2.has_fetch, 0)
                            colRef.getColRefMap().put(colName, new FunctionDataTupleCreator(express, colRef));
                            continue;
                        } else if (express instanceof FunctionCall) {
                            // "concat_ws"(',', "collect_set"("split"(i.batch_msg, '[\\w\\W]*\\|')[1]))
                            colRef.getColRefMap().put(colName, new FunctionDataTupleCreator(express, colRef));
                            continue;
                        } else if (express instanceof SubscriptExpression) {
                            // "split"(i.batch_msg, '[\\w\\W]*\\|')[1]
                            colRef.getColRefMap().put(colName, new FunctionDataTupleCreator(express, colRef));
                            continue;
                        } else if (express instanceof Identifier) {
                            processIdentifier((Identifier) express, colRef);
                            continue;
                        } else if (express instanceof StringLiteral) {
                            /**
                             *                                 select 中存在以下使用常量作为列字段
                             *                                 SELECT
                             *                                 '' as num_unit_id,
                             *                                 '' as num_unit_name
                             *                                 FROM goods
                             */
                            colRef.getColRefMap().put(colName, new FunctionDataTupleCreator(express, colRef));
                            continue;
                        } else if (express instanceof IfExpression) {
                            /**
                             * IF((COALESCE("instr"(sl.logo_url, 'http'), 0) > 0), sl.logo_url, "concat"('http://', sl.logo_url))
                             */
                            colRef.getColRefMap().put(colName, new FunctionDataTupleCreator(express, colRef));
                            continue;
                        }
                        faild(express);
                    } else if (express instanceof Identifier) {
                        // 没有设置表应用,select里面的表应该只有一个
                        // 这种情况下from中只有一个原表
                        processIdentifier((Identifier) express, colRef);
                        continue;
                    }
                    faild(express);
                // process(express, context);
                // IDataTupleCreator tupleCreator = new IDataTupleCreator() {
                // 
                // @Override
                // public Object getVal(String name) {
                // return null;
                // }
                // };
                // 
                // ValueOperator.createGatherValueOperator(new
                // ColName(single.getAlias().get().getValue()),
                // express, params);
                }
            // single.getAlias();
            } else {
                throw new IllegalStateException("item type:" + item.getClass() + "" + item);
            }
        // visitSelectItem(item, context);
        }
        // System.out.println(MoreObjects.toStringHelper(colRefMap).add);
        return result;
    // return new NodeProcessResult<Map<ColName /* colName */, String /* base */
    // >>(colRefMap);
    } finally {
        context.processSelect = false;
    }
// return super.visitSelect(node, context);
}
Also used : FunctionDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator) ColName(com.qlangtech.tis.sql.parser.ColName) ColRef(com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef) NodeProcessResult(com.qlangtech.tis.sql.parser.NodeProcessResult) IDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator)

Example 9 with IDataTupleCreator

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

the class TestTmpPaySqlParse method testParse.

public void testParse() throws Exception {
    String tmpPay = "tmp_pay";
    final TableTupleCreator tmpPayTask = this.parseSqlTaskNode(tmpPay);
    final ColRef colRef = tmpPayTask.getColsRefs();
    Assert.assertNotNull(colRef);
    final ColRef.ListMap /* colName */
    colRefMap = colRef.getColRefMap();
    Assert.assertEquals(5, colRefMap.size());
    // Map<String, IDataTupleCreator> /* ref */
    // baseRefMap = colRef.baseRefMap;
    Assert.assertEquals(1, colRef.getBaseRefSize());
    // baseRefMap.get("aa");
    IDataTupleCreator aaTupleCreator = colRef.getTupleCreator("aa");
    Assert.assertNotNull(aaTupleCreator);
    Assert.assertTrue(aaTupleCreator instanceof TableTupleCreator);
    TableTupleCreator aaTabTupleCreator = (TableTupleCreator) aaTupleCreator;
    // EntitiyRef entityRef = aaTabTupleCreator.getEntityRef();
    // Assert.assertNotNull(entityRef);
    Assert.assertEquals("innertab_aa", aaTabTupleCreator.getEntityName());
    // Assert.assertNotNull(entityRef.getTaskNode());
    // SqlTaskNode taskNode = entityRef.getTaskNode();
    Assert.assertEquals(NodeType.JOINER_SQL, aaTabTupleCreator.getNodetype());
    // Assert.assertEquals("innertab_aa", taskNode.getExportName());
    Assert.assertEquals("aa", aaTabTupleCreator.getMediaTabRef());
    final ColRef aaTabTupleColRef = aaTabTupleCreator.getColsRefs();
    // Set<Map.Entry<String, SqlTaskNode>> required = taskNode.getRequired();
    // .baseRefMap.size());
    Assert.assertEquals(1, aaTabTupleColRef.getBaseRefSize());
    // ===============================================================
    ColName pay_customer_ids = new ColName("pay_customer_ids");
    IDataTupleCreator pay_customer_idsTuple = aaTabTupleColRef.getColRefMap().get(pay_customer_ids);
    Assert.assertTrue(pay_customer_idsTuple instanceof FunctionDataTupleCreator);
    FunctionDataTupleCreator pay_customer_idsTupleFunc = (FunctionDataTupleCreator) pay_customer_idsTuple;
    Optional<TisGroupBy> pay_customer_idsTupleFuncGroup = pay_customer_idsTupleFunc.getGroupBy();
    Assert.assertTrue(pay_customer_idsTupleFuncGroup.isPresent());
    Assert.assertEquals(2, pay_customer_idsTupleFuncGroup.get().getGroups().size());
    TisGroup group = pay_customer_idsTupleFuncGroup.get().getGroups().get(0);
    Assert.assertEquals("totalpay_id", group.getColname());
    Assert.assertNotNull(group.getTabTuple());
    group = pay_customer_idsTupleFuncGroup.get().getGroups().get(1);
    Assert.assertEquals("kindpay_id", group.getColname());
    Assert.assertNotNull(group.getTabTuple());
    // ===============================================================
    Optional<Map.Entry<String, IDataTupleCreator>> dependency = aaTabTupleColRef.getBaseRefEntities().stream().findFirst();
    Assert.assertTrue(dependency.isPresent());
    Assert.assertEquals("p", dependency.get().getKey());
    Assert.assertTrue(dependency.get().getValue() instanceof TableTupleCreator);
    TableTupleCreator payTabTuple = (TableTupleCreator) dependency.get().getValue();
    Assert.assertEquals(NodeType.DUMP, payTabTuple.getNodetype());
    // ==================================================================================
    ColName kindpay = new ColName("kindpay");
    IDataTupleCreator kindPayCreator = colRefMap.get(kindpay);
    Assert.assertNotNull(kindPayCreator);
    Assert.assertTrue(kindPayCreator instanceof FunctionDataTupleCreator);
    FunctionDataTupleCreator kindPayFuncCreator = (FunctionDataTupleCreator) kindPayCreator;
    Map<ColName, IDataTupleCreator> kindpayFuncParams = kindPayFuncCreator.getParams();
    Assert.assertEquals(1, kindpayFuncParams.size());
    ColName innerAkindpay = new ColName("aakindpay");
    IDataTupleCreator innerATuple = kindpayFuncParams.get(innerAkindpay);
    Assert.assertNotNull(innerATuple);
    Assert.assertTrue(innerATuple instanceof TableTupleCreator);
    TableTupleCreator innerATableTuple = (TableTupleCreator) innerATuple;
    Assert.assertEquals("aa", innerATableTuple.getMediaTabRef());
    innerATuple = innerATableTuple.getColsRefs().getColRefMap().get(innerAkindpay);
    Assert.assertNotNull(innerATuple);
    Assert.assertTrue(innerATuple instanceof FunctionDataTupleCreator);
    Assert.assertEquals(6, ((FunctionDataTupleCreator) innerATuple).getParams().size());
    // ===================================================================================
    ColName payCustomerIds = new ColName("pay_customer_ids");
    IDataTupleCreator payCustomerIdsTuple = colRef.getColRefMap().get(payCustomerIds);
    Assert.assertTrue(payCustomerIdsTuple instanceof FunctionDataTupleCreator);
    FunctionDataTupleCreator pay_customer_idsFuncTuple = (FunctionDataTupleCreator) payCustomerIdsTuple;
    Optional<TisGroupBy> pay_customer_idsFuncTupleGroupBy = pay_customer_idsFuncTuple.getGroupBy();
    Assert.assertTrue(pay_customer_idsFuncTupleGroupBy.isPresent());
    Assert.assertEquals(1, pay_customer_idsFuncTupleGroupBy.get().getGroups().size());
    pay_customer_idsFuncTupleGroupBy.get().getGroups().forEach((r) -> {
        Assert.assertNotNull("TabRef:" + r.getTabRef(), r.getTabTuple());
    });
}
Also used : TableTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator) FunctionDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator) TisGroup(com.qlangtech.tis.sql.parser.TisGroupBy.TisGroup) ColRef(com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef) IDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator)

Example 10 with IDataTupleCreator

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

the class FunctionVisitor method visitDereferenceExpression.

@Override
protected Void visitDereferenceExpression(DereferenceExpression node, Void context) {
    ColName col = new ColName(node.getField().getValue());
    IDataTupleCreator tupleCreator = StreamTransformVisitor.createTableTupleCreator(node, this.colRef);
    this.functionParams.put(col, tupleCreator);
    boolean isAggregateNumerica = this.isAggregateNumerica();
    boolean isAggregateFunc = this.isAggregateFunc();
    // =============================================
    if (isAggregateNumerica) {
    // this.funcFormat.append("Double.parseDouble(");
    }
    if (generateContext.isJoinPoint()) {
        this.processJoinPointPram(col);
        if (!this.operatorResultTypeStack.isEmpty()) {
            OperatorResultTypeEnum opResultTypeEnum = this.operatorResultTypeStack.peek();
        // TODO: 暂时先注释掉
        // this.funcFormat.append("(").append(opResultTypeEnum.getLiteria()).append(")");
        }
        this.funcFormat.append("getMediaResult(\"" + col.getName() + "\",row)");
    } else {
        if (generateContext.isNextGroupByFunction() && !generateContext.isGroupByFunction()) {
            this.funcFormat.startLine("v.getMediaProp(\"" + col.getName() + "\")");
        } else if (this.processAggregationResult && !isAggregateFunc) {
            this.funcFormat.append("k.getKeyVal(\"").append(col.getName()).append("\")");
        } else {
            this.funcFormat.append(isAggregateFunc ? "r" : ROW_KEY);
            this.funcFormat.append(".getColumn(\"").append(col.getName()).append("\")");
        }
    }
    if (isAggregateNumerica) {
    // this.funcFormat.append(")");
    }
    // =============================================
    return null;
}
Also used : IDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator) ColName(com.qlangtech.tis.sql.parser.ColName)

Aggregations

IDataTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator)11 ColRef (com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef)6 TableTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator)5 FunctionDataTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator)4 Map (java.util.Map)3 Query (com.facebook.presto.sql.tree.Query)2 ColName (com.qlangtech.tis.sql.parser.ColName)2 NodeProcessResult (com.qlangtech.tis.sql.parser.NodeProcessResult)2 StreamTransformVisitor (com.qlangtech.tis.sql.parser.visitor.StreamTransformVisitor)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 ColumnMetaData (com.qlangtech.tis.plugin.ds.ColumnMetaData)1 DataType (com.qlangtech.tis.plugin.ds.DataType)1 IDumpNodeMapContext (com.qlangtech.tis.sql.parser.IDumpNodeMapContext)1 TisGroupBy (com.qlangtech.tis.sql.parser.TisGroupBy)1 TisGroup (com.qlangtech.tis.sql.parser.TisGroupBy.TisGroup)1 TabCriteriaEntityRecognizeVisitor (com.qlangtech.tis.sql.parser.tuple.creator.impl.TabCriteriaEntityRecognizeVisitor)1 NodeUtils (com.qlangtech.tis.sql.parser.utils.NodeUtils)1