GreptimeTeam / greptimedb

An open-source, cloud-native, unified time series database for metrics, logs and events with SQL/PromQL supported. Available on GreptimeCloud.
https://greptime.com/
Apache License 2.0
4.23k stars 303 forks source link

The script must have the same name as the function #2532

Open WenyXu opened 12 months ago

WenyXu commented 12 months ago

What type of bug is this?

Unexpected error

What subsystems are affected?

Datanode

What happened?

Mentioned in https://docs.greptime.com/user-guide/python-scripts/data-types

Notes: the function name is query_numbers

// file query_numbers.py

from greptime import PyDataFrame, col
@copr(returns = ["value"])
def query_numbers() -> vector[f64]:
    df = PyDataFrame.from_sql("select number from numbers")
    return df.filter(col('number') <= 5).collect()[0]

curl --data-binary "@query_numbers.py" -XPOST "http://localhost:4000/v1/scripts?name=query_number&db=public"

Execute sql select query_number();

Output:

ERROR 1815 (HY000): Error during planning: Invalid function 'query_number'.
Did you mean 'encode'?

However, it works via http.

curl  -XPOST \
 "http://localhost:4000/v1/run-script?name=query_number&db=public"
{"code":0,"output":[{"records":{"schema":{"column_schemas":[{"name":"value","data_type":"Float64"}]},"rows":[[0.0],[1.0],[2.0],[3.0],[4.0],[5.0]]}}],"execution_time_ms":16}%   

If you change ?name=query_number to ?name=query_numbers , everything works.

What operating system did you use?

Ubuntu 23.04

Relevant log output and stack trace

No response

How can we reproduce the bug?

Mentioned in https://docs.greptime.com/user-guide/python-scripts/data-types

Notes: the function name is query_numbers

// file query_numbers.py

from greptime import PyDataFrame, col
@copr(returns = ["value"])
def query_numbers() -> vector[f64]:
    df = PyDataFrame.from_sql("select number from numbers")
    return df.filter(col('number') <= 5).collect()[0]

curl --data-binary "@query_numbers.py" -XPOST "http://localhost:4000/v1/scripts?name=query_number&db=public"

Execute sql select query_number();

killme2008 commented 12 months ago

It's not a bug too.

If you want to call script by HTTP API, the name is the script name you pass when saving the scripts. If you want to call the function in SQL, you must use the function name in script. Sorry for the misunderstood, I will clarify it in the documentation.

tisonkun commented 6 months ago

Some more subtle manner:

  1. If the database is closed and restart, you should execute via HTTP or register a new script to trigger the scripts function load for SQL.

If we don't support multiple @coprocessor in one file, what if we always infer the name from script?

tisonkun commented 6 months ago

If the database is closed and restart, you should execute via HTTP or register a new script to trigger the scripts function load for SQL.

recompile_register_udf seems indicate that we don't auto reload scripts on restart.

tisonkun commented 6 months ago

cc @discord9