Skip to content
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

Merged
merged 6 commits into from
Oct 5, 2023

Conversation

pranavbhole
Copy link
Contributor

@pranavbhole pranavbhole commented Sep 25, 2023

Description

This PR allows to build the macros/BuiltIn Macros with aliasing and also adds the alias for ComplexDecodeBase64OperatorConversion.

This PR has:

  • been self-reviewed.
  • added documentation for new or modified features or behaviors.
  • a release note entry in the PR description.
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
  • added or updated version, license, or notice information in licenses.yaml
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • added integration tests.
  • been tested in a test Druid cluster.

@pranavbhole pranavbhole marked this pull request as ready for review September 25, 2023 22:39
@pranavbhole pranavbhole changed the title Allow aliasing of SQL operator and Macros Allow aliasing of SQL operators and Macros Sep 25, 2023
@pranavbhole pranavbhole force-pushed the allowOperatorMacroAliasing branch from e65785a to 5dcbc0e Compare September 25, 2023 22:53
*
* @return operator
*/
default Optional<SqlOperator> aliasCalciteOperator()
Copy link
Contributor

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)

Copy link
Contributor Author

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";
Copy link
Contributor

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

Copy link
Contributor Author

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.

@pranavbhole pranavbhole force-pushed the allowOperatorMacroAliasing branch 2 times, most recently from f0f5121 to e4fb4da Compare October 2, 2023 06:50
@pranavbhole pranavbhole changed the title Allow aliasing of SQL operators and Macros Allow aliasing of Macros and add new alias for complex decode 64 Oct 2, 2023
Copy link
Contributor

@somu-imply somu-imply left a 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

@pranavbhole
Copy link
Contributor Author

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()
Copy link
Member

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.

Copy link
Contributor Author

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.

@pranavbhole pranavbhole force-pushed the allowOperatorMacroAliasing branch 2 times, most recently from 6b179e9 to 8c9e2a8 Compare October 5, 2023 00:59
/***
* Alias Expression macro create an alias and deligates operations to same base macro
*/
public static class AliasExprMacro implements ExprMacroTable.ExprMacro
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Member

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

@@ -952,6 +952,15 @@ public void testComplexDecode()
),
expected
);
// test with alias
Copy link
Contributor

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

Copy link
Contributor Author

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

@pranavbhole pranavbhole force-pushed the allowOperatorMacroAliasing branch 2 times, most recently from 090684d to d2d9589 Compare October 5, 2023 02:56
@pranavbhole pranavbhole force-pushed the allowOperatorMacroAliasing branch from d2d9589 to 7e02460 Compare October 5, 2023 02:57
Copy link
Member

@clintropolis clintropolis left a 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"),
Copy link
Member

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

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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

@pranavbhole pranavbhole force-pushed the allowOperatorMacroAliasing branch from 287a78f to db226cb Compare October 5, 2023 16:37
@clintropolis clintropolis merged commit 06c5527 into apache:master Oct 5, 2023
@pranavbhole pranavbhole deleted the allowOperatorMacroAliasing branch October 6, 2023 01:03
@LakshSingla LakshSingla added this to the 28.0 milestone Oct 12, 2023
ektravel pushed a commit to ektravel/druid that referenced this pull request Oct 16, 2023
…che#15034)

* Add AliasExprMacro to allow aliasing of native expression macros
* Add decode_base64_complex alias for complex_decode_base64
CaseyPan pushed a commit to CaseyPan/druid that referenced this pull request Nov 17, 2023
…che#15034)

* Add AliasExprMacro to allow aliasing of native expression macros
* Add decode_base64_complex alias for complex_decode_base64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants