未验证 提交 90f9b45f 编写于 作者: T TheTanc 提交者: GitHub

abs(double) implementation + unbranching (#318)

上级 5a9e22f1
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* 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.math;
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.DoubleFunction;
import io.questdb.griffin.engine.functions.UnaryFunction;
import io.questdb.std.ObjList;
public class AbsDoubleFunctionFactory implements FunctionFactory {
@Override
public String getSignature() {
return "abs(D)";
}
@Override
public Function newInstance(ObjList<Function> args, int position, CairoConfiguration configuration) {
return new AbsFunction(position, args.getQuick(0));
}
private static class AbsFunction extends DoubleFunction implements UnaryFunction {
final Function function;
public AbsFunction(int position, Function function) {
super(position);
this.function = function;
}
@Override
public Function getArg() {
return function;
}
@Override
public double getDouble(Record rec) {
double value = function.getDouble(rec);
return Math.abs(value);
}
}
}
......@@ -60,10 +60,11 @@ public class AbsIntFunctionFactory implements FunctionFactory {
return arg;
}
@Override
public int getInt(Record rec) {
int value = arg.getInt(rec);
return value < 0 ? -value : value;
return Math.abs(value);
}
}
}
......@@ -50,7 +50,7 @@ public class AbsLongFunctionFactory implements FunctionFactory {
@Override
public long getLong(Record rec) {
long value = arg.getLong(rec);
return value < 0 ? -value : value;
return Math.abs(value);
}
}
}
......@@ -59,7 +59,7 @@ public class AbsShortFunctionFactory implements FunctionFactory {
@Override
public short getShort(Record rec) {
short value = function.getShort(rec);
return value < 0 ? (short) -value : value;
return (short) Math.abs(value);
}
}
}
......@@ -131,6 +131,7 @@ open module io.questdb {
io.questdb.griffin.engine.functions.math.AbsIntFunctionFactory,
io.questdb.griffin.engine.functions.math.AbsShortFunctionFactory,
io.questdb.griffin.engine.functions.math.AbsLongFunctionFactory,
io.questdb.griffin.engine.functions.math.AbsDoubleFunctionFactory,
// # '~=',
io.questdb.griffin.engine.functions.regex.MatchStrFunctionFactory,
io.questdb.griffin.engine.functions.regex.MatchCharFunctionFactory,
......
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* 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.math;
import io.questdb.griffin.FunctionFactory;
import io.questdb.griffin.SqlException;
import io.questdb.griffin.engine.AbstractFunctionFactoryTest;
import org.junit.Test;
public class AbsDoubleFunctionFactoryTest extends AbstractFunctionFactoryTest {
@Test
public void testPositive() throws SqlException {
call(13.1).andAssert(13.1, 0.0000000001);
}
@Test
public void testNegative() throws SqlException {
call(-13.1).andAssert(13.1, 0.0000000001);
}
@Override
protected FunctionFactory getFunctionFactory() { return new AbsDoubleFunctionFactory();
}
}
\ No newline at end of file
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* 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.math;
import io.questdb.griffin.FunctionFactory;
import io.questdb.griffin.SqlException;
import io.questdb.griffin.engine.AbstractFunctionFactoryTest;
import io.questdb.std.Numbers;
import org.junit.Test;
public class AbsIntFunctionFactoryTest extends AbstractFunctionFactoryTest {
@Test
public void testPositive() throws SqlException {
call(1).andAssert(1, 0.0000000001);
}
@Test
public void testNegative() throws SqlException {
call(-1).andAssert(1, 0.0000000001);
}
@Override
protected FunctionFactory getFunctionFactory() {
return new AbsIntFunctionFactory();
}
}
\ No newline at end of file
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* 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.math;
import io.questdb.griffin.FunctionFactory;
import io.questdb.griffin.SqlException;
import io.questdb.griffin.engine.AbstractFunctionFactoryTest;
import org.junit.Test;
public class AbsLongFunctionFactoryTest extends AbstractFunctionFactoryTest {
@Test
public void testPositive() throws SqlException {
call(1L).andAssert(1, 0.0000000001);
}
@Test
public void testNegative() throws SqlException {
call(-1L).andAssert(1, 0.0000000001);
}
@Override
protected FunctionFactory getFunctionFactory() {
return new AbsLongFunctionFactory();
}
}
\ No newline at end of file
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* 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.math;
import io.questdb.griffin.FunctionFactory;
import io.questdb.griffin.SqlException;
import io.questdb.griffin.engine.AbstractFunctionFactoryTest;
import org.junit.Test;
public class AbsShortFunctionFactoryTest extends AbstractFunctionFactoryTest {
@Test
public void testPositive() throws SqlException {
call(1).andAssert(1, 0.0000000001);
}
@Test
public void testNegative() throws SqlException {
call(-1).andAssert(1, 0.0000000001);
}
@Override
protected FunctionFactory getFunctionFactory() { return new AbsShortFunctionFactory();
}
}
\ No newline at end of file
......@@ -128,9 +128,11 @@ io.questdb.griffin.engine.functions.math.MulDoubleFunctionFactory
io.questdb.griffin.engine.functions.math.MulLongFunctionFactory
io.questdb.griffin.engine.functions.math.MulIntFunctionFactory
# 'abs'
io.questdb.griffin.engine.functions.math.AbsIntFunctionFactory
io.questdb.griffin.engine.functions.math.AbsShortFunctionFactory
io.questdb.griffin.engine.functions.math.AbsLongFunctionFactory
io.questdb.griffin.engine.functions.math.AbsDoubleFunctionFactory
# '~='
io.questdb.griffin.engine.functions.regex.MatchStrFunctionFactory
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册