Извлечение каталогов из SQL в Trino parser

package org.example;
 
 
import io.trino.sql.parser.ParsingOptions;
import io.trino.sql.parser.SqlParser;
import io.trino.sql.tree.*;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
 
public class Main {
    public static void main(String[] args) {
        String sql1 = """
                select distinct
                        l1.userid,
                        r33.msisdn
                from
                        catalog1.schema1.table1 as l1
                left join (select distinct msisdn,
                       websso_guid
                from (
                    select  msisdn,
                            websso_guid,
                            row_number() over (partition by websso_guid order by business_dttm desc) as win
                    from catalog1.schema2.table2) as sso
                where sso.win = 1) as r33
                on from_utf8(l1.userid)=r33.websso_guid
                where    2+2=4
                        and from_utf8(l1.app_package_name) = 'app_package_name'
                        and from_utf8(l1.eventcategory) = 'stories'
                        and from_utf8(l1.eventlabel) = 'strs_main_08_cvm_paytag_platite_smartphonom_beskontaktno'
                        and from_utf8(l1.eventaction) = 'button_tap'
                        and(l1.business_dt between date '2023-08-01' and date '2023-08-31')
                """;
 
        String sql2 = "SELECT * FROM catalog1.schema1.table1 LEFT JOIN catalog1.schema1.table2 ON id";
 
        String sql3 = "SELECT * FROM catalog1.schema1.table1";
 
        String sql4 = """
                select *
                from catalog1.schema1.table3
                where 1=1
                --and raw_dt > date '2023-07-31'
                --and bundle_id like '%music%'
                --and bundle_id in ('mobile.music', 'music.android')
                --and media_source = 'media_source'
                and campaign = 'stories_goroda'
                limit
                20
                """;
 
        String sql5 = """
                with conv as (
                select msisdn
                from catalog2.schema2.table1
                where tbm = cast('2023-06-01' as date) and abnt_category_ext_1m = 'Convergent'
                )
                , prem as (
                select msisdn
                from catalog2.schema2.table1
                where tbm = cast('2023-06-01' as date) and abnt_category_ext_1m = 'Premium only'
                )
                , prem_kion as (
                select msisdn
                from catalog2.schema2.table1
                where tbm = cast('2023-06-01' as date) and abnt_category_ext_1m = 'KION+Premium'
                )
                , pre as
                (select c.msisdn  --1058211
                from conv c
                join catalog1.schema1.table4 p on cast(c.msisdn as decimal) = p.mob_num and p.tbm = cast('2023-06-01' as date)
                and p.krem = 1 --and p.is_abnt_wtch_1m = 1
                )
                select count(distinct p.msisdn) --991  --57
                from pre p
                join prem_kion pp on p.msisdn = pp.msisdn
                """;
 
        String sql6 = """
                select count(*) from catalog1.schema1.table5
                where
                    version_dt = to_date('2023-09-04','yyyy-mm-dd')""";
 
        String sql7 = """
                select
                    business_dt,
                    from_utf8(eventlabel) NazvanieStories,
                    count(eventaction)filter (where from_utf8(eventaction) = 'cover_show') CoverShow,
                    count(eventaction)filter (where from_utf8(eventaction) = 'cover_tap') CoverTap,
                    count(eventaction)filter (where from_utf8(eventaction) = 'slide_show' and from_utf8(eventcontent) = '1') FirstSlideView,
                    count(eventaction)filter (where from_utf8(eventaction) = 'button_tap') ButtonTap
                                
                from catalog1.schema1.table6
                                
                where 2+2=4
                and from_utf8(app_package_name) = 'app_package_name'
                and from_utf8(eventcategory) = 'stories'
                and from_utf8(eventlabel) like 'strs_main%'
                                
                group by 1,2
                                
                order by 1,ButtonTap desc""";
 
        String[] sqls = new String[]{sql1, sql2, sql3, sql4, sql5, sql6, sql7};
 
        for (String sql : sqls) {
            parse(sql);
        }
 
    }
 
    private static void parse(String sql) {
        ArrayList<String> results = new ArrayList<>();
        ArrayList<String> withTableName = new ArrayList<>();
 
        SqlParser sqlParser = new SqlParser();
 
        Statement statement = sqlParser.createStatement(sql, new ParsingOptions());
 
        for (Node n : statement.getChildren()) {
            if (n instanceof QuerySpecification qs) {
                Optional<Relation> from = qs.getFrom();
                if (from.isPresent()) {
                    Relation relation = from.get();
                    recursive(relation, results);
                }
            } else if (n instanceof With withNode) {
                recursive(n, results);
                withNode.getQueries().forEach(withQuery -> withTableName.add(withQuery.getName().toString()));
            }
        }
        results.removeAll(withTableName);
        Set<String> final_result = new HashSet<>(results);
 
        System.out.println(String.join("\n", final_result));
        System.out.println("======");
    }
 
    private static void recursive(Node node, ArrayList<String> result) {
        if (node instanceof Table t) {
            result.add(t.getName().toString());
        }
        for (Node child : node.getChildren()) {
            if (child instanceof Table t) {
                result.add(t.getName().toString());
            } else {
                recursive(child, result);
            }
        }
    }
}