Skip to content

Commit

Permalink
Extract method for analyzing parameter as row count
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiafi authored and martint committed Jul 28, 2020
1 parent e748264 commit 3cfc51b
Showing 1 changed file with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2723,28 +2723,7 @@ else if (node.getRowCount() instanceof LongLiteral) {
}
else {
checkState(node.getRowCount() instanceof Parameter, "unexpected LIMIT rowCount: " + node.getRowCount().getClass().getSimpleName());
if (analysis.isDescribe()) {
analyzeExpression(node.getRowCount(), scope);
analysis.addCoercion(node.getRowCount(), BIGINT, false);
rowCount = OptionalLong.empty();
}
else {
// validate parameter index
analyzeExpression(node.getRowCount(), scope);
Expression providedValue = analysis.getParameters().get(NodeRef.of(node.getRowCount()));
try {
rowCount = OptionalLong.of((long) ExpressionInterpreter.evaluateConstantExpression(
providedValue,
BIGINT,
metadata,
session,
accessControl,
analysis.getParameters()));
}
catch (VerifyException e) {
throw semanticException(INVALID_ARGUMENTS, node, "Non constant parameter value for LIMIT: %s", providedValue);
}
}
rowCount = analyzeParameterAsRowCount((Parameter) node.getRowCount(), scope);
}
rowCount.ifPresent(count -> {
if (count < 0) {
Expand All @@ -2757,6 +2736,32 @@ else if (node.getRowCount() instanceof LongLiteral) {
return false;
}

private OptionalLong analyzeParameterAsRowCount(Parameter parameter, Scope scope)
{
if (analysis.isDescribe()) {
analyzeExpression(parameter, scope);
analysis.addCoercion(parameter, BIGINT, false);
return OptionalLong.empty();
}
else {
// validate parameter index
analyzeExpression(parameter, scope);
Expression providedValue = analysis.getParameters().get(NodeRef.of(parameter));
try {
return OptionalLong.of((long) ExpressionInterpreter.evaluateConstantExpression(
providedValue,
BIGINT,
metadata,
session,
accessControl,
analysis.getParameters()));
}
catch (VerifyException e) {
throw semanticException(INVALID_ARGUMENTS, parameter, "Non constant parameter value for LIMIT: %s", providedValue);
}
}
}

private Scope createAndAssignScope(Node node, Optional<Scope> parentScope)
{
return createAndAssignScope(node, parentScope, emptyList());
Expand Down

0 comments on commit 3cfc51b

Please sign in to comment.