Skip to content

Commit bf07127

Browse files
Kunal Khatuasohami
Kunal Khatua
authored andcommitted
DRILL-7160: e.q.max_rows QUERY-level option shown even if not set
The fix is to force setting to zero IFF autoLimit was intended to be set originally but is inapplicable; such as 'SHOW DATABASES'. If autolimit was not intended to be applied, setting the value to zero is not required. WebUI is also patched to not show the zero value set in such scenarios.
1 parent 455cee7 commit bf07127

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/DrillSqlWorker.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/QueryWrapper.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.apache.drill.common.exceptions.UserException;
2424
import org.apache.drill.common.exceptions.UserRemoteException;
25+
import org.apache.drill.exec.ExecConstants;
2526
import org.apache.drill.exec.proto.UserBitShared.QueryId;
2627
import org.apache.drill.exec.proto.UserBitShared.QueryResult.QueryState;
2728
import org.apache.drill.exec.proto.UserBitShared.QueryType;
@@ -77,7 +78,14 @@ public QueryResult run(final WorkManager workManager, final WebUserConnection we
7778
.setAutolimitRowcount(autoLimitRowCount)
7879
.build();
7980

80-
webUserConnection.setAutoLimitRowCount(autoLimitRowCount);
81+
int defaultMaxRows = webUserConnection.getSession().getOptions().getOption(ExecConstants.QUERY_MAX_ROWS).num_val.intValue();
82+
int maxRows;
83+
if (autoLimitRowCount > 0 && defaultMaxRows > 0) {
84+
maxRows = Math.min(autoLimitRowCount, defaultMaxRows);
85+
} else {
86+
maxRows = Math.max(autoLimitRowCount, defaultMaxRows);
87+
}
88+
webUserConnection.setAutoLimitRowCount(maxRows);
8189

8290
// Submit user query to Drillbit work queue.
8391
final QueryId queryId = workManager.getUserWorker().submitWork(webUserConnection, runQuery);

0 commit comments

Comments
 (0)