@@ -827,12 +827,6 @@ public static boolean checkIsGroupByTimeWindow(SqlNode sqlNode, Collection<Strin
827827 }
828828 }
829829
830- public static boolean checkIsTimeGroupByFunction (String functionName ){
831- return functionName .equalsIgnoreCase ("tumble" )
832- || functionName .equalsIgnoreCase ("session" )
833- || functionName .equalsIgnoreCase ("hop" );
834- }
835-
836830 /**
837831 * 判断group by中是否包含维表,包含则需要撤回,不管嵌套多少层子查询只要有一层包含都需要撤回
838832 *
@@ -856,50 +850,36 @@ public static boolean checkIsDimTableGroupBy(SqlNode sqlNode, Collection<String>
856850 // 2.(sub query) as alias group by
857851 // 3.tableName group by
858852 // 4.tableName as alias group by
853+ // 5.tableName no group by
854+ // 6.tableName as alias no group by
855+
856+ // 没有group by:5.no group by
857+ if (fromNode .getKind () == IDENTIFIER
858+ && (groupNodeList == null || groupNodeList .size () == 0 )) {
859+ return false ;
860+ }
859861
860862 // (子查询) group by:1.(sub query) group by
861863 if (fromNode .getKind () == SELECT ) {
862864 return checkIsDimTableGroupBy (fromNode , newRegisterTableList );
863865 }
864866
865867 // 表名 as 别名 group by、(子查询) as 别名 group by、表名 group by
866- if (fromNode .getKind () == AS || fromNode .getKind () == IDENTIFIER ) {
867- SqlNode operand ;
868+ if (fromNode .getKind () == AS ) {
868869 // 表名 as 别名 group by:4.tableName as alias group by
869- if (fromNode .getKind () == AS ) {
870- operand = ((SqlBasicCall ) fromNode ).getOperands ()[0 ];
871- } else {
872- // 表名 group by:3.tableName group by
873- operand = fromNode ;
870+ SqlNode operand = ((SqlBasicCall ) fromNode ).getOperands ()[0 ];
871+ // (子查询) as 别名 group by:2.(sub query) as alias group by
872+ if (operand .getKind () != IDENTIFIER ) {
873+ return checkIsDimTableGroupBy (fromNode , newRegisterTableList );
874874 }
875+
875876 // 最里层是表名 group by,且group by字段不为空,且表名包含在维表中
877+ // 6.tableName as alias no group by,会在这一层过滤掉
876878 if (operand .getKind () == IDENTIFIER
877879 && groupNodeList != null
878880 && groupNodeList .size () != 0
879881 && newRegisterTableList .contains (operand .toString ())) {
880- boolean isRetract = false ;
881- // 判断完所有的group by字段
882- for (SqlNode node : groupNodeList .getList ()) {
883- // 判断是否有函数
884- if (node .getKind () == OTHER_FUNCTION ) {
885- String functionName = ((SqlBasicCall ) node ).getOperator ().toString ().toLowerCase ();
886- boolean isTimeGroupByFunction = checkIsTimeGroupByFunction (functionName );
887- // 只要有窗口就不需要撤回,直接返回
888- if (isTimeGroupByFunction ) {
889- return false ;
890- }
891- // 非窗口需要撤回,继续迭代后面的字段
892- isRetract = true ;
893- } else {
894- // 其他情况需要撤回,继续迭代后面的字段
895- isRetract = true ;
896- }
897- }
898- return isRetract ;
899- } else {
900- // (子查询) as 别名 group by:2.(sub query) as alias group by
901- // 没有group by语句也会走进来,但是最后会返回不需要撤回
902- return checkIsDimTableGroupBy (fromNode , newRegisterTableList );
882+ return checkGroupByNode (groupNodeList );
903883 }
904884 }
905885
@@ -918,4 +898,37 @@ public static boolean checkIsDimTableGroupBy(SqlNode sqlNode, Collection<String>
918898 return false ;
919899 }
920900 }
901+
902+ /**
903+ * 遍历每一个group by节点,判断是否需要撤回
904+ * @param groupNodeList
905+ * @return
906+ */
907+ private static boolean checkGroupByNode (SqlNodeList groupNodeList ) {
908+ boolean isRetract = false ;
909+ // 判断完所有的group by字段
910+ for (SqlNode node : groupNodeList .getList ()) {
911+ // 判断是否有函数
912+ if (node .getKind () == OTHER_FUNCTION ) {
913+ String functionName = ((SqlBasicCall ) node ).getOperator ().toString ().toLowerCase ();
914+ boolean isTimeGroupByFunction = checkIsTimeGroupByFunction (functionName );
915+ // 只要有窗口就不需要撤回,直接返回
916+ if (isTimeGroupByFunction ) {
917+ return false ;
918+ }
919+ // 非窗口需要撤回,继续迭代后面的字段
920+ isRetract = true ;
921+ } else {
922+ // 其他情况需要撤回,继续迭代后面的字段
923+ isRetract = true ;
924+ }
925+ }
926+ return isRetract ;
927+ }
928+
929+ public static boolean checkIsTimeGroupByFunction (String functionName ){
930+ return functionName .equalsIgnoreCase ("tumble" )
931+ || functionName .equalsIgnoreCase ("session" )
932+ || functionName .equalsIgnoreCase ("hop" );
933+ }
921934}
0 commit comments