use of org.apache.druid.segment.VirtualColumns in project druid by druid-io.
the class VirtualColumnsTest method testGetColumnCapabilitiesWithFallbackDoubleBase.
@Test
public void testGetColumnCapabilitiesWithFallbackDoubleBase() {
final VirtualColumns virtualColumns = makeVirtualColumns();
final ColumnInspector baseInspector = column -> {
if (REAL_COLUMN_NAME.equals(column)) {
return ColumnCapabilitiesImpl.createSimpleNumericColumnCapabilities(ColumnType.DOUBLE);
} else {
return null;
}
};
Assert.assertEquals(ValueType.FLOAT, virtualColumns.getColumnCapabilitiesWithFallback(baseInspector, "expr").getType());
Assert.assertEquals(ValueType.DOUBLE, virtualColumns.getColumnCapabilitiesWithFallback(baseInspector, "expr2").getType());
Assert.assertEquals(ValueType.DOUBLE, virtualColumns.getColumnCapabilitiesWithFallback(baseInspector, REAL_COLUMN_NAME).getType());
}
use of org.apache.druid.segment.VirtualColumns in project hive by apache.
the class DruidStorageHandlerUtils method extractColName.
@Nullable
public static String extractColName(ExprNodeDesc expr, List<VirtualColumn> virtualColumns) {
if (!druidSupportedTypeInfos.contains(expr.getTypeInfo())) {
// We cannot pass the bloom filter to druid since bloom filter tests for exact object bytes.
return null;
}
if (expr instanceof ExprNodeColumnDesc) {
return ((ExprNodeColumnDesc) expr).getColumn();
}
ExprNodeGenericFuncDesc funcDesc = null;
if (expr instanceof ExprNodeGenericFuncDesc) {
funcDesc = (ExprNodeGenericFuncDesc) expr;
}
if (null == funcDesc) {
return null;
}
GenericUDF udf = funcDesc.getGenericUDF();
// bail out if its not a simple cast expression.
if (funcDesc.getChildren().size() == 1 && funcDesc.getChildren().get(0) instanceof ExprNodeColumnDesc) {
return null;
}
String columnName = ((ExprNodeColumnDesc) (funcDesc.getChildren().get(0))).getColumn();
ValueType targetType = null;
if (udf instanceof GenericUDFBridge) {
Class<? extends UDF> udfClass = ((GenericUDFBridge) udf).getUdfClass();
if (udfClass.equals(UDFToDouble.class)) {
targetType = ValueType.DOUBLE;
} else if (udfClass.equals(UDFToFloat.class)) {
targetType = ValueType.FLOAT;
} else if (udfClass.equals(UDFToLong.class)) {
targetType = ValueType.LONG;
}
} else if (udf instanceof GenericUDFToString) {
targetType = ValueType.STRING;
}
if (targetType == null) {
return null;
}
String virtualColumnExpr = DruidQuery.format("CAST(%s, '%s')", columnName, targetType.toString());
for (VirtualColumn column : virtualColumns) {
if (column instanceof ExpressionVirtualColumn && ((ExpressionVirtualColumn) column).getExpression().equals(virtualColumnExpr)) {
// Found an existing virtual column with same expression, no need to add another virtual column
return column.getOutputName();
}
}
Set<String> usedColumnNames = virtualColumns.stream().map(col -> col.getOutputName()).collect(Collectors.toSet());
final String name = SqlValidatorUtil.uniquify("vc", usedColumnNames, SqlValidatorUtil.EXPR_SUGGESTER);
ExpressionVirtualColumn expressionVirtualColumn = new ExpressionVirtualColumn(name, virtualColumnExpr, targetType, ExprMacroTable.nil());
virtualColumns.add(expressionVirtualColumn);
return name;
}
Aggregations