From 90be34b34fcca133f0efb982774e46923b9a389b Mon Sep 17 00:00:00 2001 From: Ouertani Date: Fri, 5 Jul 2019 15:29:01 +0300 Subject: [PATCH] feat: #2583 ound only is with precsion number --- docs/developer-guide/syntax-reference.rst | 19 ++++---------- .../function/InternalFunctionRegistry.java | 18 +++---------- .../ksql/function/udf/math/CeilKudf.java | 15 +++-------- .../ksql/function/udf/math/FloorKudf.java | 12 +++------ .../ksql/function/udf/math/RoundKudf.java | 10 ++++++- .../array-functions.json | 26 +++++++++---------- 6 files changed, 36 insertions(+), 64 deletions(-) diff --git a/docs/developer-guide/syntax-reference.rst b/docs/developer-guide/syntax-reference.rst index 97094d48c0de..85a49ae97133 100644 --- a/docs/developer-guide/syntax-reference.rst +++ b/docs/developer-guide/syntax-reference.rst @@ -1512,11 +1512,7 @@ Scalar functions | ARRAYCONTAINS | ``ARRAYCONTAINS('[1, 2, 3]', 3)`` | Given JSON or AVRO array checks if a search | | | | value contains in it | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ -| CEIL | ``CEIL(col1)`` or ``CEIL(col1, precision)`` | The ceiling of a value. | -| | | | -| | | precision is a number indicating the number of | -| | | places after the decimal to which to round | -| | | downward | +| CEIL | ``CEIL(col1)`` | The ceiling of a value. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | CONCAT | ``CONCAT(col1, '_hello')`` | Concatenate two strings. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ @@ -1565,11 +1561,7 @@ Scalar functions | | | considered to be equal to any value. FIELD is the | | | | complement to ELT. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ -| FLOOR | ``FLOOR(col1)`` or ``FLOOR(col1, precision)`` | The floor of a value. | -| | | | -| | | precision is a number indicating the number of | -| | | places after the decimal to which to round | -| | | downward | +| FLOOR | ``FLOOR(col1)`` | The floor of a value. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | GEO_DISTANCE | ``GEO_DISTANCE(lat1, lon1, lat2, lon2, unit)`` | The great-circle distance between two lat-long | | | | points, both specified in decimal degrees. An | @@ -1626,10 +1618,9 @@ Scalar functions +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | RANDOM | ``RANDOM()`` | Return a random DOUBLE value between 0.0 and 1.0. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ -| ROUND | ``ROUND(col1)`` or | Round a value to the nearest BIGINT value. | -| | ``ROUND(col1, precision)`` | | -| | | precision is a number indicating up to how many | -| | | decimal places In will be rounded | +| ROUND | ``ROUND(col1)`` or ``ROUND(col1, precision)`` | Round a value to the nearest BIGINT value. | +| | | precision is an INT indicating the number. | +| | | of decimal places to round to. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | SPLIT | ``SPLIT(col1, delimiter)`` | Splits a string into an array of substrings based | | | | on a delimiter. If the delimiter is not found, | diff --git a/ksql-engine/src/main/java/io/confluent/ksql/function/InternalFunctionRegistry.java b/ksql-engine/src/main/java/io/confluent/ksql/function/InternalFunctionRegistry.java index fe021ab616e3..c58ca3b92208 100644 --- a/ksql-engine/src/main/java/io/confluent/ksql/function/InternalFunctionRegistry.java +++ b/ksql-engine/src/main/java/io/confluent/ksql/function/InternalFunctionRegistry.java @@ -233,27 +233,15 @@ private void addMathFunctions() { AbsKudf.class)); addBuiltInFunction(KsqlFunction.createLegacyBuiltIn( - Schema.OPTIONAL_INT64_SCHEMA, + Schema.OPTIONAL_FLOAT64_SCHEMA, Collections.singletonList(Schema.OPTIONAL_FLOAT64_SCHEMA), - CeilKudf.NAME, + "CEIL", CeilKudf.class)); addBuiltInFunction(KsqlFunction.createLegacyBuiltIn( Schema.OPTIONAL_FLOAT64_SCHEMA, - ImmutableList.of(Schema.FLOAT64_SCHEMA, Schema.OPTIONAL_INT32_SCHEMA), - CeilKudf.NAME, - CeilKudf.class)); - - addBuiltInFunction(KsqlFunction.createLegacyBuiltIn( - Schema.OPTIONAL_INT64_SCHEMA, Collections.singletonList(Schema.OPTIONAL_FLOAT64_SCHEMA), - FloorKudf.NAME, - FloorKudf.class)); - - addBuiltInFunction(KsqlFunction.createLegacyBuiltIn( - Schema.OPTIONAL_FLOAT64_SCHEMA, - ImmutableList.of(Schema.FLOAT64_SCHEMA, Schema.OPTIONAL_INT32_SCHEMA), - FloorKudf.NAME, + "FLOOR", FloorKudf.class)); addBuiltInFunction(KsqlFunction.createLegacyBuiltIn( diff --git a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/math/CeilKudf.java b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/math/CeilKudf.java index e4b5051ad233..14a4b3efd657 100644 --- a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/math/CeilKudf.java +++ b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/math/CeilKudf.java @@ -20,20 +20,11 @@ public class CeilKudf implements Kudf { - public static final String NAME = "CEIL"; - @Override public Object evaluate(final Object... args) { - if (args.length != 1 && args.length != 2) { - throw new KsqlFunctionException("Ceil udf should have one or two input arguments."); - } - - final Double number = (Double) args[0]; - - if (args.length == 1) { - return (long) Math.ceil(number); + if (args.length != 1) { + throw new KsqlFunctionException("Ceil udf should have one input argument."); } - final Double round = Math.pow(10, (Integer) args[1]); - return Math.ceil(number * round) / round; + return Math.ceil((Double) args[0]); } } diff --git a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/math/FloorKudf.java b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/math/FloorKudf.java index 9d9f48ae5dc0..6705a3bba12c 100644 --- a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/math/FloorKudf.java +++ b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/math/FloorKudf.java @@ -19,18 +19,12 @@ import io.confluent.ksql.function.udf.Kudf; public class FloorKudf implements Kudf { - public static final String NAME = "FLOOR"; @Override public Object evaluate(final Object... args) { - if (args.length != 1 && args.length != 2) { - throw new KsqlFunctionException("Floor udf should have one or two input arguments."); + if (args.length != 1) { + throw new KsqlFunctionException("Floor udf should have one input argument."); } - final Double number = (Double) args[0]; - if (args.length == 1) { - return (long) Math.floor(number); - } - final Double round = Math.pow(10, (Integer) args[1]); - return Math.floor(number * round) / round; + return Math.floor((Double) args[0]); } } diff --git a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/math/RoundKudf.java b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/math/RoundKudf.java index 064f998b083f..c772d65c4d8c 100644 --- a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/math/RoundKudf.java +++ b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/math/RoundKudf.java @@ -19,6 +19,7 @@ import io.confluent.ksql.function.udf.Kudf; public class RoundKudf implements Kudf { + public static final String NAME = "ROUND"; @Override @@ -27,12 +28,19 @@ public Object evaluate(final Object... args) { throw new KsqlFunctionException("Round udf should have one or two input arguments."); } + if (args[0] == null) { + return null; + } final Double number = (Double) args[0]; if (args.length == 1) { return Math.round(number); } - final Double round = Math.pow(10, (Integer) args[1]); + + if (args[1] == null) { + return null; + } + final Double round = Math.pow(10, (Integer) args[1]); return Math.round(number * round) / round; } } diff --git a/ksql-functional-tests/src/test/resources/query-validation-tests/array-functions.json b/ksql-functional-tests/src/test/resources/query-validation-tests/array-functions.json index 95a0055437bd..05a60d41d6ca 100644 --- a/ksql-functional-tests/src/test/resources/query-validation-tests/array-functions.json +++ b/ksql-functional-tests/src/test/resources/query-validation-tests/array-functions.json @@ -40,16 +40,16 @@ {"topic": "test_topic", "value": {"v" : 1.5}}, {"topic": "test_topic", "value": {"v" : 3}}, {"topic": "test_topic", "value": {"v" : 1.234567}}, - {"topic": "test_topic", "value": {"v" : 0}}, + {"topic": "test_topic", "value": {"v" : 0.0}}, {"topic": "test_topic", "value": {"v" : null}} ], "outputs": [ - {"topic": "OUTPUT", "value": {"F0" : 1}}, - {"topic": "OUTPUT", "value": {"F0" : 1}}, - {"topic": "OUTPUT", "value": {"F0" : 1}}, - {"topic": "OUTPUT", "value": {"F0" : 3}}, - {"topic": "OUTPUT", "value": {"F0" : 1}}, - {"topic": "OUTPUT", "value": {"F0" : 0}}, + {"topic": "OUTPUT", "value": {"F0" : 1.0}}, + {"topic": "OUTPUT", "value": {"F0" : 1.0}}, + {"topic": "OUTPUT", "value": {"F0" : 1.0}}, + {"topic": "OUTPUT", "value": {"F0" : 3.0}}, + {"topic": "OUTPUT", "value": {"F0" : 1.0}}, + {"topic": "OUTPUT", "value": {"F0" : 0.0}}, {"topic": "OUTPUT", "value": {"F0" : null}} ] }, @@ -69,12 +69,12 @@ {"topic": "test_topic", "value": {"v" : null}} ], "outputs": [ - {"topic": "OUTPUT", "value": {"C0" : 2, "C00" : 2.0}}, - {"topic": "OUTPUT", "value": {"C0" : 2, "C00" : 2.0}}, - {"topic": "OUTPUT", "value": {"C0" : 2, "C00" : 2.0}}, - {"topic": "OUTPUT", "value": {"C0" : 3, "C00" : 3.0}}, - {"topic": "OUTPUT", "value": {"C0" : 2, "C00" : 2.0}}, - {"topic": "OUTPUT", "value": {"C0" : 0, "C00" : 0.0}}, + {"topic": "OUTPUT", "value": {"C0" : 2.0}}, + {"topic": "OUTPUT", "value": {"C0" : 2.0}}, + {"topic": "OUTPUT", "value": {"C0" : 2.0}}, + {"topic": "OUTPUT", "value": {"C0" : 3.0}}, + {"topic": "OUTPUT", "value": {"C0" : 2.0}}, + {"topic": "OUTPUT", "value": {"C0" : 0.0}}, {"topic": "OUTPUT", "value": {"C0" : null}} ] }