Skip to content

Commit

Permalink
Always call BigQuery API with useLegacySql set to false (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
van-mronov authored Nov 11, 2023
1 parent 8fa2133 commit eef9b09
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/req_bigquery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ defmodule ReqBigQuery do
query
|> build_request_body(options[:default_dataset_id])
|> Map.put(:maxResults, options[:max_results])
|> Map.put(:useLegacySql, false)

%{request | url: uri}
|> Request.merge_options(auth: {:bearer, token}, json: json)
Expand Down Expand Up @@ -157,7 +158,7 @@ defmodule ReqBigQuery do
end

defp build_request_body(query, dataset) when dataset in ["", nil] do
%{query: query, useLegacySql: false}
%{query: query}
end

defp build_request_body(query, dataset) when is_binary(query) do
Expand Down
32 changes: 32 additions & 0 deletions test/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@ defmodule IntegrationTest do
use ExUnit.Case, async: true
@moduletag :integration

test "returns the Google BigQuery's response when BigQuery's GoogleSQL is used and default dataset is attached",
%{
test: goth
} do
credentials =
System.get_env("GOOGLE_APPLICATION_CREDENTIALS", "credentials.json")
|> File.read!()
|> Jason.decode!()

project_id = System.get_env("PROJECT_ID", credentials["project_id"])

source = {:service_account, credentials, []}
start_supervised!({Goth, name: goth, source: source, http_client: &Req.request/1})

# DATE_DIFF is not supported by BigQuery's legacy SQL.
# But the default value of useLegacySql is true.
# So the query fails if it is not changed to false.
query = """
SELECT *
FROM `bigquery-public-data.wikipedia.table_bands`
WHERE DATE_DIFF(CURRENT_DATE, CAST(datehour AS DATE), DAY) > 14
LIMIT 10
"""

response =
Req.new()
|> ReqBigQuery.attach(project_id: project_id, default_dataset_id: "wikipedia", goth: goth)
|> Req.post!(bigquery: query)

assert response.status == 200
end

test "returns the Google BigQuery's response", %{test: goth} do
credentials =
System.get_env("GOOGLE_APPLICATION_CREDENTIALS", "credentials.json")
Expand Down
3 changes: 2 additions & 1 deletion test/req_bigquery_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ defmodule ReqBigQueryTest do
assert Jason.decode!(request.body) == %{
"defaultDataset" => %{"datasetId" => "my_awesome_dataset"},
"query" => "select * from iris",
"maxResults" => 10000
"maxResults" => 10000,
"useLegacySql" => false
}

assert URI.to_string(request.url) ==
Expand Down

0 comments on commit eef9b09

Please sign in to comment.