public boolean isProbablyDeterministic ( String sql ) { final String upperSql = sql . toUpperCase ( ) ; if ( ! upperSql . contains ( "ORDER BY" ) ) { return false ; } final int i = upperSql . lastIndexOf ( "ORDER BY" ) ; final String tail = upperSql . substring ( i ) ; final int closeCount = tail . length ( ) - tail . replace ( ")" , "" ) . length ( ) ; final int openCount = tail . length ( ) - tail . replace ( "(" , "" ) . length ( ) ; if ( closeCount > openCount ) { // The last ORDER BY occurs within parentheses. It is either in a // sub-query or in a windowed aggregate. Neither of these make the // ordering deterministic. return false ; } return true ; }
Returns whether a SQL query is likely to produce results always in the same order .