Search in sources :

Example 81 with Function

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

the class RoundDoubleFunctionFactory method newInstance.

@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) {
    final Function arg = args.getQuick(0);
    final Function scale = args.getQuick(1);
    if (scale.isConstant()) {
        int scaleValue = scale.getInt(null);
        if (scaleValue != Numbers.INT_NaN) {
            if (scaleValue == 0) {
                return new RoundDoubleZeroScaleFunctionFactory.RoundDoubleZeroScaleFunction(arg);
            }
            if (scaleValue > -1 && scaleValue + 2 < Numbers.pow10max) {
                return new FuncPosConst(arg, scaleValue);
            }
            if (scaleValue < 0 && scaleValue > -Numbers.pow10max) {
                return new FuncNegConst(arg, -scaleValue);
            }
        }
        return DoubleConstant.NULL;
    }
    return new Func(arg, args.getQuick(1));
}
Also used : DoubleFunction(io.questdb.griffin.engine.functions.DoubleFunction) UnaryFunction(io.questdb.griffin.engine.functions.UnaryFunction) BinaryFunction(io.questdb.griffin.engine.functions.BinaryFunction) Function(io.questdb.cairo.sql.Function)

Example 82 with Function

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

the class LastGeoHashGroupByFunctionFactory method newInstance.

@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) {
    Function function = args.getQuick(0);
    int type = function.getType();
    // Reuse first implementation overriding computeNext() method inline
    switch(ColumnType.tagOf(type)) {
        case ColumnType.GEOBYTE:
            return new FirstGeoHashGroupByFunctionByte(type, function) {

                @Override
                public void computeNext(MapValue mapValue, Record record) {
                    mapValue.putByte(this.valueIndex, this.function.getGeoByte(record));
                }
            };
        case ColumnType.GEOSHORT:
            return new FirstGeoHashGroupByFunctionShort(type, function) {

                @Override
                public void computeNext(MapValue mapValue, Record record) {
                    mapValue.putShort(this.valueIndex, this.function.getGeoShort(record));
                }
            };
        case ColumnType.GEOINT:
            return new FirstGeoHashGroupByFunctionInt(type, function) {

                @Override
                public void computeNext(MapValue mapValue, Record record) {
                    mapValue.putInt(this.valueIndex, this.function.getGeoInt(record));
                }
            };
        default:
            return new FirstGeoHashGroupByFunctionLong(type, function) {

                @Override
                public void computeNext(MapValue mapValue, Record record) {
                    mapValue.putLong(this.valueIndex, this.function.getGeoLong(record));
                }
            };
    }
}
Also used : Function(io.questdb.cairo.sql.Function) MapValue(io.questdb.cairo.map.MapValue) Record(io.questdb.cairo.sql.Record)

Example 83 with Function

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

the class FirstGeoHashGroupByFunctionFactory method newInstance.

@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) {
    Function function = args.getQuick(0);
    int type = function.getType();
    switch(ColumnType.tagOf(type)) {
        case ColumnType.GEOBYTE:
            return new FirstGeoHashGroupByFunctionByte(type, function);
        case ColumnType.GEOSHORT:
            return new FirstGeoHashGroupByFunctionShort(type, function);
        case ColumnType.GEOINT:
            return new FirstGeoHashGroupByFunctionInt(type, function);
        default:
            return new FirstGeoHashGroupByFunctionLong(type, function);
    }
}
Also used : Function(io.questdb.cairo.sql.Function)

Example 84 with Function

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

the class RuntimeIntervalModel method addEvaluateDynamicIntervals.

private void addEvaluateDynamicIntervals(LongList outIntervals, SqlExecutionContext sqlContext) throws SqlException {
    int size = intervals.size();
    int dynamicStart = size - dynamicRangeList.size() * STATIC_LONGS_PER_DYNAMIC_INTERVAL;
    int dynamicIndex = 0;
    for (int i = dynamicStart; i < size; i += STATIC_LONGS_PER_DYNAMIC_INTERVAL) {
        Function dynamicFunction = dynamicRangeList.getQuick(dynamicIndex++);
        short operation = IntervalUtils.getEncodedOperation(intervals, i);
        boolean negated = operation > IntervalOperation.NEGATED_BORDERLINE;
        int divider = outIntervals.size();
        if (dynamicFunction == null) {
            // copy 4 longs to output and apply the operation
            outIntervals.add(intervals, i, i + STATIC_LONGS_PER_DYNAMIC_INTERVAL);
            IntervalUtils.applyLastEncodedIntervalEx(outIntervals);
        } else {
            long lo = IntervalUtils.getEncodedPeriodLo(intervals, i);
            long hi = IntervalUtils.getEncodedPeriodHi(intervals, i);
            short adjustment = IntervalUtils.getEncodedAdjustment(intervals, i);
            short dynamicHiLo = IntervalUtils.getEncodedDynamicIndicator(intervals, i);
            dynamicFunction.init(null, sqlContext);
            if (operation != IntervalOperation.INTERSECT_INTERVALS && operation != IntervalOperation.SUBTRACT_INTERVALS) {
                long dynamicValue = getTimestamp(dynamicFunction);
                long dynamicValue2 = 0;
                if (dynamicHiLo == IntervalDynamicIndicator.IS_LO_SEPARATE_DYNAMIC) {
                    // Both ends of BETWEEN are dynamic and different values. Take next dynamic point.
                    i += STATIC_LONGS_PER_DYNAMIC_INTERVAL;
                    dynamicFunction = dynamicRangeList.getQuick(dynamicIndex++);
                    dynamicFunction.init(null, sqlContext);
                    dynamicValue2 = hi = getTimestamp(dynamicFunction);
                    lo = dynamicValue;
                } else {
                    if ((dynamicHiLo & IntervalDynamicIndicator.IS_HI_DYNAMIC) != 0) {
                        hi = dynamicValue + adjustment;
                    }
                    if ((dynamicHiLo & IntervalDynamicIndicator.IS_LO_DYNAMIC) != 0) {
                        lo = dynamicValue + adjustment;
                    }
                }
                if (dynamicValue == Numbers.LONG_NaN || dynamicValue2 == Numbers.LONG_NaN) {
                    // functions evaluated to null.
                    if (!negated) {
                        // return empty set if it's not negated
                        outIntervals.clear();
                        return;
                    } else {
                        // or full set
                        negatedNothing(outIntervals, divider);
                        continue;
                    }
                }
                if (operation == IntervalOperation.INTERSECT_BETWEEN || operation == IntervalOperation.SUBTRACT_BETWEEN) {
                    long tempHi = Math.max(hi, lo);
                    lo = Math.min(hi, lo);
                    hi = tempHi;
                }
                outIntervals.extendAndSet(divider + 1, hi);
                outIntervals.setQuick(divider, lo);
            } else {
                // This is subtract or intersect with a string interval (not a single timestamp)
                CharSequence strValue = dynamicFunction.getStr(null);
                if (operation == IntervalOperation.INTERSECT_INTERVALS) {
                    // This is intersect
                    if (parseIntervalFails(outIntervals, strValue)) {
                        // return empty set
                        outIntervals.clear();
                        return;
                    }
                } else {
                    // This is subtract
                    if (parseIntervalFails(outIntervals, strValue)) {
                        // full set
                        negatedNothing(outIntervals, divider);
                        continue;
                    }
                    IntervalUtils.invert(outIntervals, divider);
                }
            }
        }
        // if this is first element and no pre-calculated static intervals exist
        if (divider > 0) {
            switch(operation) {
                case IntervalOperation.INTERSECT:
                case IntervalOperation.INTERSECT_BETWEEN:
                case IntervalOperation.INTERSECT_INTERVALS:
                case IntervalOperation.SUBTRACT_INTERVALS:
                    IntervalUtils.intersectInplace(outIntervals, divider);
                    break;
                case IntervalOperation.SUBTRACT:
                case IntervalOperation.SUBTRACT_BETWEEN:
                    IntervalUtils.subtract(outIntervals, divider);
                    break;
                // UNION cannot be the first thing at the moment.
                default:
                    throw new UnsupportedOperationException("Interval operation " + operation + " is not supported");
            }
        }
    }
}
Also used : Function(io.questdb.cairo.sql.Function)

Example 85 with Function

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

the class RndGeoHashFunctionFactoryTest method getFunction.

private Function getFunction(int bits) throws SqlException {
    Function function = parseFunction(String.format("rnd_geohash(%d)", bits), metadata, functionParser);
    Assert.assertEquals(ColumnType.getGeoHashTypeWithBits(bits), function.getType());
    Assert.assertFalse(function.isConstant());
    function.init(null, sqlExecutionContext);
    return function;
}
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