版本 druid 1.1.13
with col_regid as (select first(regid) as regid, INID from his.OPM_OTHER_INHOSAPPLY_INFO T32 where T32.EXT18 = '0' AND T32.STATUS <> '3' group by T32.INID), col_staticname as (SELECT staticcode, first(staticname) as staticname FROM his.FIN_STATICCODE_LOCAL group by staticcode) SELECT a.orgcode, a.orgname, to_date(A.FEEDATE), first(T32.Regid), first(i.staticname), sum(A.TOTCOST - ifnull(A.FEE_DISCOUNT, 0)) FROM his.IPB_ITEMLIST A left join col_regid T32 on T32.INID = a.INID, his.OAM_OPERATION_APPLY t left join col_staticname i on i.staticcode = t.szbm WHERE a.ORGCODE <> '122' and to_date(A.FEEDATE) between TO_DATE('2021-07-01') AND date_sub(now(), 1) and a.inid = t.inid and t.applystate = '3' and t.source = '2' AND t.ORGCODE <> '122' GROUP BY a.orgcode, a.orgname, to_date(A.FEEDATE), t.szbm, a.INID
然后执行 System.out.println(SQLUtils.formatHive(sql));
生成的语句有错误
原提问者GitHub用户8debug
WITH ... AS ... 是 SQL 标准中定义的一个子句,用于在一个查询中定义一个临时的结果集,以便后续查询可以引用它。在进行 SQL 语句格式化时,可能会出现缺失的情况,可能是由于格式化器没有正确处理 WITH ... AS ... 子句导致的。
如果您使用的是阿里巴巴的 druid-sql 工具库提供的 SQL 格式化器,在格式化 WITH ... AS ... 子句时可能会出现缺失的情况。这是因为 druid-sql 的格式化器默认不支持 WITH ... AS ... 子句的格式化,会将子句中的换行符和缩进删除,导致格式化后的 SQL 语句不正确。如果您需要格式化 WITH ... AS ... 子句,可以使用 druid-sql 提供的 SQLSelectListExpr.getASTNode() 方法来获取子句的 AST(抽象语法树)节点,然后使用 AST 节点生成格式化后的 SQL 语句。
我在版本1.1.20中未重现问题,貌似已经解决。 以下是格式化输出的内容:
WITH col_regid AS ( SELECT first(regid) AS regid, INID FROM his.OPM_OTHER_INHOSAPPLY_INFO T32 WHERE T32.EXT18 = '0' AND T32.STATUS <> '3' GROUP BY T32.INID ), col_staticname AS ( SELECT staticcode, first(staticname) AS staticname FROM his.FIN_STATICCODE_LOCAL GROUP BY staticcode ) SELECT a.orgcode, a.orgname, to_date(A.FEEDATE) , first(T32.Regid), first(i.staticname) , SUM(A.TOTCOST - ifnull(A.FEE_DISCOUNT, 0)) FROM (his.IPB_ITEMLIST A LEFT JOIN col_regid T32 ON T32.INID = a.INID, his.OAM_OPERATION_APPLY t) LEFT JOIN col_staticname i ON i.staticcode = t.szbm WHERE a.ORGCODE <> '122' AND to_date(A.FEEDATE) BETWEEN TO_DATE('2021-07-01') AND date_sub(now(), 1) AND a.inid = t.inid AND t.applystate = '3' AND t.source = '2' AND t.ORGCODE <> '122' GROUP BY a.orgcode, a.orgname, to_date(A.FEEDATE), t.szbm, a.INID
原回答者GitHub用户zhangyibin1991
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。