Redshift UDF逻辑问题 [英] Redshift UDF logical issue

查看:61
本文介绍了Redshift UDF逻辑问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写redshift udf以验证时间戳.但是,它总是返回false.有人可以解释为什么吗?

I am trying to write a redshift udf to validate timestamp. But, it always returns false. Can some explain why?

create or replace function f_Is_timestamp_sql(VARCHAR(20000))
  returns timestamp  
  STABLE
as $$
       select $1::timestamp as a;
$$ language sql;

create or replace function f_Is_timestamp(val VARCHAR(20000))
  returns bool
IMMUTABLE 
as $$
    try:
       (f_Is_timestamp_sql(val));  
    except:
       return (1==2);
    else:
      return 1==1;
$$ language plpythonu;

select f_Is_timestamp('2019-10-09')

推荐答案

在阅读了AWS文档之后,我发现一个UDF无法引用另一个UDF的内容. https://docs.aws.amazon.com/redshift/latest/dg/udf-python-language-support.html 因此,我的函数总是抛出异常.我想出了另一种使用python库完成此操作的方法

After reading thru the AWS documents, I figured out that a UDF cannot reference the contents of another UDF. https://docs.aws.amazon.com/redshift/latest/dg/udf-python-language-support.html Therefore, my function always throws an exception. I figured out an alternative way to accomplish this using python library

dateutil.parser

下面的工作功能.

create or replace function f_Is_timestamp(val VARCHAR(20000))
  returns bool
IMMUTABLE 
as $$
    from dateutil.parser import parse;
    try:
        parse(val,ignoretz=True);
    except:
        return 1==2;
    else:
        return 1==1;
$$ language plpythonu;

这篇关于Redshift UDF逻辑问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆