-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow aliasing of Macros and add new alias for complex decode 64 #15034
Allow aliasing of Macros and add new alias for complex decode 64 #15034
Conversation
e65785a
to
5dcbc0e
Compare
* | ||
* @return operator | ||
*/ | ||
default Optional<SqlOperator> aliasCalciteOperator() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are not using the same technique as we do for TRUNC or SUBSTR by using the AliasedOperatorConversion ? (at least for the SQL side of things)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
used AliasedOperatorConversion
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"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we change this alias in the future is there a way to maintain backward compatibility?
Also, we might want to add native tests with the alias name to ensure the correctness
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can not change the alias name once defined else it will regression, we need to add more alias.
cool will add native test.
f0f5121
to
e4fb4da
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Followup Q. Does this also allow using aliases in the native query? Although aliasing in native should be handled separately though and we can always address that as a followup
Yes, we need to add alias at Native layer as well. #15034 (review) will make sure that Macro aliasing will work at native layer as well. |
|
||
@Override | ||
public String name() | ||
{ | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public Optional<String> alias() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit worried this might be a bit too limiting since there can only be a single alias, unlike the SQL layer which can have any number of alias for the same function.
Additionally, I think there might be a bit more work to do here in terms of making the error messaging correct. Using this function as an example, the error messaging is going to be using the output of name()
rather than the alias, so the error messaging for decode_base64_complex
is going to be reporting complex_decode_base64
, which I think might be confusing for users. We might want to consider doing aliasing similar to how it is done at the SQL layer, where we have some AliasNamedFunction
class that overrides the name()
method with the alias and then delegates everything else to the underlying expression which is being overridden, though it might take separate classes for ExprMacroFunctionExpr
, FunctionExpr
, and ApplyFunctionExpr
. Depending on how these are registered with the function tables, this might also solve the multi-alias problem this current approach has, and future proofs against cases where we need to rename functions more than one time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for review, pushed a commit to create AliasExprMacro. Please review it again.
6b179e9
to
8c9e2a8
Compare
processing/src/main/java/org/apache/druid/math/expr/BuiltInExprMacros.java
Outdated
Show resolved
Hide resolved
/*** | ||
* Alias Expression macro create an alias and deligates operations to same base macro | ||
*/ | ||
public static class AliasExprMacro implements ExprMacroTable.ExprMacro |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a need to make this static? This can exist as a separate public class maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to new public class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An internal class of ExprMacroTable
would also probably be a more appropriate place for this to live so it is more obvious that functions defined by extensions could also use this
processing/src/main/java/org/apache/druid/math/expr/BuiltInExprMacros.java
Outdated
Show resolved
Hide resolved
@@ -952,6 +952,15 @@ public void testComplexDecode() | |||
), | |||
expected | |||
); | |||
// test with alias |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit. Might be good to add a test with 2 aliases, just to drive the point home that we can support multiple aliases for the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added test to verify multiple aliases
090684d
to
d2d9589
Compare
d2d9589
to
7e02460
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
); | ||
testQuery(sql, expectedQueries, expectedResults); | ||
testQuery( | ||
StringUtils.replace(sql, "COMPLEX_DECODE_BASE64", "DECODE_BASE64_COMPLEX"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this seems kind of unnecessary to test both, maybe we should just update it to use DECODE_BASE64_COMPLEX
since that is the new preferred function name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@@ -214,4 +224,31 @@ public Object getLiteralValue() | |||
} | |||
} | |||
} | |||
|
|||
/*** | |||
* Alias Expression macro to create an alias and delegate operations to the same base macro |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
javadocs on this should probably mention that for macros aliased by this thing, the Expr
spit out by the apply
method should use name()
in all the places instead of an internal constant so that things like error messages behave correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved this class to internal class of ExprMacroTable and fixed javadoc
/*** | ||
* Alias Expression macro create an alias and deligates operations to same base macro | ||
*/ | ||
public static class AliasExprMacro implements ExprMacroTable.ExprMacro |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An internal class of ExprMacroTable
would also probably be a more appropriate place for this to live so it is more obvious that functions defined by extensions could also use this
287a78f
to
db226cb
Compare
…che#15034) * Add AliasExprMacro to allow aliasing of native expression macros * Add decode_base64_complex alias for complex_decode_base64
…che#15034) * Add AliasExprMacro to allow aliasing of native expression macros * Add decode_base64_complex alias for complex_decode_base64
Description
This PR allows to build the macros/BuiltIn Macros with aliasing and also adds the alias for ComplexDecodeBase64OperatorConversion.
This PR has: