Skip to content

Commit c61f5dc

Browse files
Pass values via additional parameters to rumble.jsoniq()
Pass values via additional parameters to rumble.jsoniq()
2 parents 05d0ee6 + cd2ed77 commit c61f5dc

4 files changed

Lines changed: 19 additions & 24 deletions

File tree

README.md

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ print(seq.json());
173173
# But if you need more (like date, bytes, etc) we will add them without any problem.
174174
# JSONiq has a rich type system.
175175
176-
rumble.bind('$c', (1,2,3,4, 5, 6))
177176
print(rumble.jsoniq("""
178177
for $v in $c
179178
let $parity := $v mod 2
@@ -183,28 +182,24 @@ return { switch($parity)
183182
case 1 return "odd"
184183
default return "?" : $v
185184
}
186-
""").json())
185+
""", c=(1,2,3,4, 5, 6)).json())
187186
188-
rumble.bind('$c', ([1,2,3],[4,5,6]))
189187
print(rumble.jsoniq("""
190188
for $i in $c
191189
return [
192190
for $j in $i
193191
return { "foo" : $j }
194192
]
195-
""").json())
193+
""", c=([1,2,3],[4,5,6])).json())
196194
197-
rumble.bind('$c', ({"foo":[1,2,3]},{"foo":[4,{"bar":[1,False, None]},6]}))
198-
print(rumble.jsoniq('{ "results" : $c.foo[[2]] }').json())
195+
print(rumble.jsoniq('{ "results" : $c.foo[[2]] }', c=({"foo":[1,2,3]},{"foo":[4,{"bar":[1,False, None]},6]})).json())
199196
200197
# It is possible to bind only one value. The it must be provided as a singleton tuple.
201198
# This is because in JSONiq, an item is the same a sequence of one item.
202-
rumble.bind('$c', (42,))
203-
print(rumble.jsoniq('for $i in 1 to $c return $i*$i').json())
199+
print(rumble.jsoniq('for $i in 1 to $c return $i*$i', c=(42,)).json())
204200
205201
# For convenience and code readability, you can also use bindOne().
206-
rumble.bindOne('$c', 42)
207-
print(rumble.jsoniq('for $i in 1 to $c return $i*$i').json())
202+
print(rumble.jsoniq('for $i in 1 to $c return $i*$i', c=42).json())
208203
209204
##########################################################
210205
##### Binding JSONiq variables to pandas DataFrames ######
@@ -217,8 +212,7 @@ data = {'Name': ['Alice', 'Bob', 'Charlie'],
217212
pdf = pd.DataFrame(data);
218213
219214
# Binding a pandas dataframe
220-
rumble.bind('$a',pdf);
221-
seq = rumble.jsoniq('$a.Name')
215+
seq = rumble.jsoniq('$a.Name', a=pdf)
222216
# Getting the output as a pandas dataframe
223217
print(seq.pdf())
224218
@@ -239,13 +233,10 @@ data = [("Alice", 30), ("Bob", 25), ("Charlie", 35)];
239233
columns = ["Name", "Age"];
240234
df = spark.createDataFrame(data, columns);
241235
242-
# This is how to bind a JSONiq variable to a dataframe. You can bind as many variables as you want.
243-
rumble.bind('$a', df);
244-
245-
# This is how to run a query. This is similar to spark.sql().
246-
# Since variable $a was bound to a DataFrame, it is automatically declared as an external variable
236+
# You can bind JSONiq variables to pyspark DataFrames as follows. You can bind as many variables as you want.
237+
# Since variable $a is bound to a DataFrame, it is automatically declared as an external variable
247238
# and can be used in the query. In JSONiq, it is logically a sequence of objects.
248-
res = rumble.jsoniq('$a.Name');
239+
res = rumble.jsoniq('$a.Name', a=df);
249240
250241
# There are several ways to collect the outputs, depending on the user needs but also
251242
# on the query supplied.
@@ -278,14 +269,12 @@ df2 = spark.sql("SELECT * FROM myview").toDF("name");
278269
df2.show();
279270
280271
# A DataFrame output by Spark SQL can be reused as input to a JSONiq query.
281-
rumble.bind('$b', df2);
282-
seq2 = rumble.jsoniq("for $i in 1 to 5 return $b");
272+
seq2 = rumble.jsoniq("for $i in 1 to 5 return $b", b=df2);
283273
df3 = seq2.df();
284274
df3.show();
285275
286276
# And a DataFrame output by JSONiq can be reused as input to another JSONiq query.
287-
rumble.bind('$b', df3);
288-
seq3 = rumble.jsoniq("$b[position() lt 3]");
277+
seq3 = rumble.jsoniq("$b[position() lt 3]", b=df3);
289278
df4 = seq3.df();
290279
df4.show();
291280
@@ -335,7 +324,7 @@ for str in rdd.take(10):
335324
# RumbleDB was already tested with up to 64 AWS machines and 100s of TBs of data.
336325
# Of course the examples below are so small that it makes more sense to process the results locally with Python,
337326
# but this shows how GBs or TBs of data obtained from JSONiq can be written back to disk.
338-
seq = rumble.jsoniq("$a.Name");
327+
seq = rumble.jsoniq("$a.Name", a=spark.createDataFrame(data, columns));
339328
seq.write().mode("overwrite").json("outputjson");
340329
seq.write().mode("overwrite").parquet("outputparquet");
341330
@@ -349,6 +338,11 @@ Even more queries can be found [here](https://colab.research.google.com/github/R
349338

350339
# Latest updates
351340

341+
## Version 0.2.0 alpha 8
342+
- Variables can now be bound to JSON values, pandas DataFrames or pyspark DataFrames with extra parameters to the rumble.jsoniq() call. It is no longer necessary to explicitly call bind(). This is similar to how DataFrames can be attached to views with extra parameters to spark.sql().
343+
- rumble.lastResult is now correctly assigned also when partial data is returned (only with the partial data).
344+
- Fixed issue with empty array constructors.
345+
352346
## Version 0.2.0 alpha 7
353347
- rumble.lastResult now returns a pyspark/pandas DataFrame or rdd or tuple and no longer the sequence object.
354348
- Enhance schema detection. When the detected static type of the overall query is DataFrame-compatible, it is now automatically possible to obtain the output as a DataFrame without explicitly giving a schema.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "jsoniq"
7-
version = "0.2.0a7"
7+
version = "0.2.0a8"
88
description = "Python edition of RumbleDB, a JSONiq engine"
99
requires-python = ">=3.11"
1010
dependencies = [
196 Bytes
Binary file not shown.

tests/test_sample.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def test1(self):
8787
}
8888
""");
8989
print(seq.json());
90+
self.assertTrue(json.dumps(seq.json()) == json.dumps(({'store': 1, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 2, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 3, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 4, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 5, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 6, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 7, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 8, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 9, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 10, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 11, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 12, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 13, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 14, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 15, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 16, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 17, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 18, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 19, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 20, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 21, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 22, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 23, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 24, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 25, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 26, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 27, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 28, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 29, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 30, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 31, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 32, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 33, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 34, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 35, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 36, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 37, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 38, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 39, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 40, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 41, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 42, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 43, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 44, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 45, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 46, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 47, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 48, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 49, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 50, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 51, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 52, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 53, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 54, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 55, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 56, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 57, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 58, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 59, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 60, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 61, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 62, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 63, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 64, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 65, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 66, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 67, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 68, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 69, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 70, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 71, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 72, 'products': ['shirt', 'toaster', 'phone', 'blender', 'tv', 'socks', 'broiler']}, {'store': 73, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 74, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 75, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 76, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 77, 'products': ['toaster', 'phone', 'blender', 'tv', 'socks', 'broiler', 'shirt']}, {'store': 78, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 79, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 80, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 81, 'products': ['phone', 'blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster']}, {'store': 82, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 83, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 84, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 85, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 86, 'products': ['blender', 'tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone']}, {'store': 87, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 88, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 89, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 90, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 91, 'products': ['tv', 'socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender']}, {'store': 92, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 93, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 94, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 95, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 96, 'products': ['socks', 'broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv']}, {'store': 97, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 98, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 99, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']}, {'store': 100, 'products': ['broiler', 'shirt', 'toaster', 'phone', 'blender', 'tv', 'socks']})))
9091

9192
############################################################
9293
###### Binding JSONiq variables to Python values ###########

0 commit comments

Comments
 (0)