Skip to content

Commit dff85b8

Browse files
authored
Merge pull request #787 from sekikn/786-add-decimal-scale-option
Add a new option to the SQL generator for specifying decimal's scale
2 parents 4c49c34 + 8d195b2 commit dff85b8

File tree

7 files changed

+49
-9
lines changed

7 files changed

+49
-9
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ public AspectModelDatabricksDenormalizedSqlVisitor( final DatabricksSqlGeneratio
8787
databricksTypeMap = ImmutableMap.<String, DatabricksType> builder()
8888
.put( XSD.xstring.getURI(), DatabricksType.STRING )
8989
.put( XSD.xboolean.getURI(), DatabricksType.BOOLEAN )
90-
.put( XSD.decimal.getURI(), new DatabricksType.DatabricksDecimal( Optional.of( config.decimalPrecision() ) ) )
91-
.put( XSD.integer.getURI(), new DatabricksType.DatabricksDecimal() )
90+
.put( XSD.decimal.getURI(), new DatabricksType.DatabricksDecimal( Optional.of( config.decimalPrecision() ),
91+
Optional.of( config.decimalScale() ) ) )
92+
.put( XSD.integer.getURI(), new DatabricksType.DatabricksDecimal( Optional.of( config.decimalPrecision() ) ) )
9293
.put( XSD.xdouble.getURI(), DatabricksType.DOUBLE )
9394
.put( XSD.xfloat.getURI(), DatabricksType.FLOAT )
9495
.put( XSD.date.getURI(), DatabricksType.STRING )
@@ -110,7 +111,7 @@ public AspectModelDatabricksDenormalizedSqlVisitor( final DatabricksSqlGeneratio
110111
.put( XSD.unsignedByte.getURI(), DatabricksType.SMALLINT )
111112
.put( XSD.unsignedShort.getURI(), DatabricksType.INT )
112113
.put( XSD.unsignedInt.getURI(), DatabricksType.BIGINT )
113-
.put( XSD.unsignedLong.getURI(), new DatabricksType.DatabricksDecimal() )
114+
.put( XSD.unsignedLong.getURI(), new DatabricksType.DatabricksDecimal( Optional.of( config.decimalPrecision() ) ) )
114115
.put( XSD.positiveInteger.getURI(), new DatabricksType.DatabricksDecimal( Optional.of( config.decimalPrecision() ) ) )
115116
.put( XSD.nonNegativeInteger.getURI(), new DatabricksType.DatabricksDecimal( Optional.of( config.decimalPrecision() ) ) )
116117
.put( XSD.negativeInteger.getURI(), new DatabricksType.DatabricksDecimal( Optional.of( config.decimalPrecision() ) ) )

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,23 @@ public record DatabricksSqlGenerationConfig(
3838
boolean includeColumnComments,
3939
Locale commentLanguage,
4040
int decimalPrecision,
41+
int decimalScale,
4142
List<DatabricksColumnDefinition> customColumns
4243
) implements SqlGenerationConfig.DialectSpecificConfig {
4344
public static final String DEFAULT_TABLE_COMMAND_PREFIX = "CREATE TABLE IF NOT EXISTS";
4445
// As defined in https://docs.databricks.com/en/sql/language-manual/data-types/decimal-type.html
4546
public static final int DECIMAL_DEFAULT_PRECISION = 10;
4647
// As defined in https://docs.databricks.com/en/sql/language-manual/data-types/decimal-type.html
4748
public static final int DECIMAL_MAX_PRECISION = 38;
49+
// As defined in https://docs.databricks.com/en/sql/language-manual/data-types/decimal-type.html
50+
public static final int DECIMAL_DEFAULT_SCALE = 0;
4851
public static final boolean DEFAULT_INCLUDE_TABLE_COMMENT = true;
4952
public static final boolean DEFAULT_INCLUDE_COLUMN_COMMENTS = true;
5053
public static final Locale DEFAULT_COMMENT_LANGUAGE = Locale.ENGLISH;
5154

5255
public DatabricksSqlGenerationConfig() {
5356
this( DEFAULT_TABLE_COMMAND_PREFIX, DEFAULT_INCLUDE_TABLE_COMMENT, DEFAULT_INCLUDE_COLUMN_COMMENTS, DEFAULT_COMMENT_LANGUAGE,
54-
DECIMAL_DEFAULT_PRECISION, List.of() );
57+
DECIMAL_DEFAULT_PRECISION, DECIMAL_DEFAULT_SCALE, List.of() );
5558
}
5659

5760
public DatabricksSqlGenerationConfig {
@@ -64,6 +67,12 @@ public DatabricksSqlGenerationConfig() {
6467
if ( decimalPrecision > DECIMAL_MAX_PRECISION ) {
6568
decimalPrecision = DECIMAL_MAX_PRECISION;
6669
}
70+
if ( decimalScale < 0 ) {
71+
decimalScale = DECIMAL_DEFAULT_SCALE;
72+
}
73+
if ( decimalScale > decimalPrecision ) {
74+
decimalScale = decimalPrecision;
75+
}
6776
if ( commentLanguage == null ) {
6877
commentLanguage = DEFAULT_COMMENT_LANGUAGE;
6978
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.eclipse.esmf.test.TestAspect;
2525
import org.eclipse.esmf.test.TestResources;
2626

27+
import org.junit.jupiter.api.Test;
2728
import org.junit.jupiter.params.ParameterizedTest;
2829
import org.junit.jupiter.params.provider.EnumSource;
2930

@@ -70,4 +71,22 @@ void testDatabricksGenerationExcludeComments( final TestAspect testAspect ) {
7071
assertThat( result ).doesNotContain( "COMMENT" );
7172
} ).doesNotThrowAnyException();
7273
}
74+
75+
@Test
76+
void testDatabricksGenerationDecimalPrecisionAndScale() {
77+
final Aspect aspect = TestResources.load( TestAspect.ASPECT_WITH_SIMPLE_TYPES ).aspect();
78+
assertThatCode( () -> {
79+
final DatabricksSqlGenerationConfig dialectSpecificConfig = DatabricksSqlGenerationConfigBuilder.builder()
80+
.decimalPrecision( 38 )
81+
.decimalScale( 38 )
82+
.build();
83+
final SqlArtifact sqlArtifact = new AspectModelSqlGenerator( aspect, SqlGenerationConfigBuilder.builder()
84+
.dialect( SqlGenerationConfig.Dialect.DATABRICKS )
85+
.dialectSpecificConfig( dialectSpecificConfig )
86+
.build() ).singleResult();
87+
final String result = sqlArtifact.getContent();
88+
89+
assertThat( result ).contains( "DECIMAL(38,38)" );
90+
} ).doesNotThrowAnyException();
91+
}
7392
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void testAspectWithEntityInstanceWithScalarListProperty() {
189189
assertThat( sql( TestAspect.ASPECT_WITH_ENTITY_INSTANCE_WITH_SCALAR_LIST_PROPERTY ) ).isEqualTo( """
190190
CREATE TABLE IF NOT EXISTS aspect_with_entity_instance_with_scalar_list_property (
191191
test_property__code SMALLINT NOT NULL,
192-
test_property__test_list ARRAY<DECIMAL> NOT NULL
192+
test_property__test_list ARRAY<DECIMAL(10)> NOT NULL
193193
)
194194
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithEntityInstanceWithScalarListProperty');
195195
""" );
@@ -369,7 +369,7 @@ CREATE TABLE IF NOT EXISTS aspect_with_operation (
369369
void testAspectWithOptionalProperties() {
370370
assertThat( sql( TestAspect.ASPECT_WITH_OPTIONAL_PROPERTIES ) ).isEqualTo( """
371371
CREATE TABLE IF NOT EXISTS aspect_with_optional_properties (
372-
number_property DECIMAL,
372+
number_property DECIMAL(10),
373373
timestamp_property TIMESTAMP NOT NULL
374374
)
375375
TBLPROPERTIES ('x-samm-aspect-model-urn'='urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithOptionalProperties');
@@ -446,7 +446,7 @@ CREATE TABLE IF NOT EXISTS aspect_with_simple_types (
446446
date_time_property TIMESTAMP NOT NULL,
447447
date_time_stamp_property TIMESTAMP NOT NULL,
448448
day_time_duration STRING NOT NULL,
449-
decimal_property DECIMAL(10) NOT NULL,
449+
decimal_property DECIMAL(10,0) NOT NULL,
450450
double_property DOUBLE NOT NULL,
451451
duration_property STRING NOT NULL,
452452
float_property FLOAT NOT NULL,
@@ -457,7 +457,7 @@ decimal_property DECIMAL(10) NOT NULL,
457457
g_year_property STRING NOT NULL,
458458
hex_binary_property BINARY NOT NULL,
459459
int_property INT NOT NULL,
460-
integer_property DECIMAL NOT NULL,
460+
integer_property DECIMAL(10) NOT NULL,
461461
lang_string_property STRING NOT NULL,
462462
long_property BIGINT NOT NULL,
463463
negative_integer_property DECIMAL(10) NOT NULL,
@@ -469,7 +469,7 @@ positive_integer_property DECIMAL(10) NOT NULL,
469469
time_property STRING NOT NULL,
470470
unsigned_byte_property SMALLINT NOT NULL,
471471
unsigned_int_property BIGINT NOT NULL,
472-
unsigned_long_property DECIMAL NOT NULL,
472+
unsigned_long_property DECIMAL(10) NOT NULL,
473473
unsigned_short_property INT NOT NULL,
474474
year_month_duration_property STRING NOT NULL
475475
)

documentation/developer-guide/modules/tooling-guide/examples/GenerateSql.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void generate() {
4949
.includeTableComment( true ) // optional
5050
.includeColumnComments( true ) // optional
5151
.decimalPrecision( 10 ) // optional
52+
.decimalScale( 0 ) // optional
5253
.customColumns( List.of( // optional
5354
DatabricksColumnDefinitionBuilder.builder()
5455
.name( "custom_column" )

documentation/developer-guide/modules/tooling-guide/pages/samm-cli.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ of model resolution] for more information.
196196
| _--decimal-precision, -dp_ : The precision to use for Databricks decimal columns
197197
(default: 10). See also notes in
198198
the xref:java-aspect-tooling.adoc#databricks-type-mapping[Databricks type mapping]. |
199+
| _--decimal-scale, -ds_ : The scale to use for Databricks decimal columns
200+
(default: 0). See also notes in
201+
the xref:java-aspect-tooling.adoc#databricks-type-mapping[Databricks type mapping]. |
199202
| _--custom-column, -col_ : Additional custom column definition, e.g. for databricks following the pattern `column_name DATATYPE [NOT NULL] [COMMENT 'custom']`. This parameter can be repeated for multiple columns. | `samm aspect AspectModel.ttl to sql --custom-column "column_name STRING NOT NULL COMMENT 'custom'"`
200203
.5+| [[aspect-to-aas]] aspect <model> to aas | Generate an Asset Administration Shell (AAS) submodel template from an
201204
Aspect Model | `samm aspect AspectModel.ttl to aas`

tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/to/AspectToSqlCommand.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ public class AspectToSqlCommand extends AbstractCommand {
8989
description = "The precision to use for Databricks decimal columns, between 1 and 38. (default: ${DEFAULT-VALUE})" )
9090
private int decimalPrecision = DatabricksSqlGenerationConfig.DECIMAL_DEFAULT_PRECISION;
9191

92+
@CommandLine.Option(
93+
names = { "--decimal-scale", "-ds" },
94+
description =
95+
"The scale to use for Databricks decimal columns, between 0 and the value of precision. (default: ${DEFAULT-VALUE})" )
96+
private int decimalScale = DatabricksSqlGenerationConfig.DECIMAL_DEFAULT_SCALE;
97+
9298
@CommandLine.Option(
9399
names = { "--custom-column", "-col" },
94100
description = "Custom column to add to the table, can be repeated for multiple columns",
@@ -130,6 +136,7 @@ public void run() {
130136
.includeColumnComments( includeColumnComments )
131137
.createTableCommandPrefix( tableCommandPrefix )
132138
.decimalPrecision( decimalPrecision )
139+
.decimalScale( decimalScale )
133140
.customColumns( customColumns )
134141
.build();
135142
final SqlGenerationConfig sqlConfig = new SqlGenerationConfig( dialect, strategy, generatorConfig );

0 commit comments

Comments
 (0)