Skip to content

Commit d3d4c0b

Browse files
committed
fix iceberg unit test
Fix sql/jdbc integration test Fix Iceberg tests
1 parent 3c827e9 commit d3d4c0b

5 files changed

Lines changed: 27 additions & 13 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"comment": "Modify this file in a trivial way to cause this test suite to run ",
3-
"modification": 3
3+
"modification": 4
44
}

sdks/java/extensions/sql/jdbc/src/test/java/org/apache/beam/sdk/extensions/sql/jdbc/BeamSqlLineIT.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ public void testSelectFromPubsub() throws Exception {
129129

130130
assertThat(
131131
Arrays.asList(
132-
Arrays.asList("2018-07-01 21:25:20", "enroute", "40.702", "-74.001"),
133-
Arrays.asList("2018-07-01 21:26:06", "enroute", "40.703", "-74.002"),
134-
Arrays.asList("2018-07-02 13:26:06", "enroute", "30.0", "-72.32324")),
132+
Arrays.asList("2018-07-01 21:25:20.000000", "enroute", "40.702", "-74.001"),
133+
Arrays.asList("2018-07-01 21:26:06.000000", "enroute", "40.703", "-74.002"),
134+
Arrays.asList("2018-07-02 13:26:06.000000", "enroute", "30.0", "-72.32324")),
135135
everyItem(IsIn.isOneOf(expectedResult.get(30, TimeUnit.SECONDS).toArray())));
136136
}
137137

@@ -170,8 +170,8 @@ public void testFilterForSouthManhattan() throws Exception {
170170

171171
assertThat(
172172
Arrays.asList(
173-
Arrays.asList("2018-07-01 21:25:20", "enroute", "40.701", "-74.001"),
174-
Arrays.asList("2018-07-01 21:26:06", "enroute", "40.702", "-74.002")),
173+
Arrays.asList("2018-07-01 21:25:20.000000", "enroute", "40.701", "-74.001"),
174+
Arrays.asList("2018-07-01 21:26:06.000000", "enroute", "40.702", "-74.002")),
175175
everyItem(IsIn.isOneOf(expectedResult.get(30, TimeUnit.SECONDS).toArray())));
176176
}
177177

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: 6 additions & 4 deletions
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();
@@ -340,7 +342,7 @@ private static Object convertLiteral(SqlLiteral literal, String field, TypeID ty
340342
return literal.getValueAs(String.class);
341343
case DATE:
342344
LocalDate date;
343-
if (SqlTypeName.STRING_TYPES.contains(typeName)) {
345+
if (SqlTypeName.STRING_TYPES.contains(typeName) || SqlTypeName.UNKNOWN.equals(typeName)) {
344346
date = LocalDate.parse(literal.getValueAs(String.class));
345347
} else if (SqlTypeName.DATE.equals(typeName)) {
346348
DateString dateValue = literal.getValueAs(DateString.class);
@@ -351,7 +353,7 @@ private static Object convertLiteral(SqlLiteral literal, String field, TypeID ty
351353
return DateTimeUtil.daysFromDate(date);
352354
case TIME:
353355
LocalTime time;
354-
if (SqlTypeName.STRING_TYPES.contains(typeName)) {
356+
if (SqlTypeName.STRING_TYPES.contains(typeName) || SqlTypeName.UNKNOWN.equals(typeName)) {
355357
time = LocalTime.parse(literal.getValueAs(String.class));
356358
} else if (SqlTypeName.TIME.equals(typeName)) {
357359
TimeString timeString = literal.getValueAs(TimeString.class);
@@ -362,7 +364,7 @@ private static Object convertLiteral(SqlLiteral literal, String field, TypeID ty
362364
return DateTimeUtil.microsFromTime(time);
363365
case TIMESTAMP:
364366
LocalDateTime datetime;
365-
if (SqlTypeName.STRING_TYPES.contains(typeName)) {
367+
if (SqlTypeName.STRING_TYPES.contains(typeName) || SqlTypeName.UNKNOWN.equals(typeName)) {
366368
String value = literal.getValueAs(String.class);
367369
datetime = getLocalDateTime(value);
368370
} else if (SqlTypeName.DATE.equals(typeName)) {

0 commit comments

Comments
 (0)