Apache Superset SQLLab中PostgreSQL <<=网络地址运算符兼容问题咨询
问题
部署新的Apache Superset实例后,通过SQLLab对PostgreSQL数据库执行SQL查询基本正常,但使用PostgreSQL表示“包含于或等于”的网络地址运算符<<=时出现异常。
对应的SQL语句如下:
WITH subnets AS ( SELECT UNNEST(networks) AS subnet, network_owner, owner_country, provider_country, registrar FROM network_data ) SELECT network_owner, owner_country, provider_country, registrar, subnet, (2 ^ (32 - MASKLEN(subnet)) - 2) AS subnet_size, ( SELECT COUNT(DISTINCT(q.ip_address)) FROM request q WHERE q.ip_address <<= subnet ) AS num_logged, u.timestamp AS blocked_timestamp FROM subnets LEFT JOIN ufw_blocked u ON subnet = u.blocked_address_space ORDER BY network_owner, subnet
该语句在pgAdmin4中可正常运行,但Superset的SQLLab将<<=误判为“小于等于”运算符,返回以下错误:
Unable to parse SQL: Error parsing near '<=' at line 18:37 WHERE q.ip_address <<= subnet ^
DB Engine Error: This database does not allow for DDL/DML, and the query could not be parsed to confirm it is a read-only query. Please contact your administrator for more assistance This may be triggered by: Issue 1022 - Database does not allow data manipulation
解决方案
PostgreSQL的<<=网络地址运算符有两种等效替代写法,可避开Superset解析器的误判:
- 使用等效函数
inet_contained_or_equal(ip_column, subnet_column),这是<<=运算符的官方函数形式 - 拆解逻辑为
q.ip_address << subnet OR q.ip_address = subnet(<<表示“包含于”,加上等于判断即等价于<<=)
修改后的子查询示例(使用函数写法):
( SELECT COUNT(DISTINCT(q.ip_address)) FROM request q WHERE inet_contained_or_equal(q.ip_address, subnet) ) AS num_logged
两种写法都能实现和原运算符完全一致的逻辑,且能被Superset的SQL解析器正确识别,避免误判为DDL/DML操作。
内容的提问来源于stack exchange,提问作者pytinkerer
相关产品推荐
相关产品推荐

