@@ -108,6 +108,7 @@ public static String beamRow2CsvLine(Row row, CSVFormat csvFormat) {
108108 * @return The casted object in Schema.Field.Type.
109109 */
110110 public static Object autoCastField (Schema .Field field , @ Nullable Object rawObj ) {
111+ // handle null
111112 if (rawObj == null ) {
112113 if (!field .getType ().getNullable ()) {
113114 throw new IllegalArgumentException (String .format ("Field %s not nullable" , field .getName ()));
@@ -116,12 +117,14 @@ public static Object autoCastField(Schema.Field field, @Nullable Object rawObj)
116117 }
117118
118119 FieldType type = field .getType ();
120+ // handle NlsString
119121 if (CalciteUtils .isStringType (type )) {
120122 if (rawObj instanceof NlsString ) {
121123 return ((NlsString ) rawObj ).getValue ();
122124 } else {
123125 return rawObj ;
124126 }
127+ // handle date/time
125128 } else if (CalciteUtils .DATE .typesEqual (type ) || CalciteUtils .NULLABLE_DATE .typesEqual (type )) {
126129 if (rawObj instanceof GregorianCalendar ) { // used by the SQL CLI
127130 GregorianCalendar calendar = (GregorianCalendar ) rawObj ;
@@ -143,6 +146,7 @@ public static Object autoCastField(Schema.Field field, @Nullable Object rawObj)
143146 } else if (CalciteUtils .isDateTimeType (type )) {
144147 // Internal representation of Date in Calcite is convertible to Joda's Datetime.
145148 return new DateTime (rawObj );
149+ // handle decimal
146150 } else if (type .getTypeName ().isNumericType ()
147151 && ((rawObj instanceof String )
148152 || (rawObj instanceof BigDecimal && type .getTypeName () != TypeName .DECIMAL ))) {
@@ -168,8 +172,14 @@ public static Object autoCastField(Schema.Field field, @Nullable Object rawObj)
168172 String .format ("Column type %s is not supported yet!" , type ));
169173 }
170174 } else if (type .getTypeName ().isPrimitiveType ()) {
175+ // handle bytes represented by ByteString
171176 if (TypeName .BYTES .equals (type .getTypeName ()) && rawObj instanceof ByteString ) {
172177 return ((ByteString ) rawObj ).getBytes ();
178+ // handle Float <-> Double mixed use
179+ } else if (TypeName .FLOAT .equals (type .getTypeName ()) && rawObj instanceof Double ) {
180+ return ((Double ) rawObj ).floatValue ();
181+ } else if (TypeName .DOUBLE .equals (type .getTypeName ()) && rawObj instanceof Float ) {
182+ return ((Float ) rawObj ).doubleValue ();
173183 }
174184 }
175185 return rawObj ;
0 commit comments