
io.questdb.griffin.engine.functions.bool.BetweenTimestampFunctionFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of questdb-jdk8 Show documentation
Show all versions of questdb-jdk8 Show documentation
QuestDB is high performance SQL time series database
The newest version!
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2020 QuestDB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package io.questdb.griffin.engine.functions.bool;
import io.questdb.cairo.CairoConfiguration;
import io.questdb.cairo.sql.Function;
import io.questdb.cairo.sql.Record;
import io.questdb.griffin.FunctionFactory;
import io.questdb.griffin.SqlException;
import io.questdb.griffin.SqlExecutionContext;
import io.questdb.griffin.engine.functions.NegatableBooleanFunction;
import io.questdb.griffin.engine.functions.TernaryFunction;
import io.questdb.griffin.engine.functions.UnaryFunction;
import io.questdb.griffin.engine.functions.constants.BooleanConstant;
import io.questdb.std.Numbers;
import io.questdb.std.ObjList;
public class BetweenTimestampFunctionFactory implements FunctionFactory {
@Override
public String getSignature() {
return "between(NNN)";
}
@Override
public Function newInstance(ObjList args, int position, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) throws SqlException {
Function arg = args.getQuick(0);
Function fromFn = args.getQuick(1);
Function toFn = args.getQuick(2);
if (fromFn.isConstant() && toFn.isConstant()) {
long fromFnTimestamp = fromFn.getTimestamp(null);
long toFnTimestamp = toFn.getTimestamp(null);
if (fromFnTimestamp == Numbers.LONG_NaN || toFnTimestamp == Numbers.LONG_NaN) {
return BooleanConstant.FALSE;
}
return new ConstFunc(position, arg, fromFnTimestamp, toFnTimestamp);
}
return new VarBetweenFunction(position, arg, fromFn, toFn);
}
private static class ConstFunc extends NegatableBooleanFunction implements UnaryFunction {
private final Function left;
private final long from;
private final long to;
public ConstFunc(int position, Function left, long from, long to) {
super(position);
this.left = left;
this.from = Math.min(from, to);
this.to = Math.max(from, to);
}
@Override
public boolean getBool(Record rec) {
long timestamp = left.getTimestamp(rec);
if (timestamp == Numbers.LONG_NaN) return false;
return from <= timestamp && timestamp <= to;
}
@Override
public Function getArg() {
return left;
}
}
private static class VarBetweenFunction extends NegatableBooleanFunction implements TernaryFunction {
private final Function arg;
private final Function from;
private final Function to;
public VarBetweenFunction(int position, Function left, Function from, Function to) {
super(position);
this.arg = left;
this.from = from;
this.to = to;
}
@Override
public boolean getBool(Record rec) {
long value = arg.getTimestamp(rec);
if (value == Numbers.LONG_NaN) {
return false;
}
long fromTs = from.getTimestamp(rec);
if (fromTs == Numbers.LONG_NaN) {
return false;
}
long toTs = to.getTimestamp(rec);
if (toTs == Numbers.LONG_NaN) {
return false;
}
return Math.min(fromTs, toTs) <= value && value <= Math.max(fromTs, toTs);
}
@Override
public Function getLeft() {
return from;
}
@Override
public Function getCenter() {
return arg;
}
@Override
public Function getRight() {
return to;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy