Skip to content

Commit

Permalink
feat: confluentinc#2583 ound only is with precsion number
Browse files Browse the repository at this point in the history
  • Loading branch information
ouertani committed Jul 5, 2019
1 parent 759a957 commit 90be34b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 64 deletions.
19 changes: 5 additions & 14 deletions docs/developer-guide/syntax-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
+------------------------+---------------------------------------------------------------------------+---------------------------------------------------+
Expand Down Expand Up @@ -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 |
Expand Down Expand Up @@ -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, |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.confluent.ksql.function.udf.Kudf;

public class RoundKudf implements Kudf {

public static final String NAME = "ROUND";

@Override
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
]
},
Expand All @@ -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}}
]
}
Expand Down

0 comments on commit 90be34b

Please sign in to comment.