Search in sources :

Example 1 with NodeProcessResult

use of com.qlangtech.tis.sql.parser.NodeProcessResult 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 2 with NodeProcessResult

use of com.qlangtech.tis.sql.parser.NodeProcessResult 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 3 with NodeProcessResult

use of com.qlangtech.tis.sql.parser.NodeProcessResult in project tis by qlangtech.

the class TableReferenceVisitor method visitJoin.

@Override
protected NodeProcessResult<?> visitJoin(Join node, StackableAstVisitorContext<Integer> context) {
    final Type type = node.getType();
    Relation left = node.getLeft();
    processLeftOrRightRelation(context, left, null, null);
    Relation right = node.getRight();
    Optional<JoinOn> joinOn = node.getCriteria().filter(criteria -> criteria instanceof JoinOn).map(criteria -> (JoinOn) criteria);
    processLeftOrRightRelation(context, right, joinOn, type);
    return null;
}
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) Type(com.facebook.presto.sql.tree.Join.Type)

Aggregations

NodeProcessResult (com.qlangtech.tis.sql.parser.NodeProcessResult)3 ColRef (com.qlangtech.tis.sql.parser.tuple.creator.impl.ColRef)3 TisGroupBy (com.qlangtech.tis.sql.parser.TisGroupBy)2 IDataTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.IDataTupleCreator)2 FunctionDataTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.impl.FunctionDataTupleCreator)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 TableTupleCreator (com.qlangtech.tis.sql.parser.tuple.creator.impl.TableTupleCreator)1 NodeUtils (com.qlangtech.tis.sql.parser.utils.NodeUtils)1 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1 StringUtils (org.apache.commons.lang.StringUtils)1