use of io.questdb.cairo.sql.Function in project questdb by bluestreak01.
the class SampleByFillValueRecordCursorFactory method createPlaceholderFunctions.
@NotNull
public static ObjList<Function> createPlaceholderFunctions(ObjList<Function> recordFunctions, @Transient IntList recordFunctionPositions, @NotNull @Transient ObjList<ExpressionNode> fillValues) throws SqlException {
final ObjList<Function> placeholderFunctions = new ObjList<>();
int fillIndex = 0;
final int fillValueCount = fillValues.size();
for (int i = 0, n = recordFunctions.size(); i < n; i++) {
Function function = recordFunctions.getQuick(i);
if (function instanceof GroupByFunction) {
if (fillIndex == fillValueCount) {
throw SqlException.position(0).put("not enough values");
}
ExpressionNode fillNode = fillValues.getQuick(fillIndex++);
try {
switch(ColumnType.tagOf(function.getType())) {
case ColumnType.INT:
placeholderFunctions.add(IntConstant.newInstance(Numbers.parseInt(fillNode.token)));
break;
case ColumnType.LONG:
placeholderFunctions.add(LongConstant.newInstance(Numbers.parseLong(fillNode.token)));
break;
case ColumnType.FLOAT:
placeholderFunctions.add(FloatConstant.newInstance(Numbers.parseFloat(fillNode.token)));
break;
case ColumnType.DOUBLE:
placeholderFunctions.add(DoubleConstant.newInstance(Numbers.parseDouble(fillNode.token)));
break;
case ColumnType.SHORT:
placeholderFunctions.add(ShortConstant.newInstance((short) Numbers.parseInt(fillNode.token)));
break;
case ColumnType.BYTE:
placeholderFunctions.add(ByteConstant.newInstance((byte) Numbers.parseInt(fillNode.token)));
break;
default:
throw SqlException.$(recordFunctionPositions.getQuick(i), "Unsupported type: ").put(ColumnType.nameOf(function.getType()));
}
} catch (NumericException e) {
throw SqlException.position(fillNode.position).put("invalid number: ").put(fillNode.token);
}
} else {
placeholderFunctions.add(function);
}
}
return placeholderFunctions;
}
use of io.questdb.cairo.sql.Function in project questdb by bluestreak01.
the class ReplaceStrFunctionFactory method newInstance.
@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) {
final Function term = args.getQuick(1);
if (term.isConstant()) {
if (term.getStrLen(null) < 1) {
return args.getQuick(0);
}
}
final Function value = args.getQuick(0);
if (value.isConstant()) {
int len = value.getStrLen(null);
if (len < 1) {
return value;
}
}
return new Func(value, term, args.getQuick(2));
}
use of io.questdb.cairo.sql.Function in project questdb by bluestreak01.
the class BindVariablesTest method testLong.
@Test
public void testLong() throws SqlException {
bindVariableService.setLong("xyz", 9);
Function func = expr("a + :xyz").withFunction(new AddLongFunctionFactory()).withColumn("a", ColumnType.LONG, 22L).$();
func.init(null, sqlExecutionContext);
Assert.assertEquals(31L, func.getLong(builder.getRecord()));
bindVariableService.setLong("xyz", 11);
Assert.assertEquals(33L, func.getLong(builder.getRecord()));
}
use of io.questdb.cairo.sql.Function in project questdb by bluestreak01.
the class BindVariablesTest method testIntIndexed.
@Test
public void testIntIndexed() throws SqlException {
bindVariableService.setInt(2, 9000);
bindVariableService.setInt(0, 9);
bindVariableService.setInt(1, 20);
Function func = expr("$1 + $2").withFunction(new AddIntFunctionFactory()).$();
func.init(null, sqlExecutionContext);
Assert.assertEquals(29, func.getInt(builder.getRecord()));
bindVariableService.setInt(0, 11);
bindVariableService.setInt(1, 33);
Assert.assertEquals(44, func.getInt(builder.getRecord()));
}
use of io.questdb.cairo.sql.Function in project questdb by bluestreak01.
the class BindVariablesTest method testTimestampIndexed.
@Test
public void testTimestampIndexed() throws SqlException, NumericException {
bindVariableService.setTimestamp(1, 25);
bindVariableService.setTimestamp(0, TimestampFormatUtils.parseTimestamp("2015-04-10T10:00:00.000Z"));
Function func = expr("to_str($1, 'yyyy-MM')").withFunction(new ToStrTimestampFunctionFactory()).$();
func.init(null, sqlExecutionContext);
TestUtils.assertEquals("2015-04", func.getStr(builder.getRecord()));
bindVariableService.setTimestamp(0, TimestampFormatUtils.parseTimestamp("2015-08-10T10:00:00.000Z"));
TestUtils.assertEquals("2015-08", func.getStr(builder.getRecord()));
}
Aggregations