Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Underscore Prepended to Numeric Column Labels #1093

Open
b-schmeling opened this issue Jul 10, 2024 · 3 comments
Open

Underscore Prepended to Numeric Column Labels #1093

b-schmeling opened this issue Jul 10, 2024 · 3 comments
Assignees
Labels
api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.

Comments

@b-schmeling
Copy link

Environment details

  • OS type and version: MacOS Sonoma 4.5 (23F79)
  • Python version: 3.11.6
  • pip version: `23.3.1
  • sqlalchemy-bigquery version: 1.11.0

Steps to reproduce

  1. Attempt to label a column with a numeric column name (with or without quoted_name)
  2. Get the cursor description for this column. The name has a prepended underscore

Code example

import sqlalchemy as sa
engine = ****insert engine creation here****

with engine.connect() as conn:
    a = sa.literal("a")
    labeled_col = a.label(sa.sql.quoted_name("2", quote=True))
    res = conn.execute(sa.select(labeled_col))
    name = res.cursor.description[0].name

    res_2 = conn.execute("SELECT 'a' AS `2`")
    name_2 = res_2.cursor.description[0].name

assert name == name_2 == "2"

Stack trace

AssertionError: assert '_2' == '2'
@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. label Jul 10, 2024
@Linchin
Copy link
Contributor

Linchin commented Jul 11, 2024

Hi @b-schmeling, thanks for opening the issue! I think bigquery requires that column names start with either an underscore or letter, as said in the documentation. A new feature flexible column name is currently in preview, when it's GA'd you would be able to use numerical column names.

I will close the issue now, but feel free to leave a comment if you have any further question!

@Linchin Linchin closed this as completed Jul 11, 2024
@Linchin Linchin added type: question Request for information or clarification. Not an issue. priority: p3 Desirable enhancement or fix. May not be included in next release. labels Jul 11, 2024
@b-schmeling
Copy link
Author

I think bigquery requires that column names start with either an underscore or letter, as said in the documentation.

You can already use a number as a column name as long as it's quoted. As per the documentation here, "Column names can be quoted identifiers or unquoted identifiers."

This is demonstrated by running the SQL:

CREATE TABLE <dataset_id>.tmp AS (SELECT 'a' as `2`);

Which creates a table with a column name "2" with no problem.

@Linchin Linchin reopened this Jul 12, 2024
@Linchin Linchin reopened this Jul 12, 2024
@Linchin
Copy link
Contributor

Linchin commented Jul 15, 2024

Thank you, indeed the backtick seems to work in this case, but I'm not entirely sure if this was by design or a happy accident. I will clarify with the docs team about what the intended format is here (I still feel like column name wouldn't allow string starting with numbers, as this is exactly the thing flexi-name project is trying to resolve. I wonder, by any chance, is your project in the preview list for flexi-name? Maybe that will explain why (SELECT 'a' as `2`) works.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

4 participants