[ASTERIXDB-2527][COMP] Fix illegal state error
- user model changes: no
- storage format changes: no
- interface changes: no
Details:
- Fix Illegal State error raised by the compiler
for a query with a quantified expression
Change-Id: I53046ffd104035e36395e8a0fa4c624ccfdedf09
Reviewed-on: https://asterix-gerrit.ics.uci.edu/3253
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
Tested-by: Dmitry Lychagin <dmitry.lychagin@couchbase.com>
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/AqlExpressionToPlanTranslator.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/AqlExpressionToPlanTranslator.java
index e777c4e..aacffbf 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/AqlExpressionToPlanTranslator.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/AqlExpressionToPlanTranslator.java
@@ -175,7 +175,7 @@
}
@Override
- protected boolean expressionNeedsNoNesting(Expression expr) {
+ protected boolean expressionNeedsNoNesting(Expression expr) throws CompilationException {
boolean isFLWOGR = expr.getKind() == Kind.FLWOGR_EXPRESSION;
boolean letOnly = true;
// No nesting is needed for a FLWOR expression that only has LETs and RETURN.
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
index 61c525e..0362820 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
@@ -1667,7 +1667,7 @@
}
- protected boolean expressionNeedsNoNesting(Expression expr) {
+ protected boolean expressionNeedsNoNesting(Expression expr) throws CompilationException {
Kind k = expr.getKind();
boolean noNesting = k == Kind.LITERAL_EXPRESSION || k == Kind.LIST_CONSTRUCTOR_EXPRESSION
|| k == Kind.RECORD_CONSTRUCTOR_EXPRESSION || k == Kind.VARIABLE_EXPRESSION;
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
index 81f054a..65d59c7 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
@@ -868,7 +868,7 @@
}
@Override
- protected boolean expressionNeedsNoNesting(Expression expr) {
+ protected boolean expressionNeedsNoNesting(Expression expr) throws CompilationException {
return super.expressionNeedsNoNesting(expr) || (translateInAsOr && expr.getKind() == Kind.QUANTIFIED_EXPRESSION
&& isInOperatorWithStaticList((QuantifiedExpression) expr));
}
@@ -884,7 +884,7 @@
// "some $y in list_expr satisfies $x = $y"
// Look for such quantified expression with a constant list_expr ([e1, e2, ... eN])
// and translate it into "$x=e1 || $x=e2 || ... || $x=eN"
- private boolean isInOperatorWithStaticList(QuantifiedExpression qe) {
+ private boolean isInOperatorWithStaticList(QuantifiedExpression qe) throws CompilationException {
if (qe.getQuantifier() != QuantifiedExpression.Quantifier.SOME) {
return false;
}
@@ -906,10 +906,17 @@
if (operandExprs.size() != 2) {
return false;
}
- int varPos = operandExprs.indexOf(qp.getVarExpr());
+ VariableExpr varExpr = qp.getVarExpr();
+ int varPos = operandExprs.indexOf(varExpr);
if (varPos < 0) {
return false;
}
+ Expression operandExpr = operandExprs.get(1 - varPos);
+
+ if (SqlppRewriteUtil.getFreeVariable(operandExpr).contains(varExpr)) {
+ return false;
+ }
+
Expression inExpr = qp.getExpr();
switch (inExpr.getKind()) {
case LIST_CONSTRUCTOR_EXPRESSION:
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/in_as_or/in_as_or.10.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/in_as_or/in_as_or.10.query.sqlpp
new file mode 100644
index 0000000..80ea1e4
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/in_as_or/in_as_or.10.query.sqlpp
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * No rewriting if both eq operands use the variable (x)
+ */
+
+SELECT t
+FROM range(1, 4) t
+WHERE ANY x IN [2, 3] SATISFIES x - t + 2 = x END
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/in_as_or/in_as_or.10.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/in_as_or/in_as_or.10.adm
new file mode 100644
index 0000000..32836b8
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/in_as_or/in_as_or.10.adm
@@ -0,0 +1 @@
+{ "t": 2 }
\ No newline at end of file