Search in sources :

Example 1 with ModifyFromSql

use of io.mycat.hbt.ast.modify.ModifyFromSql in project Mycat2 by MyCATApache.

the class SchemaConvertor method transforSchema.

public Schema transforSchema(ParseNode parseNode) {
    if (parseNode instanceof CallExpr) {
        CallExpr node = (CallExpr) parseNode;
        String name = node.getName().toLowerCase();
        List<ParseNode> exprList = node.getArgs().getExprs();
        HBTOp op = map.get(name);
        if (op == null) {
            System.err.println(name);
            Objects.requireNonNull(name);
        }
        switch(op) {
            case UNION_DISTINCT:
            case UNION_ALL:
            case EXCEPT_DISTINCT:
            case EXCEPT_ALL:
            case INTERSECT_DISTINCT:
            case INTERSECT_ALL:
                {
                    List<Schema> collect = exprList.stream().map(expr -> transforSchema(expr)).collect(Collectors.toList());
                    return set(op, collect);
                }
            case FROM_TABLE:
                {
                    List<String> collect = exprList.stream().map(i -> i.toString()).collect(Collectors.toList());
                    return fromTable(collect);
                }
            case FROM_REL_TO_SQL:
                {
                    Schema schema = transforSchema(exprList.get(1));
                    return new FromRelToSqlSchema(exprList.get(0).toString(), schema);
                }
            case FROM_SQL:
                {
                    List<FieldType> fieldTypes;
                    String targetName = null;
                    String sql = null;
                    switch(exprList.size()) {
                        case 2:
                            {
                                targetName = exprList.get(0).toString();
                                fieldTypes = Collections.emptyList();
                                sql = exprList.get(1).toString();
                                break;
                            }
                        case 3:
                            {
                                targetName = exprList.get(0).toString();
                                fieldTypes = fields(exprList.get(1));
                                sql = exprList.get(2).toString();
                                break;
                            }
                        default:
                            throw new IllegalArgumentException();
                    }
                    return new FromSqlSchema(fieldTypes, targetName, sql);
                }
            case FILTER_FROM_TABLE:
                {
                    List<String> collect = exprList.subList(1, exprList.size()).stream().map(i -> i.toString()).collect(Collectors.toList());
                    return new FilterFromTableSchema(transforExpr(exprList.get(0)), collect);
                }
            case MAP:
                {
                    List<Expr> collect = exprList.subList(1, exprList.size()).stream().map(i -> transforExpr(i)).collect(Collectors.toList());
                    Schema schema = transforSchema(exprList.get(0));
                    return map(schema, collect);
                }
            case FILTER:
                {
                    Schema schema = transforSchema(exprList.get(0));
                    Expr expr = transforExpr(exprList.get(1));
                    return filter(schema, expr);
                }
            case LIMIT:
                {
                    Schema schema = transforSchema(exprList.get(0));
                    Number offset = getNumber(exprList.get(1));
                    Number limit = getNumber(exprList.get(2));
                    return limit(schema, offset, limit);
                }
            case ORDER:
                {
                    List<OrderItem> orderItemList = order(exprList.subList(1, exprList.size()));
                    Schema schema = transforSchema(exprList.get(0));
                    return orderBy(schema, orderItemList);
                }
            case GROUP:
                {
                    int size = exprList.size();
                    CallExpr source = (CallExpr) exprList.get(0);
                    CallExpr keys = (CallExpr) exprList.get(1);
                    Schema schema = transforSchema(source);
                    List<AggregateCall> aggregating = Collections.emptyList();
                    List<GroupKey> groupkeys = keys(keys);
                    switch(size) {
                        case 2:
                            {
                                break;
                            }
                        case 3:
                            {
                                CallExpr exprs = (CallExpr) exprList.get(2);
                                aggregating = aggregating(exprs);
                                break;
                            }
                        default:
                            throw new UnsupportedOperationException();
                    }
                    return groupBy(schema, groupkeys, aggregating);
                }
            case TABLE:
                {
                    List<FieldType> fields = fields(exprList.get(0));
                    List<Object> values = values(exprList.get(1));
                    return table(fields, values);
                }
            case DISTINCT:
                {
                    Schema schema = transforSchema(exprList.get(0));
                    return distinct(schema);
                }
            case RENAME:
                {
                    Schema schema = transforSchema(exprList.get(0));
                    List<String> iterms = exprList.subList(1, exprList.size()).stream().map(i -> i.toString()).collect(Collectors.toList());
                    return new RenameSchema(schema, iterms);
                }
            case INNER_JOIN:
            case LEFT_JOIN:
            case RIGHT_JOIN:
            case FULL_JOIN:
            case SEMI_JOIN:
            case ANTI_JOIN:
                {
                    Expr expr = transforExpr(exprList.get(0));
                    Schema schema = transforSchema(exprList.get(1));
                    Schema schema2 = transforSchema(exprList.get(2));
                    return join(op, expr, schema, schema2);
                }
            case CORRELATE_INNER_JOIN:
            case CORRELATE_LEFT_JOIN:
                {
                    String refName = exprList.get(0).toString();
                    Schema leftschema = transforSchema(exprList.get(1));
                    Schema rightschema = transforSchema(exprList.get(2));
                    return correlate(op, refName, leftschema, rightschema);
                }
            case EXPLAIN:
                {
                    Schema schema = transforSchema(((CallExpr) (parseNode)).getArgs().getExprs().get(0));
                    return new CommandSchema(HBTOp.EXPLAIN, schema);
                }
            case EXPLAIN_SQL:
                {
                    Schema schema = transforSchema(((CallExpr) (parseNode)).getArgs().getExprs().get(0));
                    return new CommandSchema(HBTOp.EXPLAIN_SQL, schema);
                }
            case MODIFY_FROM_SQL:
                {
                    return new ModifyFromSql(exprList.get(0).toString(), exprList.get(1).toString());
                }
            case MERGE_MODIFY:
                {
                    return new MergeModify(exprList.stream().map(i -> (ModifyFromSql) transforSchema(i)).collect(Collectors.toList()));
                }
            default:
                {
                    throw new UnsupportedOperationException();
                }
        }
    } else if (parseNode instanceof ParenthesesExpr) {
        ParenthesesExpr parseNode1 = (ParenthesesExpr) parseNode;
        if (parseNode1.getExprs().size() == 1) {
            return transforSchema(parseNode1.getExprs().get(0));
        } else {
        }
    } else {
        return null;
    }
    return null;
}
Also used : io.mycat.hbt.parser.literal(io.mycat.hbt.parser.literal) java.util(java.util) Literal(io.mycat.hbt.ast.base.Literal) HBTOp(io.mycat.hbt.ast.HBTOp) io.mycat.hbt.ast.base(io.mycat.hbt.ast.base) MergeModify(io.mycat.hbt.ast.modify.MergeModify) CallExpr(io.mycat.hbt.parser.CallExpr) ParenthesesExpr(io.mycat.hbt.parser.ParenthesesExpr) Collectors(java.util.stream.Collectors) ParseNode(io.mycat.hbt.parser.ParseNode) BigDecimal(java.math.BigDecimal) ModifyFromSql(io.mycat.hbt.ast.modify.ModifyFromSql) NotNull(org.jetbrains.annotations.NotNull) io.mycat.hbt.ast.query(io.mycat.hbt.ast.query) ModifyFromSql(io.mycat.hbt.ast.modify.ModifyFromSql) MergeModify(io.mycat.hbt.ast.modify.MergeModify) HBTOp(io.mycat.hbt.ast.HBTOp) CallExpr(io.mycat.hbt.parser.CallExpr) ParenthesesExpr(io.mycat.hbt.parser.ParenthesesExpr) ParseNode(io.mycat.hbt.parser.ParseNode) CallExpr(io.mycat.hbt.parser.CallExpr) ParenthesesExpr(io.mycat.hbt.parser.ParenthesesExpr)

Example 2 with ModifyFromSql

use of io.mycat.hbt.ast.modify.ModifyFromSql in project Mycat2 by MyCATApache.

the class ExplainVisitor method visit.

@Override
public void visit(MergeModify mergeModify) {
    append(mergeModify.getOp().getFun());
    append("(");
    List<ModifyFromSql> list = StreamSupport.stream(mergeModify.getList().spliterator(), false).collect(Collectors.toList());
    int size = list.size();
    for (int i = 0; i < size - 1; i++) {
        ModifyFromSql modifyFromSql = list.get(i);
        modifyFromSql.accept(this);
        append(",");
    }
    list.get(size - 1).accept(this);
    append(")");
}
Also used : ModifyFromSql(io.mycat.hbt.ast.modify.ModifyFromSql)

Aggregations

ModifyFromSql (io.mycat.hbt.ast.modify.ModifyFromSql)2 HBTOp (io.mycat.hbt.ast.HBTOp)1 io.mycat.hbt.ast.base (io.mycat.hbt.ast.base)1 Literal (io.mycat.hbt.ast.base.Literal)1 MergeModify (io.mycat.hbt.ast.modify.MergeModify)1 io.mycat.hbt.ast.query (io.mycat.hbt.ast.query)1 CallExpr (io.mycat.hbt.parser.CallExpr)1 ParenthesesExpr (io.mycat.hbt.parser.ParenthesesExpr)1 ParseNode (io.mycat.hbt.parser.ParseNode)1 io.mycat.hbt.parser.literal (io.mycat.hbt.parser.literal)1 BigDecimal (java.math.BigDecimal)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 NotNull (org.jetbrains.annotations.NotNull)1