提交 8304d409 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Optimize IN(UNNEST(array)) with constant array

上级 ca80a773
......@@ -132,6 +132,16 @@ public class ExpressionList extends Expression {
return expr;
}
@Override
public boolean isConstant() {
for (Expression e : list) {
if (!e.isConstant()) {
return false;
}
}
return true;
}
@Override
public int getSubexpressionCount() {
return list.length;
......
......@@ -16,6 +16,7 @@ import org.h2.expression.ValueExpression;
import org.h2.expression.function.Function;
import org.h2.expression.function.TableFunction;
import org.h2.index.IndexCondition;
import org.h2.result.ResultInterface;
import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter;
import org.h2.value.Value;
......@@ -109,6 +110,19 @@ public class ConditionIn extends Condition {
}
}
}
if (tf.isConstant()) {
boolean allValuesNull = true;
ResultInterface ri = right.getValue(session).getResult();
ArrayList<Expression> list = new ArrayList<>(ri.getRowCount());
while (ri.next()) {
Value v = ri.currentRow()[0];
if (v != ValueNull.INSTANCE) {
allValuesNull = false;
}
list.add(ValueExpression.get(v));
}
return optimize2(session, constant, true, allValuesNull, list);
}
return this;
}
}
......@@ -129,11 +143,16 @@ public class ConditionIn extends Condition {
}
valueList.set(i, e);
}
return optimize2(session, constant, allValuesConstant, allValuesNull, valueList);
}
private Expression optimize2(Session session, boolean constant, boolean allValuesConstant, boolean allValuesNull,
ArrayList<Expression> values) {
if (constant && allValuesConstant) {
return ValueExpression.get(getValue(session));
}
if (size == 1) {
return new Comparison(session, Comparison.EQUAL, left, valueList.get(0)).optimize(session);
if (values.size() == 1) {
return new Comparison(session, Comparison.EQUAL, left, values.get(0)).optimize(session);
}
if (allValuesConstant && !allValuesNull) {
int leftType = left.getType();
......@@ -143,7 +162,7 @@ public class ConditionIn extends Condition {
if (leftType == Value.ENUM && !(left instanceof ExpressionColumn)) {
return this;
}
Expression expr = new ConditionInConstantSet(session, left, valueList);
Expression expr = new ConditionInConstantSet(session, left, values);
expr = expr.optimize(session);
return expr;
}
......
......@@ -146,4 +146,14 @@ public class TableFunction extends Function {
return getExpressionColumns(session, getValueForColumnList(session, null).getResult());
}
@Override
public boolean isConstant() {
for (Expression e : args) {
if (!e.isConstant()) {
return false;
}
}
return true;
}
}
......@@ -48,8 +48,8 @@ SELECT 4 IN(UNNEST(ARRAY[1, 2, 3]));
>> FALSE
SELECT X, X IN(UNNEST(ARRAY[2, 4])) FROM SYSTEM_RANGE(1, 5);
> X X IN(UNNEST((2, 4)))
> - --------------------
> X X IN(2, 4)
> - ----------
> 1 FALSE
> 2 TRUE
> 3 FALSE
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论