@@ -136,33 +136,10 @@ public Schema getOutputSchema(SchemaResolutionContext context) {
136
136
Integer outputPrecision = precision ;
137
137
if (type .equalsIgnoreCase ("decimal" ) && field .getSchema ().isNullable ()) {
138
138
Schema fieldSchema = field .getSchema ().getNonNullable ();
139
- Pair <Integer , Integer > scaleAndPrecision = getPrecisionAndScale (fieldSchema );
140
- Integer inputSchemaScale = scaleAndPrecision .getSecond ();
141
- Integer inputSchemaPrecision = scaleAndPrecision .getFirst ();
142
-
143
- if (scale == null && precision == null ) {
144
- outputScale = inputSchemaScale ;
145
- outputPrecision = inputSchemaPrecision ;
146
- } else if (scale == null && inputSchemaScale != null ) {
147
- if (precision - inputSchemaScale < 1 ) {
148
- throw new DirectiveParseException (String .format (
149
- "Cannot set scale as '%s' and precision as '%s' when "
150
- + "given precision - scale is less than 1 " , inputSchemaScale ,
151
- precision ));
152
- }
153
- outputScale = inputSchemaScale ;
154
- outputPrecision = precision ;
155
-
156
- } else if (precision == null && inputSchemaPrecision != null ) {
157
- if (inputSchemaPrecision - scale < 1 ) {
158
- throw new DirectiveParseException (String .format (
159
- "Cannot set scale as '%s' and precision as '%s' when "
160
- + "given precision - scale is less than 1 " , scale ,
161
- inputSchemaPrecision ));
162
- }
163
- outputScale = scale ;
164
- outputPrecision = inputSchemaPrecision ;
165
- }
139
+ Pair <Integer , Integer > scaleAndPrecision = getValidatedPrecisionAndScale (
140
+ fieldSchema , precision , scale );
141
+ outputScale = scaleAndPrecision .getSecond ();
142
+ outputPrecision = scaleAndPrecision .getFirst ();
166
143
}
167
144
return Schema .Field .of (col , ColumnConverter .getSchemaForType (type ,
168
145
outputScale , outputPrecision ));
@@ -173,20 +150,50 @@ public Schema getOutputSchema(SchemaResolutionContext context) {
173
150
}
174
151
}
175
152
)
176
- .collect (Collectors .toList ())
153
+ .collect (Collectors .toList ())
177
154
);
178
155
}
179
156
180
157
/**
181
158
* extracts precision and scale from schema string
182
159
*/
183
- public static Pair <Integer , Integer > getPrecisionAndScale (Schema fieldSchema ) {
184
- Integer precision = null ;
185
- Integer scale = null ;
186
- if (fieldSchema .getLogicalType () == LogicalType .DECIMAL ) {
187
- precision = fieldSchema .getPrecision ();
188
- scale = fieldSchema .getScale ();
160
+ public static Pair <Integer , Integer > getValidatedPrecisionAndScale (Schema fieldSchema ,
161
+ Integer precision , Integer scale )
162
+ throws DirectiveParseException { //check precision and scale
163
+ Integer outputPrecision = precision ;
164
+ Integer outputScale = scale ;
165
+ Integer inputSchemaPrecision = null ;
166
+ Integer inputSchemaScale = null ;
167
+
168
+ if (fieldSchema .getLogicalType () == LogicalType .DECIMAL ) {
169
+ inputSchemaPrecision = fieldSchema .getPrecision ();
170
+ inputSchemaScale = fieldSchema .getScale ();
171
+ }
172
+
173
+ if (scale == null && precision == null ) {
174
+ outputScale = inputSchemaScale ;
175
+ outputPrecision = inputSchemaPrecision ;
176
+ } else if (scale == null && inputSchemaScale != null ) {
177
+ if (precision - inputSchemaScale < 1 ) {
178
+ throw new DirectiveParseException (String .format (
179
+ "Cannot set scale as '%s' and precision as '%s' when "
180
+ + "given precision - scale is less than 1 " , inputSchemaScale ,
181
+ precision ));
189
182
}
190
- return new Pair <Integer , Integer >(precision , scale );
183
+ outputScale = inputSchemaScale ;
184
+ outputPrecision = precision ;
185
+
186
+ } else if (precision == null && inputSchemaPrecision != null ) {
187
+ if (inputSchemaPrecision - scale < 1 ) {
188
+ throw new DirectiveParseException (String .format (
189
+ "Cannot set scale as '%s' and precision as '%s' when "
190
+ + "given precision - scale is less than 1 " , scale ,
191
+ inputSchemaPrecision ));
192
+ }
193
+ outputScale = scale ;
194
+ outputPrecision = inputSchemaPrecision ;
191
195
}
196
+
197
+ return new Pair <Integer , Integer >(outputPrecision , outputScale );
198
+ }
192
199
}
0 commit comments