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

[CALCITE-5984] Allow disabling field trim via SqlToRelConverter config #3436

Closed
wants to merge 5 commits into from

Conversation

WegdanGhazi
Copy link
Contributor

@WegdanGhazi WegdanGhazi commented Sep 21, 2023

CALCITE-5984 Allow disabling field trim via SqlToRelConverter config

Use the value of isTrimUnusedFields set to SqlToRelConverter.Config to determine whether to trim unused fields, making it possible to completely disable trimming.

As seen here, the Prepare#trim function creates a new instance of SqlToRelConverter.Config, overwriting existing isTrimUnusedFields settings.

This PR fixes this by checking the boolean trimUnusedFields before calling #trim.

Unit tests were added to ensure that trimming can be completely disabled by controlling:

  1. Disabling isTrimUnusedFields on SqlToRelConverter.Config.
  2. Removing TrimFieldsProgram from the initialization of the optimization program.


Hook.TRIMMED.run(root.rel);
Copy link
Contributor

Choose a reason for hiding this comment

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

I see that this is same code,why you changed this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's no change in the code itself, I've only moved this logic inside a condition based on the config that determined whether or not to trim unused fields

+ " join \"hr\".\"emps\" as \"e\" on \"d\".\"deptno\" = \"e\".\"deptno\" \n")
.withHook(Hook.SQL2REL_CONVERTER_CONFIG_BUILDER,
(Consumer<Holder<Config>>) configHolder ->
configHolder.set(configHolder.get().withTrimUnusedFields(true)))
Copy link
Contributor

Choose a reason for hiding this comment

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

I see the sql is same as above,the withTrimUnusedFields method has different parameters, but the plan is the same.

Why did you use this SQL example?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Allowing field trimming happens by

  1. Specifying SQL2RelConverter Config
    or
  2. Specifying a Program that contains the Field trimming program (the standard program)

I tried to showcase all the different ways config 1 & 2 will enable trimming, while trimming will be disabled if both 1. Converter config and 2. Program Config disable it

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @WegdanGhazi ,thanks for your reply.

I have saw the difference between testJoinWithTrimmingEnabledByProgramAndConfig and testJoinWithTrimmingEnabledByProgram is the withTrimUnusedFields param value,one is false,another is true.

But the input sql and expected plan is same,so I'm a little confused about the purpose of these two unit tests.I thought a few code comments illustrating this case would make it easier for others to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it! Will add comments, thanks!

@@ -8247,6 +8343,33 @@ private static String sums(int n, boolean c) {
return b.toString();
}

/** A program that omits {@link TrimFieldsProgram}. */
private static Program getProgram() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe could use org.apache.calcite.tools.Programs#standard(org.apache.calcite.rel.metadata.RelMetadataProvider) method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The standard program is initialized as follows:

    return sequence(subQuery(metadataProvider),
        new DecorrelateProgram(),
        new TrimFieldsProgram(),
        program1,

        // Second planner pass to do physical "tweaks". This the first time
        // that EnumerableCalcRel is introduced.
        calc(metadataProvider));

Which includes the TrimFieldsProgram(), so I had to manually initialize one to avoid adding that program.
What do you think? Do you suggest a better way to create the program without TrimFieldsProgram()?

Copy link
Contributor

Choose a reason for hiding this comment

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

Could we make the standard(RelMetadataProvider) method has parameters with RelMetadataProvider and boolean flag?

The boolean flag indicates whether TrimFieldsProgram should be added,The standard() method default value is true.I finded Programs#standard(RelMetadataProvider) method only used in Programs.

Copy link
Contributor Author

@WegdanGhazi WegdanGhazi Sep 26, 2023

Choose a reason for hiding this comment

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

That sounds good! Will go for that!

@LakeShen
Copy link
Contributor

@WegdanGhazi thank you for opening this PR, left a couple of comments!

@sonarcloud
Copy link

sonarcloud bot commented Sep 26, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

94.7% 94.7% Coverage
0.0% 0.0% Duplication

@LakeShen
Copy link
Contributor

Hi @WegdanGhazi ,If you think we could add this feature in 1.36.0,please change this jira Fix Version to 1.36.0.

And also need the others review.

@WegdanGhazi
Copy link
Contributor Author

Hi @LakeShen, Thank you so much for the review! The current state is that we're waiting for more reviews, right?

@LakeShen
Copy link
Contributor

Yes,you are right.

* <a href="https://issues.apache.org/jira/browse/CALCITE-5984">[CALCITE-5984]</a>
* Disabling trimming of unused fields via config and program.
* Case: trimmingByProgram: false ; trimmingByConfig: true. */
@Test void testJoinWithTrimmingEnabledByConfig() {
Copy link
Contributor

Choose a reason for hiding this comment

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

you can use @ParameterizedTest from junit5 or make your custom input collection and unite 3 below tests into one, wdyt ?

Copy link

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

🚀

}

/** Returns the standard program with user metadata provider. */
public static Program standard(RelMetadataProvider metadataProvider) {
public static Program standard(RelMetadataProvider metadataProvider,
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 afraid that this is a public API that we need to consider compatibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

WDYT about using method overloads to keep the functionality? Pushed something.

@@ -679,6 +679,7 @@ static void assertPrepare(
Connection connection,
String sql,
boolean materializationsEnabled,
List<Pair<Hook, Consumer>> hooks,
Copy link
Member

Choose a reason for hiding this comment

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

This is also a public API (testkit are expected to be used by third party projects)

Copy link
Contributor Author

@WegdanGhazi WegdanGhazi Oct 31, 2023

Choose a reason for hiding this comment

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

Added an overload function to maintain the public API.

Copy link

sonarcloud bot commented Oct 31, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

81.8% 81.8% Coverage
0.0% 0.0% Duplication

Copy link
Member

@libenchao libenchao left a comment

Choose a reason for hiding this comment

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

+1

@libenchao libenchao closed this in 8af1f11 Nov 1, 2023
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.

5 participants