Skip to content

Commit

Permalink
Construct proper literal for DECIMAL in LiteralInterpreter.toExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Mar 19, 2018
1 parent 4bbf9aa commit d2af24f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.block.Block;
import com.facebook.presto.spi.type.CharType;
import com.facebook.presto.spi.type.DecimalType;
import com.facebook.presto.spi.type.Decimals;
import com.facebook.presto.spi.type.SqlDate;
import com.facebook.presto.spi.type.StandardTypes;
Expand Down Expand Up @@ -61,6 +62,7 @@
import static com.facebook.presto.spi.type.BigintType.BIGINT;
import static com.facebook.presto.spi.type.BooleanType.BOOLEAN;
import static com.facebook.presto.spi.type.DateType.DATE;
import static com.facebook.presto.spi.type.Decimals.isShortDecimal;
import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
import static com.facebook.presto.spi.type.IntegerType.INTEGER;
import static com.facebook.presto.spi.type.RealType.REAL;
Expand Down Expand Up @@ -172,6 +174,17 @@ else if (value.equals(Float.POSITIVE_INFINITY)) {
}
}

if (type instanceof DecimalType) {
String string;
if (isShortDecimal(type)) {
string = Decimals.toString((long) object, ((DecimalType) type).getScale());
}
else {
string = Decimals.toString((Slice) object, ((DecimalType) type).getScale());
}
return new Cast(new DecimalLiteral(string), type.getDisplayName());
}

if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
Slice value = (Slice) object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,14 @@ public void testSelect()
.row("James ", "Brown ", 4L, new BigDecimal("-7.00"))
.build();
assertEquals(expected, actual);

actual = computeActual(
"SELECT c_first_name, c_last_name " +
"FROM customer JOIN customer_address ON c_current_addr_sk = ca_address_sk " +
"WHERE ca_address_sk = 4 AND ca_gmt_offset = DECIMAL '-7.00'");
expected = resultBuilder(getSession(), actual.getTypes())
.row("James ", "Brown ")
.build();
assertEquals(expected, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static com.facebook.presto.tests.statistics.MetricComparisonStrategies.absoluteError;
import static com.facebook.presto.tests.statistics.MetricComparisonStrategies.defaultTolerance;
import static com.facebook.presto.tests.statistics.MetricComparisonStrategies.noError;
import static com.facebook.presto.tests.statistics.MetricComparisonStrategies.relativeError;
import static com.facebook.presto.tests.statistics.Metrics.OUTPUT_ROW_COUNT;
import static com.facebook.presto.tests.statistics.Metrics.distinctValuesCount;
import static java.util.Collections.emptyMap;
Expand Down Expand Up @@ -92,4 +93,33 @@ public void testCharComparison()
statisticsAssertion.check("SELECT * FROM item WHERE i_category = cast('Women' as char(50))",
checks -> checks.estimate(OUTPUT_ROW_COUNT, defaultTolerance()));
}

@Test
public void testDecimalComparison()
{
// ca_gmt_offset is decimal(5,2)
statisticsAssertion.check("SELECT * FROM customer_address WHERE ca_gmt_offset = -7",
checks -> checks
.estimate(OUTPUT_ROW_COUNT, relativeError(.6)) // distribution is non-uniform, so we need higher tolerance
.estimate(distinctValuesCount("ca_gmt_offset"), noError()));
statisticsAssertion.check("SELECT * FROM customer_address WHERE -7 = ca_gmt_offset",
checks -> checks
.estimate(OUTPUT_ROW_COUNT, relativeError(.6)) // distribution is non-uniform, so we need higher tolerance
.estimate(distinctValuesCount("ca_gmt_offset"), noError()));
statisticsAssertion.check("SELECT * FROM customer_address WHERE ca_gmt_offset = (decimal '-7.0')",
checks -> checks
.estimate(OUTPUT_ROW_COUNT, relativeError(.6)) // distribution is non-uniform, so we need higher tolerance
.estimate(distinctValuesCount("ca_gmt_offset"), noError()));
statisticsAssertion.check("SELECT * FROM customer_address WHERE ca_gmt_offset = -7.0",
checks -> checks
.estimate(OUTPUT_ROW_COUNT, relativeError(.6))); // distribution is non-uniform, so we need higher tolerance

// p_cost is decimal(15,2)
statisticsAssertion.check("SELECT * FROM promotion WHERE p_cost < 1", // p_cost is always 1000.00, so no rows should be left
checks -> checks.estimate(OUTPUT_ROW_COUNT, noError()));
statisticsAssertion.check("SELECT * FROM promotion WHERE 1 > p_cost", // p_cost is always 1000.00, so no rows should be left
checks -> checks.estimate(OUTPUT_ROW_COUNT, noError()));
statisticsAssertion.check("SELECT * FROM promotion WHERE p_cost < 2000.0", // p_cost is always 1000.00, so all rows should be left
checks -> checks.estimate(OUTPUT_ROW_COUNT, noError()));
}
}

0 comments on commit d2af24f

Please sign in to comment.