Search in sources :

Example 1 with Transient

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);
}
Also used : ColumnType(io.questdb.cairo.ColumnType) ObjList(io.questdb.std.ObjList) Transient(io.questdb.std.Transient) Function(io.questdb.cairo.sql.Function) SqlException(io.questdb.griffin.SqlException) IntList(io.questdb.std.IntList) CairoConfiguration(io.questdb.cairo.CairoConfiguration) Constants(io.questdb.griffin.engine.functions.constants.Constants) SqlExecutionContext(io.questdb.griffin.SqlExecutionContext) FunctionFactory(io.questdb.griffin.FunctionFactory) Function(io.questdb.cairo.sql.Function) ObjList(io.questdb.std.ObjList)

Aggregations

CairoConfiguration (io.questdb.cairo.CairoConfiguration)1 ColumnType (io.questdb.cairo.ColumnType)1 Function (io.questdb.cairo.sql.Function)1 FunctionFactory (io.questdb.griffin.FunctionFactory)1 SqlException (io.questdb.griffin.SqlException)1 SqlExecutionContext (io.questdb.griffin.SqlExecutionContext)1 Constants (io.questdb.griffin.engine.functions.constants.Constants)1 IntList (io.questdb.std.IntList)1 ObjList (io.questdb.std.ObjList)1 Transient (io.questdb.std.Transient)1