Skip to content

Commit

Permalink
DRILL-7160: e.q.max_rows QUERY-level option shown even if not set
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kunal Khatua authored and sohami committed Apr 10, 2019
1 parent 455cee7 commit bf07127
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
Expand Up @@ -216,27 +216,31 @@ private static PhysicalPlan getQueryPlan(QueryContext context, String sql, Point
return handler.getPlan(sqlNode);
}

private static boolean isAutoLimitShouldBeApplied(QueryContext context, SqlNode sqlNode) {
return (context.getOptions().getOption(ExecConstants.QUERY_MAX_ROWS).num_val.intValue() > 0) && sqlNode.getKind().belongsTo(SqlKind.QUERY)
&& (sqlNode.getKind() != SqlKind.ORDER_BY || isAutoLimitLessThanOrderByFetch((SqlOrderBy) sqlNode, context));
private static boolean isAutoLimitShouldBeApplied(SqlNode sqlNode, int queryMaxRows) {
return (queryMaxRows > 0) && sqlNode.getKind().belongsTo(SqlKind.QUERY)
&& (sqlNode.getKind() != SqlKind.ORDER_BY || isAutoLimitLessThanOrderByFetch((SqlOrderBy) sqlNode, queryMaxRows));
}

private static SqlNode checkAndApplyAutoLimit(SqlConverter parser, QueryContext context, String sql) {
SqlNode sqlNode = parser.parse(sql);
if (isAutoLimitShouldBeApplied(context, sqlNode)) {
sqlNode = wrapWithAutoLimit(sqlNode, context);
int queryMaxRows = context.getOptions().getOption(ExecConstants.QUERY_MAX_ROWS).num_val.intValue();
if (isAutoLimitShouldBeApplied(sqlNode, queryMaxRows)) {
sqlNode = wrapWithAutoLimit(sqlNode, queryMaxRows);
} else {
context.getOptions().setLocalOption(ExecConstants.QUERY_MAX_ROWS, 0);
//Force setting to zero IFF autoLimit was intended to be set originally but is inapplicable
if (queryMaxRows > 0) {
context.getOptions().setLocalOption(ExecConstants.QUERY_MAX_ROWS, 0);
}
}
return sqlNode;
}

private static boolean isAutoLimitLessThanOrderByFetch(SqlOrderBy orderBy, QueryContext context) {
return orderBy.fetch == null || Integer.parseInt(orderBy.fetch.toString()) > context.getOptions().getOption(ExecConstants.QUERY_MAX_ROWS).num_val.intValue();
private static boolean isAutoLimitLessThanOrderByFetch(SqlOrderBy orderBy, int queryMaxRows) {
return orderBy.fetch == null || Integer.parseInt(orderBy.fetch.toString()) > queryMaxRows;
}

private static SqlNode wrapWithAutoLimit(SqlNode sqlNode, QueryContext context) {
SqlNumericLiteral autoLimitLiteral = SqlLiteral.createExactNumeric(String.valueOf(context.getOptions().getOption(ExecConstants.QUERY_MAX_ROWS).num_val.intValue()), SqlParserPos.ZERO);
private static SqlNode wrapWithAutoLimit(SqlNode sqlNode, int queryMaxRows) {
SqlNumericLiteral autoLimitLiteral = SqlLiteral.createExactNumeric(String.valueOf(queryMaxRows), SqlParserPos.ZERO);
if (sqlNode.getKind() == SqlKind.ORDER_BY) {
SqlOrderBy orderBy = (SqlOrderBy) sqlNode;
return new SqlOrderBy(orderBy.getParserPosition(), orderBy.query, orderBy.orderList, orderBy.offset, autoLimitLiteral);
Expand Down
Expand Up @@ -22,6 +22,7 @@

import org.apache.drill.common.exceptions.UserException;
import org.apache.drill.common.exceptions.UserRemoteException;
import org.apache.drill.exec.ExecConstants;
import org.apache.drill.exec.proto.UserBitShared.QueryId;
import org.apache.drill.exec.proto.UserBitShared.QueryResult.QueryState;
import org.apache.drill.exec.proto.UserBitShared.QueryType;
Expand Down Expand Up @@ -77,7 +78,14 @@ public QueryResult run(final WorkManager workManager, final WebUserConnection we
.setAutolimitRowcount(autoLimitRowCount)
.build();

webUserConnection.setAutoLimitRowCount(autoLimitRowCount);
int defaultMaxRows = webUserConnection.getSession().getOptions().getOption(ExecConstants.QUERY_MAX_ROWS).num_val.intValue();
int maxRows;
if (autoLimitRowCount > 0 && defaultMaxRows > 0) {
maxRows = Math.min(autoLimitRowCount, defaultMaxRows);
} else {
maxRows = Math.max(autoLimitRowCount, defaultMaxRows);
}
webUserConnection.setAutoLimitRowCount(maxRows);

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

0 comments on commit bf07127

Please sign in to comment.