private static void assertBlockEquals(Type type, Block actual, Block expected) { for (int position = 0; position < actual.getPositionCount(); position++) { assertEquals( type.getObjectValue(SESSION, actual, position), type.getObjectValue(SESSION, expected, position)); } }
public static Slice toJson(Type rowType, ConnectorSession session, Slice row) { Object object = rowType.getObjectValue(session, createBlock(rowType, row), 0); try { return Slices.utf8Slice(OBJECT_MAPPER.get().writeValueAsString(object)); } catch (JsonProcessingException e) { throw Throwables.propagate(e); } }
protected void checkPageSource( ConnectorPageSource pageSource, List<TestColumn> testColumns, List<Type> types) throws IOException { try { MaterializedResult result = materializeSourceDataStream(SESSION, pageSource, types); for (MaterializedRow row : result) { for (int i = 0, testColumnsSize = testColumns.size(); i < testColumnsSize; i++) { TestColumn testColumn = testColumns.get(i); Type type = types.get(i); Object actualValue = row.getField(i); Object expectedValue = testColumn.getExpectedValue(); if (actualValue == null) { assertEquals(null, expectedValue, String.format("Expected non-null for column %d", i)); } else if (testColumn.getObjectInspector().getTypeName().equals("float") || testColumn.getObjectInspector().getTypeName().equals("double")) { assertEquals((double) actualValue, (double) expectedValue, EPSILON); } else if (testColumn.getObjectInspector().getTypeName().equals("date")) { SqlDate expectedDate = new SqlDate(((Long) expectedValue).intValue()); assertEquals(actualValue, expectedDate); } else if (testColumn.getObjectInspector().getTypeName().equals("timestamp")) { SqlTimestamp expectedTimestamp = new SqlTimestamp((Long) expectedValue, SESSION.getTimeZoneKey()); assertEquals(actualValue, expectedTimestamp); } else if (testColumn.getObjectInspector().getCategory() == Category.PRIMITIVE) { if (expectedValue instanceof Slice) { expectedValue = ((Slice) expectedValue).toStringUtf8(); } if (actualValue instanceof Slice) { actualValue = ((Slice) actualValue).toStringUtf8(); } if (actualValue instanceof SqlVarbinary) { actualValue = new String(((SqlVarbinary) actualValue).getBytes(), UTF_8); } assertEquals(actualValue, expectedValue, String.format("Wrong value for column %d", i)); } else { BlockBuilder builder = type.createBlockBuilder(new BlockBuilderStatus(), 1); type.writeObject(builder, expectedValue); expectedValue = type.getObjectValue(SESSION, builder.build(), 0); assertEquals( actualValue, expectedValue, String.format("Wrong value for column %s", testColumn.getName())); } } } } finally { pageSource.close(); } }
private Object selectSingleValue(Operator operator) { Page output = getAtMostOnePage(operator, SOURCE_PAGE); assertNotNull(output); assertEquals(output.getPositionCount(), 1); assertEquals(output.getChannelCount(), 1); Type type = operator.getTypes().get(0); Block block = output.getBlock(0); assertEquals(block.getPositionCount(), 1); return type.getObjectValue(session.toConnectorSession(), block, 0); }
@NotNull public static Object evaluatePropertyValue( Expression expression, Type expectedType, Session session, Metadata metadata) { Object value = evaluateConstantExpression(expression, expectedType, metadata, session, ImmutableSet.of()); // convert to object value type of SQL type BlockBuilder blockBuilder = expectedType.createBlockBuilder(new BlockBuilderStatus(), 1); BlockUtils.appendObject(expectedType, blockBuilder, value); Object objectValue = expectedType.getObjectValue(session.toConnectorSession(), blockBuilder, 0); if (objectValue == null) { throw new PrestoException( INVALID_SESSION_PROPERTY, "Session property value must not be null"); } return objectValue; }
public static Object evaluatePropertyValue( Expression expression, Type expectedType, Session session, Metadata metadata, List<Expression> parameters) { Expression rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(parameters), expression); Object value = evaluateConstantExpression(rewritten, expectedType, metadata, session, parameters); // convert to object value type of SQL type BlockBuilder blockBuilder = expectedType.createBlockBuilder(new BlockBuilderStatus(), 1); writeNativeValue(expectedType, blockBuilder, value); Object objectValue = expectedType.getObjectValue(session.toConnectorSession(), blockBuilder, 0); if (objectValue == null) { throw new PrestoException( INVALID_SESSION_PROPERTY, "Session property value must not be null"); } return objectValue; }
private static Object toTypeObjectValue(Session session, Type type, Object value) { BlockBuilder blockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), 1); writeNativeValue(type, blockBuilder, value); return type.getObjectValue(session.toConnectorSession(), blockBuilder, 0); }