提交 655be6c2 编写于 作者: V Vlad Ilyushchenko

GRIFFIN: str = char implementation

上级 612bf4d9
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (C) 2014-2019 Appsicle
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
package io.questdb.griffin.engine.functions.eq;
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.engine.functions.BinaryFunction;
import io.questdb.griffin.engine.functions.BooleanFunction;
import io.questdb.griffin.engine.functions.UnaryFunction;
import io.questdb.std.ObjList;
public class EqStrCharFunctionFactory implements FunctionFactory {
@Override
public String getSignature() {
return "=(SA)";
}
@Override
public Function newInstance(ObjList<Function> args, int position, CairoConfiguration configuration) {
// there are optimisation opportunities
// 1. when one of args is constant null comparison can boil down to checking
// length of non-constant (must be -1)
// 2. when one of arguments is constant, save method call and use a field
Function a = args.getQuick(0);
Function b = args.getQuick(1);
if (a.isConstant() && !b.isConstant()) {
return createHalfConstantFunc(position, a, b);
}
if (!a.isConstant() && b.isConstant()) {
return createHalfConstantFunc(position, b, a);
}
return new Func(position, a, b);
}
private Function createHalfConstantFunc(int position, Function constFunc, Function varFunc) {
return new ConstCheckFunc(position, varFunc, constFunc.getChar(null));
}
private static class ConstCheckFunc extends BooleanFunction implements UnaryFunction {
private final Function arg;
private final char constant;
public ConstCheckFunc(int position, Function arg, char constant) {
super(position);
this.arg = arg;
this.constant = constant;
}
@Override
public Function getArg() {
return arg;
}
@Override
public boolean getBool(Record rec) {
CharSequence value = arg.getStr(rec);
if (value == null) {
return false;
}
final int len = value.length();
if (len == 0) {
return false;
}
return value.charAt(0) == constant;
}
}
private static class Func extends BooleanFunction implements BinaryFunction {
private final Function left;
private final Function right;
public Func(int position, Function left, Function right) {
super(position);
this.left = left;
this.right = right;
}
@Override
public Function getLeft() {
return left;
}
@Override
public Function getRight() {
return right;
}
@Override
public boolean getBool(Record rec) {
// important to compare A and B strings in case
// these are columns of the same record
// records have re-usable character sequences
final CharSequence a = left.getStr(rec);
final char b = right.getChar(rec);
if (a == null) {
return false;
}
int len = a.length();
if (len == 0) {
return false;
}
return a.charAt(0) == b;
}
}
}
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (C) 2014-2019 Appsicle
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
package io.questdb.griffin.engine.functions.eq;
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.engine.functions.BinaryFunction;
import io.questdb.griffin.engine.functions.BooleanFunction;
import io.questdb.griffin.engine.functions.UnaryFunction;
import io.questdb.std.Chars;
import io.questdb.std.ObjList;
public class EqSymStrFunctionFactory implements FunctionFactory {
@Override
public String getSignature() {
return "=(KS)";
}
@Override
public Function newInstance(ObjList<Function> args, int position, CairoConfiguration configuration) {
// there are optimisation opportunities
// 1. when one of args is constant null comparison can boil down to checking
// length of non-constant (must be -1)
// 2. when one of arguments is constant, save method call and use a field
Function a = args.getQuick(0);
Function b = args.getQuick(1);
if (a.isConstant() && !b.isConstant()) {
return createHalfConstantFunc(position, a, b);
}
if (!a.isConstant() && b.isConstant()) {
return createHalfConstantFunc(position, b, a);
}
return new Func(position, a, b);
}
private Function createHalfConstantFunc(int position, Function constFunc, Function varFunc) {
CharSequence constValue = constFunc.getStr(null);
if (constValue == null) {
return new NullCheckFunc(position, varFunc);
}
return new ConstCheckFunc(position, varFunc, constValue);
}
private static class NullCheckFunc extends BooleanFunction implements UnaryFunction {
private final Function arg;
public NullCheckFunc(int position, Function arg) {
super(position);
this.arg = arg;
}
@Override
public Function getArg() {
return arg;
}
@Override
public boolean getBool(Record rec) {
return arg.getSymbol(rec) == null;
}
}
private static class ConstCheckFunc extends BooleanFunction implements UnaryFunction {
private final Function arg;
private final CharSequence constant;
public ConstCheckFunc(int position, Function arg, CharSequence constant) {
super(position);
this.arg = arg;
this.constant = constant;
}
@Override
public Function getArg() {
return arg;
}
@Override
public boolean getBool(Record rec) {
return Chars.equalsNc(constant, arg.getSymbol(rec));
}
}
private static class Func extends BooleanFunction implements BinaryFunction {
private final Function left;
private final Function right;
public Func(int position, Function left, Function right) {
super(position);
this.left = left;
this.right = right;
}
@Override
public Function getLeft() {
return left;
}
@Override
public Function getRight() {
return right;
}
@Override
public boolean getBool(Record rec) {
// important to compare A and B strings in case
// these are columns of the same record
// records have re-usable character sequences
final CharSequence a = left.getSymbol(rec);
final CharSequence b = right.getStr(rec);
if (a == null) {
return b == null;
}
return b != null && Chars.equals(a, b);
}
}
}
......@@ -21,6 +21,7 @@
#
################################################################################
################################################################################
# ___ _ ____ ____
# / _ \ _ _ ___ ___| |_| _ \| __ )
......@@ -55,6 +56,7 @@ io.questdb.griffin.engine.functions.eq.EqLongFunctionFactory
io.questdb.griffin.engine.functions.eq.EqDoubleFunctionFactory
io.questdb.griffin.engine.functions.eq.EqLong256StrFunctionFactory
io.questdb.griffin.engine.functions.eq.EqLong256FunctionFactory
io.questdb.griffin.engine.functions.eq.EqStrCharFunctionFactory
# '<>'
io.questdb.griffin.engine.functions.eq.NotEqStrFunctionFactory
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册