-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.py
154 lines (132 loc) · 4.23 KB
/
example.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import sys
from collections import defaultdict
import sqlalchemy as sa
from sqlalchemy import cast, create_engine, or_
from sqlalchemy.orm import DeclarativeBase, sessionmaker
from rockset_sqlalchemy.sqlalchemy.types import Array
if "ROCKSET_API_SERVER" not in os.environ:
print("ROCKSET_API_SERVER environment variable not provided!")
sys.exit(1)
if "ROCKSET_API_KEY" not in os.environ:
print("ROCKSET_API_KEY environment variable not provided!")
sys.exit(1)
engine = create_engine(
"rockset://",
connect_args={
"api_server": os.environ["ROCKSET_API_SERVER"],
"api_key": os.environ["ROCKSET_API_KEY"],
},
)
class Base(DeclarativeBase): pass
class Person(Base):
__tablename__ = "people"
_id = sa.Column(sa.String, primary_key=True)
name = sa.Column()
info = sa.Column(sa.JSON)
def __repr__(self):
return f"Person(_id={self._id}, info={self.info}, name={self.name})"
"""
INSERT INTO people
SELECT
'Joe' name,
JSON_PARSE(
'{"friends": ["Jack", "Jill"], "favorites": {"lunch": "Sweetgreen", "snack": "Peanut butter cups"}}'
) info
UNION ALL
SELECT
'Jack' name,
JSON_PARSE(
'{"friends": ["Mike"], "favorites": {"lunch": "Chipotle", "language": "Python"}}'
) info
UNION ALL
SELECT
'Jill' name,
JSON_PARSE(
'{"friends": ["Joe"], "favorites": {"lunch": "Sweetgreen", "language": "C++", "snack": "Pickles"}}'
) info
UNION ALL
SELECT
'Mike' name,
JSON_PARSE(
'{"friends": ["Jill"], "favorites": {"lunch": "Subway", "music": "Pop"}}'
) info
"""
if __name__ == "__main__":
session = sessionmaker(bind=engine)()
q = session.query(cast(Person.info["friends"], Array)).where(Person.name == "Joe")
results = q.all()[0][0]
assert len(results) == 2
assert type(results) == list
assert results == ["Jack", "Jill"]
q = session.query(Person.info.op("#>")("friends").op("->>")(1))
results = set(t[0] for t in q.all())
assert results == set(["Jack", "Mike", "Joe", "Jill"])
q = session.query(Person.info.op("#>")("friends"))
results = q.all()
d = defaultdict(lambda: 0)
for r in results:
for n in r[0]:
d[n] = d[n] + 1
assert dict(d) == {"Jill": 2, "Joe": 1, "Mike": 1, "Jack": 1}
q = session.query(Person.name, Person.info["favorites"])
results = q.all()
d = {}
for r in results:
name = r[0]
favs = r[1]
d[name] = favs
assert dict(d) == {
"Jill": {"language": "C++", "lunch": "Sweetgreen", "snack": "Pickles"},
"Mike": {"music": "Pop", "lunch": "Subway"},
"Joe": {"lunch": "Sweetgreen", "snack": "Peanut butter cups"},
"Jack": {"lunch": "Chipotle", "language": "Python"},
}
q = session.query(Person.name, Person.info["favorites"]["snack"])
results = q.all()
d = {}
for r in results:
name = r[0]
snack = r[1]
d[name] = snack
assert dict(d) == {
"Joe": "Peanut butter cups",
"Mike": None,
"Jill": "Pickles",
"Jack": None,
}
# Similar to the query just above, except we subscript using a list.
q = session.query(Person.name, Person.info[["favorites", "snack"]])
results = q.all()
d = {}
for r in results:
name = r[0]
snack = r[1]
d[name] = snack
assert dict(d) == {
"Joe": "Peanut butter cups",
"Mike": None,
"Jill": "Pickles",
"Jack": None,
}
q = session.query(Person.name).where(
Person.info["favorites"]["lunch"] == "Sweetgreen"
)
results = set(t[0] for t in q.all())
assert results == set(["Joe", "Jill"])
q = session.query(Person.name).where(
Person.info[["favorites", "lunch"]] == "Sweetgreen"
)
results = set(t[0] for t in q.all())
assert results == set(["Joe", "Jill"])
q = session.query(Person.name).filter(Person.info[["favorites", "music"]] != None)
results = set(t[0] for t in q.all())
assert results == set(["Mike"])
q = session.query(Person.name).filter(
or_(
Person.info[["favorites", "music"]] != None,
Person.info["friends"][1] == "Jack",
)
)
results = set(t[0] for t in q.all())
assert results == set(["Mike", "Joe"])