Skip to content

Commit

Permalink
Prepend underscore for parquet fields starting with number
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzhanunlu committed Sep 9, 2024
1 parent 879ccc5 commit 6c989fd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 6c989fd

Please sign in to comment.