From 157c6174d2a1855ecb187ce34aa8bf5a678784ef Mon Sep 17 00:00:00 2001 From: Oguzhan Unlu Date: Mon, 9 Sep 2024 16:35:04 +0300 Subject: [PATCH] Prepend underscore for parquet fields starting with number --- .../iglu.schemaddl/parquet/Field.scala | 10 +++++++--- .../iglu/schemaddl/parquet/FieldSpec.scala | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/core/src/main/scala/com.snowplowanalytics/iglu.schemaddl/parquet/Field.scala b/modules/core/src/main/scala/com.snowplowanalytics/iglu.schemaddl/parquet/Field.scala index d33414fe..270cc35c 100644 --- a/modules/core/src/main/scala/com.snowplowanalytics/iglu.schemaddl/parquet/Field.scala +++ b/modules/core/src/main/scala/com.snowplowanalytics/iglu.schemaddl/parquet/Field.scala @@ -68,9 +68,13 @@ object Field { .map(endMap(_)) } - private[parquet] def normalizeName(field: Field): String = - StringUtils.snakeCase - .andThen(replaceDisallowedCharacters)(field.name) + // Normalize field name and prepend underscore if name starts with a number + private[parquet] def normalizeName(field: Field): String = { + val normalized = StringUtils.snakeCase.andThen(replaceDisallowedCharacters)(field.name) + if (normalized.nonEmpty && normalized.charAt(0).isDigit) s"_$normalized" + else normalized + } + /** * Replaces disallowed parquet column characters with underscore diff --git a/modules/core/src/test/scala/com/snowplowanalytics/iglu/schemaddl/parquet/FieldSpec.scala b/modules/core/src/test/scala/com/snowplowanalytics/iglu/schemaddl/parquet/FieldSpec.scala index 899b2fe7..0d4aae20 100644 --- a/modules/core/src/test/scala/com/snowplowanalytics/iglu/schemaddl/parquet/FieldSpec.scala +++ b/modules/core/src/test/scala/com/snowplowanalytics/iglu/schemaddl/parquet/FieldSpec.scala @@ -375,11 +375,11 @@ class FieldSpec extends org.specs2.Specification { def is = s2""" (fieldNormalName("test1Test2Test3") must beEqualTo("test1_test2_test3")) and (fieldNormalName("Test1Test2TEST3") must beEqualTo("test1_test2_test3")) and (fieldNormalName("test1,test2.test3;test4") must beEqualTo("test1_test2_test3_test4")) and - (fieldNormalName("1test1,test2.test3;test4") must beEqualTo("1test1_test2_test3_test4")) and + (fieldNormalName("1test1,test2.test3;test4") must beEqualTo("_1test1_test2_test3_test4")) and (fieldNormalName("_50test1,test2.test3;test4") must beEqualTo("_50test1_test2_test3_test4")) and (fieldNormalName("_.test1,test2.test3;test4") must beEqualTo("__test1_test2_test3_test4")) and (fieldNormalName(",.;:") must beEqualTo("____")) and - (fieldNormalName("1test1,Test2Test3Test4.test5;test6") must beEqualTo("1test1_test2_test3_test4_test5_test6")) + (fieldNormalName("1test1,Test2Test3Test4.test5;test6") must beEqualTo("_1test1_test2_test3_test4_test5_test6")) } def e14 = {