Skip to content

Commit 955145c

Browse files
Merge pull request #18 from RumbleDB/Improvements
Improvements
2 parents c61f5dc + 9f0a897 commit 955145c

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ Even more queries can be found [here](https://colab.research.google.com/github/R
338338

339339
# Latest updates
340340

341+
## Version 0.2.0 alpha 9
342+
- Stability improvements.
343+
341344
## Version 0.2.0 alpha 8
342345
- 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().
343346
- rumble.lastResult is now correctly assigned also when partial data is returned (only with the partial data).

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.0a8"
7+
version = "0.2.0a9"
88
description = "Python edition of RumbleDB, a JSONiq engine"
99
requires-python = ">=3.11"
1010
dependencies = [

src/jsoniq/sequence.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ def items(self):
3636
return self.getAsList()
3737

3838
def take(self, n):
39-
return tuple(self.getFirstItemsAsList(n))
39+
self._rumblesession.lastResult = tuple(self.getFirstItemsAsList(n))
40+
return self._rumblesession.lastResult
4041

4142
def first(self):
42-
return tuple(self.getFirstItemsAsList(self._rumblesession.getRumbleConf().getResultSizeCap()))
43+
self._rumblesession.lastResult = tuple(self.getFirstItemsAsList(self._rumblesession.getRumbleConf().getResultSizeCap()))
44+
return self._rumblesession.lastResult
4345

4446
def json(self):
4547
self._rumblesession.lastResult = tuple([json.loads(l.serializeAsJSON()) for l in self._jsequence.getAsList()])

src/jsoniq/session.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ def convert(self, value):
157157
else:
158158
raise ValueError("Cannot yet convert value of type " + str(type(value)) + " to a RumbleDB item. Please open an issue and we will look into it!")
159159

160+
def unbind(self, name: str):
161+
conf = self._jrumblesession.getConfiguration();
162+
if not name.startswith("$"):
163+
raise ValueError("Variable name must start with a dollar symbol ('$').")
164+
name = name[1:]
165+
conf.resetExternalVariableValue(name);
166+
160167
def bind(self, name: str, valueToBind):
161168
conf = self._jrumblesession.getConfiguration();
162169
if not name.startswith("$"):
@@ -177,7 +184,28 @@ def bind(self, name: str, valueToBind):
177184
elif isinstance(valueToBind, tuple):
178185
conf.setExternalVariableValue(name, self.convert(valueToBind))
179186
elif isinstance(valueToBind, list):
180-
raise ValueError("To avoid confusion, a sequence of items must be provided as a Python tuple, not as a Python list. Lists are mapped to single array items, while tuples are mapped to sequences of items. If you want to interpret the list as a sequence of items (one item for each list member), then you need to change this list to a tuple by wrapping it into a tuple() call. If you want to bind the variable to one array item, then you need to wrap the provided list inside a singleton tuple and try again, or you can also call bindOne() instead.")
187+
raise ValueError("""
188+
To avoid confusion, a sequence of items must be provided as a Python tuple, not as a Python list.
189+
Lists are mapped to single array items, while tuples are mapped to sequences of items.
190+
191+
If you want to interpret the list as a sequence of items (one item for each list member), then you need to convert it to a tuple.
192+
Example: [1,2,3] should then be rewritten as tuple([1,2,3]) for the sequence of three (integer) items 1, 2, and 3.
193+
194+
If you want to interpret the list as a sequence of one array item, then you need to create a singleton tuple.
195+
Example: [1,2,3] should then be rewritten as ([1,2,3],) for the sequence of one (array) item [1,2,3].
196+
""")
197+
elif isinstance(valueToBind, dict):
198+
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
199+
elif isinstance(valueToBind, str):
200+
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
201+
elif isinstance(valueToBind, int):
202+
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
203+
elif isinstance(valueToBind, float):
204+
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
205+
elif isinstance(valueToBind, bool):
206+
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
207+
elif valueToBind is None:
208+
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
181209
elif(hasattr(valueToBind, "_get_object_id")):
182210
conf.setExternalVariableValue(name, valueToBind);
183211
else:
@@ -198,9 +226,14 @@ def bindDataFrameAsVariable(self, name: str, df):
198226
conf.setExternalVariableValue(name, df._jdf);
199227
return self;
200228

201-
def jsoniq(self, str):
229+
def jsoniq(self, str, **kwargs):
230+
for key, value in kwargs.items():
231+
self.bind(f"${key}", value);
202232
sequence = self._jrumblesession.runQuery(str);
203-
return SequenceOfItems(sequence, self);
233+
seq = SequenceOfItems(sequence, self);
234+
for key, value in kwargs.items():
235+
self.unbind(f"${key}");
236+
return seq;
204237

205238
def __getattr__(self, item):
206239
return getattr(self._sparksession, item)

0 commit comments

Comments
 (0)