From fd11b4bdec0766f2aa1fb87096f3b65940225b68 Mon Sep 17 00:00:00 2001 From: Ivan Mironov Date: Mon, 13 Nov 2023 00:00:54 +0100 Subject: [PATCH] Make the useLegacySql option configurable (#27) --- lib/req_bigquery.ex | 10 ++++++++-- test/req_bigquery_test.exs | 5 +++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/req_bigquery.ex b/lib/req_bigquery.ex index 0d9934f..f433ba0 100644 --- a/lib/req_bigquery.ex +++ b/lib/req_bigquery.ex @@ -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. @@ -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 @@ -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) @@ -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) diff --git a/test/req_bigquery_test.exs b/test/req_bigquery_test.exs index e2ff854..331d84b 100644 --- a/test/req_bigquery_test.exs +++ b/test/req_bigquery_test.exs @@ -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) == @@ -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 =