-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add CHR UDF #5559
Merged
Merged
feat: add CHR UDF #5559
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
48 changes: 48 additions & 0 deletions
48
ksqldb-engine/src/main/java/io/confluent/ksql/function/udf/string/Chr.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,48 @@ | ||
/* | ||
* Copyright 2020 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.string; | ||
|
||
import io.confluent.ksql.function.udf.Udf; | ||
import io.confluent.ksql.function.udf.UdfDescription; | ||
import io.confluent.ksql.function.udf.UdfParameter; | ||
import org.apache.commons.lang3.StringEscapeUtils; | ||
|
||
@UdfDescription( | ||
name = "Chr", | ||
description = "Returns a single-character string corresponding to the input character code.") | ||
public class Chr { | ||
|
||
@Udf | ||
public String chr(@UdfParameter( | ||
description = "Decimal codepoint") final Integer decimalCode) { | ||
if (decimalCode == null) { | ||
return null; | ||
} | ||
if (!Character.isValidCodePoint(decimalCode)) { | ||
return null; | ||
} | ||
final char[] resultChars = Character.toChars(decimalCode.intValue()); | ||
return String.valueOf(resultChars); | ||
} | ||
|
||
@Udf | ||
public String chr(@UdfParameter( | ||
description = "UTF16 code for the desired character e.g. '\\u004b'") final String utf16Code) { | ||
if (utf16Code == null || utf16Code.length() < 6 || !utf16Code.startsWith("\\u")) { | ||
return null; | ||
} | ||
return StringEscapeUtils.unescapeJava(utf16Code); | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
ksqldb-engine/src/test/java/io/confluent/ksql/function/udf/string/ChrTest.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,126 @@ | ||
/* | ||
* Copyright 2020 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.string; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import org.junit.Test; | ||
|
||
public class ChrTest { | ||
private final Chr udf = new Chr(); | ||
|
||
@Test | ||
public void shouldConvertFromDecimal() { | ||
final String result = udf.chr(75); | ||
assertThat(result, is("K")); | ||
} | ||
|
||
@Test | ||
public void shouldConvertFromUTF16String() { | ||
final String result = udf.chr("\\u004b"); | ||
assertThat(result, is("K")); | ||
} | ||
|
||
@Test | ||
public void shouldConvertFromUTF16StringWithSlash() { | ||
final String result = udf.chr("\\u004b"); | ||
assertThat(result, is("K")); | ||
} | ||
|
||
@Test | ||
public void shouldConvertZhFromDecimal() { | ||
final String result = udf.chr(22909); | ||
assertThat(result, is("好")); | ||
} | ||
|
||
@Test | ||
public void shouldConvertZhFromUTF16() { | ||
final String result = udf.chr("\\u597d"); | ||
assertThat(result, is("好")); | ||
} | ||
|
||
@Test | ||
public void shouldConvertControlChar() { | ||
final String result = udf.chr(9); | ||
assertThat(result, is("\t")); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullForNullIntegerInput() { | ||
final String result = udf.chr((Integer) null); | ||
assertThat(result, is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullForNullStringInput() { | ||
final String result = udf.chr((String) null); | ||
assertThat(result, is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullForEmptyStringInput() { | ||
final String result = udf.chr(""); | ||
assertThat(result, is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullForNegativeDecimalCode() { | ||
final String result = udf.chr(-1); | ||
assertThat(result, is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void shouldReturnSingleCharForMaxBMPDecimal() { | ||
final String result = udf.chr(65535); | ||
assertThat(result.codePointAt(0), is(65535)); | ||
assertThat(result.toCharArray().length, is(1)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnTwoCharsForNonBMPDecimal() { | ||
final String result = udf.chr(65536); | ||
assertThat(result.codePointAt(0), is(65536)); | ||
assertThat(result.toCharArray().length, is(2)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnTwoCharsForMaxUnicodeDecimal() { | ||
final String result = udf.chr(1_114_111); | ||
assertThat(result.codePointAt(0), is(1_114_111)); | ||
assertThat(result.toCharArray().length, is(2)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullForOutOfRangeDecimal() { | ||
final String result = udf.chr(1_114_112); | ||
assertThat(result, is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullForTooShortUTF16String() { | ||
final String result = udf.chr("\\u065"); | ||
assertThat(result, is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void shouldReturnTwoCharsForNonBMPString() { | ||
final String result = udf.chr("\\ud800\\udc01"); | ||
assertThat(result.codePointAt(0), is(65537)); | ||
assertThat(result.toCharArray().length, is(2)); | ||
} | ||
|
||
} |
126 changes: 126 additions & 0 deletions
126
...s/historical_plans/chr_-_codepoint_from_decimal_code_-_AVRO/6.0.0_1591421496995/plan.json
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,126 @@ | ||
{ | ||
"plan" : [ { | ||
"@type" : "ksqlPlanV1", | ||
"statementText" : "CREATE STREAM INPUT (ID STRING KEY, UTFCODE INTEGER) WITH (KAFKA_TOPIC='test_topic', VALUE_FORMAT='AVRO');", | ||
"ddlCommand" : { | ||
"@type" : "createStreamV1", | ||
"sourceName" : "INPUT", | ||
"schema" : "`ID` STRING KEY, `UTFCODE` INTEGER", | ||
"topicName" : "test_topic", | ||
"formats" : { | ||
"keyFormat" : { | ||
"format" : "KAFKA" | ||
}, | ||
"valueFormat" : { | ||
"format" : "AVRO" | ||
} | ||
} | ||
} | ||
}, { | ||
"@type" : "ksqlPlanV1", | ||
"statementText" : "CREATE STREAM OUTPUT AS SELECT\n INPUT.ID ID,\n CHR(INPUT.UTFCODE) RESULT\nFROM INPUT INPUT\nEMIT CHANGES", | ||
"ddlCommand" : { | ||
"@type" : "createStreamV1", | ||
"sourceName" : "OUTPUT", | ||
"schema" : "`ID` STRING KEY, `RESULT` STRING", | ||
"topicName" : "OUTPUT", | ||
"formats" : { | ||
"keyFormat" : { | ||
"format" : "KAFKA" | ||
}, | ||
"valueFormat" : { | ||
"format" : "AVRO" | ||
} | ||
} | ||
}, | ||
"queryPlan" : { | ||
"sources" : [ "INPUT" ], | ||
"sink" : "OUTPUT", | ||
"physicalPlan" : { | ||
"@type" : "streamSinkV1", | ||
"properties" : { | ||
"queryContext" : "OUTPUT" | ||
}, | ||
"source" : { | ||
"@type" : "streamSelectV1", | ||
"properties" : { | ||
"queryContext" : "Project" | ||
}, | ||
"source" : { | ||
"@type" : "streamSourceV1", | ||
"properties" : { | ||
"queryContext" : "KsqlTopic/Source" | ||
}, | ||
"topicName" : "test_topic", | ||
"formats" : { | ||
"keyFormat" : { | ||
"format" : "KAFKA" | ||
}, | ||
"valueFormat" : { | ||
"format" : "AVRO" | ||
} | ||
}, | ||
"sourceSchema" : "`ID` STRING KEY, `UTFCODE` INTEGER" | ||
}, | ||
"keyColumnNames" : [ "ID" ], | ||
"selectExpressions" : [ "CHR(UTFCODE) AS RESULT" ] | ||
}, | ||
"formats" : { | ||
"keyFormat" : { | ||
"format" : "KAFKA" | ||
}, | ||
"valueFormat" : { | ||
"format" : "AVRO" | ||
} | ||
}, | ||
"topicName" : "OUTPUT" | ||
}, | ||
"queryId" : "CSAS_OUTPUT_0" | ||
} | ||
} ], | ||
"configs" : { | ||
"ksql.extension.dir" : "ext", | ||
"ksql.streams.cache.max.bytes.buffering" : "0", | ||
"ksql.security.extension.class" : null, | ||
"ksql.transient.prefix" : "transient_", | ||
"ksql.persistence.wrap.single.values" : "true", | ||
"ksql.authorization.cache.expiry.time.secs" : "30", | ||
"ksql.schema.registry.url" : "", | ||
"ksql.streams.default.deserialization.exception.handler" : "io.confluent.ksql.errors.LogMetricAndContinueExceptionHandler", | ||
"ksql.output.topic.name.prefix" : "", | ||
"ksql.streams.auto.offset.reset" : "earliest", | ||
"ksql.query.pull.enable.standby.reads" : "false", | ||
"ksql.connect.url" : "http://localhost:8083", | ||
"ksql.service.id" : "some.ksql.service.id", | ||
"ksql.internal.topic.min.insync.replicas" : "1", | ||
"ksql.streams.shutdown.timeout.ms" : "300000", | ||
"ksql.internal.topic.replicas" : "1", | ||
"ksql.insert.into.values.enabled" : "true", | ||
"ksql.query.pull.max.allowed.offset.lag" : "9223372036854775807", | ||
"ksql.query.pull.max.qps" : "2147483647", | ||
"ksql.streams.default.production.exception.handler" : "io.confluent.ksql.errors.ProductionExceptionHandlerUtil$LogAndFailProductionExceptionHandler", | ||
"ksql.access.validator.enable" : "auto", | ||
"ksql.streams.bootstrap.servers" : "localhost:0", | ||
"ksql.streams.commit.interval.ms" : "2000", | ||
"ksql.metric.reporters" : "", | ||
"ksql.query.pull.metrics.enabled" : "false", | ||
"ksql.streams.auto.commit.interval.ms" : "0", | ||
"ksql.metrics.extension" : null, | ||
"ksql.streams.topology.optimization" : "all", | ||
"ksql.hidden.topics" : "_confluent.*,__confluent.*,_schemas,__consumer_offsets,__transaction_state,connect-configs,connect-offsets,connect-status,connect-statuses", | ||
"ksql.streams.num.stream.threads" : "4", | ||
"ksql.timestamp.throw.on.invalid" : "false", | ||
"ksql.authorization.cache.max.entries" : "10000", | ||
"ksql.metrics.tags.custom" : "", | ||
"ksql.pull.queries.enable" : "true", | ||
"ksql.udfs.enabled" : "true", | ||
"ksql.udf.enable.security.manager" : "true", | ||
"ksql.connect.worker.config" : "", | ||
"ksql.sink.window.change.log.additional.retention" : "1000000", | ||
"ksql.readonly.topics" : "_confluent.*,__confluent.*,_schemas,__consumer_offsets,__transaction_state,connect-configs,connect-offsets,connect-status,connect-statuses", | ||
"ksql.udf.collect.metrics" : "false", | ||
"ksql.persistent.prefix" : "query_", | ||
"ksql.query.persistent.active.limit" : "2147483647", | ||
"ksql.error.classifier.regex" : "" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these be single-backticks?
Tab
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, that totally makes sense. not sure what i was thinking when i put triple-backticks there