From cc86e19b0fb43b51939d463bc4c748a84a79cea4 Mon Sep 17 00:00:00 2001 From: Johnny Schmidt Date: Mon, 23 Dec 2024 11:33:05 -0800 Subject: [PATCH] Destination S3V2: Fix: Avro names starting with numbers --- .../airbyte/cdk/load/data/Transformations.kt | 9 +++++++- .../cdk/load/data/TransformationsTest.kt | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 airbyte-cdk/bulk/core/load/src/test/kotlin/io/airbyte/cdk/load/data/TransformationsTest.kt diff --git a/airbyte-cdk/bulk/core/load/src/main/kotlin/io/airbyte/cdk/load/data/Transformations.kt b/airbyte-cdk/bulk/core/load/src/main/kotlin/io/airbyte/cdk/load/data/Transformations.kt index f3169e1e51a7..c5a82d6dc808 100644 --- a/airbyte-cdk/bulk/core/load/src/main/kotlin/io/airbyte/cdk/load/data/Transformations.kt +++ b/airbyte-cdk/bulk/core/load/src/main/kotlin/io/airbyte/cdk/load/data/Transformations.kt @@ -42,6 +42,13 @@ class Transformations { .joinToString(separator = ".") } - fun toAvroSafeName(name: String) = toAlphanumericAndUnderscore(name) + fun toAvroSafeName(name: String): String { + val stripped = toAlphanumericAndUnderscore(name) + return if (stripped.substring(0, 1).matches("[A-Za-z_]".toRegex())) { + stripped + } else { + "_$stripped" + } + } } } diff --git a/airbyte-cdk/bulk/core/load/src/test/kotlin/io/airbyte/cdk/load/data/TransformationsTest.kt b/airbyte-cdk/bulk/core/load/src/test/kotlin/io/airbyte/cdk/load/data/TransformationsTest.kt new file mode 100644 index 000000000000..2197f534e2bd --- /dev/null +++ b/airbyte-cdk/bulk/core/load/src/test/kotlin/io/airbyte/cdk/load/data/TransformationsTest.kt @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 Airbyte, Inc., all rights reserved. + */ + +package io.airbyte.cdk.load.data + +import org.junit.jupiter.api.Test + +class TransformationsTest { + @Test + fun `test avro illegal start character`() { + val unsafeName = "1d_view" + assert(Transformations.toAvroSafeName(unsafeName) == "_1d_view") + } + + @Test + fun `test avro special characters`() { + val unsafeName = "1d_view!@#$%^&*()" + assert(Transformations.toAvroSafeName(unsafeName) == "_1d_view__________") + } +}