Skip to content
Permalink
Browse files
[NO ISSUE][COMP] No subquery coercion in EXISTS clause
- user model changes: no
- storage format changes: no
- interface changes: no

Details:
- In SQL-compat mode do not perform subquery coecrion
  if the subquery is in EXISTS or NOT EXISTS clause
- Add testscases

Change-Id: If4ae567fc449e5fa6bb040555325f429e8953256
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/14145
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Dmitry Lychagin <dmitry.lychagin@couchbase.com>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
  • Loading branch information
Dmitry Lychagin committed Nov 22, 2021
1 parent 7cdb65d commit 6dc0f84985fde15017d14b316db2ce2c590c3e15
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 2 deletions.
@@ -20,7 +20,7 @@
/*
* SQL-compat mode.
* Test no subquery coercion if the subquery is in the IN clause
* of a quantified expression
* of a quantified expression (SOME)
*/

// requesttype=application/json
@@ -0,0 +1,32 @@
/*
* 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.
*/

/*
* SQL-compat mode.
* Test no subquery coercion if the subquery is in the IN clause
* of a quantified expression (EVERY)
*/

// requesttype=application/json
// param sql-compat:json=true

select r2.r1
from (select r1 from range(1, 3) r1) r2
where every r3 in (select r4 from range(r2.r1, r2.r1) r4) satisfies r3.r4 = r2.r1
order by r2.r1;
@@ -0,0 +1,31 @@
/*
* 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.
*/

/*
* SQL-compat mode.
* Test no subquery coercion if the subquery is in the EXISTS clause
*/

// requesttype=application/json
// param sql-compat:json=true

select r2.r1, (select value { "a": r4, "b": r4 + 1 } from range(2, r2.r1) r4) x
from (select r1 from range(1, 3) r1) r2
where exists (select r4 a, r4 + 1 b from range(2, r2.r1) r4) -- 0, 1, or 2 tuples
order by r2.r1;
@@ -0,0 +1,31 @@
/*
* 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.
*/

/*
* SQL-compat mode.
* Test no subquery coercion if the subquery is in the NOT EXISTS clause
*/

// requesttype=application/json
// param sql-compat:json=true

select r2.r1, (select value { "a": r4, "b": r4 + 1 } from range(2, r2.r1) r4) x
from (select r1 from range(1, 3) r1) r2
where not exists (select r4 a, r4 + 1 b from range(2, r2.r1) r4) -- 0, 1, or 2 tuples
order by r2.r1;
@@ -0,0 +1,3 @@
{ "r1": 1 }
{ "r1": 2 }
{ "r1": 3 }
@@ -0,0 +1,2 @@
{ "r1": 2, "x": [ { "a": 2, "b": 3 } ] }
{ "r1": 3, "x": [ { "a": 2, "b": 3 }, { "a": 3, "b": 4 } ] }
@@ -0,0 +1 @@
{ "r1": 1, "x": [ ] }
@@ -10222,6 +10222,11 @@
<output-dir compare="Text">subquery_coercion_07_from</output-dir>
</compilation-unit>
</test-case>
<test-case FilePath="sql-compat">
<compilation-unit name="subquery_coercion_08_misc">
<output-dir compare="Text">subquery_coercion_08_misc</output-dir>
</compilation-unit>
</test-case>
</test-group>
<test-group name="statement-params">
<test-case FilePath="statement-params">
@@ -36,6 +36,7 @@
import org.apache.asterix.lang.common.expression.LiteralExpr;
import org.apache.asterix.lang.common.expression.OperatorExpr;
import org.apache.asterix.lang.common.expression.QuantifiedExpression;
import org.apache.asterix.lang.common.expression.UnaryExpr;
import org.apache.asterix.lang.common.expression.VariableExpr;
import org.apache.asterix.lang.common.literal.IntegerLiteral;
import org.apache.asterix.lang.common.rewrites.LangRewritingContext;
@@ -72,7 +73,8 @@
* <ol>
* <li> FROM/JOIN/UNNEST (subquery) --> no subquery coercion
* <li> WITH/LET v = (subquery) --> no subquery coercion
* <li> SOME v IN (subquery) --> no subquery coercion
* <li> SOME/EVERY v IN (subquery) --> no subquery coercion
* <li> [NOT] EXISTS (subquery) --> no subquery coercion
* <li> WHERE (x,y) = (subquery) --> coerce the subquery into a single array
* <li> WHERE x IN (subquery) --> coerce the subquery into a collection of values
* <li> WHERE (x,y) IN (subquery) --> coerce the subquery into a collection of arrays
@@ -184,6 +186,20 @@ public Expression visit(OperatorExpr opExpr, ILangExpression arg) throws Compila
return super.visit(opExpr, arg);
}

@Override
public Expression visit(UnaryExpr u, ILangExpression arg) throws CompilationException {
switch (u.getExprType()) {
case EXISTS:
case NOT_EXISTS:
Expression expr = u.getExpr();
if (expr.getKind() == Expression.Kind.SELECT_EXPRESSION) {
annotateSubqueryNoCoercion((SelectExpression) expr);
}
break;
}
return super.visit(u, arg);
}

@Override
public Expression visit(SelectExpression selectExpr, ILangExpression arg) throws CompilationException {
SqlCompatSelectExpressionCoercionAnnotation selectExprAnn = null;

0 comments on commit 6dc0f84

Please sign in to comment.