Skip to content

Commit

Permalink
Make the useLegacySql option configurable (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
van-mronov authored Nov 12, 2023
1 parent eef9b09 commit fd11b4b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/req_bigquery.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ defmodule ReqBigQuery do
alias Req.Request
alias ReqBigQuery.Result

@allowed_options ~w(goth default_dataset_id project_id bigquery max_results)a
@allowed_options ~w(goth default_dataset_id project_id bigquery max_results use_legacy_sql)a
@base_url "https://bigquery.googleapis.com/bigquery/v2"
@max_results 10_000
@use_legacy_sql false

@doc """
Attaches to Req request.
Expand All @@ -35,6 +36,10 @@ defmodule ReqBigQuery do
The rows Stream can make multiple requests if `num_rows` returned is grather than `:max_results`.
Defaults to 10000.
* `:use_legacy_sql` - Optional. Specifies whether to use BigQuery's legacy SQL dialect for this query.
If set to false, the query will use BigQuery's GoogleSQL: https://cloud.google.com/bigquery/sql-reference/
The default value is false.
If you want to set any of these options when attaching the plugin, pass them as the second argument.
## Examples
Expand Down Expand Up @@ -108,6 +113,7 @@ defmodule ReqBigQuery do
options
|> Keyword.put_new(:base_url, @base_url)
|> Keyword.put_new(:max_results, @max_results)
|> Keyword.put_new(:use_legacy_sql, @use_legacy_sql)

request
|> Request.prepend_request_steps(bigquery_run: &run/1)
Expand All @@ -127,7 +133,7 @@ defmodule ReqBigQuery do
query
|> build_request_body(options[:default_dataset_id])
|> Map.put(:maxResults, options[:max_results])
|> Map.put(:useLegacySql, false)
|> Map.put(:useLegacySql, options[:use_legacy_sql])

%{request | url: uri}
|> Request.merge_options(auth: {:bearer, token}, json: json)
Expand Down
5 changes: 3 additions & 2 deletions test/req_bigquery_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule ReqBigQueryTest do
"defaultDataset" => %{"datasetId" => "my_awesome_dataset"},
"query" => "select * from iris",
"maxResults" => 10000,
"useLegacySql" => false
"useLegacySql" => true
}

assert URI.to_string(request.url) ==
Expand Down Expand Up @@ -50,7 +50,8 @@ defmodule ReqBigQueryTest do
opts = [
goth: ctx.test,
project_id: "my_awesome_project_id",
default_dataset_id: "my_awesome_dataset"
default_dataset_id: "my_awesome_dataset",
use_legacy_sql: true
]

assert response =
Expand Down

0 comments on commit fd11b4b

Please sign in to comment.