Skip to content

Commit

Permalink
Fix NPE in DomainTranslator when coercing null values
Browse files Browse the repository at this point in the history
  • Loading branch information
erichwang committed Feb 16, 2016
1 parent f8eecb0 commit f9b128c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions presto-docs/src/main/sphinx/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Release Notes
.. toctree::
:maxdepth: 1

release/release-0.138
release/release-0.137
release/release-0.136
release/release-0.135
Expand Down
8 changes: 8 additions & 0 deletions presto-docs/src/main/sphinx/release/release-0.138.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=============
Release 0.138
=============

General Changes
---------------

* Fix planning bug with ``NULL`` literal coercions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,25 @@ protected ExtractionResult visitComparisonExpression(ComparisonExpression node,
return process(coerceDoubleToLongComparison(normalized), complement);
}

if (!TypeRegistry.canCoerce(value.getType(), fieldType)) {
Optional<NullableValue> coercedValue = coerce(value, fieldType);
if (!coercedValue.isPresent()) {
return super.visitComparisonExpression(node, complement);
}

Object coerced = new FunctionInvoker(metadata.getFunctionRegistry())
.invoke(metadata.getFunctionRegistry().getCoercion(value.getType(), fieldType), session.toConnectorSession(), value.getValue());
return createComparisonExtractionResult(normalized.getComparisonType(), symbol, fieldType, coercedValue.get().getValue(), complement);
}

return createComparisonExtractionResult(normalized.getComparisonType(), symbol, fieldType, coerced, complement);
private Optional<NullableValue> coerce(NullableValue value, Type targetType)
{
if (!TypeRegistry.canCoerce(value.getType(), targetType)) {
return Optional.empty();
}
if (value.isNull()) {
return Optional.of(NullableValue.asNull(targetType));
}
Object coercedValue = new FunctionInvoker(metadata.getFunctionRegistry())
.invoke(metadata.getFunctionRegistry().getCoercion(value.getType(), targetType), session.toConnectorSession(), value.getValue());
return Optional.of(NullableValue.of(targetType, coercedValue));
}

private ExtractionResult createComparisonExtractionResult(ComparisonExpression.Type comparisonType, Symbol column, Type type, @Nullable Object value, boolean complement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,10 @@ private String formatDomain(Domain domain)

private static String castToVarchar(Type type, Object value, Metadata metadata, Session session)
{
if (value == null) {
return "NULL";
}

Signature coercion = metadata.getFunctionRegistry().getCoercion(type, VARCHAR);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3583,6 +3583,7 @@ public void testIn()
assertQuery("SELECT 2 in (1, NULL, 3)", "values null");
assertQuery("SELECT x FROM (values DATE '1970-01-01', DATE '1970-01-03') t(x) WHERE x IN (DATE '1970-01-01')", "values DATE '1970-01-01'");
assertQuery("SELECT x FROM (values TIMESTAMP '1970-01-01 00:01:00+00:00', TIMESTAMP '1970-01-01 08:01:00+08:00', TIMESTAMP '1970-01-01 00:01:00+08:00') t(x) WHERE x IN (TIMESTAMP '1970-01-01 00:01:00+00:00')", "values TIMESTAMP '1970-01-01 00:01:00+00:00', TIMESTAMP '1970-01-01 08:01:00+08:00'");
assertQuery("SELECT COUNT(*) FROM (values 1) t(x) WHERE x IN (null, 0)", "SELECT 0");

String longValues = range(0, 20_000).asLongStream()
.mapToObj(Long::toString)
Expand Down

0 comments on commit f9b128c

Please sign in to comment.