@@ -216,27 +216,31 @@ private static PhysicalPlan getQueryPlan(QueryContext context, String sql, Point
216
216
return handler .getPlan (sqlNode );
217
217
}
218
218
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 ));
222
222
}
223
223
224
224
private static SqlNode checkAndApplyAutoLimit (SqlConverter parser , QueryContext context , String sql ) {
225
225
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 );
228
229
} 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
+ }
230
234
}
231
235
return sqlNode ;
232
236
}
233
237
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 ;
236
240
}
237
241
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 );
240
244
if (sqlNode .getKind () == SqlKind .ORDER_BY ) {
241
245
SqlOrderBy orderBy = (SqlOrderBy ) sqlNode ;
242
246
return new SqlOrderBy (orderBy .getParserPosition (), orderBy .query , orderBy .orderList , orderBy .offset , autoLimitLiteral );
0 commit comments