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 — feat: STRUCT and ARRAY support by jimfulton · Pull Request #318 · googleapis/python-bigquery-sqlalchemy

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

Conversation

@jimfulton

@jimfulton jimfulton commented Aug 30, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #293
Fixes #314
Fixed #233
Fixes #37
🦕

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Jim Fulton
feat: STRUCT and ARRAY support
52cee8c
@product-auto-label product-auto-label Bot added the api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. label Aug 30, 2021
@google-cla google-cla Bot added the cla: yes This human has signed the Contributor License Agreement. label Aug 30, 2021
Jim Fulton added 6 commits August 30, 2021 17:24
Jim Fulton
Merge branch 'main' into struct
a0b02f7
Jim Fulton
Fixed test that expected JSON rather than STRUCT
6bacc0d
Jim Fulton
Merge branch 'struct' of github.com:jimfulton/python-bigquery-sqlalch…
1ec0f88
…emy into struct
Jim Fulton
Added system test I neglected to check in before :(
74aab64
Jim Fulton
blacken
c5653e2
Jim Fulton
Merge branch 'main' into struct
a7f0b41
@jimfulton

jimfulton commented Aug 31, 2021

Copy link
Copy Markdown
Contributor Author

FTR, WRT superset, once I finally got it working :), it behaves the same with and without these changes.

BTW, we have logic that tries to unpack sub-structs, I think so that there would eventually be scalars for superset to work with.

If you have an array of structs, we still create columns for the fields of the struct in the array. This causes superset to error, because it has no way to get at structs in an array. We should probably not unpack structs in arrays.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Jim Fulton added 8 commits August 31, 2021 16:17
Jim Fulton
Don't strip <ARRAY > from parameter types
9df1804
Otherwise, the BQ doesn't handle arrays of structs.
Jim Fulton
Added system tests to verift PR 67 and issue 233
0df1701
Jim Fulton
Merge branch 'struct' of github.com:jimfulton/python-bigquery-sqlalch…
7aad07f
…emy into struct
Jim Fulton
blacken
f10a571
Jim Fulton
Renamed test file to conform to samples test-file naming conventions
ec31040
Jim Fulton
Require google-cloud-bigquery 2.25.2 to get struct field-name undersc…
accf762
…ore fix
Jim Fulton
Added STRUCT documentation
ef5f891
Jim Fulton
fix bigquery version
cce9dbb
@snippet-bot

snippet-bot Bot commented Sep 1, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 10 region tags.
  • docs/struct.rst:14, tag bigquery_sqlalchemy_create_table_with_struct
  • docs/struct.rst:34, tag bigquery_sqlalchemy_insert_struct
  • docs/struct.rst:42, tag bigquery_sqlalchemy_query_struct
  • docs/struct.rst:50, tag bigquery_sqlalchemy_query_getitem
  • docs/struct.rst:58, tag bigquery_sqlalchemy_query_STRUCT
  • samples/snippets/STRUCT.py:23, tag bigquery_sqlalchemy_create_table_with_struct
  • samples/snippets/STRUCT.py:44, tag bigquery_sqlalchemy_insert_struct
  • samples/snippets/STRUCT.py:74, tag bigquery_sqlalchemy_query_struct
  • samples/snippets/STRUCT.py:79, tag bigquery_sqlalchemy_query_STRUCT
  • samples/snippets/STRUCT.py:84, tag bigquery_sqlalchemy_query_getitem

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Jim Fulton added 7 commits September 1, 2021 10:37
Jim Fulton
Merge branch 'main' into struct
290d955
Jim Fulton
get blacken to leave sample code alone.
b697df6
I want it narrow to avoid horizonal scrolling
Jim Fulton
Check in missing file :(
6a278b9
Jim Fulton
Merge branch 'struct' of github.com:jimfulton/python-bigquery-sqlalch…
bc62a56
…emy into struct
Jim Fulton
need sqla 1.4 for unnest
84426bd
Jim Fulton
fixed typo
587a0f7
Jim Fulton
Merge branch 'main' into struct
e6f4adf
@jimfulton
jimfulton marked this pull request as ready for review September 2, 2021 13:30
@jimfulton
jimfulton requested a review from a team as a code owner September 2, 2021 13:30
@jimfulton
jimfulton requested review from a team and tmatsuo September 2, 2021 13:30
@jimfulton

jimfulton commented Sep 2, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Some notes for reviewers:

  • Heart of change is _struct.py, which isn't large, but also isn't obvious. :( I cribbed from the built-in JSON and ARRAY types. When reviewing, it's probably helpful to look at those. The "Comparator" framework is confusing, in large part because the name doesn't make sense.

    Having said that, the core logic is in _setop_getitem, which is a hook used by the base class of STRUCTs comparator.

    The __getattr__ method just delegates to (the inherited) __getitem__.

  • This PR also has 2 other small changes:

    • Machinery for mapping BQ types to SQLAlchemy types has been factored into a separate _types module, both to avoid cluttering base more and to partially avoid circular imports. (There's still a circular import issue that isn't fixable without a bigger refactoring that I deemed unwarranted.)
    • Implementation of ARRAY indexing, which wasn't implemented. A number of my tests (see test__struct.py in both unit and system tests) used an example that has nested structs and arrays.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Jim Fulton added 3 commits September 2, 2021 13:31
Jim Fulton
Merge branch 'main' into struct
ffb5aa9
Jim Fulton
Merge branch 'main' into struct
47fa14f
Jim Fulton
Merge branch 'main' into struct
402bbbe
@tswast
tswast self-requested a review September 7, 2021 20:03
tswast
tswast suggested changes Sep 7, 2021
View reviewed changes

@tswast tswast left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't quite digested everything in this PR yet, but I figured I'd share the feedback I have so far.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread docs/struct.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread setup.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread sqlalchemy_bigquery/_struct.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread sqlalchemy_bigquery/_struct.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread sqlalchemy_bigquery/_struct.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread sqlalchemy_bigquery/_struct.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

jimfulton and others added 5 commits September 7, 2021 14:54
@jimfulton @tswast
Update sqlalchemy_bigquery/_struct.py
5bf07b4
Co-authored-by: Tim Swast <[email protected]>
Jim Fulton
added STRUCT docstring
e937167
Jim Fulton
Add doc link
8661f5b
Jim Fulton
Merge branch 'struct' of github.com:jimfulton/python-bigquery-sqlalch…
b550aa1
…emy into struct
Jim Fulton
Added some comments
af68a54
tswast
tswast suggested changes Sep 8, 2021
View reviewed changes

@tswast tswast left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it! Just a few things I think we should clarify before merging.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread sqlalchemy_bigquery/_struct.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread sqlalchemy_bigquery/_struct.py Outdated
Comment on lines +73 to +79
global type_compiler

try:
process = type_compiler.process
except AttributeError:
type_compiler = base.dialect.type_compiler(base.dialect())
process = type_compiler.process

@tswast tswast Sep 8, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we put this in a _get_type_compiler / _get_process function? I don't see anywhere else we initialize type_compiler, but I'd be more comfortable having this logic closer to the # We have to delay getting the type compiler, because of circular imports. :( comment.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jimfulton jimfulton Sep 8, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored so this is combined and isolated in one place using a new, better named _get_subtype_col_spec function.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread sqlalchemy_bigquery/_struct.py Outdated
type_compiler = base.dialect.type_compiler(base.dialect())
process = type_compiler.process

fields = ", ".join(f"{name} {process(type_)}" for name, type_ in self.__fields)

@tswast tswast Sep 8, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume process is able to handle nested arrays/structs?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jimfulton jimfulton Sep 8, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread sqlalchemy_bigquery/_struct.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread sqlalchemy_bigquery/_struct.py Outdated
f"STRUCT fields can only be accessed with strings field names,"
f" not {name}."
)
subtype = self.expr.type._STRUCT__byname.get(name.lower())

@tswast tswast Sep 8, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does _STRUCT__byname come from? I'm assuming somewhere from SQLAlchemy, but I'm not getting any results when searching for byname.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tswast tswast Sep 8, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think I figured it out: https://docs.python.org/3/tutorial/classes.html#private-variables

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped.

Can we comment about this? I assume we have to do it because we know self.expr.type is a STRUCT, but it's not self.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jimfulton jimfulton Sep 8, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored this to make name mangling more explicit and consistent, so I don't think comments are needed anymore. See if you agree. :)

I mainly use "private" variables, which aren't :), to avoid namespace conflicts when subclassing across responsibility boundaries. Arguably, explicit naming is better.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread sqlalchemy_bigquery/_struct.py Outdated
return operator, index, subtype

def __getattr__(self, name):
if name.lower() in self.expr.type._STRUCT__byname:

@tswast tswast Sep 8, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused why self.__byname doesn't work in this case.

Edit: I see now that it's part of the Comparator class. Still probably worth a similar comment to the one I recommend in _setup_getitem

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jimfulton jimfulton Sep 8, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my response on name mangling

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread sqlalchemy_bigquery/_struct.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread sqlalchemy_bigquery/_types.py Outdated
for f in field.fields
]
results += _get_transitive_schema_fields(sub_fields, cur_fields)
cur_fields.pop()

@tswast tswast Sep 8, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we pop these off, does that mean we don't get the top-level struct field, just the leaf fields? I suspect this might hide some ARRAY columns if a parent node has mode REPEATED, but is not included.

Edit: I see the top field is added to results on line 83. Might be worth a comment as to why we pop here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jimfulton jimfulton Sep 8, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got rid of cur_fields. It wasn't needed (anymore).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread sqlalchemy_bigquery/_types.py Outdated
for field in fields:
results += [field]
if field.field_type == "RECORD":
cur_fields.append(field)

@tswast tswast Sep 8, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand what cur_fields is doing. Is there a better name we can pick for this? Maybe it's referring to ancestors?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jimfulton jimfulton Sep 8, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, it's not doing anything.

This is based on

python-bigquery-sqlalchemy/pybigquery/sqlalchemy_bigquery.py

Lines 503 to 523 in db64424

def _get_columns_helper(self, columns, cur_columns):
"""
Recurse into record type and return all the nested field names.
As contributed by @sumedhsakdeo on issue #17
"""
results = []
for col in columns:
results += [
SchemaField(
name=".".join(col.name for col in cur_columns + [col]),
field_type=col.field_type,
mode=col.mode,
description=col.description,
fields=col.fields,
)
]
if col.field_type == "RECORD":
cur_columns.append(col)
results += self._get_columns_helper(col.fields, cur_columns)
cur_columns.pop()
return results
, which I inherited.

I've refactored it quite a bit and failed to notice that this wasn't needed any more. Fixed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/unit/test__struct.py Outdated
(_col().NAME, "`t`.`person`.NAME"),
(_col().children, "`t`.`person`.children"),
(
_col().children[0].label("anon_1"), # SQLAlchemy doesn't add the label

@tswast tswast Sep 8, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we file an issue for this to investigate later? If so, let's add TODO and link to the issue.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jimfulton jimfulton Sep 8, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Jim Fulton and others added 7 commits September 8, 2021 13:01
Jim Fulton
Localize logic for getting subtye column specifications
da43fd2
Jim Fulton
explain semi-private name mangling
f04cac2
Jim Fulton
Make name magling more explicit
5af05bb
Jim Fulton
explain why we have different implementations of _field_index for SQL…
09866c6
…Alchemy 1.3 and 1/4
Jim Fulton
get rid of cur_fields, we're not using it anymore.
054c227
Also, check for both RECORD and STRUCT fild types, in case the API
ever starts returning STRUCT.
Jim Fulton
Add a todo to find out why Sqlalchemy doesn't generate an alias when …
1a79305
…accessing array items
@jimfulton @tswast
user repr rather than str to shpow an object in an error message
5e2ae32
Co-authored-by: Tim Swast <[email protected]>
tswast
tswast approved these changes Sep 9, 2021
View reviewed changes
Comment thread sqlalchemy_bigquery/_struct.py
global _get_subtype_col_spec

type_compiler = base.dialect.type_compiler(base.dialect())
_get_subtype_col_spec = type_compiler.process

@tswast tswast Sep 9, 2021

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fancy! I didn't realize a function could replace itself. I like it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jimfulton
jimfulton merged commit 6624b10 into googleapis:main Sep 9, 2021
@jimfulton
jimfulton deleted the struct branch September 9, 2021 15:50
@jimfulton jimfulton mentioned this pull request Sep 9, 2021
Add support for array and struct literals #67
Closed
gcf-merge-on-green Bot pushed a commit that referenced this pull request Sep 9, 2021
@release-please
chore: release 1.2.0 (#338)
f6d2799
🤖 I have created a release \*beep\* \*boop\*
---
## [1.2.0](https://www.github.com/googleapis/python-bigquery-sqlalchemy/compare/v1.1.0...v1.2.0) (2021-09-09)


### Features

* STRUCT and ARRAY support ([#318](https://www.github.com/googleapis/python-bigquery-sqlalchemy/issues/318)) ([6624b10](https://www.github.com/googleapis/python-bigquery-sqlalchemy/commit/6624b10ded73bbca6f40af73aaeaceb95c381b63))


### Bug Fixes

* the unnest function lost needed type information ([#298](https://www.github.com/googleapis/python-bigquery-sqlalchemy/issues/298)) ([1233182](https://www.github.com/googleapis/python-bigquery-sqlalchemy/commit/123318269876e7f76c7f0f2daa5f5b365026cd3f))
---


This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
AnastasiaTamazlykar
AnastasiaTamazlykar reviewed Nov 15, 2024
View reviewed changes
Comment thread sqlalchemy_bigquery/base.py
def visit_getitem_binary(self, binary, operator_, **kw):
left = self.process(binary.left, **kw)
right = self.process(binary.right, **kw)
return f"{left}[OFFSET({right})]"

@AnastasiaTamazlykar AnastasiaTamazlykar Nov 15, 2024

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SAFE_OFFSET support would be nice too

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@tmatsuo tmatsuo Awaiting requested review from tmatsuo
2 more reviewers
@AnastasiaTamazlykar AnastasiaTamazlykar AnastasiaTamazlykar left review comments
@tswast tswast tswast approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Array indexing doesn't work Execution of STRUCT item access doesn't work. Unable compare value from records array Support Base.metadata.create_all for STRUCT and ARRAY data types

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@jimfulton @tswast @AnastasiaTamazlykar
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.