@@ -216,27 +216,31 @@ private static PhysicalPlan getQueryPlan(QueryContext context, String sql, Point
216216 return handler .getPlan (sqlNode );
217217 }
218218
219- private static boolean isAutoLimitShouldBeApplied (QueryContext context , SqlNode sqlNode ) {
220- return (context . getOptions (). getOption ( ExecConstants . QUERY_MAX_ROWS ). num_val . intValue () > 0 ) && sqlNode .getKind ().belongsTo (SqlKind .QUERY )
221- && (sqlNode .getKind () != SqlKind .ORDER_BY || isAutoLimitLessThanOrderByFetch ((SqlOrderBy ) sqlNode , context ));
219+ private static boolean isAutoLimitShouldBeApplied (SqlNode sqlNode , int queryMaxRows ) {
220+ return (queryMaxRows > 0 ) && sqlNode .getKind ().belongsTo (SqlKind .QUERY )
221+ && (sqlNode .getKind () != SqlKind .ORDER_BY || isAutoLimitLessThanOrderByFetch ((SqlOrderBy ) sqlNode , queryMaxRows ));
222222 }
223223
224224 private static SqlNode checkAndApplyAutoLimit (SqlConverter parser , QueryContext context , String sql ) {
225225 SqlNode sqlNode = parser .parse (sql );
226- if (isAutoLimitShouldBeApplied (context , sqlNode )) {
227- sqlNode = wrapWithAutoLimit (sqlNode , context );
226+ int queryMaxRows = context .getOptions ().getOption (ExecConstants .QUERY_MAX_ROWS ).num_val .intValue ();
227+ if (isAutoLimitShouldBeApplied (sqlNode , queryMaxRows )) {
228+ sqlNode = wrapWithAutoLimit (sqlNode , queryMaxRows );
228229 } else {
229- context .getOptions ().setLocalOption (ExecConstants .QUERY_MAX_ROWS , 0 );
230+ //Force setting to zero IFF autoLimit was intended to be set originally but is inapplicable
231+ if (queryMaxRows > 0 ) {
232+ context .getOptions ().setLocalOption (ExecConstants .QUERY_MAX_ROWS , 0 );
233+ }
230234 }
231235 return sqlNode ;
232236 }
233237
234- private static boolean isAutoLimitLessThanOrderByFetch (SqlOrderBy orderBy , QueryContext context ) {
235- return orderBy .fetch == null || Integer .parseInt (orderBy .fetch .toString ()) > context . getOptions (). getOption ( ExecConstants . QUERY_MAX_ROWS ). num_val . intValue () ;
238+ private static boolean isAutoLimitLessThanOrderByFetch (SqlOrderBy orderBy , int queryMaxRows ) {
239+ return orderBy .fetch == null || Integer .parseInt (orderBy .fetch .toString ()) > queryMaxRows ;
236240 }
237241
238- private static SqlNode wrapWithAutoLimit (SqlNode sqlNode , QueryContext context ) {
239- SqlNumericLiteral autoLimitLiteral = SqlLiteral .createExactNumeric (String .valueOf (context . getOptions (). getOption ( ExecConstants . QUERY_MAX_ROWS ). num_val . intValue () ), SqlParserPos .ZERO );
242+ private static SqlNode wrapWithAutoLimit (SqlNode sqlNode , int queryMaxRows ) {
243+ SqlNumericLiteral autoLimitLiteral = SqlLiteral .createExactNumeric (String .valueOf (queryMaxRows ), SqlParserPos .ZERO );
240244 if (sqlNode .getKind () == SqlKind .ORDER_BY ) {
241245 SqlOrderBy orderBy = (SqlOrderBy ) sqlNode ;
242246 return new SqlOrderBy (orderBy .getParserPosition (), orderBy .query , orderBy .orderList , orderBy .offset , autoLimitLiteral );
0 commit comments