Bigquery SQL失败,带有引用其他表的相关子查询,除非可以将其解开。

huangapple go评论95阅读模式
英文:

Bigquery SQL fails with Correlated subqueries that reference other tables are not supported unless they can be de-correlated

问题

以下是您要翻译的内容:

  1. BigQuery fails on my query with the following error, and I'm not sure how to mitigate this.
  2. > Query error: Correlated subqueries that reference other tables are not
  3. > supported unless they can be de-correlated, such as by transforming
  4. > them into an efficient JOIN. at [31:1]
  5. #### Standalone SQL Query (and temporary tables) to reproduce:
  6. create temporary table records (
  7. ID int64,
  8. Events array<struct<
  9. Tag string,
  10. Info string,
  11. Citations array<struct<
  12. Tag string,
  13. SourceID int64>>>>);
  14. insert into records values
  15. (1, [
  16. ('A', 'C', [('AA', 1000), ('AB', 1001)]),
  17. ('A', 'C', [('AA', 1000), ('AB', 1001)]),
  18. ('B', 'D', [('BA', 1000), ('BB', 1001)])]);
  19. create temporary table sources (
  20. ID int64,
  21. Title string);
  22. insert into sources values
  23. (1000, "ABCD"),
  24. (1001, "EFGH");
  25. select
  26. Record.ID,
  27. array(
  28. select as struct
  29. Event.Tag,
  30. Event.Info,
  31. array(
  32. select as struct
  33. Citation.SourceID,
  34. Citation.Tag,
  35. Source.Title
  36. from unnest(Event.Citations) as Citation
  37. left join sources as Source on Citation.SourceID = Source.ID
  38. ) as Citations
  39. from unnest(Record.Events) as Event) as Events
  40. from records as Record;

The events table looks like (in json):

[{
"ID": "1",
"Events": [{
"Tag": "A",
"Info": "C",
"Citations": [{
"Tag": "AA",
"SourceID": "1000"
}, {
"Tag": "AB",
"SourceID": "1001"
}]
}, {
"Tag": "A",
"Info": "C",
"Citations": [{
"Tag": "AA",
"SourceID": "1000"
}, {
"Tag": "AB",
"SourceID": "1001"
}]
}, {
"Tag": "B",
"Info": "D",
"Citations": [{
"Tag": "BA",
"SourceID": "1000"
}, {
"Tag": "BB",
"SourceID": "1001"
}]
}]
}]

The sources table looks like:
[{
"ID": "1000",
"Title": "ABCD"
}, {
"ID": "1001",
"Title": "EFGH"
}]

  1. I'd like the output to look like:
  2. [{
  3. "ID": "1",
  4. "Events": [{
  5. "Tag": "A",
  6. "Citations": [{
  7. "Tag": "AA",
  8. "SourceID": "1000",
  9. "Title": "ABCD"
  10. }, {
  11. "Tag": "AB",
  12. "SourceID": "1001",
  13. "Title": "EFGH"
  14. }]
  15. }, {
  16. "Tag": "B",
  17. "Citations": [{
  18. "Tag": "BA",
  19. "SourceID": "1000",
  20. "Title": "ABCD"
  21. }, {
  22. "Tag": "BB",
  23. "SourceID": "1001",
  24. "Title": "EFGH"
  25. }]
  26. }]
英文:

BigQuery fails on my query with the following error, and I'm not sure how to mitigate this.

> Query error: Correlated subqueries that reference other tables are not
> supported unless they can be de-correlated, such as by transforming
> them into an efficient JOIN. at [31:1]

Standalone SQL Query (and temporary tables) to reproduce:

  1. create temporary table records (
  2. ID int64,
  3. Events array&lt;struct&lt;
  4. Tag string,
  5. Info string,
  6. Citations array&lt;struct&lt;
  7. Tag string,
  8. SourceID int64&gt;&gt;&gt;&gt;);
  9. insert into records values
  10. (1, [
  11. (&#39;A&#39;, &#39;C&#39;, [(&#39;AA&#39;, 1000), (&#39;AB&#39;, 1001)]),
  12. (&#39;A&#39;, &#39;C&#39;, [(&#39;AA&#39;, 1000), (&#39;AB&#39;, 1001)]),
  13. (&#39;B&#39;, &#39;D&#39;, [(&#39;BA&#39;, 1000), (&#39;BB&#39;, 1001)])]);
  14. create temporary table sources (
  15. ID int64,
  16. Title string);
  17. insert into sources values
  18. (1000, &quot;ABCD&quot;),
  19. (1001, &quot;EFGH&quot;);
  20. select
  21. Record.ID,
  22. array(
  23. select as struct
  24. Event.Tag,
  25. Event.Info,
  26. array(
  27. select as struct
  28. Citation.SourceID,
  29. Citation.Tag,
  30. Source.Title
  31. from unnest(Event.Citations) as Citation
  32. left join sources as Source on Citation.SourceID = Source.ID
  33. ) as Citations
  34. from unnest(Record.Events) as Event) as Events
  35. from records as Record;

The events table looks like (in json):

  1. [{
  2. &quot;ID&quot;: &quot;1&quot;,
  3. &quot;Events&quot;: [{
  4. &quot;Tag&quot;: &quot;A&quot;,
  5. &quot;Info&quot;: &quot;C&quot;,
  6. &quot;Citations&quot;: [{
  7. &quot;Tag&quot;: &quot;AA&quot;,
  8. &quot;SourceID&quot;: &quot;1000&quot;
  9. }, {
  10. &quot;Tag&quot;: &quot;AB&quot;,
  11. &quot;SourceID&quot;: &quot;1001&quot;
  12. }]
  13. }, {
  14. &quot;Tag&quot;: &quot;A&quot;,
  15. &quot;Info&quot;: &quot;C&quot;,
  16. &quot;Citations&quot;: [{
  17. &quot;Tag&quot;: &quot;AA&quot;,
  18. &quot;SourceID&quot;: &quot;1000&quot;
  19. }, {
  20. &quot;Tag&quot;: &quot;AB&quot;,
  21. &quot;SourceID&quot;: &quot;1001&quot;
  22. }]
  23. }, {
  24. &quot;Tag&quot;: &quot;B&quot;,
  25. &quot;Info&quot;: &quot;D&quot;,
  26. &quot;Citations&quot;: [{
  27. &quot;Tag&quot;: &quot;BA&quot;,
  28. &quot;SourceID&quot;: &quot;1000&quot;
  29. }, {
  30. &quot;Tag&quot;: &quot;BB&quot;,
  31. &quot;SourceID&quot;: &quot;1001&quot;
  32. }]
  33. }]
  34. }]

The sources table looks like:

  1. [{
  2. &quot;ID&quot;: &quot;1000&quot;,
  3. &quot;Title&quot;: &quot;ABCD&quot;
  4. }, {
  5. &quot;ID&quot;: &quot;1001&quot;,
  6. &quot;Title&quot;: &quot;EFGH&quot;
  7. }]

I'd like the output to look like:

  1. [{
  2. &quot;ID&quot;: &quot;1&quot;,
  3. &quot;Events&quot;: [{
  4. &quot;Tag&quot;: &quot;A&quot;,
  5. &quot;Citations&quot;: [{
  6. &quot;Tag&quot;: &quot;AA&quot;,
  7. &quot;SourceID&quot;: &quot;1000&quot;,
  8. &quot;Title&quot;: &quot;ABCD&quot;
  9. }, {
  10. &quot;Tag&quot;: &quot;AB&quot;,
  11. &quot;SourceID&quot;: &quot;1001&quot;,
  12. &quot;Title&quot;: &quot;EFGH&quot;
  13. }]
  14. }, {
  15. &quot;Tag&quot;: &quot;B&quot;,
  16. &quot;Citations&quot;: [{
  17. &quot;Tag&quot;: &quot;BA&quot;,
  18. &quot;SourceID&quot;: &quot;1000&quot;,
  19. &quot;Title&quot;: &quot;ABCD&quot;
  20. }, {
  21. &quot;Tag&quot;: &quot;BB&quot;,
  22. &quot;SourceID&quot;: &quot;1001&quot;,
  23. &quot;Title&quot;: &quot;EFGH&quot;
  24. }]
  25. }]
  26. }]

答案1

得分: 1

你可以尝试以下的代码:

  1. SELECT
  2. ID,
  3. ARRAY_AGG((SELECT AS STRUCT Event.* EXCEPT(Citations), Citations)) AS Events
  4. FROM (
  5. SELECT
  6. r.ID,
  7. ANY_VALUE(e) Event, --&gt; will discard *e.Citations* in outer query whatever it is
  8. ARRAY_AGG((SELECT AS STRUCT c.*, s.Title)) Citations
  9. FROM records r
  10. CROSS JOIN UNNEST(r.events) e WITH offset
  11. CROSS JOIN UNNEST(e.citations) c
  12. LEFT JOIN sources s ON s.ID = c.SourceID
  13. GROUP BY r.ID, offset
  14. ) GROUP BY ID;

查询结果

Bigquery SQL失败,带有引用其他表的相关子查询,除非可以将其解开。

英文:

You can try below instead.

  1. SELECT
  2. ID,
  3. ARRAY_AGG((SELECT AS STRUCT Event.* EXCEPT(Citations), Citations)) AS Events
  4. FROM (
  5. SELECT
  6. r.ID,
  7. ANY_VALUE(e) Event, --&gt; will discard *e.Citations* in outer query whatever it is
  8. ARRAY_AGG((SELECT AS STRUCT c.*, s.Title)) Citations
  9. FROM records r
  10. CROSS JOIN UNNEST(r.events) e WITH offset
  11. CROSS JOIN UNNEST(e.citations) c
  12. LEFT JOIN sources s ON s.ID = c.SourceID
  13. GROUP BY r.ID, offset
  14. ) GROUP BY ID;

Query results

Bigquery SQL失败,带有引用其他表的相关子查询,除非可以将其解开。

huangapple
  • 本文由 发表于 2023年3月1日 14:04:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定