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

fix(isthmus): allow for conversion of plans containing Calcite aggregate functions #230

Conversation

bvolpato
Copy link
Member

Some of the Calcite CoreRules might bring their own functions at a late stage, so I've decided to make the converter flexible enough and handle the superclass types correctly (since we already do a similar conversion to APPROX_COUNT_DISTINCT in the same spot).

Another alternative would be to improve the matchers and check if names match or if it's an instanceof the passing call -- today signatures is an IdentityHashMap, so it's really strict.

@bvolpato bvolpato force-pushed the aggregate-function-converter-calcite-instance branch from bc32c60 to f7a2a65 Compare February 14, 2024 01:32
@bvolpato
Copy link
Member Author

FYI Check failures are unrelated to the change:

/home/runner/work/substrait-java/substrait-java/core/src/test/java/io/substrait/type/proto/LocalFilesRoundtripTest.java:61: error: switch expressions are not supported in -source 8
    return switch (pathTypeCase) {
           ^
  (use -source 14 or higher to enable switch expressions)
/home/runner/work/substrait-java/substrait-java/core/src/test/java/io/substrait/type/proto/LocalFilesRoundtripTest.java:62: error: switch rules are not supported in -source 8
      case URI_PATH -> builder.pathType(FileOrFiles.PathType.URI_PATH).path("path");

@bvolpato bvolpato force-pushed the aggregate-function-converter-calcite-instance branch from f7a2a65 to 2139cff Compare February 14, 2024 02:10
@bvolpato bvolpato changed the title fix: change AggregateFunctionConverter to accept Calcite function instances fix(isthmus): change AggregateFunctionConverter to accept Calcite function instances Feb 14, 2024
@bvolpato bvolpato force-pushed the aggregate-function-converter-calcite-instance branch from 5501161 to b9855de Compare February 14, 2024 21:08
@vbarua vbarua force-pushed the aggregate-function-converter-calcite-instance branch from b9855de to 0e29574 Compare February 16, 2024 22:24
@vbarua vbarua self-requested a review February 17, 2024 01:21
@vbarua vbarua changed the title fix(isthmus): change AggregateFunctionConverter to accept Calcite function instances fix(isthmus): allow for conversion of plans containing Calcite aggregate functions Feb 21, 2024
@vbarua vbarua force-pushed the aggregate-function-converter-calcite-instance branch from 8201204 to 6c0c6fd Compare February 22, 2024 00:02
return Optional.of(
fun.getKind() == SqlKind.MIN ? AggregateFunctions.MIN : AggregateFunctions.MAX);
} else if (aggFunction instanceof SqlAvgAggFunction) {
return Optional.of(AggregateFunctions.AVG);
} else if (aggFunction instanceof SqlSumAggFunction) {
return Optional.of(AggregateFunctions.SUM);
} else if (aggFunction instanceof SqlSumEmptyIsZeroAggFunction) {
return Optional.of(AggregateFunctions.SUM0);
} else {
return Optional.empty();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-ordered the checks to match the function definitions above.

Copy link
Member

@vbarua vbarua left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for these changes!

I was poking around to add the conversionHandlesBuiltInSum0CallAddedByRule test and made some small changes along the way as well.

If the code in 6c0c6fd looks good to you, we can merge this in.

AggregateFunctions.getSubstraitAggVariant(aggFunction).orElse(aggFunction);
// Replace default Calcite aggregate calls with Substrait specific variants.
// See toSubstraitAggVariant for more details.
AggregateFunctions.toSubstraitAggVariant(aggFunction).orElse(aggFunction);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added more detailed docs to the function definition

public class OptimizerIntegrationTest extends PlanTestBase {

@Test
void conversionHandlesBuiltInSum0CallAddedByRule() throws SqlParseException, IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test captures how the default Calcite aggregate calls can be introduced by rules and verifies the conversion executes without failing even when this occur (thanks to your changes ✨ )

}

final SubstraitBuilder b = new SubstraitBuilder(EXTENSION_COLLECTION);
public class AggregateFunctionConverterTest extends PlanTestBase {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-using code from PlanTestBase.

@@ -55,5 +30,6 @@ public void testFunctionFinderMatch() {
null));
assertNotNull(functionFinder);
assertEquals("sum0", functionFinder.getName());
assertEquals(AggregateFunctions.SUM0, functionFinder.getOperator());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing the name is insufficient as both SqlSumEmptyIsZeroAggFunction and SubstraitSumEmptyIsZeroAggFunction have the same name.

@vbarua vbarua force-pushed the aggregate-function-converter-calcite-instance branch from 6c0c6fd to 09b6b2f Compare February 22, 2024 00:45
@bvolpato
Copy link
Member Author

Thanks for these changes!

I was poking around to add the conversionHandlesBuiltInSum0CallAddedByRule test and made some small changes along the way as well.

If the code in 6c0c6fd looks good to you, we can merge this in.

It looks good to me! Thanks for all the improvements / suggestions!

@vbarua vbarua merged commit 0bdac49 into substrait-io:main Feb 22, 2024
8 checks passed
ajegou pushed a commit to ajegou/substrait-java that referenced this pull request Mar 29, 2024
…Functions (substrait-io#230)

Calcite planning rules can introduce the Calcite variants of SqlAggFunctions

In substrait-io#180, Substrait specific variants for these function were introduced which
better matched the type inference for these functions as defined in Substrait

Those changes cause failures when converting the Calcite variants to Substrait,
which is what these changes address

---------

Co-authored-by: Victor Barua <victor.barua@datadoghq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants