Search in sources :

Example 6 with FunctionDataTupleCreator

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

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

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