Skip to content
Merged
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: 5 additions & 1 deletion docs/user/convert/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -862,10 +862,14 @@ Creates a 128-bit murmur3 hash from a string or byte array, returned as a hex st
<details>
<summary><code class="docutils literal notranslate">uuid</code></summary>

Generates a random UUID, returned as a string.
Parses or generates a UUID. Without arguments, will return a random UUID as a string, useful for feature IDs. With an argument,
it will parse the value into a ``java.util.UUID``, useful for UUID-type attributes.

============ =========================================================================================
**Function** | ``uuid()``
| ``uuid($value)``
**Usage** | ``uuid()``
| ``uuid('5ff7db5a-64e1-4459-840e-e994f293dace')``
============ =========================================================================================

.. raw:: html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

package org.locationtech.geomesa.convert2

import com.codahale.metrics.Counter
import com.typesafe.config.Config
import com.typesafe.scalalogging.LazyLogging
import io.micrometer.core.instrument.{DistributionSummary, Metrics, Tag, Timer}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,6 @@ object TypeInference {
Try(s.toBoolean).toOption.map(_ => InferredType("", BOOLEAN, CastToBoolean))

private def tryUuidParsing(s: String): Option[InferredType] =
Try(java.util.UUID.fromString(s)).toOption.map(_ => InferredType("", UUID, IdentityTransform))
Try(java.util.UUID.fromString(s)).toOption.map(_ => InferredType("", UUID, FunctionTransform("uuid(", ")")))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ class IdFunctionFactory extends TransformerFunctionFactory with LazyLogging {
args => args(0).asInstanceOf[String].getBytes(StandardCharsets.UTF_8)
}

private val uuid = TransformerFunction("uuid") { _ => UUID.randomUUID().toString }
private val uuid = TransformerFunction("uuid") { args =>
if (args == null || args.isEmpty) {
UUID.randomUUID().toString
} else if (args(0) == null) {
null
} else {
UUID.fromString(args(0).toString)
}
}

private val uuidZ3 = TransformerFunction("uuidZ3") { args =>
val geom = args(0).asInstanceOf[Point]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class TypeInferenceTest extends Specification with LazyLogging {
private def inferRowWithNames(row: Seq[(String, Any)]): Seq[InferredType] =
TypeInference.infer(row.map(v => PathWithValues(v._1, Seq(v._2))), Left("")).types.map(_.inferredType)

private def inferRowTypes(row: Seq[Any]): Seq[ObjectType] =
TypeInference.infer(row.map(v => PathWithValues("", Seq(v))), Left("")).types.map(_.inferredType.typed)
private def inferRowTypes(row: Seq[Any]): Seq[ObjectType] = inferRow(row).map(_.typed)

private def inferColType(values: Seq[Any], failureRate: Option[Float] = None): ObjectType = {
val in = Seq(PathWithValues("", values))
Expand All @@ -76,12 +75,15 @@ class TypeInferenceTest extends Specification with LazyLogging {
types mustEqual Seq(STRING, INT, LONG, DOUBLE, BOOLEAN, DOUBLE)
}
"infer complex types" in {
val types = inferRowTypes(Seq(new Date(), Array[Byte](0), uuid))
types mustEqual Seq(DATE, BYTES, UUID)
val inferred = inferRow(Seq(new Date(), Array[Byte](0), uuid))
inferred.map(_.typed) mustEqual Seq(DATE, BYTES, UUID)
inferred.last.transform mustEqual TypeInference.IdentityTransform

}
"infer complex types from strings" in {
val types = inferRowTypes(Seq("2018-01-01T00:00:00.000Z", uuidString))
types mustEqual Seq(DATE, UUID)
val inferred = inferRow(Seq("2018-01-01T00:00:00.000Z", uuidString))
inferred.map(_.typed) mustEqual Seq(DATE, UUID)
inferred.last.transform mustEqual TypeInference.FunctionTransform("uuid(", ")")
}
"infer geometry types" in {
val types = inferRowTypes(Seq(point, lineString, polygon, multiPoint, multiLineString, multiPolygon, geometryCollection))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@ class ExpressionTest extends Specification {
res must not(beNull)
res.getClass mustEqual classOf[String]
}
"parse uuids" >> {
val exp = Expression("uuid($0)")
val res = exp.apply(Array("5ff7db5a-64e1-4459-840e-e994f293dace"))
res mustEqual java.util.UUID.fromString("5ff7db5a-64e1-4459-840e-e994f293dace")
}
"generate z3 uuids" >> {
val exp = Expression("uuidZ3($0, $1, 'week')")
val geom = WKTUtils.read("POINT (103 1)")
Expand Down
Loading