Skip to content

Commit

Permalink
feat(sql): add SQL function ceiling with double and float arguments (q…
Browse files Browse the repository at this point in the history
  • Loading branch information
bsnxsourav authored and anurag-harness committed Oct 14, 2022
1 parent 6118d10 commit d48882e
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2022 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.SqlExecutionContext;
import io.questdb.griffin.engine.functions.DoubleFunction;
import io.questdb.griffin.engine.functions.UnaryFunction;
import io.questdb.std.IntList;
import io.questdb.std.ObjList;

public class CeilingDoubleFunctionFactory implements FunctionFactory {
@Override
public String getSignature() {
return "ceiling(D)";
}

@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) {
return new CeilingFunction(args.getQuick(0));
}

private static class CeilingFunction extends DoubleFunction implements UnaryFunction {
private final Function function;

public CeilingFunction(Function function) {
this.function = function;
}

@Override
public Function getArg() {
return function;
}

@Override
public double getDouble(Record rec) {
double value = function.getDouble(rec);
return Math.ceil(value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2022 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.SqlExecutionContext;
import io.questdb.griffin.engine.functions.FloatFunction;
import io.questdb.griffin.engine.functions.UnaryFunction;
import io.questdb.std.IntList;
import io.questdb.std.ObjList;

public class CeilingFloatFunctionFactory implements FunctionFactory {
@Override
public String getSignature() {
return "ceiling(F)";
}

@Override
public Function newInstance(int position, ObjList<Function> args, IntList argPositions, CairoConfiguration configuration, SqlExecutionContext sqlExecutionContext) {
return new CeilingFunction(args.getQuick(0));
}

private static class CeilingFunction extends FloatFunction implements UnaryFunction {
private final Function function;

public CeilingFunction(Function function) {
this.function = function;
}

@Override
public Function getArg() {
return function;
}

@Override
public float getFloat(Record rec) {
float value = function.getFloat(rec);
return (float) Math.ceil(value);
}
}
}
3 changes: 3 additions & 0 deletions core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@
// ceil()
io.questdb.griffin.engine.functions.math.CeilDoubleFunctionFactory,
io.questdb.griffin.engine.functions.math.CeilFloatFunctionFactory,
// ceil()
io.questdb.griffin.engine.functions.math.CeilingDoubleFunctionFactory,
io.questdb.griffin.engine.functions.math.CeilingFloatFunctionFactory,
// floor()
io.questdb.griffin.engine.functions.math.FloorDoubleFunctionFactory,
io.questdb.griffin.engine.functions.math.FloorFloatFunctionFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ io.questdb.griffin.engine.functions.math.RoundHalfEvenDoubleFunctionFactory
io.questdb.griffin.engine.functions.math.CeilDoubleFunctionFactory
io.questdb.griffin.engine.functions.math.CeilFloatFunctionFactory

# CeilingFunctionFactory
io.questdb.griffin.engine.functions.math.CeilingDoubleFunctionFactory
io.questdb.griffin.engine.functions.math.CeilingFloatFunctionFactory

# FloorFunctionFactory
io.questdb.griffin.engine.functions.math.FloorDoubleFunctionFactory
io.questdb.griffin.engine.functions.math.FloorFloatFunctionFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2022 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 CeilingDoubleFunctionFactoryTest extends AbstractFunctionFactoryTest {

@Test
public void testPositive() throws SqlException {
call(13.1).andAssert(14.0, 0.0000000001);
}

@Test
public void testNegative() throws SqlException {
call(-13.1).andAssert(-13.0, 0.0000000001);
}

@Test
public void testNaN() throws SqlException {
call(Double.NaN).andAssert(Double.NaN, 0);
}

@Override
protected FunctionFactory getFunctionFactory() { return new CeilingDoubleFunctionFactory();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2022 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 CeilingFloatFunctionFactoryTest extends AbstractFunctionFactoryTest {

@Test
public void testNegative() throws SqlException {
call(-13.1f).andAssert(-13.0, 0.0000000001);
}

@Override
protected FunctionFactory getFunctionFactory() {
return new CeilingFloatFunctionFactory();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2022 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.AbstractGriffinTest;
import io.questdb.griffin.SqlException;
import org.junit.Test;

public class CeilingFunctionFactoryTest extends AbstractGriffinTest {



@Test
public void testFloatPositive() throws SqlException {
assertQuery(
"ceiling\n" +
"14.0000\n",
"select ceiling(13.1f)",
null,
true,
true
);
}

@Test
public void testFloatNegative() throws SqlException {
assertQuery(
"ceiling\n" +
"-13.0000\n",
"select ceiling(-13.1f)",
null,
true,
true
);
}

@Test
public void testDoublePositive() throws SqlException {
assertQuery(
"ceiling\n" +
"14.0\n",
"select ceiling(13.1)",
null,
true,
true
);
}

@Test
public void testDoubleNegative() throws SqlException {
assertQuery(
"ceiling\n" +
"-13.0\n",
"select ceiling(-13.1)",
null,
true,
true
);
}

@Test
public void testNaN() throws SqlException {
assertQuery(
"ceiling\n" +
"NaN\n",
"select ceiling(NaN)",
null,
true,
true
);
}
}

0 comments on commit d48882e

Please sign in to comment.