Search in sources :

Example 1 with IDataTupleCreator

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

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

the class TestOrderCustomersSqlParse method assertA1Tuple.

private void assertA1Tuple(IDataTupleCreator tupleCreator) {
    Assert.assertNotNull(tupleCreator);
    Assert.assertTrue(tupleCreator instanceof TableTupleCreator);
    TableTupleCreator a1Tuple = (TableTupleCreator) tupleCreator;
    Assert.assertEquals("a1", a1Tuple.getMediaTabRef());
    // EntitiyRef entityRef = a1Tuple.getEntityRef();
    // Assert.assertNotNull(entityRef);
    Assert.assertEquals("tis.innertab_a1", a1Tuple.getEntityName().toString());
    // Assert.assertNotNull(entityRef.getTaskNode());
    // SqlTaskNode a1Task = entityRef.getTaskNode();
    // Assert.assertNotNull(a1Task);
    Assert.assertEquals(1, a1Tuple.getColsRefs().getBaseRefSize());
    Optional<Map.Entry<String, IDataTupleCreator>> /* ref */
    e = a1Tuple.getColsRefs().getBaseRefEntities().stream().findFirst();
    Assert.assertTrue(e.isPresent());
    Map.Entry<String, IDataTupleCreator> /* ref */
    i_ref = e.get();
    Assert.assertEquals("i", i_ref.getKey());
    Assert.assertNotNull(i_ref.getValue());
    Assert.assertTrue(i_ref.getValue() instanceof TableTupleCreator);
    TableTupleCreator iTuple = (TableTupleCreator) i_ref.getValue();
    Assert.assertEquals("i", iTuple.getMediaTabRef());
    Assert.assertEquals("order.instancedetail", iTuple.getEntityName().toString());
    Assert.assertEquals(NodeType.DUMP, iTuple.getNodetype());
    ;
}
Also used : IDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator) TableTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator) Map(java.util.Map)

Example 3 with IDataTupleCreator

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

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

the class TableReferenceVisitor method processLeftOrRightRelation.

// private NodeProcessResult<?> whenProcessJoinOn(String rightTab, JoinOn joinOn,
// StackableAstVisitorContext<Integer> context) {
// return this.process(joinOn.getExpression(), context);
// }
private void processLeftOrRightRelation(StackableAstVisitorContext<Integer> context, Relation rel, Optional<JoinOn> joinOn, Type jointype) {
    AliasedRelation aliasRel = null;
    Identifier a = null;
    IDataTupleCreator tupleCreator = null;
    if (rel instanceof Table) {
        Table tab = (Table) rel;
        tupleCreator = getDataTupleCreatorByRef(String.valueOf(tab.getName()));
        processTableReferenceRecoginize(joinOn, jointype, tab, tupleCreator, String.valueOf(tab.getName()));
    } else if (rel instanceof AliasedRelation) {
        aliasRel = (AliasedRelation) rel;
        a = aliasRel.getAlias();
        tupleCreator = getDataTupleCreatorByRef(aliasRel.getAlias().getValue());
        if (joinOn != null) {
            if (!joinOn.isPresent()) {
                StreamTransformVisitor.faild(rel);
            }
        }
        if (aliasRel.getRelation() instanceof Table) {
            processTableReferenceRecoginize(joinOn, jointype, (Table) aliasRel.getRelation(), tupleCreator, a.getValue());
        } else if (aliasRel.getRelation() instanceof TableSubquery) {
            TableSubquery subQuery = (TableSubquery) aliasRel.getRelation();
            // example:
            // TableSubquery{Query{queryBody=QuerySpecification{select=Select{distinct=false,
            // selectItems=[i.order_id, "concat_ws"(',', "collect_set"("split"(i.batch_msg,
            // '[\\w\\W]*\\|')[1])) customer_ids]},
            // from=Optional[AliasedRelation{relation=Table{instance}, alias=i}],
            // where=(i.is_valid = 1), groupBy=Optional[GroupBy{isDistinct=false,
            // groupingElements=[SimpleGroupBy{columns=[order_id]}]}], having=null,
            // orderBy=Optional.empty, limit=null}, orderBy=Optional.empty}}
            // subQuery.getQuery();
            processSubQuery(joinOn, jointype, a, tupleCreator, a.getValue(), /* alias */
            subQuery);
        // this.process(aliasRel.getRelation(), context);
        } else {
            NodeUtils.faild(aliasRel.getRelation());
        }
    } else {
        this.process(rel, context);
    }
}
Also used : IDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator)

Example 5 with IDataTupleCreator

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

the class TableReferenceVisitor method visitAliasedRelation.

@Override
protected NodeProcessResult<?> visitAliasedRelation(AliasedRelation node, StackableAstVisitorContext<Integer> context) {
    // 表引用名称
    final String alias = node.getAlias().getValue();
    IDataTupleCreator tupleCreator = getDataTupleCreatorByRef(alias);
    if (node.getRelation() instanceof Table) {
        processTableReferenceRecoginize(null, /* joinOn */
        null, /* jointype */
        (Table) node.getRelation(), tupleCreator, alias);
    } else if (node.getRelation() instanceof TableSubquery) {
        // this.process(node.getRelation(), context);
        processSubQuery(null, /* joinOn */
        null, /* jointype */
        node.getAlias(), tupleCreator, alias, (TableSubquery) node.getRelation());
    } else {
        NodeUtils.faild(node);
    }
    return null;
// return super.visitAliasedRelation(node, context);
}
Also used : IDataTupleCreator(com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator)

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