googleapis / googleapis/google-cloud-python

Params isn't being passed for arrays of small numbers

Đang mở
#15,519 2 bình luận 0 reaction 1 người được giao Được giao cho @tswast Xem trên GitHub
api: bigquery priority: p2 type: bug
Ngôn ngữ chính
Python
Star
5.4k
Fork
1.8k
Merge trung bình
3 ngày 4 giờ
Pull request đã merge (30 ngày)
122

Mô tả

#### 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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.