Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ Even more queries can be found [here](https://colab.research.google.com/github/R

# Latest updates

## Version 0.2.0 alpha 9
- Stability improvements.

## Version 0.2.0 alpha 8
- 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().
- rumble.lastResult is now correctly assigned also when partial data is returned (only with the partial data).
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "jsoniq"
version = "0.2.0a8"
version = "0.2.0a9"
description = "Python edition of RumbleDB, a JSONiq engine"
requires-python = ">=3.11"
dependencies = [
Expand Down
6 changes: 4 additions & 2 deletions src/jsoniq/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ def items(self):
return self.getAsList()

def take(self, n):
return tuple(self.getFirstItemsAsList(n))
self._rumblesession.lastResult = tuple(self.getFirstItemsAsList(n))
return self._rumblesession.lastResult

def first(self):
return tuple(self.getFirstItemsAsList(self._rumblesession.getRumbleConf().getResultSizeCap()))
self._rumblesession.lastResult = tuple(self.getFirstItemsAsList(self._rumblesession.getRumbleConf().getResultSizeCap()))
return self._rumblesession.lastResult

def json(self):
self._rumblesession.lastResult = tuple([json.loads(l.serializeAsJSON()) for l in self._jsequence.getAsList()])
Expand Down
39 changes: 36 additions & 3 deletions src/jsoniq/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ def convert(self, value):
else:
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!")

def unbind(self, name: str):
conf = self._jrumblesession.getConfiguration();
if not name.startswith("$"):
raise ValueError("Variable name must start with a dollar symbol ('$').")
name = name[1:]
conf.resetExternalVariableValue(name);

def bind(self, name: str, valueToBind):
conf = self._jrumblesession.getConfiguration();
if not name.startswith("$"):
Expand All @@ -177,7 +184,28 @@ def bind(self, name: str, valueToBind):
elif isinstance(valueToBind, tuple):
conf.setExternalVariableValue(name, self.convert(valueToBind))
elif isinstance(valueToBind, list):
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.")
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 convert it to a tuple.
Example: [1,2,3] should then be rewritten as tuple([1,2,3]) for the sequence of three (integer) items 1, 2, and 3.

If you want to interpret the list as a sequence of one array item, then you need to create a singleton tuple.
Example: [1,2,3] should then be rewritten as ([1,2,3],) for the sequence of one (array) item [1,2,3].
""")
elif isinstance(valueToBind, dict):
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
elif isinstance(valueToBind, str):
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
elif isinstance(valueToBind, int):
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
elif isinstance(valueToBind, float):
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
elif isinstance(valueToBind, bool):
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
elif valueToBind is None:
conf.setExternalVariableValue(name, self.convert((valueToBind, )))
elif(hasattr(valueToBind, "_get_object_id")):
conf.setExternalVariableValue(name, valueToBind);
else:
Expand All @@ -198,9 +226,14 @@ def bindDataFrameAsVariable(self, name: str, df):
conf.setExternalVariableValue(name, df._jdf);
return self;

def jsoniq(self, str):
def jsoniq(self, str, **kwargs):
for key, value in kwargs.items():
self.bind(f"${key}", value);
sequence = self._jrumblesession.runQuery(str);
return SequenceOfItems(sequence, self);
seq = SequenceOfItems(sequence, self);
for key, value in kwargs.items():
self.unbind(f"${key}");
return seq;

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