Skip to content
This repository has been archived by the owner on Sep 3, 2022. It is now read-only.

_recurse_subqueries not compose the main query properly if subquery has dependency queries #697

Open
HaipengSu opened this issue Jul 12, 2018 · 1 comment

Comments

@HaipengSu
Copy link
Contributor

Hello,

I encountered a case that needs compose the main query from multiple subqueries which also have dependency queries. However, The final query didn't work as expected. I will show the case in an example.

--------------------- subQuery1 ---------------------

%%bq query -n subQuery1
select state, name, year
from `bigquery-public-data.usa_names.usa_1910_2013`
limit 10

subQuery1 works.
--------------------- subQuery2 ---------------------

%%bq query -n subQuery2 --subqueries subQuery1
,
subQuery2_q1 as (
  select state, name
  from subQuery1
  limit 10
)
select *
from subQuery2_q1

subQuery2 works with subQuery1 as dependency.
Since subQuery2 has its own common table expression, then comma is needed at the front to make it working.
--------------------- main Query ---------------------

%%bq query -n mainQuery --subqueries subQuery2
select *
from subQuery2
limit 10

main query will look like,

WITH subQuery1 AS (
  select state, name, year
  from `bigquery-public-data.usa_names.usa_1910_2013`
  limit 10
),
subQuery2 AS (
  ,
  subQuery2_q1 as (
    select state, name
    from subQuery1
    limit 10
  )  
  select *
  from subQuery2_q1
)
select *
from subQuery2
limit 10

In this case, the main query won't work, because subQuery2 here is not valid.

I am looking at the _recurse_subqueries functionhere, instead of putting subQuery1 and subQuery2 into a flat list, should subQuery1 be inside of subQuery2?

And Query should be,

with subQuery2 AS (
  WITH subQuery1 AS (
    select state, name, year
    from `bigquery-public-data.usa_names.usa_1910_2013`
    limit 10
  ),
  subQuery2_q1 as (
    select state, name
    from subQuery1
    limit 10
  )
  select *
  from subQuery2_q1
)
select *
from subQuery2
limit 10

or

  WITH subQuery1 AS (
    select state, name, year
    from `bigquery-public-data.usa_names.usa_1910_2013`
    limit 10
),
subQuery2 AS (
 with subQuery2_q1 as (
    select state, name
    from subQuery1
    limit 10
  )
  select *
  from subQuery2_q1
)
select *
from subQuery2
limit 10

thanks a lot.

@HaipengSu
Copy link
Contributor Author

any update?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant