Accessing input parameter in python script

Padawan

I'm writing a script that uses a string parameter (filepath). It starts like this:

 

 

CREATE PYTHON SCALAR SCRIPT "ReadFile"(filepath VARCHAR(200)) EMITS (...) AS

 

 

In what parts of the script can I access this parameter? I know I can do it in the RUN function through the ctx parameter:

 

 

def run(ctx):

 

 

but I need to access  through the whole script. I will need it in some way to be able to dynamically define the return fields in :

 

 

def default_output_columns():

 

 

or perhaps outside these functions?

Please assume that I don't know much about python. I'm mostly coding through google searches. I appreciate any help I can get. Thanks.

4 REPLIES 4

Team Exasol
Team Exasol

Hi andreeroos,


The function default_output_columns() gets called before the run function. You can access the names and types of the input parameter via the exa.meta object with exa.meta.input_columns. You can find information about exa.meta in our documentation. However, you cannot access the input data in the default_output_columns() function, you are limited to schema information. In our documentation, we provide an example which shows how you can implement the function default_output_columns().

SQL-Fighter

You can pass ctx from run function to other functions as well.

 

def default_output_columns(x):
    # everything you can do using ctx, can be done here with x
    file_path = x.FILEPATH
    x.emit(...)

def run(ctx):
    default_output_columns(ctx)

 

 

Padawan

It seems the default_output_columns() function is called by Exasol before the run(ctx) function:

TypeError: default_output_columns() takes exactly 1 argument (0 given)

I have also played around with global variables initiated in the run() function, but it doesn't seem to work.

SQL-Fighter

Below is a working example:

-- UDF Definition
CREATE OR REPLACE PYTHON3 SCALAR SCRIPT EMIT_EXAMPLE(NAME VARCHAR(10), REPEAT_COUNT INT) EMITS(OUTPUT_MESSAGE VARCHAR(20))
AS
def repeater(x):
    for i in range(x.REPEAT_COUNT):
        x.emit("Hello {}!".format(x.NAME))

def run(ctx):
    repeater(ctx)
/

-- Call UDF
SELECT EMIT_EXAMPLE('Jack', 3);

 

Output:

Hello Jack!
Hello Jack!
Hello Jack!