-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce transform and reduce invocation function for lambdas
- Loading branch information
1 parent
ea30397
commit a9eb41d
Showing
58 changed files
with
5,073 additions
and
371 deletions.
There are no files selected for viewing
55 changes: 0 additions & 55 deletions
55
ksqldb-engine/src/main/java/io/confluent/ksql/function/udf/array/ArrayTransform.java
This file was deleted.
Oops, something went wrong.
96 changes: 96 additions & 0 deletions
96
ksqldb-engine/src/main/java/io/confluent/ksql/function/udf/lambda/Reduce.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,96 @@ | ||
/* | ||
* Copyright 2021 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.function.udf.lambda; | ||
|
||
import io.confluent.ksql.execution.codegen.helpers.TriFunction; | ||
import io.confluent.ksql.function.FunctionCategory; | ||
import io.confluent.ksql.function.udf.Udf; | ||
import io.confluent.ksql.function.udf.UdfDescription; | ||
import io.confluent.ksql.function.udf.UdfParameter; | ||
import io.confluent.ksql.util.KsqlConstants; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.function.BiFunction; | ||
|
||
/** | ||
* Reduce a collection using an initial state and function | ||
*/ | ||
@UdfDescription( | ||
name = "reduce", | ||
category = FunctionCategory.LAMBDA, | ||
description = "Reduce the input collection down to a single value " | ||
+ "using an initial state and a function. " | ||
+ "The initial state (s) is passed into the scope of the function. " | ||
+ "Each invocation returns a new value for s, " | ||
+ "which the next invocation will receive. " | ||
+ "The final value for s is returned.", | ||
author = KsqlConstants.CONFLUENT_AUTHOR | ||
) | ||
public class Reduce { | ||
|
||
@Udf(description = "When reducing an array, " | ||
+ "the reduce function must have two arguments. " | ||
+ "The two arguments for the reduce function are in order: " | ||
+ "the array item and the state. " | ||
+ "The final state is returned." | ||
) | ||
public <T,S> S reduceArray( | ||
@UdfParameter(description = "The initial state.") final S initialState, | ||
@UdfParameter(description = "The array.") final List<T> list, | ||
@UdfParameter(description = "The reduce function.") final BiFunction<S, T, S> biFunction | ||
) { | ||
if (initialState == null) { | ||
return null; | ||
} | ||
|
||
if (list == null) { | ||
return initialState; | ||
} | ||
|
||
S state = initialState; | ||
for (T listItem: list) { | ||
state = biFunction.apply(state, listItem); | ||
} | ||
return state; | ||
} | ||
|
||
@Udf(description = "When reducing a map, " | ||
+ "the reduce function must have three arguments. " | ||
+ "The three arguments for the reduce function are in order: " | ||
+ "the key, the value, and the state. " | ||
+ "The final state is returned." | ||
) | ||
public <K,V,S> S reduceMap( | ||
@UdfParameter(description = "The initial state.") final S initialState, | ||
@UdfParameter(description = "The map.") final Map<K, V> map, | ||
@UdfParameter(description = "The reduce function.") final TriFunction<S, K, V, S> triFunction | ||
) { | ||
if (initialState == null) { | ||
return null; | ||
} | ||
|
||
if (map == null) { | ||
return initialState; | ||
} | ||
|
||
S state = initialState; | ||
for (Entry<K, V> entry : map.entrySet()) { | ||
state = triFunction.apply(state, entry.getKey(), entry.getValue()); | ||
} | ||
return state; | ||
} | ||
} |
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
60 changes: 0 additions & 60 deletions
60
ksqldb-engine/src/main/java/io/confluent/ksql/function/udf/map/ReduceMap.java
This file was deleted.
Oops, something went wrong.
83 changes: 0 additions & 83 deletions
83
ksqldb-engine/src/test/java/io/confluent/ksql/function/udf/array/ArrayTransformTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.