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

Raw SQL mode fails to extract table name from FROM clause when using variables or when using FROM in a subquery. #314

Closed
peikk0 opened this issue Feb 26, 2021 · 1 comment · Fixed by #318
Milestone

Comments

@peikk0
Copy link

peikk0 commented Feb 26, 2021

Bug Report

When using raw SQL queries, _extractFromClause() fails to extract the project, dataset and table name from the FROM clause when using variables or when using FROM in a subquery.

Expected Behavior

Variables are interpolated before extracting the FROM clause and there are no API errors when using raw SQL queries with variables and/or subqueries.

Actual Behavior

Given a variable $source_table set as my_project.my_dataset.my_table:

SELECT
  CURRENT_TIMESTAMP() AS time,
  SUM(cost) AS value
FROM
  `$source_table`
WHERE
  $__timeFilter(usage_start_time)
GROUP BY 1
ORDER BY 1 ASC

results in this error:

t=2021-02-26T02:15:51+0000 lvl=info msg=Requesting logger=data-proxy-log url=https://www.googleapis.com/bigquery/v2/projects/$source_table/datasets/undefined/tables/undefined
t=2021-02-26T02:15:52+0000 lvl=info msg="Request Completed" logger=context userId=1 orgId=1 uname=admin method=GET path=/api/datasources/proxy/2/bigquery/v2/projects/$source_table/datasets/undefined/tables/undefined status=400 remote_addr=172.17.0.1 time_ms=713 size=279 referer="http://localhost:3000/d/XXX/my-dashboard?editPanel=20&orgId=1"

Also if using FROM in a subquery earlier in the SELECT clause for instance:

SELECT
  CURRENT_TIMESTAMP() AS time,
  SUM(IFNULL((SELECT SUM(c.amount) FROM UNNEST(credits) c), 0)) AS value
FROM
  `$source_table`
WHERE
  $__timeFilter(usage_start_time)
GROUP BY 1
ORDER BY 1 ASC

results in this error:

t=2021-02-26T02:18:32+0000 lvl=info msg=Requesting logger=data-proxy-log url=https://www.googleapis.com/bigquery/v2/projects/NNEST%28credits/datasets/undefined/tables/undefined
t=2021-02-26T02:18:33+0000 lvl=info msg="Request Completed" logger=context userId=1 orgId=1 uname=admin method=GET path=/api/datasources/proxy/2/bigquery/v2/projects/NNEST(credits/datasets/undefined/tables/undefined status=400 remote_addr=172.17.0.1 time_ms=503 size=282 referer="http://localhost:3000/d/XXX/my-dashboard?editPanel=54&orgId=1"

Steps to Reproduce the Problem

Variable interpolation case:

  1. Create a textbox variable source_table set as the full table name
  2. Add a BigQuery panel using the variable source_table in the FROM clause (see example above)
  3. Notice error in Grafana logs

Subquery case:

  1. Add a BigQuery panel using a subquery in the SELECT clause (see example above)
  2. Notice error in Grafana logs

Specifications

  • Version: 2.0.1
  • Platform: OSX / Docker
  • Grafana Version: 7.4.3
@peikk0
Copy link
Author

peikk0 commented Feb 26, 2021

Also could we have an option for disabling "fixing" the raw queries? I prefer adding the partition filter explicitly myself and not having the query altered in any way (aside from variable/macro interpolation).

@ofir5300 ofir5300 added this to the 2.0.2 milestone Mar 31, 2021
ofir5300 added a commit that referenced this issue Mar 31, 2021
…s & table ID's which is stored in Grafana variables (Git: #294, #314)
@ofir5300 ofir5300 mentioned this issue Apr 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants