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));
}
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));
}
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);
}
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);
}
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");
}
}
Aggregations