Query Data

We provide two ways to easily query data from GreptimeDB in Python Coprocessor:

  • SQL: run a SQL string and return the query result.
  • DataFrame API: a builtin module that describes and executes the query similar to a Pandas DataFrame or Spark DataFrame.

SQL

Use the greptime module’s query method to retrieve a query engine, then call sql function to execute a SQL string, for example:

python

  1. @copr(returns=["value"])
  2. def query_numbers()->vector[f64]:
  3. from greptime import query
  4. return query().sql("select number from numbers limit 10")[0]

Call it via SQL client:

sql

  1. SQL > select query_numbers();
  2. +-----------------+
  3. | query_numbers() |
  4. +-----------------+
  5. | 0 |
  6. | 1 |
  7. | 2 |
  8. | 3 |
  9. | 4 |
  10. | 5 |
  11. | 6 |
  12. | 7 |
  13. | 8 |
  14. | 9 |
  15. +-----------------+
  16. 10 rows in set (1.78 sec)

The sql function returns a list of columns, and each column is a vector of values.

In the above example, sql("select number from numbers limit 10") returns a list of vectors. And use [0] to retrieve the first column vector which is the number column in select SQL.

Post-Query Processing

The coprocessor is helpful when processing a query result before it returns to the user. For example, we want to normalize the value:

  • Return zero instead of null or NaN if it misses,
  • If it is greater than 5, return 5,
  • If it is less than zero, return zero.

Then we can create a normalize.py:

python

  1. import math
  2. def normalize0(x):
  3. if x is None or math.isnan(x):
  4. return 0
  5. elif x > 5:
  6. return 5
  7. elif x < 0:
  8. return 0
  9. else:
  10. return x
  11. @coprocessor(args=["number"], sql="select number from numbers limit 10", returns=["value"])
  12. def normalize(v) -> vector[i64]:
  13. return [normalize0(x) for x in v]

The normalize0 function behaves as described above. And the normalize function is the coprocessor entry point:

  • Execute the SQL select number from numbers limit 10,
  • Extract the column number in the query result and use it as the argument in the normalize function. Then invoke the function.
  • In function, use list comprehension to process the number vector, which processes every element by the normalize0 function.
  • Returns the result named as value column.

The -> vector[i64] part specifies the return column types for generating the output schema.

This example also shows how to import the stdlib and define other functions(the normalize0) for invoking. The normalize coprocessor will be called in streaming. The query result may contain several batches, and the engine will call the coprocessor with each batch. And we should remember that the columns extracted from the query result are all vectors. We will cover vectors in the next chapter.

Submit and run this script will generate the output:

json

  1. {
  2. "output": [
  3. {
  4. "records": {
  5. "schema": {
  6. "column_schemas": [
  7. {
  8. "name": "value",
  9. "data_type": "Int64"
  10. }
  11. ]
  12. },
  13. "rows": [
  14. [0],
  15. [1],
  16. [2],
  17. [3],
  18. [4],
  19. [5],
  20. [5],
  21. [5],
  22. [5],
  23. [5]
  24. ]
  25. }
  26. }
  27. ]
  28. }