这是来自钻用户邮件列表。希望对您有所帮助:
http://mail-archives.apache.org/mod_mbox/drill-user/201509.mbox/%3CB6A39848-AACB-4AD2-BD62-58C395D6CC9E@maprtech.com%3E
您也可以使用 POSTION、STRPOS 或 LOCATE 来查找字符串中的子字符串并返回位置。
LOCATE 是最有用的 IMO,因为它允许您指定在字符串中从何处开始搜索,但是这些都没有 INSTR 或 substring-index 功能来指定要查找的子字符串的出现位置。
UDF 的一个很好的例子。
以下是使用子查询将 IP 地址分解为不同部分的解决方法。
select p2.ip_address, p2.part1, p2.part2, substr(p2.rest2, 1, locate('.',p2.rest2)-1) as part3,
substr(rest2, locate('.',rest2)+1) as part4
from
(select p1.ip_address, p1.part1, substr(rest1, 1, locate('.',rest1)-1) as part2,
substr(rest1, locate('.',rest1)+1) as rest2
from
(select ip_address, substr(ip_address, 1, locate('.',ip_address)-1) as part1,
substr(ip_address, locate('.',ip_address)+1) as rest1 from `/ip`) as p1) as p2
+---------------+--------+--------+--------+--------+
| ip_address | part1 | part2 | part3 | part4 |
+---------------+--------+--------+--------+--------+
| 172.16.254.1 | 172 | 16 | 254 | 1 |
+---------------+--------+--------+--------+--------+
您可以在此处找到所有支持的钻柱功能:
https://drill.apache.org/docs/string-manipulation/#strpos