- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试为 mysql 编写一个自定义的 propercase 用户定义函数,所以我从 http://www.mysqludf.org/index.php 中获取了 str_ucwords 函数作为构建我自己的示例。
my_bool str_ucwords_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
/* make sure user has provided exactly one string argument */
if (args->arg_count != 1 || args->arg_type[0] != STRING_RESULT || args->args[0] == NULL)
{
strcpy(message,"str_ucwords requires one string argument");
return 1;
}
/* str_ucwords() will not be returning null */
initid->maybe_null=0;
return 0;
}
char *str_ucwords(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *res_length,
char *null_value, char *error)
{
int i;
int new_word = 0;
// copy the argument string into result
strncpy(result, args->args[0], args->lengths[0]);
*res_length = args->lengths[0];
// capitalize the first character of each word in the string
for (i = 0; i < *res_length; i++)
{
if (my_isalpha(&my_charset_latin1, result[i]))
{
if (!new_word)
{
new_word = 1;
result[i] = my_toupper(&my_charset_latin1, result[i]);
}
}
else
{
new_word = 0;
}
}
return result;
}
如果我尝试使用 select str_ucwords("test string");
则效果很好,但是如果我尝试从数据库中选择一个字段,例如 select str_ucwords(name) from name;
我没有得到任何返回。
如何更改此函数,以便它允许我从数据库中的字段中提取数据?
我已经尝试从 init 函数中删除 args->arg_type[0] != STRING_RESULT
。
最佳答案
问题不在于参数的类型,而是在调用 str_ucwords_init
时它是 NULL
(因为在检索之前调用了 str_ucwords_init
任何行)。要使 str_ucwords
与字段一起使用,您必须通过在 _init
函数中将 initid->maybe_null
设置为 1 来支持 NULL 值,并设置当实际参数为 null 时,*null_value
为 1(并且 result
为 NULL,尽管这可能不是必需的)在 str_ucwords
中。
my_bool str_ucwords_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
unsigned long res_length;
/* make sure user has provided exactly one argument */
if (args->arg_count != 1) {
snprintf(message, MYSQL_ERRMSG_SIZE, "wrong number of arguments: str_ucwords requires one string argument, got %d arguments", args->arg_count);
return 1;
}
/* make sure user has provided a string argument */
if (args->arg_type[0] != STRING_RESULT) {
snprintf(message, MYSQL_ERRMSG_SIZE, "str_ucwords requires one string argument (expected type %d, got type %d)", STRING_RESULT, args->arg_type[0]);
return 1;
}
res_length = args->lengths[0];
if (SIZE_MAX < res_length) {
snprintf(message, MYSQL_ERRMSG_SIZE, "res_length (%lu) cannot be greater than SIZE_MAX (%zu)", res_length, (size_t) (SIZE_MAX));
return 1;
}
initid->ptr = NULL;
if (res_length > 255) {
char *tmp = (char *) malloc((size_t) res_length); /* This is a safe cast because res_length <= SIZE_MAX. */
if (tmp == NULL) {
snprintf(message, MYSQL_ERRMSG_SIZE, "malloc() failed to allocate %zu bytes of memory", (size_t) res_length);
return 1;
}
initid->ptr = tmp;
}
initid->maybe_null = 1;
initid->max_length = res_length;
return 0;
}
char *str_ucwords(UDF_INIT *initid, UDF_ARGS *args,
char *result, unsigned long *res_length,
char *null_value, char *error)
{
int i;
int new_word = 1;
if (args->args[0] == NULL) {
result = NULL;
*res_length = 0;
*null_value = 1;
} else {
if (initid->ptr != NULL) {
result = initid->ptr;
}
// copy the argument string into result
memcpy(result, args->args[0], args->lengths[0]);
*res_length = args->lengths[0];
// capitalize the first character of each word in the string
for (i = 0; i < *res_length; i++) {
if (my_isalpha(&my_charset_latin1, result[i])) {
if (new_word) {
new_word = 0;
result[i] = my_toupper(&my_charset_latin1, result[i]);
} else {
result[i] = my_tolower(&my_charset_latin1, result[i]);
}
} else {
new_word = 1;
}
}
}
return result;
}
lib_mysqludf_str 的更高版本应该支持函数中的 NULL 值而无需更改,这意味着它们也应该与表列一起使用。
关于mysql - propercase mysql udf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3272824/
我在弄清楚如何从另一个 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),它以一些单元格作为参数。 这些单元格包含相同的数据,但精度不同;该功能显示可用的最佳精度。 函数的参数按精度升序编写。 这是一个例子: +---+--------
我是一名优秀的程序员,十分优秀!