Skip to content

query

database.query.sql

                                                                                
 Documentation                                                                  
                          Execute a sql query against an (Arrow) table.         
                                                                                
 Origin                                                                         
                          Authors   Markus Binsteiner (markus@frkl.io)          
                                                                                
 Context                                                                        
                          Tags         core                                     
                          Labels       package: kiara_modules.core              
                          References   source_repo:                             
                                       https://github.com/DHARPA-Project/kia…   
                                       documentation:                           
                                       https://dharpa.org/kiara_modules.core/   
                                       module_doc:                              
                                       https://dharpa.org/kiara_modules.core…   
                                       source_url:                              
                                       https://github.com/DHARPA-Project/kia…   
                                                                                
 Module config                                                                  
                          Field       Type     Description           Required   
                         ─────────────────────────────────────────────────────  
                          constants   object   Value constants for   no         
                                               this module.                     
                          defaults    object   Value defaults for    no         
                                               this module.                     
                          query       string   The query to          no         
                                               execute. If not                  
                                               specified, the user              
                                               will be able to                  
                                               provide their own.               
                                                                                
 Module config          -- no config --                                         
 Python class                                                                   
                          class_name    QueryTableSQL                           
                          module_name   kiara_modules.core.database.query       
                          full_name     kiara_modules.core.database.query.Qu…   
                                                                                
 Processing source code  ─────────────────────────────────────────────────────  
                          def process(self, inputs: ValueSet, outputs: Value…   
                                                                                
                              import pandas as pd                               
                              import pyarrow as pa                              
                                                                                
                              if self.get_config_value("query") is None:        
                                  _query: str = inputs.get_value_data("query…   
                              else:                                             
                                  _query = self.get_config_value("query")       
                                                                                
                              _database: KiaraDatabase = inputs.get_value_da…   
                                                                                
                              # can't re-use the default engine, because pan…   
                              engine = create_engine(_database.db_url)          
                              df = pd.read_sql(_query, con=engine)              
                              table = pa.Table.from_pandas(df)                  
                                                                                
                              outputs.set_value("query_result", table)          
                                                                                
                         ─────────────────────────────────────────────────────