sql-metadata icon indicating copy to clipboard operation
sql-metadata copied to clipboard

Got uncorrect table names for complex sql

Open dl-sdu opened this issue 2 years ago • 0 comments

Firstly thanks for your tool, it helps a lot. I found some bugs when I used it, hope it could help improve this tool.

Here is the case of the sql query

select
    event_day,
    cuid,
    event_product_all,
    max(os_name) as os_name,
    max(app_version) as app_version,
    max(if(event_product_all ='tomas' ,if(is_bdapp_new='1',ch_4th_class,'-'),ta.channel) ) as channel,
    max(age) as age,
    max(age_point) as age_point,
    max(is_bdapp_new) as is_new,
    action_type,
    max(if(is_feed_dau=1, immersive_type, 0)) AS detail_page_type
from
( 
    select  event_day  ,
            event_product_all,
            os_name,
            app_version,
            channel,
            age,
            age_point,
            is_bdapp_new,
            action_type,
            is_feed_dau,
            immersive_type,
            attr_channel
    from bdapp_ads_bhv_cuid_all_1d
    lateral view explode(array(
        case when is_bdapp_dau=1 then 'bdapp' end,
        case when is_feed_dau=1 then 'feed' end,
        case when is_search_dau=1 then 'search' end,
        case when is_novel_dau=1 then 'novel' end,
        case when is_tts_dau=1 then 'radio' end
        )) lv AS action_type
    lateral view explode(
        case when event_product = 'lite' and appid in ('hao123', 'flyflow', 'lite_mission') then array('lite', appid)
        when event_product = 'lite' and appid = '10001' then array('lite', 'purelite')
        else array(event_product) end 
        ) lv AS event_product_all
    where event_day in ('20230102')
        and event_product in ('lite', 'tomas')
        and is_bdapp_dau = '1'
        and action_type is not null
)ta  
left outer join
(
    select channel,max(split(fourth_category,'~')[2]) as ch_4th_class 
    from udw_ns.default.ug_dim_channel_new_df
    where event_day = '20230102'
    group by  channel

)tb on ta.attr_channel=tb.channel
group by event_day, cuid, event_product_all, action_type
limit 100

Obviously, there are two tables used in this query which are baiduapp_ads_bhv_cuid_all_1d & ug_dim_channel_new_df. However this is what I got from Parser(sql).tables

['bdapp_ads_bhv_cuid_all_1d', 'array', 'case', 'appid', 'event_product', 'ug_dim_channel_new_df']

dl-sdu avatar Feb 02 '23 10:02 dl-sdu