-
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: Add int/bigint/double conversion functions from bytes (#8426)
- Loading branch information
Showing
17 changed files
with
1,200 additions
and
0 deletions.
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
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
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
66 changes: 66 additions & 0 deletions
66
ksqldb-engine/src/main/java/io/confluent/ksql/function/udf/conversions/BigIntFromBytes.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,66 @@ | ||
/* | ||
* Copyright 2021 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community 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.conversions; | ||
|
||
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.BytesUtils; | ||
import io.confluent.ksql.util.KsqlConstants; | ||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
|
||
@UdfDescription( | ||
name = "bigint_from_bytes", | ||
category = FunctionCategory.CONVERSIONS, | ||
description = "Converts a BYTES value to an BIGINT value according to the specified" | ||
+ " byte order. BYTES must be 8 bytes long or a NULL value will be returned.", | ||
author = KsqlConstants.CONFLUENT_AUTHOR | ||
) | ||
public class BigIntFromBytes { | ||
private static final int BYTES_LENGTH = 8; | ||
|
||
@Udf(description = "Converts a BYTES value to an BIGINT value using the 'BIG_ENDIAN' byte order." | ||
+ " BYTES must be 8 bytes long or a NULL value will be returned.") | ||
public Long bigIntFromBytes( | ||
@UdfParameter(description = "The BYTES value to convert.") | ||
final ByteBuffer value | ||
) { | ||
return bigIntFromBytes(value, ByteOrder.BIG_ENDIAN); | ||
} | ||
|
||
@Udf(description = "Converts a BYTES value to an BIGINT value according to the specified" | ||
+ " byte order. BYTES must be 8 bytes long or a NULL value will be returned.") | ||
public Long bigIntFromBytes( | ||
@UdfParameter(description = "The BYTES value to convert.") | ||
final ByteBuffer value, | ||
@UdfParameter(description = "The byte order. Valid orders are 'BIG_ENDIAN' and" | ||
+ " 'LITTLE_ENDIAN'. If omitted, 'BIG_ENDIAN' is used.") | ||
final String byteOrder | ||
) { | ||
return bigIntFromBytes(value, BytesUtils.byteOrderType(byteOrder)); | ||
} | ||
|
||
private Long bigIntFromBytes(final ByteBuffer value, final ByteOrder byteOrder) { | ||
if (value == null) { | ||
return null; | ||
} | ||
|
||
BytesUtils.checkBytesSize(value, BYTES_LENGTH); | ||
value.rewind(); | ||
return value.order(byteOrder).getLong(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
ksqldb-engine/src/main/java/io/confluent/ksql/function/udf/conversions/DoubleFromBytes.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,66 @@ | ||
/* | ||
* Copyright 2021 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community 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.conversions; | ||
|
||
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.BytesUtils; | ||
import io.confluent.ksql.util.KsqlConstants; | ||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
|
||
@UdfDescription( | ||
name = "double_from_bytes", | ||
category = FunctionCategory.CONVERSIONS, | ||
description = "Converts a BYTES value to an DOUBLE value according to the specified" | ||
+ " byte order. BYTES must be 8 bytes long or a NULL value will be returned.", | ||
author = KsqlConstants.CONFLUENT_AUTHOR | ||
) | ||
public class DoubleFromBytes { | ||
private static final int BYTES_LENGTH = 8; | ||
|
||
@Udf(description = "Converts a BYTES value to an DOUBLE value using the 'BIG_ENDIAN' byte order." | ||
+ " BYTES must be 8 bytes long or a NULL value will be returned.") | ||
public Double doubleFromBytes( | ||
@UdfParameter(description = "The BYTES value to convert.") | ||
final ByteBuffer value | ||
) { | ||
return doubleFromBytes(value, ByteOrder.BIG_ENDIAN); | ||
} | ||
|
||
@Udf(description = "Converts a BYTES value to an DOUBLE value according to the specified" | ||
+ " byte order. BYTES must be 8 bytes long or a NULL value will be returned.") | ||
public Double doubleFromBytes( | ||
@UdfParameter(description = "The BYTES value to convert.") | ||
final ByteBuffer value, | ||
@UdfParameter(description = "The byte order. Valid orders are 'BIG_ENDIAN' and" | ||
+ " 'LITTLE_ENDIAN'. If omitted, 'BIG_ENDIAN' is used.") | ||
final String byteOrder | ||
) { | ||
return doubleFromBytes(value, BytesUtils.byteOrderType(byteOrder)); | ||
} | ||
|
||
private Double doubleFromBytes(final ByteBuffer value, final ByteOrder byteOrder) { | ||
if (value == null) { | ||
return null; | ||
} | ||
|
||
BytesUtils.checkBytesSize(value, BYTES_LENGTH); | ||
value.rewind(); | ||
return value.order(byteOrder).getDouble(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
ksqldb-engine/src/main/java/io/confluent/ksql/function/udf/conversions/IntFromBytes.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,66 @@ | ||
/* | ||
* Copyright 2021 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community 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.conversions; | ||
|
||
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.BytesUtils; | ||
import io.confluent.ksql.util.KsqlConstants; | ||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
|
||
@UdfDescription( | ||
name = "int_from_bytes", | ||
category = FunctionCategory.CONVERSIONS, | ||
description = "Converts a BYTES value to an INT value according to the specified" | ||
+ " byte order. BYTES must be 4 bytes long or a NULL value will be returned.", | ||
author = KsqlConstants.CONFLUENT_AUTHOR | ||
) | ||
public class IntFromBytes { | ||
private static final int BYTES_LENGTH = 4; | ||
|
||
@Udf(description = "Converts a BYTES value to an INT value using the 'BIG_ENDIAN' byte order." | ||
+ " BYTES must be 4 bytes long or a NULL value will be returned.") | ||
public Integer intFromBytes( | ||
@UdfParameter(description = "The BYTES value to convert.") | ||
final ByteBuffer value | ||
) { | ||
return intFromBytes(value, ByteOrder.BIG_ENDIAN); | ||
} | ||
|
||
@Udf(description = "Converts a BYTES value to an INT value according to the specified" | ||
+ " byte order. BYTES must be 4 bytes long or a NULL value will be returned.") | ||
public Integer intFromBytes( | ||
@UdfParameter(description = "The BYTES value to convert.") | ||
final ByteBuffer value, | ||
@UdfParameter(description = "The byte order. Valid orders are 'BIG_ENDIAN' and" | ||
+ " 'LITTLE_ENDIAN'. If omitted, 'BIG_ENDIAN' is used.") | ||
final String byteOrder | ||
) { | ||
return intFromBytes(value, BytesUtils.byteOrderType(byteOrder)); | ||
} | ||
|
||
private Integer intFromBytes(final ByteBuffer value, final ByteOrder byteOrder) { | ||
if (value == null) { | ||
return null; | ||
} | ||
|
||
BytesUtils.checkBytesSize(value, BYTES_LENGTH); | ||
value.rewind(); | ||
return value.order(byteOrder).getInt(); | ||
} | ||
} |
Oops, something went wrong.