Search in sources :

Example 86 with Function

use of io.questdb.cairo.sql.Function in project questdb by bluestreak01.

the class TimestampAddFunctionFactory method newInstance.

@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) {
    Function period = args.getQuick(0);
    Function interval = args.getQuick(1);
    if (period.isConstant()) {
        char periodValue = period.getChar(null);
        if (periodValue < addFunctionsMax) {
            LongAddIntFunction func = addFunctions.getQuick(periodValue);
            if (func != null) {
                if (interval.isConstant()) {
                    if (interval.getInt(null) != Numbers.INT_NaN) {
                        return new AddLongIntVarConstFunction(args.getQuick(2), interval.getInt(null), func);
                    }
                    return TimestampConstant.NULL;
                }
                return new AddLongIntVarVarFunction(args.getQuick(2), args.getQuick(1), func);
            }
        }
        return TimestampConstant.NULL;
    }
    return new DateAddFunc(args.getQuick(2), args.getQuick(0), args.getQuick(1));
}
Also used : TimestampFunction(io.questdb.griffin.engine.functions.TimestampFunction) UnaryFunction(io.questdb.griffin.engine.functions.UnaryFunction) BinaryFunction(io.questdb.griffin.engine.functions.BinaryFunction) Function(io.questdb.cairo.sql.Function) TernaryFunction(io.questdb.griffin.engine.functions.TernaryFunction)

Example 87 with Function

use of io.questdb.cairo.sql.Function in project questdb by bluestreak01.

the class TimestampDiffFunctionFactory method newInstance.

@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) {
    final Function perionFunction = args.getQuick(0);
    if (perionFunction.isConstant()) {
        final Function start = args.getQuick(1);
        final Function end = args.getQuick(2);
        final char period = perionFunction.getChar(null);
        if (period < diffFunctionsMax) {
            final LongDiffFunction func = diffFunctions.getQuick(period);
            if (func != null) {
                if (start.isConstant() && start.getTimestamp(null) != Numbers.LONG_NaN) {
                    return new DiffVarConstFunction(args.getQuick(2), start.getLong(null), func);
                }
                if (end.isConstant() && end.getTimestamp(null) != Numbers.LONG_NaN) {
                    return new DiffVarConstFunction(args.getQuick(1), end.getLong(null), func);
                }
                return new DiffVarVarFunction(args.getQuick(1), args.getQuick(2), func);
            }
        }
        return TimestampConstant.NULL;
    }
    return new DateDiffFunc(args.getQuick(0), args.getQuick(1), args.getQuick(2));
}
Also used : LongFunction(io.questdb.griffin.engine.functions.LongFunction) UnaryFunction(io.questdb.griffin.engine.functions.UnaryFunction) BinaryFunction(io.questdb.griffin.engine.functions.BinaryFunction) Function(io.questdb.cairo.sql.Function) TernaryFunction(io.questdb.griffin.engine.functions.TernaryFunction)

Example 88 with Function

use of io.questdb.cairo.sql.Function in project questdb by bluestreak01.

the class SwitchFunctionFactory method getIfElseFunction.

private Function getIfElseFunction(ObjList<Function> args, IntList argPositions, int position, int n, Function keyFunction, int returnType, Function elseBranch) throws SqlException {
    final CaseFunctionPicker picker;
    final ObjList<Function> argsToPoke;
    if (n == 3) {
        // only one conditional branch
        boolean value = args.getQuick(1).getBool(null);
        final Function branch = args.getQuick(2);
        final Function elseB = getElseFunction(returnType, elseBranch);
        if (value) {
            picker = record -> keyFunction.getBool(record) ? branch : elseB;
        } else {
            picker = record -> keyFunction.getBool(record) ? elseB : branch;
        }
        argsToPoke = new ObjList<>();
        argsToPoke.add(keyFunction);
        argsToPoke.add(elseB);
        argsToPoke.add(branch);
    } else if (n == 5) {
        final boolean a = args.getQuick(1).getBool(null);
        final Function branchA = args.getQuick(2);
        final boolean b = args.getQuick(3).getBool(null);
        final Function branchB = args.getQuick(4);
        if (a && b || !a && !b) {
            throw SqlException.$(argPositions.getQuick(3), "duplicate branch");
        }
        if (a) {
            picker = record -> keyFunction.getBool(record) ? branchA : branchB;
        } else {
            picker = record -> keyFunction.getBool(record) ? branchB : branchA;
        }
        argsToPoke = new ObjList<>();
        argsToPoke.add(keyFunction);
        argsToPoke.add(branchA);
        argsToPoke.add(branchB);
    } else {
        throw SqlException.$(argPositions.getQuick(5), "too many branches");
    }
    return CaseCommon.getCaseFunction(position, returnType, picker, argsToPoke);
}
Also used : ColumnType(io.questdb.cairo.ColumnType) Function(io.questdb.cairo.sql.Function) SqlException(io.questdb.griffin.SqlException) CairoConfiguration(io.questdb.cairo.CairoConfiguration) Constants(io.questdb.griffin.engine.functions.constants.Constants) Record(io.questdb.cairo.sql.Record) SqlCompiler(io.questdb.griffin.SqlCompiler) SqlExecutionContext(io.questdb.griffin.SqlExecutionContext) FunctionFactory(io.questdb.griffin.FunctionFactory) io.questdb.std(io.questdb.std) Function(io.questdb.cairo.sql.Function)

Example 89 with Function

use of io.questdb.cairo.sql.Function in project questdb by bluestreak01.

the class SwitchFunctionFactory method getIntKeyedFunction.

private Function getIntKeyedFunction(ObjList<Function> args, IntList argPositions, int position, int n, Function keyFunction, int valueType, Function elseBranch, IntMethod intMethod) throws SqlException {
    final IntObjHashMap<Function> map = new IntObjHashMap<>();
    final ObjList<Function> argsToPoke = new ObjList<>();
    for (int i = 1; i < n; i += 2) {
        final Function fun = args.getQuick(i);
        final int key = intMethod.getKey(fun, null);
        final int index = map.keyIndex(key);
        if (index < 0) {
            throw SqlException.$(argPositions.getQuick(i), "duplicate branch");
        }
        map.putAt(index, key, args.getQuick(i + 1));
        argsToPoke.add(args.getQuick(i + 1));
    }
    final Function elseB = getElseFunction(valueType, elseBranch);
    final CaseFunctionPicker picker = record -> {
        final int index = map.keyIndex(intMethod.getKey(keyFunction, record));
        if (index < 0) {
            return map.valueAtQuick(index);
        }
        return elseB;
    };
    argsToPoke.add(elseB);
    argsToPoke.add(keyFunction);
    return CaseCommon.getCaseFunction(position, valueType, picker, argsToPoke);
}
Also used : ColumnType(io.questdb.cairo.ColumnType) Function(io.questdb.cairo.sql.Function) SqlException(io.questdb.griffin.SqlException) CairoConfiguration(io.questdb.cairo.CairoConfiguration) Constants(io.questdb.griffin.engine.functions.constants.Constants) Record(io.questdb.cairo.sql.Record) SqlCompiler(io.questdb.griffin.SqlCompiler) SqlExecutionContext(io.questdb.griffin.SqlExecutionContext) FunctionFactory(io.questdb.griffin.FunctionFactory) io.questdb.std(io.questdb.std) Function(io.questdb.cairo.sql.Function)

Example 90 with Function

use of io.questdb.cairo.sql.Function in project questdb by bluestreak01.

the class SwitchFunctionFactory method newInstance.

@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) throws SqlException {
    int n = args.size();
    final Function keyFunction = args.getQuick(0);
    final int keyType = keyFunction.getType();
    final Function elseBranch;
    if (n % 2 == 0) {
        elseBranch = args.getLast();
        n--;
    } else {
        elseBranch = null;
    }
    int returnType = -1;
    for (int i = 1; i < n; i += 2) {
        final Function keyFunc = args.getQuick(i);
        final int keyArgType = keyFunc.getType();
        if (!keyFunc.isConstant()) {
            throw SqlException.$(argPositions.getQuick(i), "constant expected");
        }
        if (!SqlCompiler.isAssignableFrom(keyType, keyArgType)) {
            throw SqlException.position(argPositions.getQuick(i)).put("type mismatch [expected=").put(ColumnType.nameOf(keyType)).put(", actual=").put(ColumnType.nameOf(keyArgType)).put(']');
        }
        // determine common return type
        final Function value = args.getQuick(i + 1);
        returnType = CaseCommon.getCommonType(returnType, value.getType(), argPositions.getQuick(i + 1));
    }
    // start with 2 to avoid offsetting each function position
    for (int i = 2; i < n; i += 2) {
        args.setQuick(i, CaseCommon.getCastFunction(args.getQuick(i), argPositions.getQuick(i), returnType, configuration, sqlExecutionContext));
    }
    switch(ColumnType.tagOf(keyType)) {
        case ColumnType.CHAR:
            return getIntKeyedFunction(args, argPositions, position, n, keyFunction, returnType, elseBranch, GET_CHAR);
        case ColumnType.INT:
            return getIntKeyedFunction(args, argPositions, position, n, keyFunction, returnType, elseBranch, GET_INT);
        case ColumnType.BYTE:
            return getIntKeyedFunction(args, argPositions, position, n, keyFunction, returnType, elseBranch, GET_BYTE);
        case ColumnType.SHORT:
            return getIntKeyedFunction(args, argPositions, position, n, keyFunction, returnType, elseBranch, GET_SHORT);
        case ColumnType.LONG:
            return getLongKeyedFunction(args, argPositions, position, n, keyFunction, returnType, elseBranch, GET_LONG);
        case ColumnType.DATE:
            return getLongKeyedFunction(args, argPositions, position, n, keyFunction, returnType, elseBranch, GET_DATE);
        case ColumnType.TIMESTAMP:
            return getLongKeyedFunction(args, argPositions, position, n, keyFunction, returnType, elseBranch, GET_TIMESTAMP);
        case ColumnType.BOOLEAN:
            return getIfElseFunction(args, argPositions, position, n, keyFunction, returnType, elseBranch);
        case ColumnType.STRING:
            return getCharSequenceKeyedFunction(args, argPositions, position, n, keyFunction, returnType, elseBranch, GET_STRING);
        case ColumnType.SYMBOL:
            return getCharSequenceKeyedFunction(args, argPositions, position, n, keyFunction, returnType, elseBranch, GET_SYMBOL);
        default:
            throw SqlException.$(argPositions.getQuick(0), "type ").put(ColumnType.nameOf(keyType)).put(" is not supported in 'switch' type of 'case' statement");
    }
}
Also used : Function(io.questdb.cairo.sql.Function)

Aggregations

Function (io.questdb.cairo.sql.Function)204 Test (org.junit.Test)101 UnaryFunction (io.questdb.griffin.engine.functions.UnaryFunction)39 IntList (io.questdb.std.IntList)33 Record (io.questdb.cairo.sql.Record)28 ToStrTimestampFunctionFactory (io.questdb.griffin.engine.functions.date.ToStrTimestampFunctionFactory)28 ToStrDateFunctionFactory (io.questdb.griffin.engine.functions.date.ToStrDateFunctionFactory)27 NotFunctionFactory (io.questdb.griffin.engine.functions.bool.NotFunctionFactory)26 InStrFunctionFactory (io.questdb.griffin.engine.functions.bool.InStrFunctionFactory)25 EqDoubleFunctionFactory (io.questdb.griffin.engine.functions.eq.EqDoubleFunctionFactory)25 EqIntFunctionFactory (io.questdb.griffin.engine.functions.eq.EqIntFunctionFactory)25 EqLongFunctionFactory (io.questdb.griffin.engine.functions.eq.EqLongFunctionFactory)25 OrFunctionFactory (io.questdb.griffin.engine.functions.bool.OrFunctionFactory)24 CastStrToGeoHashFunctionFactory (io.questdb.griffin.engine.functions.cast.CastStrToGeoHashFunctionFactory)23 CursorDereferenceFunctionFactory (io.questdb.griffin.engine.functions.catalogue.CursorDereferenceFunctionFactory)23 SysdateFunctionFactory (io.questdb.griffin.engine.functions.date.SysdateFunctionFactory)23 LengthStrFunctionFactory (io.questdb.griffin.engine.functions.str.LengthStrFunctionFactory)23 LengthSymbolFunctionFactory (io.questdb.griffin.engine.functions.str.LengthSymbolFunctionFactory)23 ToCharBinFunctionFactory (io.questdb.griffin.engine.functions.str.ToCharBinFunctionFactory)23 SwitchFunctionFactory (io.questdb.griffin.engine.functions.conditional.SwitchFunctionFactory)22