public Stream<Object> getOnlyColumn() { checkState(types.size() == 1, "result set must have exactly one column"); return rows.stream() .map(row -> row.getField(0)); }
@Test public void testNonDeterministicProjection() { MaterializedResult materializedResult = computeActual("SELECT r, r + 1 FROM (SELECT rand(100) r FROM orders) LIMIT 10"); assertEquals(materializedResult.getRowCount(), 10); for (MaterializedRow materializedRow : materializedResult) { assertEquals(materializedRow.getFieldCount(), 2); assertEquals(((Number) materializedRow.getField(0)).intValue() + 1, materializedRow.getField(1)); } }
private static MaterializedRow convertToTestTypes(MaterializedRow prestoRow) for (int field = 0; field < prestoRow.getFieldCount(); field++) { Object prestoValue = prestoRow.getField(field); Object convertedValue; if (prestoValue instanceof SqlDate) { return new MaterializedRow(prestoRow.getPrecision(), convertedValues);
private static MaterializedResult extractColumns(MaterializedResult materializedRows, List<Integer> indexes, List<Type> types) { ImmutableList.Builder<MaterializedRow> rows = ImmutableList.builder(); for (MaterializedRow row : materializedRows) { Object[] values = new Object[indexes.size()]; for (int i = 0; i < indexes.size(); i++) { values[i] = row.getField(indexes.get(i)); } rows.add(new MaterializedRow(MaterializedResult.DEFAULT_PRECISION, values)); } return new MaterializedResult(rows.build(), types); }
assertEquals(input.getFieldCount(), 5); return (String) input.getField(0); }); assertEquals(properties.get("test_string"), new MaterializedRow(1, "test_string", "foo string", "test default", "varchar", "test string property")); assertEquals(properties.get("test_long"), new MaterializedRow(1, "test_long", "424242", "42", "bigint", "test long property")); assertEquals(properties.get(TESTING_CATALOG + ".connector_string"), new MaterializedRow(1, TESTING_CATALOG + ".connector_string", "bar string", "connector default", "varchar", "connector string property")); assertEquals(properties.get(TESTING_CATALOG + ".connector_long"), new MaterializedRow(1, TESTING_CATALOG + ".connector_long", "11", "33", "bigint", "connector long property"));
.filter(row -> ((String) row.getField(1)).startsWith("system_tables_test")) .collect(toImmutableMap(row -> ((String) row.getField(1)), identity())); assertEquals(map.size(), 6); assertEquals( map.get("system_tables_test0").getFields(), asList("tpch", "system_tables_test0", null, null, null, null, null, Boolean.FALSE)); assertEquals( map.get("system_tables_test1").getFields(), asList("tpch", "system_tables_test1", "c10", null, null, null, null, Boolean.FALSE)); assertEquals( map.get("system_tables_test2").getFields(), asList("tpch", "system_tables_test2", "c20", ImmutableList.of("c22", "c21"), null, null, null, Boolean.FALSE)); assertEquals( map.get("system_tables_test3").getFields(), asList("tpch", "system_tables_test3", "c30", null, null, 40L, ImmutableList.of("c34", "c33"), Boolean.FALSE)); assertEquals( map.get("system_tables_test4").getFields(), asList("tpch", "system_tables_test4", "c40", ImmutableList.of("c41", "c42"), "test_distribution", 50L, ImmutableList.of("c43", "c44"), Boolean.FALSE)); assertEquals( map.get("system_tables_test5").getFields(), asList("tpch", "system_tables_test5", null, ImmutableList.of("c51", "c52"), "test_distribution", 50L, ImmutableList.of("c53", "c54"), Boolean.TRUE)); .filter(row -> ((String) row.getField(1)).startsWith("system_tables_test")) .count(); assertEquals(actualRowCount, 6);
public synchronized Builder row(Object... values) { rows.add(new MaterializedRow(DEFAULT_PRECISION, values)); return this; }
public void execute(QueryRunner prestoExecutor, Session session, DataSetup dataSetup) { List<Type> expectedTypes = inputs.stream().map(Input::getPrestoResultType).collect(toList()); List<Object> expectedResults = inputs.stream().map(Input::toPrestoQueryResult).collect(toList()); try (TestTable testTable = dataSetup.setupTestTable(unmodifiableList(inputs))) { MaterializedResult materializedRows = prestoExecutor.execute(session, "SELECT * from " + testTable.getName()); assertEquals(materializedRows.getTypes(), expectedTypes); List<Object> actualResults = getOnlyElement(materializedRows).getFields(); assertEquals(actualResults.size(), expectedResults.size(), "lists don't have the same size"); for (int i = 0; i < expectedResults.size(); i++) { assertEquals(actualResults.get(i), expectedResults.get(i), "Element " + i); } } }
@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"); }
public synchronized Builder row(Object... values) { rows.add(new MaterializedRow(DEFAULT_PRECISION, values)); return this; }
public Stream<Object> getOnlyColumn() { checkState(types.size() == 1, "result set must have exactly one column"); return rows.stream() .map(row -> row.getField(0)); }
@Test public void testNonDeterministicTableScanPredicatePushdown() { MaterializedResult materializedResult = computeActual("" + "SELECT COUNT(*)\n" + "FROM (\n" + " SELECT *\n" + " FROM lineitem\n" + " LIMIT 1000\n" + ")\n" + "WHERE rand() > 0.5"); 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 < 1000); }
private static MaterializedRow convertToTestTypes(MaterializedRow prestoRow) for (int field = 0; field < prestoRow.getFieldCount(); field++) { Object prestoValue = prestoRow.getField(field); Object convertedValue; if (prestoValue instanceof SqlDate) { return new MaterializedRow(prestoRow.getPrecision(), convertedValues);
private static Function<List<Object>, MaterializedRow> dataToRow(final List<Type> types) { return data -> { checkArgument(data.size() == types.size(), "columns size does not match types size"); List<Object> row = new ArrayList<>(); for (int i = 0; i < data.size(); i++) { Object value = data.get(i); Type type = types.get(i); row.add(convertToRowValue(type, value)); } return new MaterializedRow(DEFAULT_PRECISION, row); }; }
public Object getOnlyValue() { checkState(rows.size() == 1, "result set must have exactly one row"); checkState(types.size() == 1, "result set must have exactly one column"); return rows.get(0).getField(0); }
@Test public void testNonDeterministicAggregationPredicatePushdown() { MaterializedResult materializedResult = computeActual("" + "SELECT COUNT(*)\n" + "FROM (\n" + " SELECT orderkey, COUNT(*)\n" + " FROM lineitem\n" + " GROUP BY orderkey\n" + " LIMIT 1000\n" + ")\n" + "WHERE rand() > 0.5"); 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 < 1000); }
public synchronized Builder page(Page page) { requireNonNull(page, "page is null"); checkArgument(page.getChannelCount() == types.size(), "Expected a page with %s columns, but got %s columns", types.size(), page.getChannelCount()); for (int position = 0; position < page.getPositionCount(); position++) { List<Object> values = new ArrayList<>(page.getChannelCount()); for (int channel = 0; channel < page.getChannelCount(); channel++) { Type type = types.get(channel); Block block = page.getBlock(channel); values.add(type.getObjectValue(session, block, position)); } values = Collections.unmodifiableList(values); rows.add(new MaterializedRow(DEFAULT_PRECISION, values)); } return this; }
public Object getOnlyValue() { checkState(rows.size() == 1, "result set must have exactly one row"); checkState(types.size() == 1, "result set must have exactly one column"); return rows.get(0).getField(0); }
@Test public void testJoinWithNonDeterministicLessThan() { MaterializedRow actualRow = getOnlyElement(computeActual( "SELECT count(*) FROM " + "customer c1 JOIN customer c2 ON c1.nationkey=c2.nationkey " + "WHERE c1.custkey - RANDOM(CAST(c1.custkey AS BIGINT)) < c2.custkey").getMaterializedRows()); assertEquals(actualRow.getFieldCount(), 1); long actualCount = (Long) actualRow.getField(0); // this should be around ~69000 MaterializedRow expectedAtLeastRow = getOnlyElement(computeActual( "SELECT count(*) FROM " + "customer c1 JOIN customer c2 ON c1.nationkey=c2.nationkey " + "WHERE c1.custkey < c2.custkey").getMaterializedRows()); assertEquals(expectedAtLeastRow.getFieldCount(), 1); long expectedAtLeastCount = (Long) expectedAtLeastRow.getField(0); // this is exactly 45022 // Technically non-deterministic unit test but has hopefully a next to impossible chance of a false positive assertTrue(actualCount > expectedAtLeastCount); }
public synchronized Builder page(Page page) { requireNonNull(page, "page is null"); checkArgument(page.getChannelCount() == types.size(), "Expected a page with %s columns, but got %s columns", types.size(), page.getChannelCount()); for (int position = 0; position < page.getPositionCount(); position++) { List<Object> values = new ArrayList<>(page.getChannelCount()); for (int channel = 0; channel < page.getChannelCount(); channel++) { Type type = types.get(channel); Block block = page.getBlock(channel); values.add(type.getObjectValue(session, block, position)); } values = Collections.unmodifiableList(values); rows.add(new MaterializedRow(DEFAULT_PRECISION, values)); } return this; }