Spark SQL函数中对应的PG数据库函数测试

Spark SQL函数中对应的PG数据库函数测试

背景:
统计PG存储过程中使用到的函数,同spark sql中的函数做对比并进行适当转换,方便后续开发工作
注:仅限于简单测试,发现不对请多指正

to_char

pg:存储过程中多数用于转换时间,转换格式为’yyyy-mm-dd’
spark:已注册为udf函数

coalesce

pg:COALESCE(currentcount,0),有值返回currentcount,否则返回0
spark:用法同pg相同,string类型不适用,仅测试int类型

like

pg:drddd.purchase_way_name like ‘%国家集采%’
spark:
//Using it on SQL to filter rows
df.createOrReplaceTempView(“TAB”)
spark.sql(“select * from TAB where name like ‘%rose%’”).show()

distinct

pg:存储过程中多数配合count和sum使用
spark:select count(distinct class)/select sum(distinct class)

SUBSTRING

pg:SUBSTRING(v_day_start, 1 , 7),存储过程中多数用于截取时间
spark:同pg一样的使用方式

to_number

pg:coalesce(to_number(dept_id,‘9999999999999’) ,-1),存储过程中使用于类型转换
spark:可用cast函数代替,coalesce(cast(ceshi_id as integer),-1)

extract

pg:extract(year from age(v_day_start::timestamp, to_date(prac_register_time, ‘YYYY-MM-DD’) )) <1
先用age取差集,在取year的子域,最后取小于1的
spark:floor(abs(months_between(starttime,endtime))/12) < 1 (date 与 timestamp 类型均可用)
months_between先计算两个时间之间差几个月
abs将差值转成绝对值,除以12
floor,向下舍入

CAST

pg:数据类型转换
spark:cast(ceshi_id as integer)

case when

pg:用于匹配 case when a.total_amount>0 or a.total_amount=0 then 1 when a.total_amount<0 then -1 end
spark: case when starttime is null then now() when starttime is not null then starttime end spark 中可用

date_part

pg:用于提取时间中的子域
spark:可使用对应的时间函数

nullif

pg:coalesce(nullif(dsiimro.oper_doc_id, ‘’), ‘-10’),用于判断字段值是 否是 ‘空’,再使用coalesce赋值
spark:需要自定义,当类型为string时,使用自定义的函数,类型为integer:ifnull(cs_id,-1)

similar to

pg:正则表达式,相当于like ‘’ or like ‘’(op.oper_code similar to ‘%(74.0|74.1|74.2|74.4|74.990)%’)
spark:先使用like代替

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Get key

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值