[ASTERIXDB-2830][IDX] Supporting nested universal quantification optimization.
- user mode changes: no
- storage format changes: no
- interface changes: no
We now support the acceleration of universal quantification within
universal. This also improves the recognition for applicable optimizable
functions for use with array indexes.
Change-Id: I457e8c7d88b0b1a17470149cacf1839dd417bb49
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/12106
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Dmitry Lychagin <dmitry.lychagin@couchbase.com>
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceSelectAccessMethodRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceSelectAccessMethodRule.java
index c64e517..0d9d866 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceSelectAccessMethodRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceSelectAccessMethodRule.java
@@ -129,10 +129,13 @@
protected static Map<FunctionIdentifier, List<IAccessMethod>> accessMethods = new HashMap<>();
static {
- registerAccessMethod(ArrayBTreeAccessMethod.INSTANCE, accessMethods);
registerAccessMethod(BTreeAccessMethod.INSTANCE, accessMethods);
registerAccessMethod(RTreeAccessMethod.INSTANCE, accessMethods);
registerAccessMethod(InvertedIndexAccessMethod.INSTANCE, accessMethods);
+ registerAccessMethod(ArrayBTreeAccessMethod.INSTANCE, accessMethods);
+ for (Pair<FunctionIdentifier, Boolean> f : ArrayBTreeAccessMethod.INSTANCE.getOptimizableFunctions()) {
+ SelectInSubplanBranchCreator.addOptimizableFunction(f.first);
+ }
}
/**
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/util/SelectInSubplanBranchCreator.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/util/SelectInSubplanBranchCreator.java
index 6efef16..c03870f 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/util/SelectInSubplanBranchCreator.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/util/SelectInSubplanBranchCreator.java
@@ -45,6 +45,7 @@
import org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression;
import org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression;
import org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression;
+import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
import org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator;
import org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator;
import org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator;
@@ -81,14 +82,14 @@
* SELECT_1(some variable AND array is not empty)
* SUBPLAN_1 -------------------------------|
* (parent branch input) AGGREGATE(EMPTY-STREAM)
- * SELECT_2(NOT(IF-MISSING-OR-NULL(some predicate)))
+ * SELECT_2(NOT(IF-MISSING-OR-NULL(some optimizable predicate)))
* (UNNEST/ASSIGN)*
* UNNEST(on variable)
* NESTED-TUPLE-SOURCE
* </pre>
* We return the following branch:
* <pre>
- * SELECT_2(some predicate) <--- removed the NOT(IF-MISSING-OR-NULL(...))!
+ * SELECT_2(some optimizable predicate) <--- removed the NOT(IF-MISSING-OR-NULL(...))!
* (UNNEST/ASSIGN)*
* UNNEST(on variable)
* (parent branch input)
@@ -101,12 +102,20 @@
Arrays.asList(new IAlgebricksConstantValue[] { new AsterixConstantValue(new AInt64(0)),
new AsterixConstantValue(new AInt32(0)), new AsterixConstantValue(new AInt16((short) 0)),
new AsterixConstantValue(new AInt8((byte) 0)) });
+ private final static List<FunctionIdentifier> optimizableFunctions = new ArrayList<>();
private IOptimizationContext context;
private SourceLocation sourceLocation;
private SelectOperator originalSelectRoot;
/**
+ * Add an optimizable function from an access method that can take advantage of this throwaway branch rewrite.
+ */
+ public static void addOptimizableFunction(FunctionIdentifier functionIdentifier) {
+ optimizableFunctions.add(functionIdentifier);
+ }
+
+ /**
* Create a new branch to match that of the form:
*
* <pre>
@@ -141,7 +150,7 @@
return null;
}
- return traverseSubplanBranch(subplanOperator);
+ return traverseSubplanBranch(subplanOperator, null);
}
/**
@@ -151,7 +160,8 @@
return originalSelectRoot;
}
- private SelectOperator traverseSubplanBranch(SubplanOperator subplanOperator) throws AlgebricksException {
+ private SelectOperator traverseSubplanBranch(SubplanOperator subplanOperator, ILogicalOperator parentInput)
+ throws AlgebricksException {
// We only expect one plan, and one root.
if (subplanOperator.getNestedPlans().size() > 1
|| subplanOperator.getNestedPlans().get(0).getRoots().size() > 1) {
@@ -169,12 +179,12 @@
// Try to find a SELECT that we can optimize (i.e. has a function call).
SelectOperator optimizableSelect = null;
for (Mutable<ILogicalOperator> opInput : workingSubplanRoot.getInputs()) {
- ILogicalOperator subplanOrSelect = findSubplanOrSelect(opInput.getValue());
+ ILogicalOperator subplanOrSelect = findSubplanOrOptimizableSelect(opInput.getValue());
if (subplanOrSelect == null) {
return null;
} else if (subplanOrSelect.getOperatorTag().equals(LogicalOperatorTag.SUBPLAN)) {
- optimizableSelect = traverseSubplanBranch((SubplanOperator) subplanOrSelect);
+ optimizableSelect = traverseSubplanBranch((SubplanOperator) subplanOrSelect, opInput.getValue());
} else {
optimizableSelect = (SelectOperator) subplanOrSelect;
@@ -190,8 +200,7 @@
optimizableSelect.getRetainMissing(), optimizableSelect.getMissingPlaceholderVariable());
// Ensure that this SELECT represents a predicate for an existential query, and is a query we can optimize.
- newSelectOperator = normalizeSelectCondition(workingSubplanRootAsAggregate, newSelectOperator,
- subplanOperator.getInputs().get(0).getValue());
+ newSelectOperator = normalizeSelectCondition(workingSubplanRootAsAggregate, newSelectOperator);
if (newSelectOperator == null) {
return null;
}
@@ -205,7 +214,7 @@
if (workingOriginalOperator.getInputs().isEmpty()) {
throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE,
workingSubplanRoot.getSourceLocation(),
- "NESTED-TUPLE-SOURCE expected in nested plan branch," + " but not found.");
+ "NESTED-TUPLE-SOURCE expected in nested plan branch, but not found.");
}
switch (workingOriginalOperator.getOperatorTag()) {
@@ -228,10 +237,6 @@
workingNewOperator = newAssign;
break;
- case SUBPLAN:
- // TODO (GLENN): Work on supporting nested universal quantification.
- return null;
-
case AGGREGATE:
case SELECT:
break;
@@ -243,10 +248,17 @@
workingOriginalOperator = workingOriginalOperator.getInputs().get(0).getValue();
}
+ // Sanity check: we should always be working with an UNNEST at this stage.
+ if (bottommostNewUnnest == null) {
+ throw new CompilationException(ErrorCode.COMPILATION_ERROR, workingSubplanRoot.getSourceLocation(),
+ "UNNEST expected in nested plan branch, but not found.");
+ }
+
// If we are working with universal quantification, then we must also check whether or not we have a conjunct
// that asserts that the array should also be non-empty.
if (isUniversalQuantification(workingSubplanRootAsAggregate)
- && !isArrayNonEmptyConjunctIncluded(bottommostNewUnnest, subplanOperator)) {
+ && isArrayNonEmptyConjunctMissing(bottommostNewUnnest, subplanOperator.getInputs().get(0).getValue())
+ && (parentInput == null || isArrayNonEmptyConjunctMissing(bottommostNewUnnest, parentInput))) {
return null;
}
@@ -263,49 +275,49 @@
(AggregateFunctionCallExpression) workingSubplanRoot.getExpressions().get(0).getValue();
if (aggregateFunctionCallExpression.getFunctionIdentifier().equals(BuiltinFunctions.EMPTY_STREAM)) {
return true;
+
} else if (aggregateFunctionCallExpression.getFunctionIdentifier().equals(BuiltinFunctions.NON_EMPTY_STREAM)) {
return false;
+
} else {
throw new CompilationException(ErrorCode.COMPILATION_ERROR, workingSubplanRoot.getSourceLocation(),
"Unexpected aggregate function: " + aggregateFunctionCallExpression.getFunctionIdentifier());
}
}
- private boolean isArrayNonEmptyConjunctIncluded(UnnestOperator firstUnnestInNTS, SubplanOperator subplanOperator) {
+ private boolean isArrayNonEmptyConjunctMissing(UnnestOperator firstUnnestInNTS, ILogicalOperator subplanInput)
+ throws AlgebricksException {
UnnestingFunctionCallExpression unnestFunction =
(UnnestingFunctionCallExpression) firstUnnestInNTS.getExpressionRef().getValue();
VariableReferenceExpression unnestVarExpr =
(VariableReferenceExpression) unnestFunction.getArguments().get(0).getValue();
LogicalVariable arrayVariable = unnestVarExpr.getVariableReference();
- // TODO (GLENN): The SELECT directly below the SUBPLAN is the only operator we explore. This does not cover
- // all predicates where the array may be non-empty (say, having an existential predicate located after this
- // subplan).
- if (!subplanOperator.getInputs().get(0).getValue().getOperatorTag().equals(LogicalOperatorTag.SELECT)) {
- return false;
+ if (!subplanInput.getOperatorTag().equals(LogicalOperatorTag.SELECT)) {
+ return true;
}
- SelectOperator subplanInputOperator = (SelectOperator) subplanOperator.getInputs().get(0).getValue();
- ILogicalExpression selectCondExpr = subplanInputOperator.getCondition().getValue();
+ SelectOperator subplanInputAsSelect = normalizeSelectCondition(null, (SelectOperator) subplanInput);
+ ILogicalExpression selectCondExpr = subplanInputAsSelect.getCondition().getValue();
List<Mutable<ILogicalExpression>> conjunctsFromSelect = new ArrayList<>();
if (selectCondExpr.splitIntoConjuncts(conjunctsFromSelect)) {
// We have a collection of conjuncts. Analyze each conjunct w/ a function.
- for (Mutable<ILogicalExpression> mutableConjuct : conjunctsFromSelect) {
- ILogicalExpression workingConjunct = mutableConjuct.getValue();
+ for (Mutable<ILogicalExpression> mutableConjunct : conjunctsFromSelect) {
+ ILogicalExpression workingConjunct = mutableConjunct.getValue();
if (workingConjunct.getExpressionTag().equals(LogicalExpressionTag.FUNCTION_CALL)
&& analyzeConjunctForArrayNonEmptiness(arrayVariable,
(ScalarFunctionCallExpression) workingConjunct)) {
- return true;
+ return false;
}
}
// No such conjunct found.
- return false;
+ return true;
}
if (!selectCondExpr.getExpressionTag().equals(LogicalExpressionTag.FUNCTION_CALL)) {
- return false;
+ return true;
}
- return analyzeConjunctForArrayNonEmptiness(arrayVariable, (ScalarFunctionCallExpression) selectCondExpr);
+ return !analyzeConjunctForArrayNonEmptiness(arrayVariable, (ScalarFunctionCallExpression) selectCondExpr);
}
private boolean analyzeConjunctForArrayNonEmptiness(LogicalVariable arrayVariable,
@@ -349,22 +361,18 @@
return false;
}
- private SelectOperator normalizeSelectCondition(AggregateOperator aggregateOperator, SelectOperator selectOperator,
- ILogicalOperator subplanInputOperator) throws AlgebricksException {
+ private SelectOperator normalizeSelectCondition(AggregateOperator aggregateOperator, SelectOperator selectOperator)
+ throws AlgebricksException {
// The purpose of this function is to remove the NOT(IF-MISSING-OR-NULL(...)) functions for a universal
// quantification query. The {@code ArrayBTreeAccessMethod} does not recognize the former as optimizable
// functions, so we remove them here. This SELECT will never make it to the final query plan (after the
// {@code IntroduceSelectAccessMethodRule}), which allows us to get away with this logically incorrect branch.
- if (!isUniversalQuantification(aggregateOperator)) {
+ if (aggregateOperator != null && !isUniversalQuantification(aggregateOperator)) {
// We are working with an existential quantification query. Do not modify the SELECT.
return selectOperator;
} else {
// We are working with a universal quantification query.
- if (!subplanInputOperator.getOperatorTag().equals(LogicalOperatorTag.SELECT)) {
- return null;
- }
-
ScalarFunctionCallExpression notFunction =
(ScalarFunctionCallExpression) selectOperator.getCondition().getValue();
if (!notFunction.getFunctionIdentifier().equals(BuiltinFunctions.NOT)) {
@@ -385,28 +393,36 @@
}
}
- private ILogicalOperator findSubplanOrSelect(ILogicalOperator operator) {
- // We are trying to find a SELECT operator with a function call that is not "NOT(IF-MISSING-OR-NULL(...))".
+ private ILogicalOperator findSubplanOrOptimizableSelect(ILogicalOperator operator) {
+ // We are trying to find a SELECT operator with an optimizable function call.
if (operator.getOperatorTag().equals(LogicalOperatorTag.SELECT)) {
SelectOperator selectOperator = (SelectOperator) operator;
ILogicalExpression selectCondExpr = selectOperator.getCondition().getValue();
if (selectCondExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
- // Follow the chain of NOT(IF-MISSING-OR-NULL(...)) to see if we have a variable at the end.
- ScalarFunctionCallExpression notFunction =
- (ScalarFunctionCallExpression) selectOperator.getCondition().getValue();
- if (notFunction.getFunctionIdentifier().equals(BuiltinFunctions.NOT)) {
- ScalarFunctionCallExpression ifMissingOrNullFunction =
- (ScalarFunctionCallExpression) notFunction.getArguments().get(0).getValue();
- if (ifMissingOrNullFunction.getFunctionIdentifier().equals(BuiltinFunctions.IF_MISSING_OR_NULL)) {
- ILogicalExpression finalExpr = ifMissingOrNullFunction.getArguments().get(0).getValue();
- if (finalExpr.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
- return selectOperator;
- }
+ // We have a NOT function call. Determine if this follows the NOT(IF-MISSING-OR-NULL(...)) pattern.
+ ScalarFunctionCallExpression notExpr = (ScalarFunctionCallExpression) selectCondExpr;
+ if (notExpr.getFunctionIdentifier().equals(BuiltinFunctions.NOT)) {
+
+ // This does not follow the NOT(IF-MISSING-OR-NULL(...)) pattern, but NOT is an optimizable
+ // function call. Return this.
+ ILogicalExpression notCondExpr = notExpr.getArguments().get(0).getValue();
+ if (!notCondExpr.getExpressionTag().equals(LogicalExpressionTag.FUNCTION_CALL)
+ && optimizableFunctions.contains(BuiltinFunctions.NOT)) {
+ return selectOperator;
}
- } else {
+ // Inside the NOT(IF-MISSING-OR-NULL(...)) is an optimizable function. Return this.
+ ScalarFunctionCallExpression ifMissingOrNullExpr = (ScalarFunctionCallExpression) notCondExpr;
+ ILogicalExpression finalExpr = ifMissingOrNullExpr.getArguments().get(0).getValue();
+ if (doesExpressionContainOptimizableFunction(finalExpr)) {
+ return selectOperator;
+ }
+
+ } else if (doesExpressionContainOptimizableFunction(selectCondExpr)) {
+ // We have an optimizable function. Return this.
return selectOperator;
+
}
}
} else if (operator.getOperatorTag().equals(LogicalOperatorTag.SUBPLAN)) {
@@ -417,8 +433,56 @@
// No matching operator found. Recurse on current operator input.
if (operator.getInputs().isEmpty()) {
return null;
+
} else {
- return findSubplanOrSelect(operator.getInputs().get(0).getValue());
+ return findSubplanOrOptimizableSelect(operator.getInputs().get(0).getValue());
}
}
+
+ private boolean doesExpressionContainOptimizableFunction(ILogicalExpression inputExpr) {
+ if (!inputExpr.getExpressionTag().equals(LogicalExpressionTag.FUNCTION_CALL)) {
+ return false;
+ }
+
+ // Check if the input expression itself is an optimizable function.
+ ScalarFunctionCallExpression inputExprAsFunc = (ScalarFunctionCallExpression) inputExpr;
+ if (isFunctionOptimizable(inputExprAsFunc)) {
+ return true;
+ }
+
+ // We have a collection of conjuncts. Return true if any of these conjuncts are optimizable.
+ List<Mutable<ILogicalExpression>> conjuncts = new ArrayList<>();
+ if (inputExprAsFunc.splitIntoConjuncts(conjuncts)) {
+ for (Mutable<ILogicalExpression> mutableConjunct : conjuncts) {
+ ILogicalExpression workingConjunct = mutableConjunct.getValue();
+ if (workingConjunct.getExpressionTag().equals(LogicalExpressionTag.FUNCTION_CALL)
+ && (isFunctionOptimizable((ScalarFunctionCallExpression) workingConjunct))) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ private boolean isFunctionOptimizable(ScalarFunctionCallExpression inputExpr) {
+ if (inputExpr.getFunctionIdentifier().equals(BuiltinFunctions.GT)) {
+ // Avoid the GT(LEN(array-field), 0) function.
+ ILogicalExpression gtExpr = inputExpr.getArguments().get(0).getValue();
+ return ((!gtExpr.getExpressionTag().equals(LogicalExpressionTag.FUNCTION_CALL))
+ || !((ScalarFunctionCallExpression) gtExpr).getFunctionIdentifier().equals(BuiltinFunctions.LEN))
+ && optimizableFunctions.contains(BuiltinFunctions.GT);
+
+ } else if (inputExpr.getFunctionIdentifier().equals(BuiltinFunctions.LT)) {
+ // Avoid the LT(0, LEN(array-field)) function.
+ ILogicalExpression ltExpr = inputExpr.getArguments().get(1).getValue();
+ return ((!ltExpr.getExpressionTag().equals(LogicalExpressionTag.FUNCTION_CALL))
+ || !((ScalarFunctionCallExpression) ltExpr).getFunctionIdentifier().equals(BuiltinFunctions.LEN))
+ && optimizableFunctions.contains(BuiltinFunctions.LT);
+
+ }
+
+ // Otherwise, check if the function itself is optimizable.
+ return (optimizableFunctions.contains(inputExpr.getFunctionIdentifier()));
+ }
}
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query3.sqlpp b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query3.sqlpp
index 8b3fd61..b4ffe06 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query3.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query3.sqlpp
@@ -34,7 +34,6 @@
CREATE DATASET YelpCheckin(CheckinType) PRIMARY KEY checkin_id AUTOGENERATED;
CREATE INDEX IdxYelpCheckinDates ON YelpCheckin (UNNEST checkin_times UNNEST dates);
-/* TODO (GLENN) Support checking for nested length clauses (i.e. EVERY, then EVERY). */
SELECT C.business_id
FROM YelpCheckin C
WHERE LEN(C.checkin_times) > 0 AND
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query4.sqlpp b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query4.sqlpp
new file mode 100644
index 0000000..1cadb11
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query4.sqlpp
@@ -0,0 +1,43 @@
+/*
+ * 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.
+ */
+SET `compiler.arrayindex` "true";
+
+DROP DATAVERSE TestYelp IF EXISTS;
+CREATE DATAVERSE TestYelp;
+USE TestYelp;
+
+CREATE TYPE CheckinType AS {
+ checkin_id: uuid,
+ business_id: string,
+ checkin_times: [{
+ dates: [string],
+ times: [string]
+ }]
+};
+
+CREATE DATASET YelpCheckin(CheckinType) PRIMARY KEY checkin_id AUTOGENERATED;
+CREATE INDEX IdxYelpCheckinDates ON YelpCheckin (UNNEST checkin_times UNNEST dates);
+
+FROM YelpCheckin C
+WHERE LEN(C.checkin_times) > 0 AND
+ ( SOME CT IN C.checkin_times
+ SATISFIES ( LEN(CT.dates) > 0 AND
+ ( EVERY D IN CT.dates
+ SATISFIES D > "2019-06-07" ) ) )
+SELECT C.business_id;
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query5.sqlpp b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query5.sqlpp
new file mode 100644
index 0000000..b27e7b0
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query5.sqlpp
@@ -0,0 +1,43 @@
+/*
+ * 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.
+ */
+SET `compiler.arrayindex` "true";
+
+DROP DATAVERSE TestYelp IF EXISTS;
+CREATE DATAVERSE TestYelp;
+USE TestYelp;
+
+CREATE TYPE CheckinType AS {
+ checkin_id: uuid,
+ business_id: string,
+ checkin_times: [{
+ dates: [string],
+ times: [string]
+ }]
+};
+
+CREATE DATASET YelpCheckin(CheckinType) PRIMARY KEY checkin_id AUTOGENERATED;
+CREATE INDEX IdxYelpCheckinDates ON YelpCheckin (UNNEST checkin_times UNNEST dates);
+
+FROM YelpCheckin C
+WHERE LEN(C.checkin_times) > 0 AND
+ ( EVERY CT IN C.checkin_times
+ SATISFIES ( LEN(CT.dates) > 0 AND
+ ( EVERY D IN CT.dates
+ SATISFIES D > "2019-06-07" ) ) )
+SELECT C.business_id;
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query6.sqlpp b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query6.sqlpp
new file mode 100644
index 0000000..1426b3b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/array-index/select-quantified-queries/use-case-4/query6.sqlpp
@@ -0,0 +1,43 @@
+/*
+ * 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.
+ */
+SET `compiler.arrayindex` "true";
+
+DROP DATAVERSE TestYelp IF EXISTS;
+CREATE DATAVERSE TestYelp;
+USE TestYelp;
+
+CREATE TYPE CheckinType AS {
+ checkin_id: uuid,
+ business_id: string,
+ checkin_times: [{
+ dates: [string],
+ times: [string]
+ }]
+};
+
+CREATE DATASET YelpCheckin(CheckinType) PRIMARY KEY checkin_id AUTOGENERATED;
+CREATE INDEX IdxYelpCheckinDates ON YelpCheckin (UNNEST checkin_times UNNEST dates);
+
+// This should NOT be optimized, we are missing the non-emptiness conjunct.
+FROM YelpCheckin C
+WHERE ( EVERY CT IN C.checkin_times
+ SATISFIES ( LEN(CT.dates) > 0 AND
+ ( EVERY D IN CT.dates
+ SATISFIES D > "2019-06-07" ) ) )
+SELECT C.business_id;
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/array-index/select-quantified-queries/use-case-4/query4.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/array-index/select-quantified-queries/use-case-4/query4.plan
new file mode 100644
index 0000000..2f44d43
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/array-index/select-quantified-queries/use-case-4/query4.plan
@@ -0,0 +1,40 @@
+-- DISTRIBUTE_RESULT |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ASSIGN |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- STREAM_SELECT |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- SUBPLAN |PARTITIONED|
+ {
+ -- AGGREGATE |LOCAL|
+ -- STREAM_SELECT |LOCAL|
+ -- SUBPLAN |LOCAL|
+ {
+ -- AGGREGATE |LOCAL|
+ -- STREAM_SELECT |LOCAL|
+ -- UNNEST |LOCAL|
+ -- NESTED_TUPLE_SOURCE |LOCAL|
+ }
+ -- STREAM_SELECT |LOCAL|
+ -- ASSIGN |LOCAL|
+ -- UNNEST |LOCAL|
+ -- NESTED_TUPLE_SOURCE |LOCAL|
+ }
+ -- STREAM_SELECT |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ASSIGN |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- BTREE_SEARCH (TestYelp.YelpCheckin.YelpCheckin) |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- PRE_SORTED_DISTINCT_BY |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- STABLE_SORT [$$58(ASC)] |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- BTREE_SEARCH (TestYelp.YelpCheckin.IdxYelpCheckinDates) |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- ASSIGN |PARTITIONED|
+ -- EMPTY_TUPLE_SOURCE |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/array-index/select-quantified-queries/use-case-4/query5.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/array-index/select-quantified-queries/use-case-4/query5.plan
new file mode 100644
index 0000000..0f3a84c
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/array-index/select-quantified-queries/use-case-4/query5.plan
@@ -0,0 +1,39 @@
+-- DISTRIBUTE_RESULT |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ASSIGN |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- STREAM_SELECT |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- SUBPLAN |PARTITIONED|
+ {
+ -- AGGREGATE |LOCAL|
+ -- STREAM_SELECT |LOCAL|
+ -- SUBPLAN |LOCAL|
+ {
+ -- AGGREGATE |LOCAL|
+ -- STREAM_SELECT |LOCAL|
+ -- UNNEST |LOCAL|
+ -- NESTED_TUPLE_SOURCE |LOCAL|
+ }
+ -- ASSIGN |LOCAL|
+ -- UNNEST |LOCAL|
+ -- NESTED_TUPLE_SOURCE |LOCAL|
+ }
+ -- STREAM_SELECT |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ASSIGN |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- BTREE_SEARCH (TestYelp.YelpCheckin.YelpCheckin) |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- PRE_SORTED_DISTINCT_BY |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- STABLE_SORT [$$58(ASC)] |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- BTREE_SEARCH (TestYelp.YelpCheckin.IdxYelpCheckinDates) |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- ASSIGN |PARTITIONED|
+ -- EMPTY_TUPLE_SOURCE |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/array-index/select-quantified-queries/use-case-4/query6.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/array-index/select-quantified-queries/use-case-4/query6.plan
new file mode 100644
index 0000000..3f101b7
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/array-index/select-quantified-queries/use-case-4/query6.plan
@@ -0,0 +1,29 @@
+-- DISTRIBUTE_RESULT |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ASSIGN |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- STREAM_SELECT |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- SUBPLAN |PARTITIONED|
+ {
+ -- AGGREGATE |LOCAL|
+ -- STREAM_SELECT |LOCAL|
+ -- SUBPLAN |LOCAL|
+ {
+ -- AGGREGATE |LOCAL|
+ -- STREAM_SELECT |LOCAL|
+ -- UNNEST |LOCAL|
+ -- NESTED_TUPLE_SOURCE |LOCAL|
+ }
+ -- ASSIGN |LOCAL|
+ -- UNNEST |LOCAL|
+ -- NESTED_TUPLE_SOURCE |LOCAL|
+ }
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ASSIGN |PARTITIONED|
+ -- STREAM_PROJECT |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- DATASOURCE_SCAN (TestYelp.YelpCheckin) |PARTITIONED|
+ -- ONE_TO_ONE_EXCHANGE |PARTITIONED|
+ -- EMPTY_TUPLE_SOURCE |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-1/use-case-1.6.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-1/use-case-1.6.query.sqlpp
index e64a93c..5ccbf5d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-1/use-case-1.6.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-1/use-case-1.6.query.sqlpp
@@ -20,7 +20,8 @@
USE TestYelp;
-SELECT COUNT(*)
-FROM YelpCheckin C
-WHERE EVERY D IN C.dates
-SATISFIES D > "2016" AND D < "2017";
+FROM YelpCheckin C
+WHERE LEN(C.dates) > 0 AND
+ ( EVERY D IN C.dates
+ SATISFIES D > "2016" AND D < "2017" )
+SELECT COUNT(*);
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-2/use-case-2.6.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-2/use-case-2.6.query.sqlpp
index c9652f1..86e3d9f 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-2/use-case-2.6.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-2/use-case-2.6.query.sqlpp
@@ -20,7 +20,8 @@
USE TestYelp;
-SELECT COUNT(*)
-FROM YelpCheckin C
-WHERE EVERY D IN C.checkin_times.dates
-SATISFIES D > "2016" AND D < "2017";
+FROM YelpCheckin C
+WHERE LEN(C.checkin_times.dates) > 0 AND
+ ( EVERY D IN C.checkin_times.dates
+ SATISFIES D > "2016" AND D < "2017" )
+SELECT COUNT(*);
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-3/use-case-3.6.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-3/use-case-3.6.query.sqlpp
index 6a73591..f55fa3c 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-3/use-case-3.6.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-3/use-case-3.6.query.sqlpp
@@ -20,7 +20,8 @@
USE TestYelp;
-SELECT C.business_id
-FROM YelpCheckin C
-WHERE EVERY D IN C.checkin_times
-SATISFIES D.date BETWEEN "2016" AND "2017";
+FROM YelpCheckin C
+WHERE LEN(C.checkin_times) > 0 AND
+ ( EVERY D IN C.checkin_times
+ SATISFIES D.date > "2016" AND D.date < "2017" )
+SELECT C.business_id;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-4/use-case-4.6.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-4/use-case-4.6.query.sqlpp
index 490dbdd..c4bb9a1 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-4/use-case-4.6.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-4/use-case-4.6.query.sqlpp
@@ -20,10 +20,9 @@
USE TestYelp;
-SELECT C.business_id
-FROM YelpCheckin C
-WHERE EVERY CT IN C.checkin_times
-SATISFIES (
- SOME D IN CT.dates
- SATISFIES "2019-06-07" = D
-);
+FROM YelpCheckin C
+WHERE LEN(C.checkin_times) > 0 AND
+ ( EVERY CT IN C.checkin_times
+ SATISFIES ( SOME D IN CT.dates
+ SATISFIES "2019-06-07" = D ) )
+SELECT C.business_id;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-4/use-case-4.7.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-4/use-case-4.7.query.sqlpp
index 4032e84..da9da19 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-4/use-case-4.7.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/use-case-4/use-case-4.7.query.sqlpp
@@ -20,10 +20,10 @@
USE TestYelp;
-SELECT C.business_id
-FROM YelpCheckin C
-WHERE EVERY CT IN C.checkin_times
-SATISFIES (
- EVERY D IN CT.dates
- SATISFIES D > "2019-06-05"
-);
+FROM YelpCheckin C
+WHERE LEN(C.checkin_times) > 0 AND
+ ( EVERY CT IN C.checkin_times
+ SATISFIES ( LEN(CT.dates) > 0 AND
+ ( EVERY D IN CT.dates
+ SATISFIES D > "2019-06-05" ) ) )
+SELECT C.business_id;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/with-composite-pk/with-composite-pk.6.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/with-composite-pk/with-composite-pk.6.query.sqlpp
index e64a93c..803aa44 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/with-composite-pk/with-composite-pk.6.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/with-composite-pk/with-composite-pk.6.query.sqlpp
@@ -20,7 +20,8 @@
USE TestYelp;
-SELECT COUNT(*)
-FROM YelpCheckin C
-WHERE EVERY D IN C.dates
-SATISFIES D > "2016" AND D < "2017";
+FROM YelpCheckin C
+WHERE LEN(C.dates) > 0 AND
+ ( EVERY D IN C.dates
+ SATISFIES D > "2016" AND D < "2017" )
+SELECT COUNT(*);
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/with-open-index/with-open-index.6.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/with-open-index/with-open-index.6.query.sqlpp
index e64a93c..5ccbf5d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/with-open-index/with-open-index.6.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/array-index/select-quantified-queries/with-open-index/with-open-index.6.query.sqlpp
@@ -20,7 +20,8 @@
USE TestYelp;
-SELECT COUNT(*)
-FROM YelpCheckin C
-WHERE EVERY D IN C.dates
-SATISFIES D > "2016" AND D < "2017";
+FROM YelpCheckin C
+WHERE LEN(C.dates) > 0 AND
+ ( EVERY D IN C.dates
+ SATISFIES D > "2016" AND D < "2017" )
+SELECT COUNT(*);
\ No newline at end of file