GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Magic params flag fails with negative numbers · Issue #108 · googleapis/python-bigquery

This repository was archived by the owner on Mar 6, 2026. It is now read-only.
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Magic params flag fails with negative numbers #108

Copy link
Copy link
Closed
#213
#119
Closed
Magic params flag fails with negative numbers#108
#213
#119
Copy link
Assignees
plamut
Labels
api: bigqueryIssues related to the googleapis/python-bigquery 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: docsImprovement to the documentation for an API.

Description

@teebr
teebr
opened on May 13, 2020
Issue body actions

Environment details

Colab (Ubuntu 18.04 LTS, Python 3.6.9, pip 19.3.1, bigquery 1.21.0)

Issue/ Example

For a parameterised query, the magic fails to parse negative numbers.

For example, this parameter dict:

params = {
    "threshold": -5.0,
    "min_year": 2000
}

and this query:

%%bigquery  --project my-project-name --params $params
SELECT
    year, MIN(mean_temp) as min, COUNTIF(snow) / COUNT(snow) * 100 AS pc_snowing
FROM
     `bigquery-public-data.samples.gsod`
WHERE mean_temp < @threshold
AND year >= @min_year
GROUP BY year
ORDER BY year

runs fine if threshold is >= 0, but if it's negative, the Magic gets an exception:

UsageError: unrecognized arguments: -5.0, 'min_year': 2000}

This happens whether it's a dictionary or a JSON string, and happens with ints as well as floats.

Activity

  1. product-auto-label
    added
    api: bigqueryIssues related to the googleapis/python-bigquery API.
    on May 13, 2020
  2. teebr commented on May 13, 2020

    @teebr
    teebr
    on May 13, 2020
    Author
    More actions

    It seems this happens in the IPython's magic_arguments call to arg_split. That uses shlex, and I think that interprets whitespace followed by a hyphen as a new argument. Anyway, dumping the dictionary to JSON with no whitespace does work:

    pj = json.dumps(params, separators=(',', ':'))
    ...
    %%bigquery  --project my-project-name --params $pj
    
  3. plamut
    added
    type: questionRequest for information or clarification. Not an issue.
    on May 14, 2020
  4. plamut commented on May 14, 2020

    @plamut
    plamut
    on May 14, 2020
    Contributor
    More actions

    @teebr Interesting, thanks for the report!

    I was able to reproduce it, as well as confirming that the workaround works. We now need to see if there's an easy fix to that.

  5. plamut
    added
    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.
    and removed
    type: questionRequest for information or clarification. Not an issue.
    on May 14, 2020
  6. plamut
    self-assigned this
    on Jun 2, 2020
  7. plamut commented on Jun 2, 2020

    @plamut
    plamut
    on Jun 2, 2020
    Contributor
    More actions

    This doesn't seem easily solvable, unfortunately. Using the following minimal example, for instance:

    >>> params = {"num": -17}
    %%bigquery --params $params
    SELECT @num as num
    

    passes the following line contents to the BigQuery's _cell_magic() function:

    >>> line
    "--params {'num': -17}"

    which is tokenized in IPython's MagicArgumentParser.parse_argstring() method as follows:

    >>> argv
    ['--params', '{"num":', '-17}']

    The tokenizer can't really know that it should treat the tokens after "--params" as a single entity representing a Python dict, thus it's understandable why the above fails - -17} is treated as a yet another argument, and not as part of the value of another argument.

    BigQuery's magic actually has to manually reconstruct the dict from these fragments, which is a bit of a hack that deals with the symptom:

    ast.literal_eval("".join(args.params))

    In order to force the parser to treat the params dict as a single entity, the dict should be passed in as a JSON string as @teebr noted, and this limitation should be documented in bigquery cell magic docs.

    Even if not perfect, this solution probably has the best benefit/cost ratio.

  8. plamut
    added
    type: docsImprovement to the documentation for an API.
    on Jun 3, 2020
  9. plamut
    closed this as completedin #119on Jun 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @plamut
    plamut

Labels

api: bigqueryIssues related to the googleapis/python-bigquery 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: docsImprovement to the documentation for an API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.