use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.
the class TestRaptorIntegrationSmokeTest method testShardingByTemporalTimestampColumn.
@Test
public void testShardingByTemporalTimestampColumn() {
assertUpdate("CREATE TABLE test_shard_temporal_timestamp(col1 BIGINT, col2 TIMESTAMP) WITH (temporal_column = 'col2')");
int rows = 20;
StringJoiner joiner = new StringJoiner(", ", "INSERT INTO test_shard_temporal_timestamp VALUES ", "");
for (int i = 0; i < rows; i++) {
joiner.add(format("(%s, TIMESTAMP '2016-08-08 01:00' + interval '%s' hour)", i, i * 4));
}
assertUpdate(joiner.toString(), format("VALUES(%s)", rows));
MaterializedResult results = computeActual("SELECT format_datetime(col2 AT TIME ZONE 'UTC', 'yyyyMMdd'), \"$shard_uuid\" FROM test_shard_temporal_timestamp");
assertEquals(results.getRowCount(), rows);
// Each shard will only contain data of one date.
SetMultimap<String, String> shardDateMap = HashMultimap.create();
for (MaterializedRow row : results.getMaterializedRows()) {
shardDateMap.put((String) row.getField(1), (String) row.getField(0));
}
for (Collection<String> dates : shardDateMap.asMap().values()) {
assertEquals(dates.size(), 1);
}
// Ensure one shard can contain different timestamps from the same day
assertLessThan(shardDateMap.size(), rows);
}
use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.
the class AbstractTestJoinQueries method testNonDeterministicJoinPredicatePushdown.
@Test
public void testNonDeterministicJoinPredicatePushdown() {
MaterializedResult materializedResult = computeActual("" + "SELECT COUNT(*)\n" + "FROM (\n" + " SELECT DISTINCT *\n" + " FROM (\n" + " SELECT 'abc' AS col1a, 500 AS col1b FROM lineitem LIMIT 1\n" + " ) table1\n" + " JOIN (\n" + " SELECT 'abc' AS col2a FROM lineitem LIMIT 1000000\n" + " ) table2\n" + " ON table1.col1a = table2.col2a\n" + " WHERE rand() * 1000 > table1.col1b\n" + ")");
MaterializedRow row = getOnlyElement(materializedResult.getMaterializedRows());
assertEquals(row.getFieldCount(), 1);
long count = (Long) row.getField(0);
// Technically non-deterministic unit test but has essentially a next to impossible chance of a false positive
assertTrue(count > 0 && count < 1000000);
}
use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.
the class TestMySqlIntegrationSmokeTest method testMySqlTinyint1.
@Test
public void testMySqlTinyint1() throws Exception {
execute("CREATE TABLE tpch.mysql_test_tinyint1 (c_tinyint tinyint(1))");
MaterializedResult actual = computeActual("SHOW COLUMNS FROM mysql_test_tinyint1");
MaterializedResult expected = MaterializedResult.resultBuilder(getSession(), VARCHAR, VARCHAR, VARCHAR, VARCHAR).row("c_tinyint", "tinyint", "", "").build();
assertEquals(actual, expected);
execute("INSERT INTO tpch.mysql_test_tinyint1 VALUES (127), (-128)");
MaterializedResult materializedRows = computeActual("SELECT * FROM tpch.mysql_test_tinyint1 WHERE c_tinyint = 127");
assertEquals(materializedRows.getRowCount(), 1);
MaterializedRow row = getOnlyElement(materializedRows);
assertEquals(row.getFields().size(), 1);
assertEquals(row.getField(0), (byte) 127);
assertUpdate("DROP TABLE mysql_test_tinyint1");
}
use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.
the class H2QueryRunner method rowMapper.
private static RowMapper<MaterializedRow> rowMapper(List<? extends Type> types) {
return new RowMapper<MaterializedRow>() {
private Object getValue(Type type, ResultSet resultSet, int position) throws SQLException {
if (BOOLEAN.equals(type)) {
boolean booleanValue = resultSet.getBoolean(position);
return resultSet.wasNull() ? null : booleanValue;
} else if (TINYINT.equals(type)) {
byte byteValue = resultSet.getByte(position);
return resultSet.wasNull() ? null : byteValue;
} else if (SMALLINT.equals(type)) {
short shortValue = resultSet.getShort(position);
return resultSet.wasNull() ? null : shortValue;
} else if (INTEGER.equals(type)) {
int intValue = resultSet.getInt(position);
return resultSet.wasNull() ? null : intValue;
} else if (BIGINT.equals(type)) {
long longValue = resultSet.getLong(position);
return resultSet.wasNull() ? null : longValue;
} else if (REAL.equals(type)) {
float floatValue = resultSet.getFloat(position);
return resultSet.wasNull() ? null : floatValue;
} else if (DOUBLE.equals(type)) {
double doubleValue = resultSet.getDouble(position);
return resultSet.wasNull() ? null : doubleValue;
} else if (isVarcharType(type)) {
String stringValue = resultSet.getString(position);
return resultSet.wasNull() ? null : stringValue;
} else if (isCharType(type)) {
String stringValue = resultSet.getString(position);
return resultSet.wasNull() ? null : padEnd(stringValue, ((CharType) type).getLength(), ' ');
} else if (VARBINARY.equals(type)) {
byte[] binary = resultSet.getBytes(position);
return resultSet.wasNull() ? null : binary;
} else if (DATE.equals(type)) {
// resultSet.getDate(i) doesn't work if JVM's zone skipped day being retrieved (e.g. 2011-12-30 and Pacific/Apia zone)
LocalDate dateValue = resultSet.getObject(position, LocalDate.class);
return resultSet.wasNull() ? null : dateValue;
} else if (TIME.equals(type)) {
// resultSet.getTime(i) doesn't work if JVM's zone had forward offset change during 1970-01-01 (e.g. America/Hermosillo zone)
LocalTime timeValue = resultSet.getObject(position, LocalTime.class);
return resultSet.wasNull() ? null : timeValue;
} else if (TIME_WITH_TIME_ZONE.equals(type)) {
throw new UnsupportedOperationException("H2 does not support TIME WITH TIME ZONE");
} else if (TIMESTAMP.equals(type)) {
// resultSet.getTimestamp(i) doesn't work if JVM's zone had forward offset at the date/time being retrieved
LocalDateTime timestampValue;
try {
timestampValue = resultSet.getObject(position, LocalDateTime.class);
} catch (SQLException first) {
// H2 cannot convert DATE to LocalDateTime in their JDBC driver (even though it can convert to java.sql.Timestamp), we need to do this manually
try {
timestampValue = Optional.ofNullable(resultSet.getObject(position, LocalDate.class)).map(LocalDate::atStartOfDay).orElse(null);
} catch (RuntimeException e) {
first.addSuppressed(e);
throw first;
}
}
return resultSet.wasNull() ? null : timestampValue;
} else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
// This means H2 is unsuitable for testing TIMESTAMP WITH TIME ZONE-bearing queries. Those need to be tested manually.
throw new UnsupportedOperationException();
} else if (UNKNOWN.equals(type)) {
Object objectValue = resultSet.getObject(position);
checkState(resultSet.wasNull(), "Expected a null value, but got %s", objectValue);
return null;
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
BigDecimal decimalValue = resultSet.getBigDecimal(position);
return resultSet.wasNull() ? null : decimalValue.setScale(decimalType.getScale(), BigDecimal.ROUND_HALF_UP).round(new MathContext(decimalType.getPrecision()));
} else if (type instanceof ArrayType) {
Array array = resultSet.getArray(position);
return resultSet.wasNull() ? null : newArrayList(mapArrayValues(((ArrayType) type), (Object[]) array.getArray()));
} else if (type instanceof RowType) {
Array array = resultSet.getArray(position);
return resultSet.wasNull() ? null : newArrayList(mapRowValues((RowType) type, (Object[]) array.getArray()));
} else if (type instanceof TypeWithName) {
return getValue(((TypeWithName) type).getType(), resultSet, position);
} else {
throw new AssertionError("unhandled type: " + type);
}
}
@Override
public MaterializedRow map(ResultSet resultSet, StatementContext context) throws SQLException {
int count = resultSet.getMetaData().getColumnCount();
checkArgument(types.size() == count, "expected types count (%s) does not match actual column count (%s)", types.size(), count);
List<Object> row = new ArrayList<>(count);
for (int i = 1; i <= count; i++) {
row.add(getValue(types.get(i - 1), resultSet, i));
}
return new MaterializedRow(MaterializedResult.DEFAULT_PRECISION, row);
}
};
}
use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.
the class AbstractTestQueries method testSpecialFloatingPointValues.
@Test
public void testSpecialFloatingPointValues() {
MaterializedResult actual = computeActual("SELECT nan(), infinity(), -infinity()");
MaterializedRow row = getOnlyElement(actual.getMaterializedRows());
assertEquals(row.getField(0), Double.NaN);
assertEquals(row.getField(1), Double.POSITIVE_INFINITY);
assertEquals(row.getField(2), Double.NEGATIVE_INFINITY);
}
Aggregations