| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.expression; |
| 7 |
|
| 8 |
import java.util.Arrays; |
| 9 |
|
| 10 |
import org.h2.engine.SessionLocal; |
| 11 |
import org.h2.message.DbException; |
| 12 |
import org.h2.table.ColumnResolver; |
| 13 |
import org.h2.table.TableFilter; |
| 14 |
import org.h2.value.TypeInfo; |
| 15 |
|
| 16 |
/** |
| 17 |
* Operation with many arguments. |
| 18 |
*/ |
| 19 |
public abstract class OperationN extends Expression implements ExpressionWithVariableParameters { |
| 20 |
|
| 21 |
/** |
| 22 |
* The array of arguments. |
| 23 |
*/ |
| 24 |
protected Expression[] args; |
| 25 |
|
| 26 |
/** |
| 27 |
* The number of arguments. |
| 28 |
*/ |
| 29 |
protected int argsCount; |
| 30 |
|
| 31 |
/** |
| 32 |
* The type of the result. |
| 33 |
*/ |
| 34 |
protected TypeInfo type; |
| 35 |
|
| 36 |
protected OperationN(Expression[] args) { |
| 37 |
this.args = args; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public void addParameter(Expression param) { |
| 42 |
int capacity = args.length; |
| 43 |
if (argsCount >= capacity) { |
| 44 |
args = Arrays.copyOf(args, capacity * 2); |
| 45 |
} |
| 46 |
args[argsCount++] = param; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void doneWithParameters() throws DbException { |
| 51 |
if (args.length != argsCount) { |
| 52 |
args = Arrays.copyOf(args, argsCount); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public TypeInfo getType() { |
| 58 |
return type; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void mapColumns(ColumnResolver resolver, int level, int state) { |
| 63 |
for (Expression e : args) { |
| 64 |
e.mapColumns(resolver, level, state); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Optimizes arguments. |
| 70 |
* |
| 71 |
* @param session |
| 72 |
* the session |
| 73 |
* @param allConst |
| 74 |
* whether operation is deterministic |
| 75 |
* @return whether operation is deterministic and all arguments are |
| 76 |
* constants |
| 77 |
*/ |
| 78 |
protected boolean optimizeArguments(SessionLocal session, boolean allConst) { |
| 79 |
for (int i = 0, l = args.length; i < l; i++) { |
| 80 |
Expression e = args[i].optimize(session); |
| 81 |
args[i] = e; |
| 82 |
if (allConst && !e.isConstant()) { |
| 83 |
allConst = false; |
| 84 |
} |
| 85 |
} |
| 86 |
return allConst; |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void setEvaluatable(TableFilter tableFilter, boolean value) { |
| 91 |
for (Expression e : args) { |
| 92 |
e.setEvaluatable(tableFilter, value); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void updateAggregate(SessionLocal session, int stage) { |
| 98 |
for (Expression e : args) { |
| 99 |
e.updateAggregate(session, stage); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 105 |
for (Expression e : args) { |
| 106 |
if (!e.isEverything(visitor)) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
} |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public int getCost() { |
| 115 |
int cost = args.length + 1; |
| 116 |
for (Expression e : args) { |
| 117 |
cost += e.getCost(); |
| 118 |
} |
| 119 |
return cost; |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public int getSubexpressionCount() { |
| 124 |
return args.length; |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public Expression getSubexpression(int index) { |
| 129 |
return args[index]; |
| 130 |
} |
| 131 |
|
| 132 |
} |