Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/trino-main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>5.1.0</version>
</dependency>

<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
Expand Down
57 changes: 53 additions & 4 deletions core/trino-main/src/main/java/io/trino/type/UuidOperators.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Copy link
Member

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 randomUUID the code used to use?

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provide explicit deterministic declaration.

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That results in a rather nasty exception

trino> select uuid_v6( current_timestamp(9) );
Query 20251128_140821_00027_837vf failed: variable 'u' is already set to 9 when trying to set 6
java.lang.IllegalStateException: variable 'u' is already set to 9 when trying to set 6

Engine is not supposed to throw anything other than TrinoExceptions.

{
java.util.UUID uuid = V6_GENERATOR.construct(rawTimestamp);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt com.fasterxml.uuid.impl.TimeBasedEpochRandomGenerator knows the structure of trino timestamp tz values

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);
}

Expand Down