Module query.table in plugin tabular v0.5.3
Execute a sql query against an (Arrow) table.
The default relation name for the sql query is 'data', but can be modified by the 'relation_name' config option/input.
If the 'query' module config option is not set, users can provide their own query, otherwise the pre-set one will be used.
Author(s) | Markus Binsteiner (markus@frkl.io) |
Tags | tabular |
Python class | kiara_plugin.tabular.modules.table.QueryTableSQL |
Module configuration options
Configuration class: kiara_plugin.tabular.modules.table.QueryTableSQLModuleConfig
Name | Description | Type | Required? | Default |
---|---|---|---|---|
constants | Value constants for this module. | object | false | null |
defaults | Value defaults for this module. | object | false | null |
query | The query to execute. If not specified, the user will be able to provide their own. | anyOf: [{'type': 'string'}, {'type': 'null'}] | false | null |
relation_name | The name the table is referred to in the sql query. If not specified, the user will be able to provide their own. | anyOf: [{'type': 'string'}, {'type': 'null'}] | false | "data" |
Module source code
class QueryTableSQL(KiaraModule): """Execute a sql query against an (Arrow) table.
The default relation name for the sql query is 'data', but can be modified by the 'relation_name' config option/input.
If the 'query' module config option is not set, users can provide their own query, otherwise the pre-set one will be used. """
_module_type_name = "query.table" _config_cls = QueryTableSQLModuleConfig
def create_inputs_schema( self, ) -> ValueMapSchema:
inputs = { "table": { "type": "table", "doc": "The table to query", } }
if self.get_config_value("query") is None: inputs["query"] = { "type": "string", "doc": "The query, use the value of the 'relation_name' input as table, e.g. 'select * from data'.", } inputs["relation_name"] = { "type": "string", "doc": "The name the table is referred to in the sql query.", "default": "data", }
return inputs
def create_outputs_schema( self, ) -> ValueMapSchema:
return {"query_result": {"type": "table", "doc": "The query result."}}
def process(self, inputs: ValueMap, outputs: ValueMap) -> None:
import duckdb
if self.get_config_value("query") is None: _query: str = inputs.get_value_data("query") _relation_name: str = inputs.get_value_data("relation_name") else: _query = self.get_config_value("query") _relation_name = self.get_config_value("relation_name")
if _relation_name.upper() in RESERVED_SQL_KEYWORDS: raise KiaraProcessingException( f"Invalid relation name '{_relation_name}': this is a reserved sql keyword, please select a different name." )
_table: KiaraTable = inputs.get_value_data("table") rel_from_arrow = duckdb.arrow(_table.arrow_table) result: duckdb.DuckDBPyRelation = rel_from_arrow.query(_relation_name, _query)
outputs.set_value("query_result", result.arrow())