use of org.apache.druid.segment.column.TypeStrategy in project druid by druid-io.
the class FunctionTest method testComplexDecode.
@Test
public void testComplexDecode() {
TypeStrategiesTest.NullableLongPair expected = new TypeStrategiesTest.NullableLongPair(1L, 2L);
TypeStrategy strategy = TypeStrategiesTest.NULLABLE_TEST_PAIR_TYPE.getStrategy();
final byte[] bytes = new byte[strategy.estimateSizeBytes(expected)];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
int written = strategy.write(buffer, expected, bytes.length);
Assert.assertEquals(bytes.length, written);
assertExpr(StringUtils.format("complex_decode_base64('%s', '%s')", TypeStrategiesTest.NULLABLE_TEST_PAIR_TYPE.getComplexTypeName(), StringUtils.encodeBase64String(bytes)), expected);
}
use of org.apache.druid.segment.column.TypeStrategy in project druid by druid-io.
the class ParserTest method testConstantComplexAndNestedArrays.
@Test
public void testConstantComplexAndNestedArrays() {
ExpressionProcessing.initializeForTests(true);
// they can be built with array builder functions though...
validateConstantExpression("array(['foo', 'bar', 'baz'], ['baz','foo','bar'])", new Object[] { new Object[] { "foo", "bar", "baz" }, new Object[] { "baz", "foo", "bar" } });
// nested arrays cannot be mixed types, the first element choo-choo-chooses for you
validateConstantExpression("array(['foo', 'bar', 'baz'], ARRAY<LONG>[1,2,3])", new Object[] { new Object[] { "foo", "bar", "baz" }, new Object[] { "1", "2", "3" } });
// complex types too
TypeStrategiesTest.NullableLongPair l1 = new TypeStrategiesTest.NullableLongPair(1L, 2L);
TypeStrategiesTest.NullableLongPair l2 = new TypeStrategiesTest.NullableLongPair(2L, 3L);
TypeStrategy byteStrategy = TypeStrategiesTest.NULLABLE_TEST_PAIR_TYPE.getStrategy();
final byte[] b1 = new byte[byteStrategy.estimateSizeBytes(l1)];
final byte[] b2 = new byte[byteStrategy.estimateSizeBytes(l2)];
ByteBuffer bb1 = ByteBuffer.wrap(b1);
ByteBuffer bb2 = ByteBuffer.wrap(b2);
int w1 = byteStrategy.write(bb1, l1, b1.length);
int w2 = byteStrategy.write(bb2, l2, b2.length);
Assert.assertTrue(w1 > 0);
Assert.assertTrue(w2 > 0);
String l1String = StringUtils.format("complex_decode_base64('%s', '%s')", TypeStrategiesTest.NULLABLE_TEST_PAIR_TYPE.getComplexTypeName(), StringUtils.encodeBase64String(b1));
String l2String = StringUtils.format("complex_decode_base64('%s', '%s')", TypeStrategiesTest.NULLABLE_TEST_PAIR_TYPE.getComplexTypeName(), StringUtils.encodeBase64String(b2));
validateConstantExpression(l1String, l1);
validateConstantExpression(StringUtils.format("array(%s,%s)", l1String, l2String), new Object[] { l1, l2 });
ExpressionProcessing.initializeForTests(null);
}
use of org.apache.druid.segment.column.TypeStrategy in project druid by druid-io.
the class ComplexExpr method stringify.
@Override
public String stringify() {
if (value == null) {
return StringUtils.format("complex_decode_base64('%s', %s)", outputType.getComplexTypeName(), NULL_LITERAL);
}
TypeStrategy strategy = outputType.getStrategy();
byte[] bytes = new byte[strategy.estimateSizeBytes(value)];
ByteBuffer wrappedBytes = ByteBuffer.wrap(bytes);
int remaining = strategy.write(wrappedBytes, 0, value, bytes.length);
if (remaining < 0) {
bytes = new byte[bytes.length - remaining];
wrappedBytes = ByteBuffer.wrap(bytes);
strategy.write(wrappedBytes, 0, value, bytes.length);
}
return StringUtils.format("complex_decode_base64('%s', '%s')", outputType.getComplexTypeName(), StringUtils.encodeBase64String(bytes));
}
use of org.apache.druid.segment.column.TypeStrategy in project druid by druid-io.
the class ExpressionTypeFactory method getTypeStrategy.
@Override
public <T> TypeStrategy<T> getTypeStrategy(ExpressionType expressionType) {
final TypeStrategy strategy;
switch(expressionType.getType()) {
case LONG:
strategy = TypeStrategies.LONG;
break;
case DOUBLE:
strategy = TypeStrategies.DOUBLE;
break;
case STRING:
strategy = TypeStrategies.STRING;
break;
case ARRAY:
strategy = new TypeStrategies.ArrayTypeStrategy(expressionType);
break;
case COMPLEX:
TypeStrategy<?> complexStrategy = TypeStrategies.getComplex(expressionType.getComplexTypeName());
if (complexStrategy == null) {
throw new IAE("Cannot find strategy for type [%s]", expressionType.asTypeString());
}
strategy = complexStrategy;
break;
default:
throw new ISE("Unsupported column type[%s]", expressionType.getType());
}
return strategy;
}
Aggregations