@@ -3473,7 +3473,6 @@ protected void renderQueryGroup(QueryGroup queryGroup, boolean renderOrderByAndO
34733473 final int queryPartForRowNumberingClauseDepth = this .queryPartForRowNumberingClauseDepth ;
34743474 final boolean needsSelectAliases = this .needsSelectAliases ;
34753475 try {
3476- String queryGroupAlias = null ;
34773476 // See the field documentation of queryPartForRowNumbering etc. for an explanation about this
34783477 final QueryPart currentQueryPart = queryPartStack .getCurrent ();
34793478 if ( currentQueryPart != null && queryPartForRowNumberingClauseDepth != clauseStack .depth () ) {
@@ -3482,88 +3481,12 @@ protected void renderQueryGroup(QueryGroup queryGroup, boolean renderOrderByAndO
34823481 // If explicit column aliases were defined we should still use them when rendering the select clause
34833482 this .needsSelectAliases = columnAliases != null ;
34843483 }
3485- // If we are row numbering the current query group, this means that we can't render the
3486- // order by and offset fetch clause, so we must do row counting on the query group level
3487- final boolean needsRowNumberingWrapper = queryPartForRowNumbering == queryGroup
3488- || additionalWherePredicate != null && !additionalWherePredicate .isEmpty ();
3489- final boolean needsQueryGroupWrapper =
3490- currentQueryPart instanceof QueryGroup && !dialect .supportsSimpleQueryGrouping ();
3491- final boolean needsParenthesis ;
3492- if ( currentQueryPart instanceof QueryGroup ) {
3493- // When this is query group within a query group, we can only do simple grouping if that is supported,
3494- // and we don't already add a query group wrapper
3495- needsParenthesis = !needsRowNumberingWrapper && !needsQueryGroupWrapper ;
3496- }
3497- else {
3498- needsParenthesis = queryGroup .hasOffsetOrFetchClause () && !queryGroup .isRoot ();
3499- }
3500- if ( needsParenthesis ) {
3501- appendSql ( OPEN_PARENTHESIS );
3502- }
3503- if ( needsRowNumberingWrapper ) {
3504- this .needsSelectAliases = true ;
3505- queryGroupAlias = "grp_" + queryGroupAliasCounter + '_' ;
3506- queryGroupAliasCounter ++;
3507- appendSql ( "select " );
3508- appendSql ( queryGroupAlias );
3509- appendSql ( ".* " );
3510- final SelectClause firstSelectClause = queryGroup .getFirstQuerySpec ().getSelectClause ();
3511- final List <SqlSelection > sqlSelections = firstSelectClause .getSqlSelections ();
3512- final int sqlSelectionsSize = sqlSelections .size ();
3513- // We need this synthetic select clause to properly render the ORDER BY within the OVER clause
3514- // of the row numbering functions
3515- final SelectClause syntheticSelectClause = new SelectClause ( sqlSelectionsSize );
3516- for ( int i = 0 ; i < sqlSelectionsSize ; i ++ ) {
3517- syntheticSelectClause .addSqlSelection (
3518- new SqlSelectionImpl (
3519- i ,
3520- new ColumnReference (
3521- queryGroupAlias ,
3522- "c" + i ,
3523- false ,
3524- null ,
3525- getIntegerType ()
3526- )
3527- )
3528- );
3529- }
3530- renderRowNumberingSelectItems ( syntheticSelectClause , queryPartForRowNumbering );
3531- appendSql ( " from (" );
3532- }
3533- else if ( needsQueryGroupWrapper ) {
3534- // Query group nested inside a query group
3535- this .needsSelectAliases = true ;
3536- queryGroupAlias = "grp_" + queryGroupAliasCounter + '_' ;
3537- queryGroupAliasCounter ++;
3538- appendSql ( "select " );
3539- appendSql ( queryGroupAlias );
3540- appendSql ( ".* " );
3541- appendSql ( " from (" );
3542- }
3543- queryPartStack .push ( queryGroup );
3544- final List <QueryPart > queryParts = queryGroup .getQueryParts ();
3545- final String setOperatorString = ' ' + queryGroup .getSetOperator ().sqlString () + ' ' ;
3546- String separator = "" ;
3547- for ( int i = 0 ; i < queryParts .size (); i ++ ) {
3548- appendSql ( separator );
3549- queryParts .get ( i ).accept ( this );
3550- separator = setOperatorString ;
3551- }
3552-
3553- if ( renderOrderByAndOffsetFetchClause ) {
3554- visitOrderBy ( queryGroup .getSortSpecifications () );
3555- visitOffsetFetchClause ( queryGroup );
3556- }
3557- if ( queryGroupAlias != null ) {
3558- appendSql ( ") " );
3559- appendSql ( queryGroupAlias );
3560- if ( additionalWherePredicate != null && !additionalWherePredicate .isEmpty () ) {
3561- visitWhereClause ( additionalWherePredicate );
3562- }
3563- }
3564- if ( needsParenthesis ) {
3565- appendSql ( CLOSE_PARENTHESIS );
3566- }
3484+ renderQueryGroup (
3485+ queryGroup ,
3486+ queryPartForRowNumbering ,
3487+ currentQueryPart ,
3488+ renderOrderByAndOffsetFetchClause
3489+ );
35673490 }
35683491 finally {
35693492 queryPartStack .pop ();
@@ -3573,6 +3496,146 @@ else if ( needsQueryGroupWrapper ) {
35733496 }
35743497 }
35753498
3499+ private void renderQueryGroup (
3500+ QueryGroup queryGroup ,
3501+ QueryPart queryPartForRowNumbering ,
3502+ QueryPart currentQueryPart ,
3503+ boolean renderOrderByAndOffsetFetchClause ) {
3504+ final boolean needsRowNumberingWrapper =
3505+ needsRowNumbering ( queryGroup , queryPartForRowNumbering );
3506+ final boolean needsQueryGroupWrapper =
3507+ nonsimpleQueryGrouping ( currentQueryPart );
3508+ final boolean needsParenthesis =
3509+ needsParenthesesAroundQueryGroup (
3510+ queryGroup ,
3511+ currentQueryPart ,
3512+ needsRowNumberingWrapper ,
3513+ needsQueryGroupWrapper
3514+ );
3515+ if ( needsParenthesis ) {
3516+ appendSql ( OPEN_PARENTHESIS );
3517+ }
3518+ beforeQueryGroup ( queryGroup , currentQueryPart );
3519+ final String queryGroupAlias =
3520+ wrapQueryPartsIfNecessary (
3521+ queryGroup ,
3522+ queryPartForRowNumbering ,
3523+ needsRowNumberingWrapper ,
3524+ needsQueryGroupWrapper
3525+ );
3526+ queryPartStack .push ( queryGroup );
3527+ renderQueryParts ( queryGroup );
3528+ afterQueryGroup ( queryGroup , currentQueryPart );
3529+
3530+ if ( renderOrderByAndOffsetFetchClause ) {
3531+ visitOrderBy ( queryGroup .getSortSpecifications () );
3532+ visitOffsetFetchClause ( queryGroup );
3533+ }
3534+ if ( queryGroupAlias != null ) {
3535+ appendSql ( CLOSE_PARENTHESIS );
3536+ appendSql ( WHITESPACE );
3537+ appendSql ( queryGroupAlias );
3538+ if ( additionalWherePredicate != null && !additionalWherePredicate .isEmpty () ) {
3539+ visitWhereClause ( additionalWherePredicate );
3540+ }
3541+ }
3542+ if ( needsParenthesis ) {
3543+ appendSql ( CLOSE_PARENTHESIS );
3544+ }
3545+ }
3546+
3547+ private boolean nonsimpleQueryGrouping (QueryPart currentQueryPart ) {
3548+ return currentQueryPart instanceof QueryGroup
3549+ && !dialect .supportsSimpleQueryGrouping ();
3550+ }
3551+
3552+ private boolean needsRowNumbering (QueryGroup queryGroup , QueryPart queryPartForRowNumbering ) {
3553+ // If we are row numbering the current query group, this means that we can't render the
3554+ // order by and offset fetch clause, so we must do row counting on the query group level
3555+ return queryPartForRowNumbering == queryGroup
3556+ || additionalWherePredicate != null && !additionalWherePredicate .isEmpty ();
3557+ }
3558+
3559+ private String wrapQueryPartsIfNecessary (
3560+ QueryGroup queryGroup ,
3561+ QueryPart queryPartForRowNumbering ,
3562+ boolean needsRowNumberingWrapper ,
3563+ boolean needsQueryGroupWrapper ) {
3564+ if ( needsRowNumberingWrapper ) {
3565+ this .needsSelectAliases = true ;
3566+ final String queryGroupAlias = "grp_" + queryGroupAliasCounter + '_' ;
3567+ queryGroupAliasCounter ++;
3568+ appendSql ( "select " );
3569+ appendSql ( queryGroupAlias );
3570+ appendSql ( ".* " );
3571+ final SelectClause firstSelectClause = queryGroup .getFirstQuerySpec ().getSelectClause ();
3572+ final int sqlSelectionsSize = firstSelectClause .getSqlSelections ().size ();
3573+ // We need this synthetic select clause to properly render the ORDER BY within the OVER clause
3574+ // of the row numbering functions
3575+ final SelectClause syntheticSelectClause = new SelectClause ( sqlSelectionsSize );
3576+ for ( int i = 0 ; i < sqlSelectionsSize ; i ++ ) {
3577+ syntheticSelectClause .addSqlSelection (
3578+ new SqlSelectionImpl (
3579+ i ,
3580+ new ColumnReference (
3581+ queryGroupAlias ,
3582+ "c" + i ,
3583+ false ,
3584+ null ,
3585+ getIntegerType ()
3586+ )
3587+ )
3588+ );
3589+ }
3590+ renderRowNumberingSelectItems ( syntheticSelectClause , queryPartForRowNumbering );
3591+ appendSql ( " from " );
3592+ appendSql ( OPEN_PARENTHESIS );
3593+ return queryGroupAlias ;
3594+ }
3595+ else if ( needsQueryGroupWrapper ) {
3596+ // Query group nested inside a query group
3597+ this .needsSelectAliases = true ;
3598+ final String queryGroupAlias = "grp_" + queryGroupAliasCounter + '_' ;
3599+ queryGroupAliasCounter ++;
3600+ appendSql ( "select " );
3601+ appendSql ( queryGroupAlias );
3602+ appendSql ( ".* " );
3603+ appendSql ( " from " );
3604+ appendSql ( OPEN_PARENTHESIS );
3605+ return queryGroupAlias ;
3606+ }
3607+ else {
3608+ return null ;
3609+ }
3610+ }
3611+
3612+ private void renderQueryParts (QueryGroup queryGroup ) {
3613+ final var queryParts = queryGroup .getQueryParts ();
3614+ final String setOperatorString = ' ' + queryGroup .getSetOperator ().sqlString () + ' ' ;
3615+ String separator = "" ;
3616+ for ( int i = 0 ; i < queryParts .size (); i ++ ) {
3617+ appendSql ( separator );
3618+ queryParts .get ( i ).accept ( this );
3619+ separator = setOperatorString ;
3620+ }
3621+ }
3622+
3623+ protected void afterQueryGroup (QueryGroup queryGroup , QueryPart currentQueryPart ) {
3624+ }
3625+
3626+ protected void beforeQueryGroup (QueryGroup queryGroup , QueryPart currentQueryPart ) {
3627+ }
3628+
3629+ protected boolean needsParenthesesAroundQueryGroup (
3630+ QueryGroup queryGroup , QueryPart currentQueryPart ,
3631+ boolean needsRowNumberingWrapper , boolean needsQueryGroupWrapper ) {
3632+ return currentQueryPart instanceof QueryGroup
3633+ // When this is query group within a query group, we can only do simple grouping
3634+ // if that is supported, and we don't already add a query group wrapper
3635+ ? !needsRowNumberingWrapper && !needsQueryGroupWrapper
3636+ : queryGroup .hasOffsetOrFetchClause () && !queryGroup .isRoot ();
3637+ }
3638+
35763639 @ Override
35773640 public void visitQuerySpec (QuerySpec querySpec ) {
35783641 final QueryPart queryPartForRowNumbering = this .queryPartForRowNumbering ;
0 commit comments