- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 MySQL 中创建 UDF 函数。
我想根据输入参数返回 7 位十进制/ double 。
我将以下函数安装到 MySQL 中,如下所示:
CREATE FUNCTION postrank_udf RETURNS REAL SONAME 'postrank.so'
我的函数 Select postrank(120, 0, 1444763428) 的正确结果应该是 13.8604775。但是,除非我执行 postrank(120, 0, 1444763428.0000000),否则它现在返回 14。下面是我的 UDF 函数。有什么想法吗?
#ifdef STANDARD
/* STANDARD is defined, don't use any mysql functions */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef __WIN__
typedef unsigned __int64 ulonglong;/* Microsofts 64 bit types */
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/
#else
#include <my_global.h>
#include <my_sys.h>
#if defined(MYSQL_SERVER)
#include <m_string.h>/* To get strmov() */
#else
/* when compiled as standalone */
#include <string.h>
#define strmov(a,b) stpcpy(a,b)
#define bzero(a,b) memset(a,0,b)
#define memcpy_fixed(a,b,c) memcpy(a,b,c)
#endif
#endif
#include <mysql.h>
#include <ctype.h>
#ifdef HAVE_DLOPEN
#if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_SOLARIS_STYLE_GETHOST)
static pthread_mutex_t LOCK_hostname;
#endif
#include <math.h>
my_bool postrank_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void postrank_deinit(UDF_INIT *initid __attribute__((unused)));
double postrank(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
char* is_null __attribute__((unused)), char* error __attribute__((unused)));
my_bool postrank_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
if(!(args->arg_count == 3)) {
strcpy(message, "Expected three arguments");
return 1;
}
args->arg_type[0] = REAL_RESULT;
args->arg_type[1] = REAL_RESULT;
args->arg_type[2] = REAL_RESULT;
return 0;
}
void postrank_deinit(UDF_INIT *initid __attribute__((unused)))
{
}
double postrank(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
char* is_null __attribute__((unused)), char* error __attribute__((unused)))
{
double ups = *((double *)(args->args[0]));
double downs = *((double *)(args->args[1]));
double d = *((double *)(args->args[2]));
double final = 0.00;
double my_sign = 1.0;
if(ups - downs > 0){
my_sign = 1.0;
}else if(ups - downs < 0){
my_sign = -1.0;
}else{
my_sign = 0.0;
}
int myups = (int) ups;
int mydowns = (int) downs;
int my_max = 1;
if(abs(myups-mydowns) > 1){
my_max = abs(myups-mydowns);
}
double dbmy_max = (double) my_max;
double result = log10(dbmy_max) * my_sign + ((d - 1444000000.0) / 64800.0);
final = round( 10000000.0 * result ) / 10000000.0;
return final;
}
#endif /* HAVE_DLOPEN */
最佳答案
您的代码很好,请尝试这个小调试代码,它会打印出您所期望的内容。
#include <usual.h>
int main( )
{
// int ups = *((int *)(args->args[0]));
// int downs = *((int *)(args->args[1]));
// double d = *((double *)(args->args[2]));
int ups = 120;
int downs = 0;
double d = 1444763428;
double debugd = 0.00;
double my_sign = 1.0;
if ( ups - downs > 0 )
{
my_sign = 1.0;
}
else if ( ups - downs < 0 )
{
my_sign = -1.0;
}
else
{
my_sign = 0.0;
}
int my_max = 1;
if ( abs( ups - downs ) > 1 )
{
my_max = abs( ups - downs );
}
double dbmy_max = ( double ) my_max;
double result =
log10( dbmy_max ) * my_sign + ( d - 1444000000.0 ) / 64800.0;
// return round(10000000.0 * result) / 10000000.0;
debugd = round( 10000000.0 * result ) / 10000000.0;
printf( "\n debugd == %5.7f ", debugd );
}
关于MySQL UDF 只返回整数 - double 想要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33115783/
我在弄清楚如何从另一个 UDF 返回 UDF 中的数组时遇到了一些麻烦。这里的一个是简单的指数移动平均 UDF,我试图将数组返回到另一个 UDF,但我收到 #value 错误。我觉得有一个我没有看到的
我需要从另一个模块(在同一个工作簿中)调用以前制作的 UDF 来构建另一个 UDF。如何调用第一个函数? 这适用于 Excel VBA。我已经构建了我的第一个函数,它计算产品在特定时间的账面值(val
这个问题可能对许多 VBA 程序员有用。它涉及实现两个有用的独立任务并使它们同时工作。 第一个任务是为 UDF 制作 Excel 函数工具提示。虽然似乎还没有找到明确的解决方案,但目前我对自定义插入函
我正在将 Spark 与 Scala 一起使用,并希望将整行传递给 udf 并选择 udf 中的每个列名和列值。我怎样才能做到这一点? 我正在尝试以下 - inputDataDF.withColumn
这个问题在这里已经有了答案: Spark functions vs UDF performance? (3 个答案) 关闭2 年前。 我从 Pyspark 网站获取了以下 UDF,因为我试图了解是否
我已经使用 Spark 2.4 一段时间了,最近几天才开始切换到 Spark 3.0。切换到 Spark 3.0 运行后出现此错误 udf((x: Int) => x, IntegerType)
这个问题源自 SQLServer: Why avoid Table-Valued User Defined Functions? 。我开始在一些评论中提出问题,而对我评论的回复却偏离了主题。 这样您就
这是我的 hive 表 CREATE TABLE `dum`(`val` map>); insert into dum select map('A',array('1','2','3'),'B',ar
我想知道编写 spark udf 是否会降低性能。一般来说,我更喜欢组合做一件事的小函数…… 这是一个简单的例子,给定一个 DataFrame df: def inc = udf( (i: Doubl
我正在尝试根据另一列的值在 Spark 数据集中创建一个新列。另一列的值作为键在 json 文件中搜索,返回的值是用于新列的值。 这是我尝试过的代码,但它不起作用,而且我不确定 UDF 是如何工作的。
SPARK_VERSION = 2.2.0 我在尝试做 filter 时遇到了一个有趣的问题。在具有使用 UDF 添加的列的数据帧上。我能够用较小的数据集复制问题。 鉴于虚拟案例类: case cla
我正在 Java 中使用 Spark 来处理 XML 文件。来自databricks的spark-xml包用于将xml文件读入dataframe。 示例 xml 文件是: 1 joh
我正在尝试创建一个 MySQL UDF getFile(),它应该从磁盘上的某个目录返回文本文件的内容。问题是调用一次或两次有效,但在第二次或第三次调用 UDF 时,MySQL 服务器崩溃。 我无法通
我听说 Microsoft SQL Server 中有多种方法可以查找“最差”存储过程:按执行次数、按 CPU 工作时间、按队列等待时间等。 我正在寻找一种方法来查找最差(最慢/最常用)的 UDF -
我已经为一个项目构建了一个包含多个公式的 Excel 工作表。然后,我添加了一个用于折叠/展开某些单元组的命令按钮。 命令按钮代码是: Private Sub CommandButton1_Click
MySQL版本:5.1.73数据库客户端版本:libmysql - 5.1.73 我试图检查 NEW.src 在过去一小时内是否存在,如果不存在则执行 sys_exec udf。 我在 mysql 中
我正在尝试将元组列表传递给 scala 中的 udf。我不确定如何为此准确定义数据类型。我试图将它作为一整行传递,但它无法真正解决它。我需要根据元组的第一个元素对列表进行排序,然后返回 n 个元素。我
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 此问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-topic
我正在尝试创建一个类似 =Extractinfo("A2","Name") 的函数,它可以从原始数据中提取姓名、电话和电子邮件 ID,一个用于所有 3 次提取的函数,我已经有一个提取电子邮件 ID 的
我正在编写一个用户定义函数(UDF),它以一些单元格作为参数。 这些单元格包含相同的数据,但精度不同;该功能显示可用的最佳精度。 函数的参数按精度升序编写。 这是一个例子: +---+--------
我是一名优秀的程序员,十分优秀!