googleapis / googleapis/google-cloud-python

Params isn't being passed for arrays of small numbers

Abierto
#15,519 2 comentarios 0 reacciones 1 asignado Asignado a @tswast Ver en GitHub
api: bigquery priority: p2 type: bug
Lenguaje dominante
Python
Estrellas
5.4k
Forks
1.8k
Merge medio
3 d 4 h
PR fusionados (30 d)
122

Descripción

#### Environment details

Google Colab

#### Steps to reproduce

1. Authenticate
```
from google.colab import auth
auth.authenticate_user()
```
2. Get some arrays
```
array1 = {"my_array": [1.0, 1.0, -3.0]}
array2 = {"my_array": [0.1, 0.1, -0.3]}
```
3. Run a query - this throws an error and shouldn't
```
%%bigquery the_array --project my-project --params $array2

SELECT @my_array AS my_array;
```
4. Run a query - this is nearly identical but doesn't throw the error
```
%%bigquery the_array --project my-project --params $array1

SELECT @my_array AS my_array;
```

#### Stack trace
```
Traceback (most recent call last):
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/bigquery.py", line 440, in _parse_magic_args
params_option_value, rest_of_args = _split_args_line(line)
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/bigquery.py", line 487, in _split_args_line
tree = lap.Parser(lap.Lexer(line)).input_line()
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/line_arg_parser/parser.py", line 201, in input_line
options = self.option_list()
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/line_arg_parser/parser.py", line 262, in option_list
option = self.params_option()
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/line_arg_parser/parser.py", line 321, in params_option
opt_value = self.py_dict()
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/line_arg_parser/parser.py", line 337, in py_dict
dict_items = self.dict_items()
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/line_arg_parser/parser.py", line 353, in dict_items
item = self.dict_item()
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/line_arg_parser/parser.py", line 381, in dict_item
value = self.py_value()
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/line_arg_parser/parser.py", line 435, in py_value
list_node = self.py_list()
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/line_arg_parser/parser.py", line 473, in py_list
items = self.collection_items()
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/line_arg_parser/parser.py", line 489, in collection_items
item = self.collection_item()
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/line_arg_parser/parser.py", line 514, in collection_item
result = self.py_value()
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/line_arg_parser/parser.py", line 444, in py_value
self.error(msg, exc_type=lap_exceptions.QueryParamsParseError)
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/line_arg_parser/parser.py", line 191, in error
raise exc_type(message)
QueryParamsParseError: Unexpected token type UNKNOWN at position 65.
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/local/lib/python3.11/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File [""](https://localhost:8080/#), line 1, in
get_ipython().run_cell_magic('bigquery', 'the_array --project my-project --params $array2', '\nSELECT @my_array AS my_array;\n')
File "/usr/local/lib/python3.11/dist-packages/google/colab/_shell.py", line 358, in run_cell_magic
return super().run_cell_magic(magic_name, line, cell)
File "/usr/local/lib/python3.11/dist-packages/IPython/core/interactiveshell.py", line 2473, in run_cell_magic
result = fn(*args, **kwargs)
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/bigquery.py", line 419, in _cell_magic
params, args = _parse_magic_args(line)
File "/usr/local/lib/python3.11/dist-packages/bigquery_magics/bigquery.py", line 443, in _parse_magic_args
raise SyntaxError(
File "", line unknown
SyntaxError: --params is not a correctly formatted JSON string or a JSON serializable dictionary
```

#### How I got here
From here:

https://cloud.google.com/python/docs/reference/bigquery/latest/magics

I followed the link to the BigQuery Magics Documentation, clicking around on that documentation brings me to:

https://googleapis.dev/python/bigquery-magics/latest/magics.html#module-bigquery_magics.bigquery

Which has an API reference section, the `--params` parameter has the following documentation

```
--params (Optional[line argument]):

If present, the argument following the --params flag must be either:

str - A JSON string representation of a dictionary in the format {"param_name": "param_value"} (ex. {"num": 17}). Use of the parameter in the query should be indicated with @param_name. See In[5] in the Examples section below.

dict reference - A reference to a dict in the format {"param_name": "param_value"}, where the value types must be JSON serializable. The variable reference is indicated by a $ before the variable name (ex. $my_dict_var). See In[6] and In[7] in the Examples section below.
```

I have a much bigger array that I want to use in queries and I would like to not hardcode it, so passing it as a parameter would be ideal. Not sure if there are other ways to do that.

I can use the python api with the same array, but I like the bigquery magic highlighting in Colab better for sharing with a group.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.