forked from questdb/questdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sql): add SQL function ceiling with double and float arguments (q…
- Loading branch information
1 parent
6118d10
commit d48882e
Showing
7 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
.../src/main/java/io/questdb/griffin/engine/functions/math/CeilingDoubleFunctionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
core/src/main/java/io/questdb/griffin/engine/functions/math/CeilingFloatFunctionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
.../test/java/io/questdb/griffin/engine/functions/math/CeilingDoubleFunctionFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...c/test/java/io/questdb/griffin/engine/functions/math/CeilingFloatFunctionFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
core/src/test/java/io/questdb/griffin/engine/functions/math/CeilingFunctionFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |