Skip to content

Commit 32508a6

Browse files
committed
Fix Iceberg tests
1 parent 5e6e178 commit 32508a6

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/schema/BeamTableUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigquery/BeamSqlUnparseContext.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ public SqlNode toSql(RexProgram program, RexNode rex) {
128128
} else if (SqlKind.SEARCH.equals(rex.getKind())) {
129129
// Workaround CALCITE-4716
130130
RexCall search = (RexCall) rex;
131-
RexLocalRef ref = (RexLocalRef) search.operands.get(1);
132-
RexLiteral literal = (RexLiteral) program.getExprList().get(ref.getIndex());
133-
rex = search.clone(search.getType(), ImmutableList.of(search.operands.get(0), literal));
131+
if (search.operands.get(1) instanceof RexLocalRef) {
132+
RexLocalRef ref = (RexLocalRef) search.operands.get(1);
133+
RexLiteral literal = (RexLiteral) program.getExprList().get(ref.getIndex());
134+
rex = search.clone(search.getType(), ImmutableList.of(search.operands.get(0), literal));
135+
}
134136
}
135137

136138
return super.toSql(program, rex);

sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/FilterUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ static Expression convert(@Nullable String filter, Schema schema) {
153153
}
154154

155155
private static Expression convert(SqlNode expression, Schema schema) throws SqlParseException {
156-
checkArgument(expression instanceof SqlBasicCall);
156+
checkArgument(
157+
expression instanceof SqlBasicCall,
158+
String.format("Expected SqlBasicCall, get %s", expression.getClass().getName()));
157159
SqlBasicCall call = (SqlBasicCall) expression;
158160

159161
SqlOperator op = call.getOperator();

0 commit comments

Comments
 (0)