Search in sources :

Example 1 with ColRef

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

the class SqlTaskNode method reflectTableCols.

// 依赖的数据库表,可能有相同的表明,但是来自两个数据库的情况
// private static final Map<String /* tableName */, List<TableTupleCreator>> dumpNodes;
// static {
// try {
// 
// List<TableTupleCreator> tables = null;
// Map<String /* tableName */, List<TableTupleCreator>> builder = Maps.newHashMap();
// 
// File f = new File(
// "D:\\j2ee_solution\\eclipse-java-oxygen-mars-develop\\workspace\\tis-mars\\tis-sql-parser\\src\\main\\resources\\dump_tabs\\dump_tabs.txt");
// LineIterator lineIt = FileUtils.lineIterator(f, "utf8");
// String line = null;
// TableTupleCreator tupleCreator = null;
// 
// EntityName entityName = null;
// while (lineIt.hasNext()) {
// line = lineIt.nextLine();
// 
// entityName = EntityName.parse(line);
// 
// tupleCreator = new TableTupleCreator(line, NodeType.DUMP);
// 
// tables = builder.get(entityName.getTabName());
// if (tables == null) {
// tables = Lists.newArrayList();
// builder.put(entityName.getTabName(), tables);
// }
// 
// tupleCreator.setRealEntityName(entityName);
// tables.add(tupleCreator);
// 
// }
// dumpNodes = Collections.unmodifiableMap(builder);// builder.build();
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// 
// }
public static List<ColumnMetaData> reflectTableCols(String sql) {
    if (StringUtils.isEmpty(sql)) {
        throw new IllegalArgumentException("param sql can not be null");
    }
    List<ColumnMetaData> result = Lists.newArrayList();
    Query query = parseQuery(sql);
    StreamTransformVisitor v = new StreamTransformVisitor(null);
    query.accept(v, new StackableAstVisitorContext<>(1));
    ColRef colsRef = v.getColsRef();
    int index = 0;
    for (Map.Entry<ColName, IDataTupleCreator> /* colName */
    entry : colsRef.getColRefMap().entrySet()) {
        // int index, String key, int type, boolean pk
        result.add(new ColumnMetaData(index++, StringUtils.lowerCase(entry.getKey().getName()), new DataType(-1), /**
         * 暂时无法取到类型,先用-1占一下位置
         */
        false));
    }
    return result;
}
Also used : StreamTransformVisitor(com.qlangtech.tis.sql.parser.visitor.StreamTransformVisitor) Query(com.facebook.presto.sql.tree.Query) ColRef(com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef) IDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator) DataType(com.qlangtech.tis.plugin.ds.DataType) ColumnMetaData(com.qlangtech.tis.plugin.ds.ColumnMetaData) Map(java.util.Map)

Example 2 with ColRef

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

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

the class StreamTransformVisitor method visitQuerySpecification.

// @Override
// protected NodeProcessResult<?> visitSelectItem(SelectItem node,
// StackableAstVisitorContext<Integer> context) {
// 
// System.out.println(node);
// 
// return null;
// // return super.visitSelectItem(node, context);
// }
@SuppressWarnings("all")
protected NodeProcessResult<?> visitQuerySpecification(QuerySpecification body, StackableAstVisitorContext<Integer> context) {
    // return visitQueryBody(node, context);
    // Select select = ;
    // this.visitSelect(, context);
    // 
    NodeProcessResult<ColRef> selectresult = (NodeProcessResult<ColRef>) this.process(body.getSelect(), context);
    if (selectresult == null || selectresult.getResult() == null || selectresult.getResult().getColRefMap().size() < 1) {
        throw new IllegalStateException("selectresult can not be null");
    }
    this.colsRef = selectresult.getResult();
    if (body.getLimit().isPresent()) {
        System.out.println("Limit = " + body.getLimit().get());
    }
    // final List<Expression> groupIds = Lists.newArrayList();
    Optional<GroupBy> gby = body.getGroupBy();
    GroupBy group = null;
    if (gby.isPresent()) {
        group = gby.get();
        this.visitGroupBy(group, context);
    }
    Optional<Relation> from = body.getFrom();
    // /////////////////////////////////////////////////////////////////////////
    // System.out.println("======================================================");
    // List<SelectItem>
    // SingleColumn single = null;
    Relation rel = null;
    // Table table = null;
    if (from.isPresent()) {
        rel = from.get();
        // rewriter.newline().append("FROM ");
        // processRelation(rel, rewriter);
        processFromRemoveAlias(colsRef, rel, context);
    // this.processRelation(rel, context);
    } else {
        throw new IllegalStateException("have not set from \n" + body);
    }
    // colsRef.colRefMap.entrySet().stream()
    // .forEach((r) -> System.out.println("[" + r.getKey() + "]," +
    // r.getValue().toString()));
    Optional<Expression> w = body.getWhere();
    Expression where = null;
    if (w.isPresent()) {
        where = w.get();
        process(where, context);
    // rewriter.newline().append("WHERE ");
    // rewriter.append(table);
    // processExpression(where, rewriter);
    } else {
    // if (table != null) {
    // rewriter.newline().append("WHERE ");
    // }
    }
    return null;
}
Also used : ColRef(com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef) NodeProcessResult(com.qlangtech.tis.sql.parser.NodeProcessResult) TisGroupBy(com.qlangtech.tis.sql.parser.TisGroupBy)

Example 4 with ColRef

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

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

ColRef (com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef)6 IDataTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator)5 FunctionDataTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator)3 TableTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator)3 Query (com.facebook.presto.sql.tree.Query)2 NodeProcessResult (com.qlangtech.tis.sql.parser.NodeProcessResult)2 StreamTransformVisitor (com.qlangtech.tis.sql.parser.visitor.StreamTransformVisitor)2 Map (java.util.Map)2 ColumnMetaData (com.qlangtech.tis.plugin.ds.ColumnMetaData)1 DataType (com.qlangtech.tis.plugin.ds.DataType)1 ColName (com.qlangtech.tis.sql.parser.ColName)1 TisGroupBy (com.qlangtech.tis.sql.parser.TisGroupBy)1 TisGroup (com.qlangtech.tis.sql.parser.TisGroupBy.TisGroup)1