简体   繁体   English

如何在 MonetDB 中调试 SQL/Python UDF

[英]How to debug a SQL/Python UDF in MonetDB

Using native Python code in SQL UDFs in Monetdb is really powerful.在 Monetdb 的 SQL UDF 中使用本机 Python 代码非常强大。 BUT, debugging such UDFs could benefit from more support.但是,调试此类 UDF 可能会受益于更多支持。 In particular, if I use the old-fashioned print('debugging info') it disappears in the big black void.特别是,如果我使用老式的 print('debugging info') 它会消失在黑色的大空隙中。

create function dummy() 
returns string
language python{
    print('Entering the dummy UDF')
    return 'hello';
};

How to retrieve this information from the server or MonetDB client.如何从服务器或 MonetDB 客户端检索此信息。

I was debugging some Python UDF last week:)上周我正在调试一些 Python UDF:)

Step 1: first make sure your Python code at least works in a Python interpreter.第 1 步:首先确保您的 Python 代码至少可以在 Python 解释器中运行。

Step 2: in a Python UDF, write your debugging info.第 2 步:在 Python UDF 中,编写调试信息。 to a file, eg:到一个文件,例如:

    f = open('/tmp/debug.out', 'w')
    f.write('my debugging info\n')
    f.close()

This isn't ideal, but it works.这并不理想,但它确实有效。 Also, I used this to export the parameter values of my Python UDF.另外,我用它来导出我的 Python UDF 的参数值。 In this way, I can run the body of my Python UDF in a Python interpreter with the exact data I receive from MonetDB.通过这种方式,我可以使用从 MonetDB 接收到的确切数据在 Python 解释器中运行我的 Python UDF 的主体。

In case someone is still interested in this problem.如果有人仍然对这个问题感兴趣。 There are two novel ways of debugging MonetDB's Python/UDFs.调试 MonetDB 的 Python/UDF 有两种新颖的方法。

1) Using the python client pymonetdb ( https://github.com/gijzelaerr/pymonetdb ). 1) 使用 python 客户端 pymonetdb ( https://github.com/gijzelaerr/pymonetdb )。 You can install it throw pip你可以安装它 throw pip

pip install numpy

To use it, think of the following setting with a table that holds an integer and a UDF that computes the mean absolute deviation of a given column.要使用它,请考虑以下设置,其中包含一个包含整数的表和一个计算给定列的平均绝对偏差的 UDF。

CREATE TABLE integers(i INTEGER);
INSERT INTO integers VALUES (1), (3), (6), (8), (10);

CREATE OR REPLACE FUNCTION mean_deviation(column INTEGER)
RETURNS DOUBLE LANGUAGE PYTHON {
  mean = 0.0
  for i in range (0, len(column)):
    mean += column[I]
  mean = mean / len(column)
  distance = 0.0
  for i in range (0, len(column)):
    distance += column[i] - mean
  deviation = distance/len(column)
  return deviation;
};

To debug your function using terminal debugging (ie, pdb) you just need to open a database connection using pymonetdb.connect(), later you get a cursor object from the connection, and through the cursor object you call the debug() function, sending as parameters the SQL you want to examine and the UDF name you wish to debug.要使用终端调试(即 pdb)调试你的函数,你只需要使用 pymonetdb.connect() 打开一个数据库连接,稍后你从连接中获得一个游标对象,并通过游标对象调用 debug() 函数,将要检查的 SQL 和要调试的 UDF 名称作为参数发送。

import pymonetdb
conn = pymonetdb.connect(database='demo') #Open Database connection
c = conn.cursor()
sql = 'select mean_deviation(i) from integers;'
c.debug(sql, 'mean_deviation') #Console Debugging

There is an optional sampling step that only transfers a uniform random sample of the data instead of the full input data set.有一个可选的采样步骤,它只传输数据的统一随机样本而不是完整的输入数据集。 If you wish to sample you just need to send the number of elements you wish to get from the sampling (eg, c.debug(sql, 'mean_deviation', 10) in case you desire the subset of 10 elements)如果你想抽样,你只需要发送你希望从抽样中获得的元素数量(例如, c.debug(sql, 'mean_deviation', 10) 如果你想要 10 个元素的子集)

2) Using a POC plugin for PyCharm called devudf, which you can install throw the plugin page of pycharm, or by directly going to the JetBrains page: https://plugins.jetbrains.com/plugin/12063-devudf . 2) 使用 PyCharm 的 POC 插件 devudf,你可以在 pycharm 的插件页面安装它,或者直接转到 JetBrains 页面: https ://plugins.jetbrains.com/plugin/12063-devudf。 It adds an option to the main menu called "UDF Development" and allows for you do directly import and export UDFs from your database directly to pycharm, and enjoy the IDE's debugging capabilities.它在主菜单中添加了一个名为“UDF 开发”的选项,允许您直接将 UDF 从数据库导入和导出到 pycharm,并享受 IDE 的调试功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM