Skip to content

Commit b36167b

Browse files
committed
Fix Databricks SQL generation to work with multiline comments
1 parent 2d80da9 commit b36167b

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/sql/databricks/AspectModelDatabricksDenormalizedSqlVisitor.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,12 @@ public String visitStructureElement( final StructureElement structureElement, fi
165165
return result.toString();
166166
}
167167

168-
private String escapeComment( final String comment ) {
169-
return comment.replace( "'", "\\'" );
170-
}
171-
172168
@Override
173169
public String visitAspect( final Aspect aspect, final Context context ) {
174-
175170
final String columnDeclarations = visitStructureElement( aspect, context );
176171
final String comment = config.includeTableComment()
177172
? Optional.ofNullable( aspect.getDescription( config.commentLanguage() ) ).map( description ->
178-
"COMMENT '" + escapeComment( description ) + "'\n" ).orElse( "" )
173+
new DatabricksCommentDefinition( description ) + "\n" ).orElse( "" )
179174
: "";
180175
return "%s %s (\n%s%s)\n%sTBLPROPERTIES ('%s'='%s');\n".formatted(
181176
config.createTableCommandPrefix(),
@@ -242,7 +237,7 @@ public String visitCharacteristic( final Characteristic characteristic, final Co
242237

243238
private String column( final String columnName, final String columnType, final boolean isNullable, final Optional<String> comment ) {
244239
return "%s %s%s".formatted( columnName, columnType, isNullable ? "" : " NOT NULL" )
245-
+ comment.map( args -> " COMMENT '%s'".formatted( escapeComment( args ) ) ).orElse( "" );
240+
+ comment.map( args -> " " + new DatabricksCommentDefinition( args ) ).orElse( "" );
246241
}
247242

248243
@Override

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/sql/databricks/DatabricksColumnDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ public String toString() {
3838
name(),
3939
type(),
4040
nullable ? "" : " NOT NULL",
41-
comment.map( c -> " COMMENT '" + c.replaceAll( "'", "\\\\'" ) + "'" ).orElse( "" ) );
41+
comment.map( theComment -> " " + new DatabricksCommentDefinition( theComment ) ).orElse( "" ) );
4242
}
4343
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2025 Robert Bosch Manufacturing Solutions GmbH
3+
*
4+
* See the AUTHORS file(s) distributed with this work for additional
5+
* information regarding authorship.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
10+
*
11+
* SPDX-License-Identifier: MPL-2.0
12+
*/
13+
14+
package org.eclipse.esmf.aspectmodel.generator.sql.databricks;
15+
16+
public record DatabricksCommentDefinition( String comment ) {
17+
@Override
18+
public String toString() {
19+
return "COMMENT '" + comment
20+
.replace( "'", "\\'" )
21+
.replace( "\n", "\\n" )
22+
+ "'";
23+
}
24+
}

core/esmf-aspect-model-document-generators/src/main/java/org/eclipse/esmf/aspectmodel/generator/sql/databricks/DatabricksType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ record DatabricksStructEntry( String name, DatabricksType type, boolean nullable
8080
@Override
8181
public String toString() {
8282
return name + ": " + type + ( nullable ? "" : " NOT NULL" )
83-
+ comment.map( c -> " COMMENT '%s'".formatted( c.replace( "'", "\\'" ) ) ).orElse( "" );
83+
+ comment.map( theComment -> " " + new DatabricksCommentDefinition( theComment ) ).orElse( "" );
8484
}
8585
}
8686

core/esmf-aspect-model-document-generators/src/test/java/org/eclipse/esmf/aspectmodel/generator/sql/databricks/DatabricksColumnDefinitionParserTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ void testMinimalDefinition() {
3232
}
3333

3434
@ParameterizedTest
35-
@EnumSource( value = TestAspect.class, mode = EnumSource.Mode.EXCLUDE, names = {
36-
"ASPECT_WITH_MARKDOWN_DESCRIPTION"
37-
} )
35+
@EnumSource( value = TestAspect.class )
3836
void testParseSqlForAspectModel( final TestAspect testAspect ) {
3937
final String sql = sql( testAspect );
4038
final String parsedAndSerializedSql = sql.lines()

0 commit comments

Comments
 (0)