use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.
the class AbstractTestQueries method testTableSampleBernoulli.
@Test
public void testTableSampleBernoulli() {
DescriptiveStatistics stats = new DescriptiveStatistics();
int total = computeExpected("SELECT orderkey FROM orders", ImmutableList.of(BIGINT)).getMaterializedRows().size();
for (int i = 0; i < 100; i++) {
List<MaterializedRow> values = computeActual("SELECT orderkey FROM orders TABLESAMPLE BERNOULLI (50)").getMaterializedRows();
assertEquals(values.size(), ImmutableSet.copyOf(values).size(), "TABLESAMPLE produced duplicate rows");
stats.addValue(values.size() * 1.0 / total);
}
double mean = stats.getGeometricMean();
assertTrue(mean > 0.45 && mean < 0.55, format("Expected mean sampling rate to be ~0.5, but was %s", mean));
}
use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.
the class AbstractTestQueries method testArrayShuffle.
@Test
public void testArrayShuffle() {
List<Integer> expected = IntStream.rangeClosed(1, 500).boxed().collect(toList());
Set<List<Integer>> distinctResults = new HashSet<>();
distinctResults.add(expected);
for (int i = 0; i < 3; i++) {
MaterializedResult results = computeActual(format("SELECT shuffle(ARRAY %s) FROM orders LIMIT 10", expected));
List<MaterializedRow> rows = results.getMaterializedRows();
assertEquals(rows.size(), 10);
for (MaterializedRow row : rows) {
List<Integer> actual = (List<Integer>) row.getField(0);
// check if the result is a correct permutation
assertEqualsIgnoreOrder(actual, expected);
distinctResults.add(actual);
}
}
assertTrue(distinctResults.size() >= 24, "shuffle must produce at least 24 distinct results");
}
use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.
the class AbstractTestQueries method testApproxPercentile.
@Test
public void testApproxPercentile() {
MaterializedResult raw = computeActual("SELECT orderstatus, orderkey, totalprice FROM orders");
Multimap<String, Long> orderKeyByStatus = ArrayListMultimap.create();
Multimap<String, Double> totalPriceByStatus = ArrayListMultimap.create();
for (MaterializedRow row : raw.getMaterializedRows()) {
orderKeyByStatus.put((String) row.getField(0), ((Number) row.getField(1)).longValue());
totalPriceByStatus.put((String) row.getField(0), (Double) row.getField(2));
}
MaterializedResult actual = computeActual("" + "SELECT orderstatus, " + " approx_percentile(orderkey, 0.5), " + " approx_percentile(totalprice, 0.5)," + " approx_percentile(orderkey, 2, 0.5)," + " approx_percentile(totalprice, 2, 0.5)\n" + "FROM orders\n" + "GROUP BY orderstatus");
for (MaterializedRow row : actual.getMaterializedRows()) {
String status = (String) row.getField(0);
Long orderKey = ((Number) row.getField(1)).longValue();
Double totalPrice = (Double) row.getField(2);
Long orderKeyWeighted = ((Number) row.getField(3)).longValue();
Double totalPriceWeighted = (Double) row.getField(4);
List<Long> orderKeys = Ordering.natural().sortedCopy(orderKeyByStatus.get(status));
List<Double> totalPrices = Ordering.natural().sortedCopy(totalPriceByStatus.get(status));
// verify real rank of returned value is within 1% of requested rank
assertTrue(orderKey >= orderKeys.get((int) (0.49 * orderKeys.size())));
assertTrue(orderKey <= orderKeys.get((int) (0.51 * orderKeys.size())));
assertTrue(orderKeyWeighted >= orderKeys.get((int) (0.49 * orderKeys.size())));
assertTrue(orderKeyWeighted <= orderKeys.get((int) (0.51 * orderKeys.size())));
assertTrue(totalPrice >= totalPrices.get((int) (0.49 * totalPrices.size())));
assertTrue(totalPrice <= totalPrices.get((int) (0.51 * totalPrices.size())));
assertTrue(totalPriceWeighted >= totalPrices.get((int) (0.49 * totalPrices.size())));
assertTrue(totalPriceWeighted <= totalPrices.get((int) (0.51 * totalPrices.size())));
}
}
use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.
the class AbstractTestQueries method testValuesWithTimestamp.
@Test
public void testValuesWithTimestamp() {
MaterializedResult actual = computeActual("VALUES (current_timestamp, now())");
List<MaterializedRow> rows = actual.getMaterializedRows();
assertEquals(rows.size(), 1);
MaterializedRow row = rows.get(0);
assertEquals(row.getField(0), row.getField(1));
}
use of com.facebook.presto.testing.MaterializedRow in project presto by prestodb.
the class AbstractTestDistributedQueries method testViewMetadata.
@Test
public void testViewMetadata() {
skipTestUnless(supportsViews());
@Language("SQL") String query = "SELECT BIGINT '123' x, 'foo' y";
assertUpdate("CREATE VIEW meta_test_view AS " + query);
// test INFORMATION_SCHEMA.TABLES
MaterializedResult actual = computeActual(format("SELECT table_name, table_type FROM information_schema.tables WHERE table_schema = '%s'", getSession().getSchema().get()));
MaterializedResult expected = resultBuilder(getSession(), actual.getTypes()).row("customer", "BASE TABLE").row("lineitem", "BASE TABLE").row("meta_test_view", "VIEW").row("nation", "BASE TABLE").row("orders", "BASE TABLE").row("part", "BASE TABLE").row("partsupp", "BASE TABLE").row("region", "BASE TABLE").row("supplier", "BASE TABLE").build();
assertContains(actual, expected);
// test SHOW TABLES
actual = computeActual("SHOW TABLES");
MaterializedResult.Builder builder = resultBuilder(getSession(), actual.getTypes());
for (MaterializedRow row : expected.getMaterializedRows()) {
builder.row(row.getField(0));
}
expected = builder.build();
assertContains(actual, expected);
// test INFORMATION_SCHEMA.VIEWS
String user = getSession().getUser();
actual = computeActual(format("SELECT table_name, view_owner, view_definition FROM information_schema.views WHERE table_schema = '%s'", getSession().getSchema().get()));
expected = resultBuilder(getSession(), actual.getTypes()).row("meta_test_view", user, formatSqlText(query)).build();
assertContains(actual, expected);
// test SHOW COLUMNS
actual = computeActual("SHOW COLUMNS FROM meta_test_view");
expected = resultBuilder(getSession(), VARCHAR, VARCHAR, VARCHAR, VARCHAR).row("x", "bigint", "", "").row("y", "varchar(3)", "", "").build();
assertEquals(actual, expected);
// test SHOW CREATE VIEW
String expectedSql = formatSqlText(format("CREATE VIEW %s.%s.%s AS %s", getSession().getCatalog().get(), getSession().getSchema().get(), "meta_test_view", query)).trim();
actual = computeActual("SHOW CREATE VIEW meta_test_view");
assertEquals(getOnlyElement(actual.getOnlyColumnAsSet()), expectedSql);
assertUpdate("DROP VIEW meta_test_view");
}
Aggregations