How to get all database tables referenced in a presto query using presto-parser

丶灬走出姿态 提交于 2021-02-11 14:52:38

问题


I'm using presto-parser to figure out which database tables are being queried in any given Presto query. I implemented a class that extends DefaultTraversalVisitor like this

public class ProcessTables extends DefaultTraversalVisitor<Void, Void> {
  private Set<String> tables;

  ProcessTables() {
    this.tables = new LinkedHashSet<>();
  }

  Set<String> getTables() {
    return tables;
  }

  @Override
  protected Void visitTable(Table node, Void context) {
    this.tables.add(node.getName().toString());
    return visitQueryBody(node, context);
  }
}

However, I find that conceptual tables (I'm not sure of the correct term here) are also treated as Tables without a way to distinguish them from actual database tables. For example

with RANDOMNAME as (
    select  column_a, column_b
    from    db.tablename
    where   condition
)

would visitTable for both RANDOMNAME and db.tablename. Is there a better way to do this? Am I missing something, like another method that I should override as well to distinguish between the two?

来源:https://stackoverflow.com/questions/61361107/how-to-get-all-database-tables-referenced-in-a-presto-query-using-presto-parser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!