-
Notifications
You must be signed in to change notification settings - Fork 3.4k
WIP: Add uuid_v6 and uuid_v7 functions #27493
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,8 +13,13 @@ | |
| */ | ||
| package io.trino.type; | ||
|
|
||
| import com.fasterxml.uuid.Generators; | ||
| import com.fasterxml.uuid.impl.RandomBasedGenerator; | ||
| import com.fasterxml.uuid.impl.TimeBasedEpochRandomGenerator; | ||
| import com.fasterxml.uuid.impl.TimeBasedReorderedGenerator; | ||
| import io.airlift.slice.Slice; | ||
| import io.trino.spi.TrinoException; | ||
| import io.trino.spi.function.Constraint; | ||
| import io.trino.spi.function.Description; | ||
| import io.trino.spi.function.LiteralParameters; | ||
| import io.trino.spi.function.ScalarFunction; | ||
|
|
@@ -28,18 +33,62 @@ | |
| import static io.trino.spi.function.OperatorType.CAST; | ||
| import static io.trino.spi.type.UuidType.javaUuidToTrinoUuid; | ||
| import static io.trino.spi.type.UuidType.trinoUuidToJavaUuid; | ||
| import static java.util.UUID.randomUUID; | ||
|
|
||
| public final class UuidOperators | ||
| { | ||
| // All generators are thread safe | ||
| private static final RandomBasedGenerator V4_GENERATOR = Generators.randomBasedGenerator(); | ||
| private static final TimeBasedReorderedGenerator V6_GENERATOR = Generators.timeBasedReorderedGenerator(); | ||
| private static final TimeBasedEpochRandomGenerator V7_GENERATOR = Generators.timeBasedEpochRandomGenerator(); | ||
|
|
||
| private UuidOperators() {} | ||
|
|
||
| @Description("Generates a random UUID") | ||
| @ScalarFunction(deterministic = false) | ||
| @Description("Generates a random UUID v4 (RFC 4122)") | ||
| @ScalarFunction(deterministic = false, alias = "uuid_v4") | ||
| @SqlType(StandardTypes.UUID) | ||
| public static Slice uuid() | ||
| { | ||
| java.util.UUID uuid = randomUUID(); | ||
| java.util.UUID uuid = V4_GENERATOR.generate(); | ||
| return javaUuidToTrinoUuid(uuid); | ||
| } | ||
|
|
||
| @Description("Generates a random UUID v6 (RFC-9562)") | ||
| @ScalarFunction(deterministic = false) | ||
| @SqlType(StandardTypes.UUID) | ||
| public static Slice uuid_v6() | ||
| { | ||
| java.util.UUID uuid = V6_GENERATOR.generate(); | ||
| return javaUuidToTrinoUuid(uuid); | ||
| } | ||
|
|
||
| @Description("Generates a random UUID v6 from a given timestamp (RFC-9562)") | ||
| @ScalarFunction | ||
|
Comment on lines
+64
to
+65
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Provide explicit
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW this function looks deterministic. What's the use-case to have it? |
||
| @SqlType(StandardTypes.UUID) | ||
| @LiteralParameters("u") | ||
| @Constraint(variable = "u", expression = "min(u, 6)") | ||
| public static Slice uuid_v6(@SqlType("timestamp(u) with time zone") long rawTimestamp) | ||
|
Comment on lines
+68
to
+69
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That results in a rather nasty exception Engine is not supposed to throw anything other than TrinoExceptions. |
||
| { | ||
| java.util.UUID uuid = V6_GENERATOR.construct(rawTimestamp); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I doubt |
||
| return javaUuidToTrinoUuid(uuid); | ||
| } | ||
|
|
||
| @Description("Generates a random UUID v7 (RFC-9562)") | ||
| @ScalarFunction(deterministic = false) | ||
| @SqlType(StandardTypes.UUID) | ||
| public static Slice uuid_v7() | ||
| { | ||
| java.util.UUID uuid = V7_GENERATOR.generate(); | ||
| return javaUuidToTrinoUuid(uuid); | ||
| } | ||
|
|
||
| @Description("Generates a random UUID v7 from a given timestamp (RFC-9562)") | ||
| @ScalarFunction(deterministic = false) | ||
| @SqlType(StandardTypes.UUID) | ||
| @LiteralParameters("u") | ||
| @Constraint(variable = "u", expression = "min(u, 6)") | ||
| public static Slice uuid_v7(@SqlType("timestamp(u) with time zone") long rawTimestamp) | ||
| { | ||
| java.util.UUID uuid = V7_GENERATOR.construct(rawTimestamp); | ||
| return javaUuidToTrinoUuid(uuid); | ||
| } | ||
|
|
||
|
|
||
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.
Is this better, different, or same as
randomUUIDthe code used to use?