Skip to content

Commit 8bdb7c6

Browse files
authored
GEOMESA-3558 Converters - Fix type inference for UUIDs (#3512)
1 parent ea8efa5 commit 8bdb7c6

File tree

6 files changed

+28
-10
lines changed

6 files changed

+28
-10
lines changed

docs/user/convert/functions.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,10 +862,14 @@ Creates a 128-bit murmur3 hash from a string or byte array, returned as a hex st
862862
<details>
863863
<summary><code class="docutils literal notranslate">uuid</code></summary>
864864

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

867868
============ =========================================================================================
868869
**Function** | ``uuid()``
870+
| ``uuid($value)``
871+
**Usage** | ``uuid()``
872+
| ``uuid('5ff7db5a-64e1-4459-840e-e994f293dace')``
869873
============ =========================================================================================
870874

871875
.. raw:: html

geomesa-convert/geomesa-convert-common/src/main/scala/org/locationtech/geomesa/convert2/AbstractConverter.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
package org.locationtech.geomesa.convert2
1010

11-
import com.codahale.metrics.Counter
1211
import com.typesafe.config.Config
1312
import com.typesafe.scalalogging.LazyLogging
1413
import io.micrometer.core.instrument.{DistributionSummary, Metrics, Tag, Timer}

geomesa-convert/geomesa-convert-common/src/main/scala/org/locationtech/geomesa/convert2/TypeInference.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,6 @@ object TypeInference {
431431
Try(s.toBoolean).toOption.map(_ => InferredType("", BOOLEAN, CastToBoolean))
432432

433433
private def tryUuidParsing(s: String): Option[InferredType] =
434-
Try(java.util.UUID.fromString(s)).toOption.map(_ => InferredType("", UUID, IdentityTransform))
434+
Try(java.util.UUID.fromString(s)).toOption.map(_ => InferredType("", UUID, FunctionTransform("uuid(", ")")))
435435
}
436436
}

geomesa-convert/geomesa-convert-common/src/main/scala/org/locationtech/geomesa/convert2/transforms/IdFunctionFactory.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ class IdFunctionFactory extends TransformerFunctionFactory with LazyLogging {
3030
args => args(0).asInstanceOf[String].getBytes(StandardCharsets.UTF_8)
3131
}
3232

33-
private val uuid = TransformerFunction("uuid") { _ => UUID.randomUUID().toString }
33+
private val uuid = TransformerFunction("uuid") { args =>
34+
if (args == null || args.isEmpty) {
35+
UUID.randomUUID().toString
36+
} else if (args(0) == null) {
37+
null
38+
} else {
39+
UUID.fromString(args(0).toString)
40+
}
41+
}
3442

3543
private val uuidZ3 = TransformerFunction("uuidZ3") { args =>
3644
val geom = args(0).asInstanceOf[Point]

geomesa-convert/geomesa-convert-common/src/test/scala/org/locationtech/geomesa/convert2/TypeInferenceTest.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class TypeInferenceTest extends Specification with LazyLogging {
5252
private def inferRowWithNames(row: Seq[(String, Any)]): Seq[InferredType] =
5353
TypeInference.infer(row.map(v => PathWithValues(v._1, Seq(v._2))), Left("")).types.map(_.inferredType)
5454

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

5857
private def inferColType(values: Seq[Any], failureRate: Option[Float] = None): ObjectType = {
5958
val in = Seq(PathWithValues("", values))
@@ -76,12 +75,15 @@ class TypeInferenceTest extends Specification with LazyLogging {
7675
types mustEqual Seq(STRING, INT, LONG, DOUBLE, BOOLEAN, DOUBLE)
7776
}
7877
"infer complex types" in {
79-
val types = inferRowTypes(Seq(new Date(), Array[Byte](0), uuid))
80-
types mustEqual Seq(DATE, BYTES, UUID)
78+
val inferred = inferRow(Seq(new Date(), Array[Byte](0), uuid))
79+
inferred.map(_.typed) mustEqual Seq(DATE, BYTES, UUID)
80+
inferred.last.transform mustEqual TypeInference.IdentityTransform
81+
8182
}
8283
"infer complex types from strings" in {
83-
val types = inferRowTypes(Seq("2018-01-01T00:00:00.000Z", uuidString))
84-
types mustEqual Seq(DATE, UUID)
84+
val inferred = inferRow(Seq("2018-01-01T00:00:00.000Z", uuidString))
85+
inferred.map(_.typed) mustEqual Seq(DATE, UUID)
86+
inferred.last.transform mustEqual TypeInference.FunctionTransform("uuid(", ")")
8587
}
8688
"infer geometry types" in {
8789
val types = inferRowTypes(Seq(point, lineString, polygon, multiPoint, multiLineString, multiPolygon, geometryCollection))

geomesa-convert/geomesa-convert-common/src/test/scala/org/locationtech/geomesa/convert2/transforms/ExpressionTest.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ class ExpressionTest extends Specification {
502502
res must not(beNull)
503503
res.getClass mustEqual classOf[String]
504504
}
505+
"parse uuids" >> {
506+
val exp = Expression("uuid($0)")
507+
val res = exp.apply(Array("5ff7db5a-64e1-4459-840e-e994f293dace"))
508+
res mustEqual java.util.UUID.fromString("5ff7db5a-64e1-4459-840e-e994f293dace")
509+
}
505510
"generate z3 uuids" >> {
506511
val exp = Expression("uuidZ3($0, $1, 'week')")
507512
val geom = WKTUtils.read("POINT (103 1)")

0 commit comments

Comments
 (0)