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 7e02460
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,11 @@ public void testMetricsSumEstimateIntersect()
.build()
);
testQuery(sql, expectedQueries, expectedResults);
testQuery(sql.replaceAll("COMPLEX_DECODE_BASE64", "DECODE_BASE64_COMPLEX"), expectedQueries, expectedResults);
testQuery(
StringUtils.replace(sql, "COMPLEX_DECODE_BASE64", "DECODE_BASE64_COMPLEX"),
expectedQueries,
expectedResults
);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,26 @@
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 class ComplexDecodeBase64ExprMacro implements ExprMacroTable.ExprMacro
{
public static final String NAME = "complex_decode_base64";
public static final String ALIAS_NAME = "decode_base64_complex";
public static final String ALIAS = "decode_base64_complex";

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

@Override
public Optional<String> alias()
{
return Optional.of(ALIAS_NAME);
}

@Override
public Expr apply(List<Expr> args)
{
Expand All @@ -60,7 +58,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,7 +154,6 @@ public Object getLiteralValue()

public static class StringDecodeBase64UTFExprMacro implements ExprMacroTable.ExprMacro
{

public static final String NAME = "decode_base64_utf8";

@Override
Expand All @@ -166,6 +163,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 +178,7 @@ final class StringDecodeBase64UTFExpression extends ExprMacroTable.BaseScalarUni
{
public StringDecodeBase64UTFExpression(Expr arg)
{
super(NAME, arg);
super(name(), arg);
}

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

/***
* Alias Expression macro to create an alias and delegate operations to the 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,8 +43,13 @@
*/
public class ExprMacroTable
{
private static final BuiltInExprMacros.ComplexDecodeBase64ExprMacro COMPLEX_DECODE_BASE_64_EXPR_MACRO = new BuiltInExprMacros.ComplexDecodeBase64ExprMacro();
private static final List<ExprMacro> BUILT_IN = ImmutableList.of(
new BuiltInExprMacros.ComplexDecodeBase64ExprMacro(),
COMPLEX_DECODE_BASE_64_EXPR_MACRO,
new BuiltInExprMacros.AliasExprMacro(
COMPLEX_DECODE_BASE_64_EXPR_MACRO,
BuiltInExprMacros.ComplexDecodeBase64ExprMacro.ALIAS
),
new BuiltInExprMacros.StringDecodeBase64UTFExprMacro()
);
private static final ExprMacroTable NIL = new ExprMacroTable(Collections.emptyList());
Expand All @@ -52,19 +58,9 @@ public class ExprMacroTable

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 @@ -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.ComplexDecodeBase64ExprMacro.ALIAS
))
.add(new DecodeBase64UTFOperatorConversion())
.build();
Expand Down

0 comments on commit 7e02460

Please sign in to comment.