Search in sources :

Example 16 with ObjList

use of io.questdb.std.ObjList 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

ObjList (io.questdb.std.ObjList)16 Test (org.junit.Test)14 Function (io.questdb.cairo.sql.Function)12 FunctionFactory (io.questdb.griffin.FunctionFactory)11 IntList (io.questdb.std.IntList)11 AbstractFunctionFactoryTest (io.questdb.griffin.engine.AbstractFunctionFactoryTest)10 DoubleConstant (io.questdb.griffin.engine.functions.constants.DoubleConstant)7 SqlException (io.questdb.griffin.SqlException)3 DateConstant (io.questdb.griffin.engine.functions.constants.DateConstant)3 FloatConstant (io.questdb.griffin.engine.functions.constants.FloatConstant)3 TimestampConstant (io.questdb.griffin.engine.functions.constants.TimestampConstant)3 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)2 ExpressionNode (io.questdb.griffin.model.ExpressionNode)2 QueryColumn (io.questdb.griffin.model.QueryColumn)2 Rnd (io.questdb.std.Rnd)2 CairoConfiguration (io.questdb.cairo.CairoConfiguration)1 ColumnType (io.questdb.cairo.ColumnType)1 Record (io.questdb.cairo.sql.Record)1 VirtualRecord (io.questdb.cairo.sql.VirtualRecord)1 SqlExecutionContext (io.questdb.griffin.SqlExecutionContext)1