Search in sources :

Example 1 with Function

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;
}
Also used : GroupByFunction(io.questdb.griffin.engine.functions.GroupByFunction) GroupByFunction(io.questdb.griffin.engine.functions.GroupByFunction) Function(io.questdb.cairo.sql.Function) ExpressionNode(io.questdb.griffin.model.ExpressionNode) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Function

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));
}
Also used : Function(io.questdb.cairo.sql.Function) StrFunction(io.questdb.griffin.engine.functions.StrFunction)

Example 3 with Function

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()));
}
Also used : Function(io.questdb.cairo.sql.Function) Test(org.junit.Test)

Example 4 with Function

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()));
}
Also used : Function(io.questdb.cairo.sql.Function) Test(org.junit.Test)

Example 5 with Function

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()));
}
Also used : ToStrTimestampFunctionFactory(io.questdb.griffin.engine.functions.date.ToStrTimestampFunctionFactory) Function(io.questdb.cairo.sql.Function) Test(org.junit.Test)

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