Skip to content

Params isn't being passed for arrays of small numbers #108

Open
@shortcipher3

Description

@shortcipher3

Environment details

Google Colab

Steps to reproduce

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

SELECT @my_array AS my_array;
  1. 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 ["<ipython-input-6-fae3dea44cc2>"](https://localhost:8080/#), line 1, in <cell line: 0>
    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 "<string>", 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 <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.

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the googleapis/python-bigquery-magics API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions