You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/06/02 15:25:45 UTC

[GitHub] [pulsar] cbornet opened a new pull request, #15903: Transforms core

cbornet opened a new pull request, #15903:
URL: https://github.com/apache/pulsar/pull/15903

   <!--
   ### Contribution Checklist
     
     - PR title format should be *[type][component] summary*. For details, see *[Guideline - Pulsar PR Naming Convention](https://docs.google.com/document/d/1d8Pw6ZbWk-_pCKdOmdvx9rnhPiyuxwq60_TrD68d7BA/edit#heading=h.trs9rsex3xom)*. 
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   ### Motivation
   
   This PR implements [PIP-173 : Create a built-in Function implementing the most common basic transformations](https://github.com/apache/pulsar/issues/15902).
   
   It provides a first set of basic functions:
   * `cast`: modifies the key or value schema to a target compatible schema passed in the `schema-type` argument. This PR only enables `STRING` schema-type. The `part` argument allows to choose on which part to apply between `key` and `value`. If `part` is null or absent the transformations aplpies to both the key and value.
   * `drop-fields`: drops fields given as a string list in parameter `fields`. The `part` argument allows to choose on which part to apply between `key` and `value`. If `part` is null or absent the transformations aplpies to both the key and value.
   * `merge-key-value`: merges the fields of KeyValue records where both the key and value are structured types of the same schema type.
   * `unwrap-key-value`: if the record is a KeyValue, extract the KeyValue's value and make it the record value. If parameter `unwrapKey` is present and set to `true`, extract the KeyValue's key instead.
   
   ### Modifications
   
   Add a `transforms` module in multi-module `pulsar-function` project.
   Create classes `TransformFunction`, `TransformContext`, interface `TransformStep` described in PIP-173
   Create implementations `CastStep`, `DropFieldsStep`, `MergeKeyValueStep`, `UnwrapKeyValueStep` with tests
   Add logic to parse `userConfig` in `TransformFunction` and call the `TransformStep` implementations.
    
   ### Verifying this change
   
   - [ ] Make sure that the change passes the CI checks.
   
   This change added tests and can be verified as follows:
   
   Run tests from the `transforms` module
   
   ### Does this pull request potentially affect one of the following parts:
   
   no
   
   *If `yes` was chosen, please highlight the changes*
   
     - Dependencies (does it add or upgrade a dependency): (yes / no)
     - The public API: (yes / no)
     - The schema: (yes / no / don't know)
     - The default values of configurations: (yes / no)
     - The wire protocol: (yes / no)
     - The rest endpoints: (yes / no)
     - The admin cli options: (yes / no)
     - Anything that affects deployment: (yes / no / don't know)
   
   ### Documentation
   
   Check the box below or label this PR directly.
   
   Need to update docs? 
   
   - [x] `doc-required` 
   (Your PR needs to update docs and you will update later)
     
   - [ ] `doc-not-needed` 
   (Please explain why)
     
   - [ ] `doc` 
   (Your PR contains doc changes)
   
   - [ ] `doc-complete`
   (Docs have been already added)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] cbornet commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
cbornet commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r893373737


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformFunction.java:
##########
@@ -0,0 +1,169 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Function;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j

Review Comment:
   done. PTAL



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/CastStep.java:
##########
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.common.schema.SchemaType;
+
+@Slf4j
+public class CastStep implements TransformStep {
+
+    private final SchemaType keySchemaType;
+    private final SchemaType valueSchemaType;
+
+    public CastStep(SchemaType keySchemaType, SchemaType valueSchemaType) {
+        this.keySchemaType = keySchemaType;
+        this.valueSchemaType = valueSchemaType;
+    }
+
+    @Override
+    public void process(TransformContext transformContext) {
+        if (transformContext.getKeySchema() != null) {
+            Object outputKeyObject = transformContext.getKeyObject();
+            Schema<?> outputSchema = transformContext.getKeySchema();
+            if (keySchemaType == SchemaType.STRING) {
+                outputSchema = Schema.STRING;
+                outputKeyObject = outputKeyObject.toString();
+            }

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] eolivelli commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
eolivelli commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r890004881


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/MergeKeyValueStep.java:
##########
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.common.schema.SchemaType;
+
+@Slf4j
+public class MergeKeyValueStep implements TransformStep {
+
+    private final Map<org.apache.avro.Schema, Map<org.apache.avro.Schema, org.apache.avro.Schema>> schemaCache =
+            new ConcurrentHashMap<>();
+
+    @Override
+    public void process(TransformContext transformContext) {
+        Schema<?> keySchema = transformContext.getKeySchema();
+        if (keySchema == null) {

Review Comment:
   what does it mean ?
   are we assuming that the Key is byte[] or String when we are not using KeyValue Schema ?
   



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/CastStep.java:
##########
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.common.schema.SchemaType;
+
+@Slf4j
+public class CastStep implements TransformStep {

Review Comment:
   the purpose if this Step is not clear to me.
   what does this step do ?



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;
+    private Schema<?> keySchema;
+    private Object keyObject;
+    private boolean keyModified;
+    private Schema<?> valueSchema;
+    private Object valueObject;
+    private boolean valueModified;
+    private KeyValueEncodingType keyValueEncodingType;
+    private String key;
+    private Map<String, String> properties;
+    private String outputTopic;

Review Comment:
   final?



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;
+    private Schema<?> keySchema;
+    private Object keyObject;
+    private boolean keyModified;
+    private Schema<?> valueSchema;
+    private Object valueObject;
+    private boolean valueModified;
+    private KeyValueEncodingType keyValueEncodingType;
+    private String key;
+    private Map<String, String> properties;
+    private String outputTopic;
+
+    public TransformContext(Context context, Object value) {
+        Record<?> currentRecord = context.getCurrentRecord();
+        this.context = context;
+        this.outputTopic = context.getOutputTopic();
+        Schema<?> schema = currentRecord.getSchema();
+        //TODO: should we make a copy ?

Review Comment:
   no need to perform a copy, but maybe it is better to wrap to Collections.unmodifiableMap() if we are not supposed to change the contents



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/CastStep.java:
##########
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.common.schema.SchemaType;
+
+@Slf4j
+public class CastStep implements TransformStep {
+
+    private final SchemaType keySchemaType;
+    private final SchemaType valueSchemaType;
+
+    public CastStep(SchemaType keySchemaType, SchemaType valueSchemaType) {
+        this.keySchemaType = keySchemaType;
+        this.valueSchemaType = valueSchemaType;
+    }
+
+    @Override
+    public void process(TransformContext transformContext) {
+        if (transformContext.getKeySchema() != null) {
+            Object outputKeyObject = transformContext.getKeyObject();
+            Schema<?> outputSchema = transformContext.getKeySchema();
+            if (keySchemaType == SchemaType.STRING) {
+                outputSchema = Schema.STRING;
+                outputKeyObject = outputKeyObject.toString();
+            }
+            transformContext.setKeySchema(outputSchema);
+            transformContext.setKeyObject(outputKeyObject);
+        }
+        if (valueSchemaType == SchemaType.STRING) {

Review Comment:
   else ?
   we should throw a IllegalStateException here if we are not supporting other types



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;

Review Comment:
   final?



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformFunction.java:
##########
@@ -0,0 +1,169 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Function;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j

Review Comment:
   please add javadoc



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;
+    private Schema<?> keySchema;
+    private Object keyObject;
+    private boolean keyModified;
+    private Schema<?> valueSchema;
+    private Object valueObject;
+    private boolean valueModified;
+    private KeyValueEncodingType keyValueEncodingType;
+    private String key;
+    private Map<String, String> properties;
+    private String outputTopic;
+
+    public TransformContext(Context context, Object value) {
+        Record<?> currentRecord = context.getCurrentRecord();
+        this.context = context;
+        this.outputTopic = context.getOutputTopic();
+        Schema<?> schema = currentRecord.getSchema();
+        //TODO: should we make a copy ?
+        this.properties = currentRecord.getProperties();
+        if (schema instanceof KeyValueSchema && value instanceof KeyValue) {
+            KeyValueSchema kvSchema = (KeyValueSchema) schema;
+            KeyValue kv = (KeyValue) value;
+            this.keySchema = kvSchema.getKeySchema();
+            this.keyObject = this.keySchema.getSchemaInfo().getType() == SchemaType.AVRO
+                    ? ((org.apache.pulsar.client.api.schema.GenericRecord) kv.getKey()).getNativeObject()
+                    : kv.getKey();
+            this.valueSchema = kvSchema.getValueSchema();
+            this.valueObject = this.valueSchema.getSchemaInfo().getType() == SchemaType.AVRO
+                    ? ((org.apache.pulsar.client.api.schema.GenericRecord) kv.getValue()).getNativeObject()
+                    : kv.getValue();
+            this.keyValueEncodingType = kvSchema.getKeyValueEncodingType();
+        } else {
+            this.valueSchema = schema;
+            this.valueObject = value;
+            this.key = currentRecord.getKey().orElse(null);
+        }
+    }
+
+    public void send() throws IOException {
+        if (keyModified && keySchema != null && keySchema.getSchemaInfo().getType() == SchemaType.AVRO) {
+            GenericRecord genericRecord = (GenericRecord) keyObject;
+            keySchema = Schema.NATIVE_AVRO(genericRecord.getSchema());
+            keyObject = serializeGenericRecord(genericRecord);
+        }
+        if (valueModified && valueSchema != null && valueSchema.getSchemaInfo().getType() == SchemaType.AVRO) {
+            GenericRecord genericRecord = (GenericRecord) valueObject;
+            valueSchema = Schema.NATIVE_AVRO(genericRecord.getSchema());
+            valueObject = serializeGenericRecord(genericRecord);
+        }
+
+        Schema outputSchema;
+        Object outputObject;
+        GenericObject recordValue = (GenericObject) context.getCurrentRecord().getValue();
+        if (keySchema != null) {
+            outputSchema = Schema.KeyValue(keySchema, valueSchema, keyValueEncodingType);
+            Object outputKeyObject = !keyModified && keySchema.getSchemaInfo().getType().isStruct()
+                    ? ((KeyValue) recordValue.getNativeObject()).getKey()
+                    : keyObject;
+            Object outputValueObject = !valueModified && valueSchema.getSchemaInfo().getType().isStruct()
+                    ? ((KeyValue) recordValue.getNativeObject()).getValue()
+                    : valueObject;
+            outputObject = new KeyValue(outputKeyObject, outputValueObject);
+        } else {
+            outputSchema = valueSchema;
+            outputObject = !valueModified && valueSchema.getSchemaInfo().getType().isStruct()
+                    ? recordValue
+                    : valueObject;
+        }
+
+        if (log.isDebugEnabled()) {
+            log.debug("output {} schema {}", outputObject, outputSchema);
+        }
+        TypedMessageBuilder<?> message = context.newOutputMessage(outputTopic, outputSchema)
+                .properties(properties)
+                .value(outputObject);
+        if (key != null) {

Review Comment:
   if we are using KeyValue Schema we should not set the Key this way.



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/UnwrapKeyValueStep.java:
##########
@@ -0,0 +1,42 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+public class UnwrapKeyValueStep implements TransformStep {
+
+    private final boolean unwrapKey;
+
+    public UnwrapKeyValueStep(boolean unwrapKey) {
+        this.unwrapKey = unwrapKey;
+    }
+
+    @Override
+    public void process(TransformContext transformContext) throws Exception {
+        if (transformContext.getKeySchema() != null) {
+            if (unwrapKey) {
+                transformContext.setValueSchema(transformContext.getKeySchema());
+                transformContext.setValueObject(transformContext.getKeyObject());
+            }
+            // TODO: can we avoid making the conversion to NATIVE_AVRO ?

Review Comment:
   can you please clarify ?



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/CastStep.java:
##########
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.common.schema.SchemaType;
+
+@Slf4j
+public class CastStep implements TransformStep {
+
+    private final SchemaType keySchemaType;
+    private final SchemaType valueSchemaType;
+
+    public CastStep(SchemaType keySchemaType, SchemaType valueSchemaType) {
+        this.keySchemaType = keySchemaType;
+        this.valueSchemaType = valueSchemaType;
+    }
+
+    @Override
+    public void process(TransformContext transformContext) {
+        if (transformContext.getKeySchema() != null) {
+            Object outputKeyObject = transformContext.getKeyObject();
+            Schema<?> outputSchema = transformContext.getKeySchema();
+            if (keySchemaType == SchemaType.STRING) {
+                outputSchema = Schema.STRING;
+                outputKeyObject = outputKeyObject.toString();
+            }

Review Comment:
   else ?
   we should throw a IllegalStateException here if we are not supporting other types



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] github-actions[bot] commented on pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#issuecomment-1185103632

   The pr had no activity for 30 days, mark with Stale label.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] cbornet commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
cbornet commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r890057282


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;
+    private Schema<?> keySchema;
+    private Object keyObject;
+    private boolean keyModified;
+    private Schema<?> valueSchema;
+    private Object valueObject;
+    private boolean valueModified;
+    private KeyValueEncodingType keyValueEncodingType;
+    private String key;
+    private Map<String, String> properties;
+    private String outputTopic;
+
+    public TransformContext(Context context, Object value) {
+        Record<?> currentRecord = context.getCurrentRecord();
+        this.context = context;
+        this.outputTopic = context.getOutputTopic();
+        Schema<?> schema = currentRecord.getSchema();
+        //TODO: should we make a copy ?
+        this.properties = currentRecord.getProperties();
+        if (schema instanceof KeyValueSchema && value instanceof KeyValue) {
+            KeyValueSchema kvSchema = (KeyValueSchema) schema;
+            KeyValue kv = (KeyValue) value;
+            this.keySchema = kvSchema.getKeySchema();
+            this.keyObject = this.keySchema.getSchemaInfo().getType() == SchemaType.AVRO
+                    ? ((org.apache.pulsar.client.api.schema.GenericRecord) kv.getKey()).getNativeObject()
+                    : kv.getKey();
+            this.valueSchema = kvSchema.getValueSchema();
+            this.valueObject = this.valueSchema.getSchemaInfo().getType() == SchemaType.AVRO
+                    ? ((org.apache.pulsar.client.api.schema.GenericRecord) kv.getValue()).getNativeObject()
+                    : kv.getValue();
+            this.keyValueEncodingType = kvSchema.getKeyValueEncodingType();
+        } else {
+            this.valueSchema = schema;
+            this.valueObject = value;
+            this.key = currentRecord.getKey().orElse(null);
+        }
+    }
+
+    public void send() throws IOException {
+        if (keyModified && keySchema != null && keySchema.getSchemaInfo().getType() == SchemaType.AVRO) {
+            GenericRecord genericRecord = (GenericRecord) keyObject;
+            keySchema = Schema.NATIVE_AVRO(genericRecord.getSchema());
+            keyObject = serializeGenericRecord(genericRecord);
+        }
+        if (valueModified && valueSchema != null && valueSchema.getSchemaInfo().getType() == SchemaType.AVRO) {
+            GenericRecord genericRecord = (GenericRecord) valueObject;
+            valueSchema = Schema.NATIVE_AVRO(genericRecord.getSchema());
+            valueObject = serializeGenericRecord(genericRecord);
+        }
+
+        Schema outputSchema;
+        Object outputObject;
+        GenericObject recordValue = (GenericObject) context.getCurrentRecord().getValue();
+        if (keySchema != null) {
+            outputSchema = Schema.KeyValue(keySchema, valueSchema, keyValueEncodingType);
+            Object outputKeyObject = !keyModified && keySchema.getSchemaInfo().getType().isStruct()
+                    ? ((KeyValue) recordValue.getNativeObject()).getKey()
+                    : keyObject;
+            Object outputValueObject = !valueModified && valueSchema.getSchemaInfo().getType().isStruct()
+                    ? ((KeyValue) recordValue.getNativeObject()).getValue()
+                    : valueObject;
+            outputObject = new KeyValue(outputKeyObject, outputValueObject);
+        } else {
+            outputSchema = valueSchema;
+            outputObject = !valueModified && valueSchema.getSchemaInfo().getType().isStruct()
+                    ? recordValue
+                    : valueObject;
+        }
+
+        if (log.isDebugEnabled()) {
+            log.debug("output {} schema {}", outputObject, outputSchema);
+        }
+        TypedMessageBuilder<?> message = context.newOutputMessage(outputTopic, outputSchema)
+                .properties(properties)
+                .value(outputObject);
+        if (key != null) {

Review Comment:
   Indeed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] cbornet commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
cbornet commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r890045748


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;
+    private Schema<?> keySchema;
+    private Object keyObject;
+    private boolean keyModified;
+    private Schema<?> valueSchema;
+    private Object valueObject;
+    private boolean valueModified;
+    private KeyValueEncodingType keyValueEncodingType;
+    private String key;
+    private Map<String, String> properties;
+    private String outputTopic;

Review Comment:
   Steps should have the possibility to modify the output topic. So we can implement basic routing operations such as [RegexRouter](https://docs.confluent.io/platform/current/connect/transforms/regexrouter.html#regexrouter)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] cbornet commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
cbornet commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r890040559


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;
+    private Schema<?> keySchema;
+    private Object keyObject;
+    private boolean keyModified;
+    private Schema<?> valueSchema;
+    private Object valueObject;
+    private boolean valueModified;
+    private KeyValueEncodingType keyValueEncodingType;
+    private String key;
+    private Map<String, String> properties;
+    private String outputTopic;
+
+    public TransformContext(Context context, Object value) {
+        Record<?> currentRecord = context.getCurrentRecord();
+        this.context = context;
+        this.outputTopic = context.getOutputTopic();
+        Schema<?> schema = currentRecord.getSchema();
+        //TODO: should we make a copy ?

Review Comment:
   That's the thing. We want to give the possibility of steps to modify the properties. Wrapping in an unmodifiable map is a good idea as the step would have to make the copy itself.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] cbornet commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
cbornet commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r893373941


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/CastStep.java:
##########
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.common.schema.SchemaType;
+
+@Slf4j
+public class CastStep implements TransformStep {
+
+    private final SchemaType keySchemaType;
+    private final SchemaType valueSchemaType;
+
+    public CastStep(SchemaType keySchemaType, SchemaType valueSchemaType) {
+        this.keySchemaType = keySchemaType;
+        this.valueSchemaType = valueSchemaType;
+    }
+
+    @Override
+    public void process(TransformContext transformContext) {
+        if (transformContext.getKeySchema() != null) {
+            Object outputKeyObject = transformContext.getKeyObject();
+            Schema<?> outputSchema = transformContext.getKeySchema();
+            if (keySchemaType == SchemaType.STRING) {
+                outputSchema = Schema.STRING;
+                outputKeyObject = outputKeyObject.toString();
+            }
+            transformContext.setKeySchema(outputSchema);
+            transformContext.setKeyObject(outputKeyObject);
+        }
+        if (valueSchemaType == SchemaType.STRING) {

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] cbornet commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
cbornet commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r893373345


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;
+    private Schema<?> keySchema;
+    private Object keyObject;
+    private boolean keyModified;
+    private Schema<?> valueSchema;
+    private Object valueObject;
+    private boolean valueModified;
+    private KeyValueEncodingType keyValueEncodingType;
+    private String key;
+    private Map<String, String> properties;
+    private String outputTopic;
+
+    public TransformContext(Context context, Object value) {
+        Record<?> currentRecord = context.getCurrentRecord();
+        this.context = context;
+        this.outputTopic = context.getOutputTopic();
+        Schema<?> schema = currentRecord.getSchema();
+        //TODO: should we make a copy ?

Review Comment:
   I changed to lazily set the properties before sending the message if it was not set by one of the steps.
   Note that PulsarRecord properties are already unmodifiable.
   This can be changed when we implement a step that modifies the properties.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] cbornet commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
cbornet commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r893682505


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/UnwrapKeyValueStep.java:
##########
@@ -0,0 +1,42 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+public class UnwrapKeyValueStep implements TransformStep {
+
+    private final boolean unwrapKey;
+
+    public UnwrapKeyValueStep(boolean unwrapKey) {
+        this.unwrapKey = unwrapKey;
+    }
+
+    @Override
+    public void process(TransformContext transformContext) throws Exception {
+        if (transformContext.getKeySchema() != null) {
+            if (unwrapKey) {
+                transformContext.setValueSchema(transformContext.getKeySchema());
+                transformContext.setValueObject(transformContext.getKeyObject());
+            }
+            // TODO: can we avoid making the conversion to NATIVE_AVRO ?

Review Comment:
   Well theoretically we have the Schema.AVRO schema and value of the wrapped key or value and could use that instead of unwrapping the AVRO schema and rewrapping in Schema.NATIVE_AVRO. But there's no clean way to give this information to the TransformContext. So I'll remove this comment for now.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] cbornet commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
cbornet commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r890056661


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;

Review Comment:
   👍 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] cbornet commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
cbornet commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r890055996


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/CastStep.java:
##########
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.common.schema.SchemaType;
+
+@Slf4j
+public class CastStep implements TransformStep {

Review Comment:
   It changes the schema to another compatible one. The name is inspired from [SMTs](https://docs.confluent.io/platform/current/connect/transforms/cast.html#cast)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] eolivelli commented on pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
eolivelli commented on PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#issuecomment-1217674931

   Moved to a dedicated repository:
   https://github.com/datastax/pulsar-transformations 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] eolivelli closed pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
eolivelli closed pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations
URL: https://github.com/apache/pulsar/pull/15903


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] cbornet commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
cbornet commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r890046999


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/MergeKeyValueStep.java:
##########
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.common.schema.SchemaType;
+
+@Slf4j
+public class MergeKeyValueStep implements TransformStep {
+
+    private final Map<org.apache.avro.Schema, Map<org.apache.avro.Schema, org.apache.avro.Schema>> schemaCache =
+            new ConcurrentHashMap<>();
+
+    @Override
+    public void process(TransformContext transformContext) {
+        Schema<?> keySchema = transformContext.getKeySchema();
+        if (keySchema == null) {

Review Comment:
   It means that if we don't have a KeyValue, we don't do anything.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] cbornet commented on a diff in pull request #15903: [feature][functions]Create a built-in Function implementing the most common basic transformations

Posted by GitBox <gi...@apache.org>.
cbornet commented on code in PR #15903:
URL: https://github.com/apache/pulsar/pull/15903#discussion_r893373501


##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;
+    private Schema<?> keySchema;
+    private Object keyObject;
+    private boolean keyModified;
+    private Schema<?> valueSchema;
+    private Object valueObject;
+    private boolean valueModified;
+    private KeyValueEncodingType keyValueEncodingType;
+    private String key;
+    private Map<String, String> properties;
+    private String outputTopic;
+
+    public TransformContext(Context context, Object value) {
+        Record<?> currentRecord = context.getCurrentRecord();
+        this.context = context;
+        this.outputTopic = context.getOutputTopic();
+        Schema<?> schema = currentRecord.getSchema();
+        //TODO: should we make a copy ?
+        this.properties = currentRecord.getProperties();
+        if (schema instanceof KeyValueSchema && value instanceof KeyValue) {
+            KeyValueSchema kvSchema = (KeyValueSchema) schema;
+            KeyValue kv = (KeyValue) value;
+            this.keySchema = kvSchema.getKeySchema();
+            this.keyObject = this.keySchema.getSchemaInfo().getType() == SchemaType.AVRO
+                    ? ((org.apache.pulsar.client.api.schema.GenericRecord) kv.getKey()).getNativeObject()
+                    : kv.getKey();
+            this.valueSchema = kvSchema.getValueSchema();
+            this.valueObject = this.valueSchema.getSchemaInfo().getType() == SchemaType.AVRO
+                    ? ((org.apache.pulsar.client.api.schema.GenericRecord) kv.getValue()).getNativeObject()
+                    : kv.getValue();
+            this.keyValueEncodingType = kvSchema.getKeyValueEncodingType();
+        } else {
+            this.valueSchema = schema;
+            this.valueObject = value;
+            this.key = currentRecord.getKey().orElse(null);
+        }
+    }
+
+    public void send() throws IOException {
+        if (keyModified && keySchema != null && keySchema.getSchemaInfo().getType() == SchemaType.AVRO) {
+            GenericRecord genericRecord = (GenericRecord) keyObject;
+            keySchema = Schema.NATIVE_AVRO(genericRecord.getSchema());
+            keyObject = serializeGenericRecord(genericRecord);
+        }
+        if (valueModified && valueSchema != null && valueSchema.getSchemaInfo().getType() == SchemaType.AVRO) {
+            GenericRecord genericRecord = (GenericRecord) valueObject;
+            valueSchema = Schema.NATIVE_AVRO(genericRecord.getSchema());
+            valueObject = serializeGenericRecord(genericRecord);
+        }
+
+        Schema outputSchema;
+        Object outputObject;
+        GenericObject recordValue = (GenericObject) context.getCurrentRecord().getValue();
+        if (keySchema != null) {
+            outputSchema = Schema.KeyValue(keySchema, valueSchema, keyValueEncodingType);
+            Object outputKeyObject = !keyModified && keySchema.getSchemaInfo().getType().isStruct()
+                    ? ((KeyValue) recordValue.getNativeObject()).getKey()
+                    : keyObject;
+            Object outputValueObject = !valueModified && valueSchema.getSchemaInfo().getType().isStruct()
+                    ? ((KeyValue) recordValue.getNativeObject()).getValue()
+                    : valueObject;
+            outputObject = new KeyValue(outputKeyObject, outputValueObject);
+        } else {
+            outputSchema = valueSchema;
+            outputObject = !valueModified && valueSchema.getSchemaInfo().getType().isStruct()
+                    ? recordValue
+                    : valueObject;
+        }
+
+        if (log.isDebugEnabled()) {
+            log.debug("output {} schema {}", outputObject, outputSchema);
+        }
+        TypedMessageBuilder<?> message = context.newOutputMessage(outputTopic, outputSchema)
+                .properties(properties)
+                .value(outputObject);
+        if (key != null) {

Review Comment:
   done



##########
pulsar-functions/transforms/src/main/java/org/apache/pulsar/functions/transforms/TransformContext.java:
##########
@@ -0,0 +1,131 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pulsar.functions.transforms;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Map;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.avro.generic.GenericDatumWriter;
+import org.apache.avro.generic.GenericRecord;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.TypedMessageBuilder;
+import org.apache.pulsar.client.api.schema.GenericObject;
+import org.apache.pulsar.client.api.schema.KeyValueSchema;
+import org.apache.pulsar.common.schema.KeyValue;
+import org.apache.pulsar.common.schema.KeyValueEncodingType;
+import org.apache.pulsar.common.schema.SchemaType;
+import org.apache.pulsar.functions.api.Context;
+import org.apache.pulsar.functions.api.Record;
+
+@Slf4j
+@Data
+public class TransformContext {
+    private Context context;

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org