Package: ipython-polypheny
Polypheny Extension for IPythonThis IPython extension adds%polymagics for queryingPolyphenyusing any of the supported query languages. This extension takes inspiration from the brilliantIPython SQL Extension.InstallationVia PyPiThe recommended way to install the package is with pip:pipinstallipython-polyphenyFrom SourceIf you do not want to use pip, you can download the code and build it manually.Download the source code.At the root directory, run python -m build. This will produce .tar.gz and .whl files in the dist/ folder.Install the package using: python -m pip install ./dist/.whl.UsageActivate the extension using:%load_extpolyYou can utilize both line magics (%poly) and cell magics (%%poly). A command must always follow the magic keyword. For instance:# Print help%polyhelpCommands requiring arguments should separate them using a colon (:):# Specify the http-interface address of a running Polypheny instance.%polydb:http://localhost:13137The colon can also be replaced by a line break when using cell magics.
This is the ideal syntax for querying the database, where the command specifies the query language:%%polysqlSELECT*FROMempsThe result is automatically printed as a nicely formatted table.Interact with the retrieved data in multiple ways:Storing the result in a variable:result=_# Or when using line magics (note the required colon that separates the query from the command):result=%polysql:SELECT*FROMempsAdditionally to the query language, a namespace can be specified.
It is also possible to set flags. The-cflag deactivates the cache for this query:%%polymqlmynamespace-cdb.emps.find({})Working With the ResultThe result object provides useful ways to work with the retrieved data.result=%polysql:SELECT*FROMempsGetting the rawResultSet:result.result_setThe data can be accessed like a two-dimensionallist:# get the value of the element in the first row and second columnresult[0][1]Iterate over the rows asdicts:foremployeeinresult.dicts():print(employee['name'],employee['salary'])ProvidedPandasis installed, it is possible to transform the result into aDataFrame:df=result.as_df()Advanced FeaturesIt is possible to expand variables defined in the local namespace into a query.
For this to work, the--template(shorter:-t) flag must be set:key='salary'x=10000%%poly-tsql:SELECT*FROMempsWHERE${key}>${x}# is equal to%%polysql:SELECT*FROMempsWHEREsalary>10000Be careful to not accidentally inject unwanted queries, as the values are not escaped.Data TypesPolypheny's data types are mapped to Python's as follows:Type in PolyphenyType in PythonBIGINT,INTEGER,SMALLINT,TINYINTintDECIMAL,DOUBLE,REALfloatBOOLEANboolDOCUMENT,JSON,NODE,PATHdictARRAYlistOther types are stored as str. Failed casting operations will also result in str.  If the raw data as a nestedlistofstris required, one can get it from theResultSet:raw_data=result.result_set['data']LimitationsWorking with multimedia and other blob types is currently not supported. While it does not result in an error, only the identifier is stored, not the actual content.ContributingThank you for considering contributing! We truly appreciate any effort, whether it's fixing bugs, improving documentation, or suggesting new features. Here's a guide to help streamline the process:Start by Forking: Begin by forking the repository. Once done, you can work on your changes and then submit them as a pull request.Development Guidelines: Before diving in, take a moment to explore ourDocumentation. Pay special attention to the 'For Developers' section — it offers insights on setup, code style, organization, and other valuable resources tailored for developers.Adherence to Code of Conduct: We are committed to fostering an open and welcoming environment. As such, we request all contributors to uphold the standards outlined in ourcode of conductthroughout their interactions related to the project.Setting up for Development:Editable Installation: To see your code changes reflected in real-time, install the extension in an editable mode. Execute the following command from the root directory of the extension:python-mpipinstall-e.This allows any modifications in the codebase to be instantly visible post-reloading the extension.Utilize Autoreload: For a smoother development experience, consider activating theautoreloadextension. This tool automatically reloads the extension and incorporates the recent code changes:%load_extautoreload%autoreload2%load_extpolyThank you for your dedication and enthusiasm for enhancing the Polypheny ecosystem! We look forward to reviewing your valuable contributions.LicenseThe Apache 2.0 License