Skip to content

Commit

Permalink
adding AliasExprMacro
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavbhole committed Oct 5, 2023
1 parent 8e24394 commit 6b179e9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,32 @@
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class BuiltInExprMacros
{
public static final String COMPLEX_DECODE_BASE64_NAME = "complex_decode_base64";
public static final String COMPLEX_DECODE_BASE64_ALIAS_NAME = "decode_base64_complex";
public static final String STRING_DECODE_BASE64_NAME = "decode_base64_utf8";

public static class ComplexDecodeBase64ExprMacro implements ExprMacroTable.ExprMacro
{
public static final String NAME = "complex_decode_base64";
public static final String ALIAS_NAME = "decode_base64_complex";
private final String NAME;

@Override
public String name()
public ComplexDecodeBase64ExprMacro(final String name)
{
return NAME;
this.NAME = name;
}

/**
* use name() in closure scope to allow Alias macro to override it with alias.
*
* @return String
*/
@Override
public Optional<String> alias()
public String name()
{
return Optional.of(ALIAS_NAME);
return NAME;
}

@Override
Expand All @@ -60,7 +66,7 @@ final class ComplexDecodeBase64Expression extends ExprMacroTable.BaseScalarMacro

public ComplexDecodeBase64Expression(List<Expr> args)
{
super(NAME, args);
super(name(), args);
validationHelperCheckArgumentCount(args, 2);
final Expr arg0 = args.get(0);

Expand Down Expand Up @@ -156,8 +162,12 @@ public Object getLiteralValue()

public static class StringDecodeBase64UTFExprMacro implements ExprMacroTable.ExprMacro
{
private final String NAME;

public static final String NAME = "decode_base64_utf8";
public StringDecodeBase64UTFExprMacro(final String name)
{
this.NAME = name;
}

@Override
public Expr apply(List<Expr> args)
Expand All @@ -166,6 +176,11 @@ public Expr apply(List<Expr> args)
return new StringDecodeBase64UTFExpression(args.get(0));
}

/**
* use name() in closure scope to allow Alias macro to override it with alias.
*
* @return String
*/
@Override
public String name()
{
Expand All @@ -176,7 +191,7 @@ final class StringDecodeBase64UTFExpression extends ExprMacroTable.BaseScalarUni
{
public StringDecodeBase64UTFExpression(Expr arg)
{
super(NAME, arg);
super(name(), arg);
}

@Override
Expand Down Expand Up @@ -222,4 +237,31 @@ public Object getLiteralValue()
}
}
}

/***
* Alias Expression macro create an alias and deligates operations to same base macro
*/
public static class AliasExprMacro implements ExprMacroTable.ExprMacro
{
private final ExprMacroTable.ExprMacro exprMacro;
private final String alias;

public AliasExprMacro(final ExprMacroTable.ExprMacro baseExprMacro, final String alias)
{
this.exprMacro = baseExprMacro;
this.alias = alias;
}

@Override
public Expr apply(List<Expr> args)
{
return exprMacro.apply(args);
}

@Override
public String name()
{
return alias;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Mechanism by which Druid expressions can define new functions for the Druid expression language. When
Expand All @@ -42,29 +43,25 @@
*/
public class ExprMacroTable
{
private static final BuiltInExprMacros.ComplexDecodeBase64ExprMacro COMPLEX_DECODE_BASE_64_EXPR_MACRO = new BuiltInExprMacros.ComplexDecodeBase64ExprMacro(
BuiltInExprMacros.COMPLEX_DECODE_BASE64_NAME);
private static final List<ExprMacro> BUILT_IN = ImmutableList.of(
new BuiltInExprMacros.ComplexDecodeBase64ExprMacro(),
new BuiltInExprMacros.StringDecodeBase64UTFExprMacro()
COMPLEX_DECODE_BASE_64_EXPR_MACRO,
new BuiltInExprMacros.AliasExprMacro(
COMPLEX_DECODE_BASE_64_EXPR_MACRO,
BuiltInExprMacros.COMPLEX_DECODE_BASE64_ALIAS_NAME
),
new BuiltInExprMacros.StringDecodeBase64UTFExprMacro(BuiltInExprMacros.STRING_DECODE_BASE64_NAME)
);
private static final ExprMacroTable NIL = new ExprMacroTable(Collections.emptyList());

private final Map<String, ExprMacro> macroMap;

public ExprMacroTable(final List<ExprMacro> macros)
{
this.macroMap = Maps.newHashMapWithExpectedSize(BUILT_IN.size() + 1 + macros.size());
BUILT_IN.forEach(m -> {
macroMap.put(StringUtils.toLowerCase(m.name()), m);
if (m.alias().isPresent()) {
macroMap.put(StringUtils.toLowerCase(m.alias().get()), m);
}
});
for (ExprMacro macro : macros) {
macroMap.put(StringUtils.toLowerCase(macro.name()), macro);
if (macro.alias().isPresent()) {
macroMap.put(StringUtils.toLowerCase(macro.alias().get()), macro);
}
}
this.macroMap = Maps.newHashMapWithExpectedSize(BUILT_IN.size() + macros.size());
macroMap.putAll(BUILT_IN.stream().collect(Collectors.toMap(m -> StringUtils.toLowerCase(m.name()), m -> m)));
macroMap.putAll(macros.stream().collect(Collectors.toMap(m -> StringUtils.toLowerCase(m.name()), m -> m)));
}

public static ExprMacroTable nil()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
* Common stuff for "named" functions of "functional" expressions, such as {@link FunctionExpr},
Expand All @@ -39,14 +38,6 @@ public interface NamedFunction
*/
String name();

/**
* Alias of the function
*/
default Optional<String> alias()
{
return Optional.empty();
}

/**
* Helper method for creating a {@link ExpressionValidationException} with the specified reason
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public class ComplexDecodeBase64OperatorConversion implements SqlOperatorConvers
};

private static final SqlFunction SQL_FUNCTION = OperatorConversions
.operatorBuilder(StringUtils.toUpperCase(BuiltInExprMacros.ComplexDecodeBase64ExprMacro.NAME))
.operatorBuilder(StringUtils.toUpperCase(BuiltInExprMacros.COMPLEX_DECODE_BASE64_NAME))
.operandTypeChecker(
OperandTypes.sequence(
"'" + StringUtils.toUpperCase(BuiltInExprMacros.ComplexDecodeBase64ExprMacro.NAME) + "(typeName, base64)'",
"'" + StringUtils.toUpperCase(BuiltInExprMacros.COMPLEX_DECODE_BASE64_NAME) + "(typeName, base64)'",
OperandTypes.and(OperandTypes.family(SqlTypeFamily.STRING), OperandTypes.LITERAL),
OperandTypes.ANY
)
Expand Down Expand Up @@ -86,7 +86,7 @@ public DruidExpression toDruidExpression(
String arg0 = druidExpressions.get(0).getExpression();
return DruidExpression.ofExpression(
ColumnType.ofComplex(arg0.substring(1, arg0.length() - 1)),
DruidExpression.functionCall(BuiltInExprMacros.ComplexDecodeBase64ExprMacro.NAME),
DruidExpression.functionCall(BuiltInExprMacros.COMPLEX_DECODE_BASE64_NAME),
druidExpressions
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class DecodeBase64UTFOperatorConversion extends DirectOperatorConversion
{

private static final SqlFunction SQL_FUNCTION = OperatorConversions
.operatorBuilder(StringUtils.toUpperCase(BuiltInExprMacros.StringDecodeBase64UTFExprMacro.NAME))
.operatorBuilder(StringUtils.toUpperCase(BuiltInExprMacros.STRING_DECODE_BASE64_NAME))
.operandTypes(SqlTypeFamily.CHARACTER)
.returnTypeNullable(SqlTypeName.VARCHAR)
.functionCategory(SqlFunctionCategory.STRING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public class DruidOperatorTable implements SqlOperatorTable
.add(COMPLEX_DECODE_OPERATOR_CONVERSIONS)
.add(new AliasedOperatorConversion(
COMPLEX_DECODE_OPERATOR_CONVERSIONS,
BuiltInExprMacros.ComplexDecodeBase64ExprMacro.ALIAS_NAME
BuiltInExprMacros.COMPLEX_DECODE_BASE64_ALIAS_NAME
))
.add(new DecodeBase64UTFOperatorConversion())
.build();
Expand Down

0 comments on commit 6b179e9

Please sign in to comment.