diff --git a/README.md b/README.md index ad39141..33d6257 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/pyproject.toml b/pyproject.toml index d0ec270..8adad2d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = [ diff --git a/src/jsoniq/sequence.py b/src/jsoniq/sequence.py index 442fad2..b4844ad 100644 --- a/src/jsoniq/sequence.py +++ b/src/jsoniq/sequence.py @@ -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()]) diff --git a/src/jsoniq/session.py b/src/jsoniq/session.py index f194ecd..5d5c9bf 100644 --- a/src/jsoniq/session.py +++ b/src/jsoniq/session.py @@ -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("$"): @@ -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: @@ -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) \ No newline at end of file