use of io.questdb.std.Transient in project questdb by bluestreak01.
the class CaseFunctionFactory method newInstance.
@Override
public Function newInstance(int position, @Transient ObjList<Function> args, @Transient IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) throws SqlException {
int n = args.size();
int returnType = -1;
final ObjList<Function> vars = new ObjList<>(n);
final ObjList<Function> argsToPoke = new ObjList<>(n);
Function elseBranch;
int elseBranchPosition;
if (n % 2 == 1) {
elseBranch = args.getQuick(n - 1);
elseBranchPosition = argPositions.getQuick(n - 1);
n--;
} else {
elseBranch = null;
elseBranchPosition = 0;
}
// compute return type in this loop
for (int i = 0; i < n; i += 2) {
Function bool = args.getQuick(i);
Function value = args.getQuick(i + 1);
if (!ColumnType.isBoolean(bool.getType())) {
throw SqlException.position(argPositions.getQuick(i)).put("BOOLEAN expected, found ").put(ColumnType.nameOf(bool.getType()));
}
returnType = CaseCommon.getCommonType(returnType, value.getType(), argPositions.getQuick(i + 1));
vars.add(bool);
vars.add(value);
argsToPoke.add(bool);
argsToPoke.add(value);
}
if (elseBranch != null) {
returnType = CaseCommon.getCommonType(returnType, elseBranch.getType(), elseBranchPosition);
argsToPoke.add(elseBranch);
}
// next calculate cast functions
for (int i = 1; i < n; i += 2) {
vars.setQuick(i, CaseCommon.getCastFunction(vars.getQuick(i), argPositions.getQuick(i), returnType, configuration, sqlExecutionContext));
}
if (elseBranch != null) {
elseBranch = CaseCommon.getCastFunction(elseBranch, elseBranchPosition, returnType, configuration, sqlExecutionContext);
}
final int argsLen = vars.size();
final Function elseB = elseBranch != null ? elseBranch : Constants.getNullConstant(returnType);
final CaseFunctionPicker picker = record -> {
for (int i = 0; i < argsLen; i += 2) {
if (vars.getQuick(i).getBool(record)) {
return vars.getQuick(i + 1);
}
}
return elseB;
};
return CaseCommon.getCaseFunction(position, returnType, picker, argsToPoke);
}
Aggregations