Search in sources :

Example 1 with Operator

use of com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator in project tis by qlangtech.

the class StreamTransformVisitor method visitArithmeticBinary.

@Override
protected NodeProcessResult<?> visitArithmeticBinary(ArithmeticBinaryExpression arithmeticBinaryExpression, StackableAstVisitorContext<Integer> context) {
    // ArithmeticBinaryExpression arithmeticBinaryExpression =
    // (ArithmeticBinaryExpression) expression;
    // rewriter.append("(");
    process(arithmeticBinaryExpression.getLeft(), context);
    Operator operator = arithmeticBinaryExpression.getOperator();
    // System.out.println("operator:" + operator.getValue());
    // rewriter.append(operator.getValue());
    process(arithmeticBinaryExpression.getRight(), context);
    return null;
// return super.visitArithmeticBinary(node, context);
}
Also used : Operator(com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator)

Example 2 with Operator

use of com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator in project tis by qlangtech.

the class TestGroupBySqlParser method process.

// private static class Visitor extends TISStackableAstVisitor<Void, Integer> {
// 
// @Override
// public Void process(Node node, StackableAstVisitorContext<Integer> context) {
// return super.process(node, context);
// }
// 
// protected Void visitNode(Node node, StackableAstVisitorContext<Integer>
// context) {
// throw new UnsupportedOperationException(String.valueOf(node));
// }
// 
// protected Void visitAliasedRelation(AliasedRelation node,
// StackableAstVisitorContext<Integer> context) {
// 
// this.process(node.getRelation(), context);
// 
// if (context.getPreviousNode().isPresent()) {
// System.out.println("getPreviousNode:" + context.getPreviousNode().get());
// }
// 
// System.out.println(node.getAlias().getValue());
// 
// return null;
// }
// 
// @Override
// protected Void visitComparisonExpression(ComparisonExpression node,
// StackableAstVisitorContext<Integer> context) {
// 
// node.getLeft();
// // System.out.println(node.getOperator() + "," +
// node.getOperator().getValue());
// node.getRight();
// 
// return null;
// // return super.visitComparisonExpression(node, context);
// }
// 
// @Override
// protected Void visitLogicalBinaryExpression(LogicalBinaryExpression node,
// StackableAstVisitorContext<Integer> context) {
// 
// System.out.println("operator:" + node.getOperator());
// 
// this.process(node.getLeft(), context);
// 
// this.process(node.getRight(), context);
// 
// return null;
// 
// // return super.visitLogicalBinaryExpression(node, context);
// }
// 
// @Override
// protected Void visitTable(Table node, StackableAstVisitorContext<Integer>
// context) {
// 
// // System.out.println(node.getName() + "," +
// context.getPreviousNode().get());
// 
// return null;
// }
// 
// protected Void visitQuery(Query node, StackableAstVisitorContext<Integer>
// context) {
// if (node.getWith().isPresent()) {
// 
// }
// 
// processRelation(node.getQueryBody(), context);
// 
// if (node.getOrderBy().isPresent()) {
// process(node.getOrderBy().get(), context);
// }
// 
// if (node.getLimit().isPresent()) {
// // append(indent, "LIMIT " + node.getLimit().get())
// // .append('\n');
// }
// 
// return null;
// }
// 
// @Override
// protected Void visitSelect(Select node, StackableAstVisitorContext<Integer>
// context) {
// 
// for (SelectItem item : node.getSelectItems()) {
// visitSelectItem(item, context);
// }
// return null;
// // return super.visitSelect(node, context);
// }
// 
// @Override
// protected Void visitSelectItem(SelectItem node,
// StackableAstVisitorContext<Integer> context) {
// 
// // System.out.println(node);
// 
// return null;
// // return super.visitSelectItem(node, context);
// }
// 
// protected Void visitQuerySpecification(QuerySpecification body,
// StackableAstVisitorContext<Integer> context) {
// // return visitQueryBody(node, context);
// 
// // Select select = ;
// this.visitSelect(body.getSelect(), context);
// // System.out.println("Columns = " + select.getSelectItems());
// 
// // System.out.println("FromClass = " + body.getFrom().get().getClass());
// 
// // System.out.println("From = " + body.getFrom().get());
// 
// // Optional<Expression> where = body.getWhere();
// // System.out.println("Where = " + where.get());
// // System.out.println("Group by = " + body.getGroupBy());
// // System.out.println("Order by = " + body.getOrderBy());
// 
// if (body.getLimit().isPresent()) {
// // System.out.println("Limit = " + body.getLimit().get());
// }
// 
// // final List<Expression> groupIds = Lists.newArrayList();
// Optional<GroupBy> gby = body.getGroupBy();
// if (gby.isPresent()) {
// this.visitGroupBy(gby.get(), context);
// // GroupBy group = gby.get();
// // for (GroupingElement ge : group.getGroupingElements()) {
// // ge.enumerateGroupingSets().forEach((e) -> e.stream().forEach((r) -> {
// // // rewriter.append(r.getClass()).append(",");
// // groupIds.add(r);
// // }));
// // }
// }
// Optional<Relation> from = body.getFrom();
// ///////////////////////////////////////////////////////////////////////////
// //
// System.out.println("======================================================");
// // List<SelectItem>
// SingleColumn single = null;
// 
// // int itemSize = select.getSelectItems().size();
// // int i = 0;
// // for (SelectItem col : select.getSelectItems()) {
// //
// // if (col instanceof SingleColumn) {
// // single = (SingleColumn) col;
// // processExpression(single.getExpression(), rewriter);
// // Optional<Identifier> alias = single.getAlias();
// //
// // if (alias.isPresent()) {
// // // 这个可能是function的列
// // rewriter.addColumnReference(alias.get().getValue(),
// single.getExpression(),
// // from);
// // System.out.println(
// // single.getExpression().getClass() + ":" + single.getExpression() + ":" +
// // alias.get());
// //
// // rewriter.append(" AS ").append(alias.get());
// // } else {
// // System.out.println(
// // "==================" + single.getExpression() + "<<" +
// // single.getExpression().getClass());
// // // rewriter.append(single.getExpression());
// // // processExpression(single.getExpression(), rewriter);
// // DereferenceExpression deref = null;
// // if (single.getExpression() instanceof DereferenceExpression) {
// // deref = (DereferenceExpression) single.getExpression();
// //
// // rewriter.addColumnReference(deref.getField().getValue(), deref, from);
// // } else if (single.getExpression() instanceof Identifier) {
// // if (!from.isPresent()) {
// // throw new IllegalStateException("have not set from \n" +
// query.toString());
// // }
// //
// // rewriter.addColumnReference( //
// // new ColName(((Identifier) single.getExpression()).getValue()) //
// // , null //
// // , from);
// //
// // } else {
// // throw new IllegalStateException(
// // "illegal type " + single.getExpression() + "," +
// // single.getExpression().getClass());
// // }
// //
// // }
// //
// // } else {
// //
// // }
// //
// // if (i++ < (itemSize - 1)) {
// // rewriter.append(",");
// // }
// // }
// 
// Relation rel = null;
// // Table table = null;
// if (from.isPresent()) {
// rel = from.get();
// // rewriter.newline().append("FROM ");
// // processRelation(rel, rewriter);
// 
// this.processRelation(rel, context);
// 
// } else {
// throw new IllegalStateException("have not set from \n" + body);
// }
// 
// 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 ");
// // }
// }
// 
// // for (int ii = 0; ii < groupIds.size(); ii++) {
// // processExpression(groupIds.get(ii), rewriter);
// // if (ii < groupIds.size() - 1) {
// // rewriter.append(",");
// // }
// // }
// 
// return null;
// }
// 
// @Override
// protected Void visitJoin(Join node, StackableAstVisitorContext<Integer>
// context) {
// 
// JoinCriteria criteria = node.getCriteria().orElse(null);
// String type = node.getType().toString();
// if (criteria instanceof NaturalJoin) {
// type = "NATURAL " + type;
// }
// 
// if (node.getType() != Join.Type.IMPLICIT) {
// // builder.append('(');
// }
// process(node.getLeft(), context);
// 
// // builder.append('\n');
// if (node.getType() == Join.Type.IMPLICIT) {
// // append(indent, ", ");
// } else {
// // append(indent, type).append(" JOIN ");
// }
// 
// process(node.getRight(), context);
// 
// if (node.getType() != Join.Type.CROSS && node.getType() !=
// Join.Type.IMPLICIT) {
// if (criteria instanceof JoinUsing) {
// JoinUsing using = (JoinUsing) criteria;
// // builder.append(" USING (").append(Joiner.on(",
// // ").join(using.getColumns())).append(")");
// } else if (criteria instanceof JoinOn) {
// JoinOn on = (JoinOn) criteria;
// // builder.append(" ON ").append(formatExpression(on.getExpression(),
// // parameters));
// } else if (!(criteria instanceof NaturalJoin)) {
// throw new UnsupportedOperationException("unknown join criteria: " +
// criteria);
// }
// }
// 
// if (node.getType() != Join.Type.IMPLICIT) {
// // builder.append(")");
// }
// 
// return null;
// 
// }
// 
// private void processRelation(Relation relation,
// StackableAstVisitorContext<Integer> indent) {
// // TODO: handle this properly
// if (relation instanceof Table) {
// // builder.append("TABLE ").append(((Table)
// relation).getName()).append('\n');
// } else {
// process(relation, indent);
// }
// }
// 
// }
public void process(Query query, Rewriter rewriter) {
    QuerySpecification body = (QuerySpecification) query.getQueryBody();
    Select select = body.getSelect();
    if (body.getLimit().isPresent()) {
    // System.out.println("Limit = " + body.getLimit().get());
    }
    final List<Expression> groupIds = Lists.newArrayList();
    Optional<GroupBy> gby = body.getGroupBy();
    if (gby.isPresent()) {
        GroupBy group = gby.get();
        for (GroupingElement ge : group.getGroupingElements()) {
            ge.enumerateGroupingSets().forEach((e) -> e.stream().forEach((r) -> {
                // rewriter.append(r.getClass()).append(",");
                groupIds.add(r);
            }));
        }
    }
    Optional<Relation> from = body.getFrom();
    // /////////////////////////////////////////////////////////////////////////
    // System.out.println("======================================================");
    // List<SelectItem>
    SingleColumn single = null;
    rewriter.append("SELECT ");
    int itemSize = select.getSelectItems().size();
    int i = 0;
    for (SelectItem col : select.getSelectItems()) {
        if (col instanceof SingleColumn) {
            single = (SingleColumn) col;
            processExpression(single.getExpression(), rewriter);
            Optional<Identifier> alias = single.getAlias();
            if (alias.isPresent()) {
                // 这个可能是function的列
                rewriter.addColumnReference(alias.get().getValue(), single.getExpression(), from);
                // System.out.println(
                // single.getExpression().getClass() + ":" + single.getExpression() + ":" +
                // alias.get());
                rewriter.append(" AS ").append(alias.get());
            } else {
                // System.out.println(
                // "==================" + single.getExpression() + "<<" +
                // single.getExpression().getClass());
                // rewriter.append(single.getExpression());
                // processExpression(single.getExpression(), rewriter);
                DereferenceExpression deref = null;
                if (single.getExpression() instanceof DereferenceExpression) {
                    deref = (DereferenceExpression) single.getExpression();
                    rewriter.addColumnReference(deref.getField().getValue(), deref, from);
                } else if (single.getExpression() instanceof Identifier) {
                    if (!from.isPresent()) {
                        throw new IllegalStateException("have not set from \n" + query.toString());
                    }
                    // 
                    // 
                    // 
                    rewriter.addColumnReference(new ColName(((Identifier) single.getExpression()).getValue()), null, from);
                } else {
                    throw new IllegalStateException("illegal type " + single.getExpression() + "," + single.getExpression().getClass());
                }
            }
        } else {
        }
        if (i++ < (itemSize - 1)) {
            rewriter.append(",");
        }
    }
    Relation rel = null;
    // Table table = null;
    if (from.isPresent()) {
        rel = from.get();
        rewriter.newline().append("FROM ");
        processRelation(rel, rewriter);
    } else {
        throw new IllegalStateException("have not set from \n" + query.toString());
    }
    Optional<Expression> w = body.getWhere();
    Expression where = null;
    if (w.isPresent()) {
        where = w.get();
        rewriter.newline().append("WHERE ");
        // rewriter.append(table);
        processExpression(where, rewriter);
    } else {
    // if (table != null) {
    // rewriter.newline().append("WHERE ");
    // }
    }
    for (int ii = 0; ii < groupIds.size(); ii++) {
        processExpression(groupIds.get(ii), rewriter);
        if (ii < groupIds.size() - 1) {
            rewriter.append(",");
        }
    }
}
Also used : QualifiedName(com.facebook.presto.sql.tree.QualifiedName) Table(com.facebook.presto.sql.tree.Table) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) Stack(java.util.Stack) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) Identifier(com.facebook.presto.sql.tree.Identifier) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) Join(com.facebook.presto.sql.tree.Join) WhenClause(com.facebook.presto.sql.tree.WhenClause) Lists(com.google.common.collect.Lists) Operator(com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator) Map(java.util.Map) SelectItem(com.facebook.presto.sql.tree.SelectItem) TableSubquery(com.facebook.presto.sql.tree.TableSubquery) Cast(com.facebook.presto.sql.tree.Cast) JoinOn(com.facebook.presto.sql.tree.JoinOn) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) GroupBy(com.facebook.presto.sql.tree.GroupBy) JoinCriteria(com.facebook.presto.sql.tree.JoinCriteria) Query(com.facebook.presto.sql.tree.Query) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) Relation(com.facebook.presto.sql.tree.Relation) Node(com.facebook.presto.sql.tree.Node) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) GroupingElement(com.facebook.presto.sql.tree.GroupingElement) Maps(com.google.common.collect.Maps) List(java.util.List) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) Expression(com.facebook.presto.sql.tree.Expression) DefaultTraversalVisitor(com.facebook.presto.sql.tree.DefaultTraversalVisitor) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) Optional(java.util.Optional) SqlDataFlowTopology(com.qlangtech.tis.sql.parser.SqlTaskNodeMeta.SqlDataFlowTopology) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Type(com.facebook.presto.sql.tree.Join.Type) Select(com.facebook.presto.sql.tree.Select) Assert(com.qlangtech.tis.common.utils.Assert) Collections(java.util.Collections) GroupBy(com.facebook.presto.sql.tree.GroupBy) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) GroupingElement(com.facebook.presto.sql.tree.GroupingElement) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation) Relation(com.facebook.presto.sql.tree.Relation) Identifier(com.facebook.presto.sql.tree.Identifier) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) SelectItem(com.facebook.presto.sql.tree.SelectItem) Select(com.facebook.presto.sql.tree.Select)

Example 3 with Operator

use of com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator in project tis by qlangtech.

the class TestGroupBySqlParser method processExpression.

// private void processExpression(Expression expression, Rewriter rewriter) {
// processExpression(expression, rewriter, false /* fromSelectItem */);
// }
private void processExpression(Expression expression, Rewriter rewriter) {
    if (expression instanceof ArithmeticBinaryExpression) {
        // ((COALESCE(pay.coupon_fee, 0) - COALESCE(pay.coupon_cost, 0)) *
        // COALESCE(pay.coupon_num, 0))
        ArithmeticBinaryExpression arithmeticBinaryExpression = (ArithmeticBinaryExpression) expression;
        rewriter.append("(");
        processExpression(arithmeticBinaryExpression.getLeft(), rewriter);
        Operator operator = arithmeticBinaryExpression.getOperator();
        rewriter.append(operator.getValue());
        processExpression(arithmeticBinaryExpression.getRight(), rewriter);
        rewriter.append(")");
    } else if (expression instanceof Cast) {
        Cast cast = (Cast) expression;
        rewriter.append("CAST(");
        processExpression(cast.getExpression(), rewriter);
        rewriter.append(",").append(cast.getType()).append(")");
    } else if (expression instanceof LogicalBinaryExpression) {
        process((LogicalBinaryExpression) expression, rewriter);
    } else if (expression instanceof ComparisonExpression) {
        process((ComparisonExpression) expression, rewriter);
    } else if (expression instanceof CoalesceExpression) {
        // COALESCE(a2.has_fetch, 0)
        process((CoalesceExpression) expression, rewriter);
    } else if (expression instanceof FunctionCall) {
        // "concat_ws"(',', "collect_set"("split"(i.batch_msg, '[\\w\\W]*\\|')[1]))
        process((FunctionCall) expression, rewriter);
    } else if (expression instanceof SearchedCaseExpression) {
        // (CASE WHEN ("sum"("op_and"(i.draw_status, 8)) > 0) THEN 1 ELSE 0 END)
        SearchedCaseExpression e = (SearchedCaseExpression) expression;
        process(e, rewriter);
    } else if (expression instanceof StringLiteral) {
        rewriter.append(expression);
    } else if (expression instanceof SubscriptExpression) {
        SubscriptExpression subscript = (SubscriptExpression) expression;
        processExpression(subscript.getBase(), rewriter);
        rewriter.append("[");
        processExpression(subscript.getIndex(), rewriter);
        rewriter.append("]");
    // rewriter.append("base:").append(subscript.getBase()).append(",index:").append(subscript.getIndex());
    } else if (expression instanceof DereferenceExpression) {
        DereferenceExpression dereference = (DereferenceExpression) expression;
        processExpression(dereference.getBase(), rewriter);
        rewriter.append(".").append(dereference.getField());
    } else if (expression instanceof Identifier) {
        rewriter.append(expression);
    } else if (expression instanceof LongLiteral) {
        LongLiteral longLiteral = (LongLiteral) expression;
        rewriter.append(longLiteral.getValue());
    } else {
        throw new IllegalStateException("illegal:" + expression.getClass() + ",expression:" + expression + ",loc:colnum:" + expression.getLocation().get().getColumnNumber() + ",line:" + expression.getLocation().get().getLineNumber());
    }
}
Also used : ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) Operator(com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator) Cast(com.facebook.presto.sql.tree.Cast) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Identifier(com.facebook.presto.sql.tree.Identifier) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression)

Aggregations

Operator (com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator)3 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)2 Cast (com.facebook.presto.sql.tree.Cast)2 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)2 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)2 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)2 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)2 Identifier (com.facebook.presto.sql.tree.Identifier)2 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)2 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)2 SearchedCaseExpression (com.facebook.presto.sql.tree.SearchedCaseExpression)2 StringLiteral (com.facebook.presto.sql.tree.StringLiteral)2 SubscriptExpression (com.facebook.presto.sql.tree.SubscriptExpression)2 AliasedRelation (com.facebook.presto.sql.tree.AliasedRelation)1 DefaultTraversalVisitor (com.facebook.presto.sql.tree.DefaultTraversalVisitor)1 Expression (com.facebook.presto.sql.tree.Expression)1 GroupBy (com.facebook.presto.sql.tree.GroupBy)1 GroupingElement (com.facebook.presto.sql.tree.GroupingElement)1 Join (com.facebook.presto.sql.tree.Join)1 Type (com.facebook.presto.sql.tree.Join.Type)1