Skip to content

Commit db7ff8a

Browse files
authored
Merge branch 'main' into 654-fixed-point-constraint-scale-is-not-validated-by-the-json-schema
2 parents c91d38b + dfee56f commit db7ff8a

File tree

3 files changed

+93
-17
lines changed

3 files changed

+93
-17
lines changed

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

+60-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.eclipse.esmf.metamodel.characteristic.Collection;
3636
import org.eclipse.esmf.metamodel.characteristic.Either;
3737
import org.eclipse.esmf.metamodel.characteristic.Trait;
38+
import org.eclipse.esmf.metamodel.characteristic.impl.DefaultList;
3839
import org.eclipse.esmf.metamodel.vocabulary.SAMM;
3940
import org.eclipse.esmf.samm.KnownVersion;
4041

@@ -170,6 +171,7 @@ private String escapeComment( final String comment ) {
170171

171172
@Override
172173
public String visitAspect( final Aspect aspect, final Context context ) {
174+
173175
final String columnDeclarations = visitStructureElement( aspect, context );
174176
final String comment = config.includeTableComment()
175177
? Optional.ofNullable( aspect.getDescription( config.commentLanguage() ) ).map( description ->
@@ -194,7 +196,7 @@ public String visitProperty( final Property property, final Context context ) {
194196
}
195197

196198
return property.getCharacteristic().get().accept( this, context.copy()
197-
.prefix( ( context.prefix().isEmpty() ? "" : context.prefix() + LEVEL_DELIMITER ) + columnName( property ) )
199+
.prefix( (context.prefix().isEmpty() ? "" : context.prefix() + LEVEL_DELIMITER) + columnName( property ) )
198200
.currentProperty( property )
199201
.build() );
200202
}
@@ -256,11 +258,63 @@ public String visitCollection( final Collection collection, final Context contex
256258
? Optional.ofNullable( Optional.ofNullable( context.forceDescriptionFromElement() ).orElse( property )
257259
.getDescription( config.commentLanguage() ) )
258260
: Optional.empty();
259-
final String typeDef = type.isComplexType()
260-
? entityToStruct( type.as( ComplexType.class ), false ).toString()
261-
: type.accept( this, context );
262-
return column( context.prefix(), "ARRAY<" + typeDef + ">", property.isOptional() || context.forceOptional(),
263-
comment );
261+
262+
if ( type.isComplexType() ) {
263+
// Flattening collections of complex types
264+
final ComplexType complexType = type.as( ComplexType.class );
265+
collection.as( Collection.class ).getCollectionType();
266+
return processComplexType( complexType, context, context.prefix(), collection.is( DefaultList.class ) );
267+
} else {
268+
// Handle scalar types normally
269+
final String typeDef = type.accept( this, context );
270+
return column(
271+
context.prefix(),
272+
"ARRAY<" + typeDef + ">",
273+
property.isOptional() || context.forceOptional(),
274+
comment
275+
);
276+
}
277+
}
278+
279+
private String processComplexType( final ComplexType entity, final Context context, final String parentPrefix,
280+
final boolean isDefaultList ) {
281+
StringBuilder columns = new StringBuilder();
282+
final String lineDelimiter = ",\n ";
283+
284+
entity.getAllProperties().forEach( property -> {
285+
if ( property.getDataType().isEmpty() || property.isNotInPayload() ) {
286+
return; // Skip properties with no data type or not in payload
287+
}
288+
289+
final Type type = property.getDataType().get();
290+
String columnPrefix = columnName( property );
291+
292+
if ( parentPrefix.contains( LEVEL_DELIMITER ) ) {
293+
columnPrefix = parentPrefix + LEVEL_DELIMITER + columnName( property );
294+
295+
columns.append( column( columnPrefix + "_id", "BIGINT", false, Optional.empty() ) )
296+
.append( lineDelimiter );
297+
}
298+
299+
if ( isDefaultList && !parentPrefix.contains( LEVEL_DELIMITER ) ) {
300+
columnPrefix = parentPrefix + LEVEL_DELIMITER + columnName( property );
301+
}
302+
303+
if ( type instanceof Scalar ) {
304+
final String typeDef = type.accept( this, context );
305+
columns.append( column( columnPrefix, typeDef, property.isOptional(),
306+
Optional.ofNullable( property.getDescription( config.commentLanguage() ) ) ) )
307+
.append( lineDelimiter );
308+
} else if ( type instanceof ComplexType ) {
309+
columns.append( processComplexType( type.as( ComplexType.class ), context, columnPrefix, type.is( DefaultList.class ) ) );
310+
}
311+
} );
312+
313+
if ( !columns.isEmpty() && columns.toString().endsWith( ",\n " ) ) {
314+
columns.setLength( columns.length() - 4 ); // Remove last ",\n "
315+
}
316+
317+
return columns.toString();
264318
}
265319

266320
private DatabricksType.DatabricksStruct entityToStruct( final ComplexType entity, final boolean isInsideNestedType ) {

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

+32-10
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ CREATE TABLE IF NOT EXISTS aspect_with_collections_with_element_characteristic_a
7373
void testAspectWithCollectionAndElementCharacteristic() {
7474
assertThat( sql( TestAspect.ASPECT_WITH_COLLECTION_AND_ELEMENT_CHARACTERISTIC ) ).isEqualTo( """
7575
CREATE TABLE IF NOT EXISTS aspect_with_collection_and_element_characteristic (
76-
items ARRAY<STRUCT<test_property: STRING NOT NULL>> NOT NULL
76+
test_property STRING NOT NULL
7777
)
7878
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithCollectionAndElementCharacteristic');
7979
""" );
@@ -106,7 +106,8 @@ CREATE TABLE IF NOT EXISTS aspect_with_complex_collection_enum (
106106
void testAspectWithComplexEntityCollectionEnum() {
107107
assertThat( sql( TestAspect.ASPECT_WITH_COMPLEX_ENTITY_COLLECTION_ENUM ) ).isEqualTo( """
108108
CREATE TABLE IF NOT EXISTS aspect_with_complex_entity_collection_enum (
109-
my_property_one__entity_property_one ARRAY<STRUCT<entity_property_two: STRING NOT NULL>> NOT NULL
109+
my_property_one__entity_property_one__entity_property_two_id BIGINT NOT NULL,
110+
my_property_one__entity_property_one__entity_property_two STRING NOT NULL
110111
)
111112
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithComplexEntityCollectionEnum');
112113
""" );
@@ -198,7 +199,11 @@ CREATE TABLE IF NOT EXISTS aspect_with_entity_instance_with_scalar_list_property
198199
void testAspectWithEntityList() {
199200
assertThat( sql( TestAspect.ASPECT_WITH_ENTITY_LIST ) ).isEqualTo( """
200201
CREATE TABLE IF NOT EXISTS aspect_with_entity_list (
201-
test_list ARRAY<STRUCT<test_string: STRING NOT NULL, test_int: INT NOT NULL, test_float: FLOAT NOT NULL, test_local_date_time: TIMESTAMP NOT NULL, random_value: STRING NOT NULL>> NOT NULL
202+
test_list__test_string STRING NOT NULL,
203+
test_list__test_int INT NOT NULL,
204+
test_list__test_float FLOAT NOT NULL,
205+
test_list__test_local_date_time TIMESTAMP NOT NULL,
206+
test_list__random_value STRING NOT NULL
202207
)
203208
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithEntityList');
204209
""" );
@@ -209,7 +214,8 @@ void testAspectWithEntityWithNestedEntityListProperty() {
209214
assertThat( sql( TestAspect.ASPECT_WITH_ENTITY_WITH_NESTED_ENTITY_LIST_PROPERTY ) ).isEqualTo( """
210215
CREATE TABLE IF NOT EXISTS aspect_with_entity_with_nested_entity_list_property (
211216
test_property__code SMALLINT NOT NULL,
212-
test_property__test_list ARRAY<STRUCT<nested_entity_property: STRING NOT NULL>> NOT NULL
217+
test_property__test_list__nested_entity_property_id BIGINT NOT NULL,
218+
test_property__test_list__nested_entity_property STRING NOT NULL
213219
)
214220
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithEntityWithNestedEntityListProperty');
215221
""" );
@@ -219,7 +225,8 @@ CREATE TABLE IF NOT EXISTS aspect_with_entity_with_nested_entity_list_property (
219225
void testAspectWithExtendedEntity() {
220226
assertThat( sql( TestAspect.ASPECT_WITH_EXTENDED_ENTITY ) ).isEqualTo( """
221227
CREATE TABLE IF NOT EXISTS aspect_with_extended_entity (
222-
test_property ARRAY<STRUCT<parent_string: STRING NOT NULL, parent_of_parent_string: STRING NOT NULL>> NOT NULL COMMENT 'This is a test property.'
228+
parent_string STRING NOT NULL,
229+
parent_of_parent_string STRING NOT NULL
223230
)
224231
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithExtendedEntity');
225232
""" );
@@ -308,8 +315,16 @@ CREATE TABLE IF NOT EXISTS aspect_with_multiple_entities_on_multiple_levels (
308315
void testAspectWithMultipleEntityCollections() {
309316
assertThat( sql( TestAspect.ASPECT_WITH_MULTIPLE_ENTITY_COLLECTIONS ) ).isEqualTo( """
310317
CREATE TABLE IF NOT EXISTS aspect_with_multiple_entity_collections (
311-
test_list_one ARRAY<STRUCT<test_string: STRING NOT NULL, test_int: INT NOT NULL, test_float: FLOAT NOT NULL, test_local_date_time: TIMESTAMP NOT NULL, random_value: STRING NOT NULL>> NOT NULL,
312-
test_list_two ARRAY<STRUCT<test_string: STRING NOT NULL, test_int: INT NOT NULL, test_float: FLOAT NOT NULL, test_local_date_time: TIMESTAMP NOT NULL, random_value: STRING NOT NULL>> NOT NULL
318+
test_list_one__test_string STRING NOT NULL,
319+
test_list_one__test_int INT NOT NULL,
320+
test_list_one__test_float FLOAT NOT NULL,
321+
test_list_one__test_local_date_time TIMESTAMP NOT NULL,
322+
test_list_one__random_value STRING NOT NULL,
323+
test_list_two__test_string STRING NOT NULL,
324+
test_list_two__test_int INT NOT NULL,
325+
test_list_two__test_float FLOAT NOT NULL,
326+
test_list_two__test_local_date_time TIMESTAMP NOT NULL,
327+
test_list_two__random_value STRING NOT NULL
313328
)
314329
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithMultipleEntityCollections');
315330
""" );
@@ -319,7 +334,13 @@ CREATE TABLE IF NOT EXISTS aspect_with_multiple_entity_collections (
319334
void testAspectWithNestedEntityList() {
320335
assertThat( sql( TestAspect.ASPECT_WITH_NESTED_ENTITY_LIST ) ).isEqualTo( """
321336
CREATE TABLE IF NOT EXISTS aspect_with_nested_entity_list (
322-
test_list ARRAY<STRUCT<test_string: STRING NOT NULL, test_int: INT NOT NULL, test_float: FLOAT NOT NULL, test_second_list: STRUCT<test_local_date_time: TIMESTAMP, random_value: STRING> NOT NULL>> NOT NULL
337+
test_list__test_string STRING NOT NULL,
338+
test_list__test_int INT NOT NULL,
339+
test_list__test_float FLOAT NOT NULL,
340+
test_list__test_second_list__test_local_date_time_id BIGINT NOT NULL,
341+
test_list__test_second_list__test_local_date_time TIMESTAMP NOT NULL,
342+
test_list__test_second_list__random_value_id BIGINT NOT NULL,
343+
test_list__test_second_list__random_value STRING NOT NULL
323344
)
324345
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithNestedEntityList');
325346
""" );
@@ -471,7 +492,8 @@ CREATE TABLE IF NOT EXISTS aspect_with_sorted_set (
471492
void testAspectWithTimeSeries() {
472493
assertThat( sql( TestAspect.ASPECT_WITH_TIME_SERIES ) ).isEqualTo( """
473494
CREATE TABLE IF NOT EXISTS aspect_with_time_series (
474-
test_property ARRAY<STRUCT<value: STRING NOT NULL COMMENT 'The value that was recorded and is part of a time series.', timestamp: TIMESTAMP NOT NULL COMMENT 'The specific point in time when the corresponding value was recorded.'>> NOT NULL COMMENT 'This is a test property.'
495+
value STRING NOT NULL COMMENT 'The value that was recorded and is part of a time series.',
496+
timestamp TIMESTAMP NOT NULL COMMENT 'The specific point in time when the corresponding value was recorded.'
475497
)
476498
COMMENT 'This is a test description'
477499
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithTimeSeries');
@@ -482,7 +504,7 @@ CREATE TABLE IF NOT EXISTS aspect_with_time_series (
482504
void testAspectWithComplexSet() {
483505
assertThat( sql( TestAspect.ASPECT_WITH_COMPLEX_SET ) ).isEqualTo( """
484506
CREATE TABLE IF NOT EXISTS aspect_with_complex_set (
485-
test_property ARRAY<STRUCT<product_id: STRING NOT NULL>> NOT NULL COMMENT 'This is a test property.'
507+
product_id STRING NOT NULL
486508
)
487509
COMMENT 'This is a test description'
488510
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithComplexSet');

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.junit.jupiter.params.ParameterizedTest;
2424
import org.junit.jupiter.params.provider.EnumSource;
2525

26-
public class DatabricksColumnDefinitionParserTest extends DatabricksTestBase {
26+
class DatabricksColumnDefinitionParserTest extends DatabricksTestBase {
2727
@Test
2828
void testMinimalDefinition() {
2929
final DatabricksColumnDefinition definition = new DatabricksColumnDefinitionParser( "abc STRING" ).get();

0 commit comments

Comments
 (0)