-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathSTRUCT.py
90 lines (75 loc) · 3.25 KB
/
STRUCT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Copyright (c) 2021 The sqlalchemy-bigquery Authors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
def example(engine):
# fmt: off
# [START bigquery_sqlalchemy_create_table_with_struct]
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, String, Integer, Float
from sqlalchemy_bigquery import STRUCT
Base = declarative_base()
class Car(Base):
__tablename__ = "Cars"
model = Column(String, primary_key=True)
engine = Column(
STRUCT(
cylinder=STRUCT(("count", Integer),
("compression", Float)),
horsepower=Integer)
)
# [END bigquery_sqlalchemy_create_table_with_struct]
Car.__table__.create(engine)
# [START bigquery_sqlalchemy_insert_struct]
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()
sebring = Car(model="Sebring",
engine=dict(
cylinder=dict(
count=6,
compression=18.0),
horsepower=235))
townc = Car(model="Town and Counttry",
engine=dict(
cylinder=dict(
count=6,
compression=16.0),
horsepower=251))
xj8 = Car(model="XJ8",
engine=dict(
cylinder=dict(
count=8,
compression=10.75),
horsepower=575))
session.add_all((sebring, townc, xj8))
session.commit()
# [END bigquery_sqlalchemy_insert_struct]
# [START bigquery_sqlalchemy_query_struct]
sixes = session.query(Car).filter(Car.engine.cylinder.count == 6)
# [END bigquery_sqlalchemy_query_struct]
sixes1 = list(sixes)
# [START bigquery_sqlalchemy_query_STRUCT]
sixes = session.query(Car).filter(Car.engine.CYLINDER.COUNT == 6)
# [END bigquery_sqlalchemy_query_STRUCT]
sixes2 = list(sixes)
# [START bigquery_sqlalchemy_query_getitem]
sixes = session.query(Car).filter(Car.engine["cylinder"]["count"] == 6)
# [END bigquery_sqlalchemy_query_getitem]
# fmt: on
sixes3 = list(sixes)
return sixes1, sixes2, sixes3