Skip to content

解析with......insert overwrite......类sql问题 #4933

@debughx

Description

@debughx

使用visitor遍历sql的SQLExprTableSource,发现vistitor在遍历类似 wtih......insert overwrite......这种类型的sql时,不会遍历with里的表,而with......select 则不会出现这种情况。

visitor定义如下:

public static class TableVisitor extends OdpsASTVisitorAdapter {

    public List<String> tableList = new ArrayList<>();

    @Override
    public boolean visit(SQLExprTableSource tableSource) {
        tableList.add(tableSource.getTableName());
        return true;
    }
}

遍历代码如下:

 String sql1 = "with \n" +
        "  a as (select * from src where key is not  null),\n" +
        "  b as (select  * from src2 where value>0),\n" +
        "  c as (select * from src3 where value>0),\n" +
        "  d as (select a.key,b.value from a join b on a.key=b.key  ),\n" +
        "  e as (select a.key,c.value from a left outer join c on a.key=c.key and c.key is not null )\n" +
        "  insert overwrite table x select * from y;";

SQLStatement statement = SQLUtils.parseSingleStatement(sql1, DbType.odps, false);
TableVisitor visitor = new TableVisitor();
statement.accept(visitor);
System.out.println(visitor.tableList.toString());

打印结果:
[x, y]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions