-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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-6605] Lattice SQL supports complex column expressions #3987
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
* limitations under the License. | ||
*/ | ||
package org.apache.calcite.materialize; | ||
|
||
import org.apache.calcite.materialize.Lattice.Measure; | ||
import org.apache.calcite.prepare.PlannerImpl; | ||
import org.apache.calcite.rel.RelRoot; | ||
import org.apache.calcite.schema.SchemaPlus; | ||
|
@@ -35,6 +37,8 @@ | |
import org.apache.calcite.tools.Planner; | ||
import org.apache.calcite.tools.RelConversionException; | ||
import org.apache.calcite.tools.ValidationException; | ||
import org.apache.calcite.util.ImmutableBitSet; | ||
import org.apache.calcite.util.ImmutableBitSet.Builder; | ||
import org.apache.calcite.util.Util; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
@@ -752,6 +756,97 @@ private void checkDerivedColumn(Lattice lattice, List<String> tables, | |
checkDerivedColumn(lattice, tables, derivedColumns, 3, "n11", false); | ||
} | ||
|
||
/** Test case for | ||
* <a href="https://issues.apache.org/jira/browse/CALCITE-6605">[CALCITE-6605] | ||
* Lattice SQL supports complex column expressions </a>. */ | ||
@Test void testExpressionLatticeSql() throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can add some blank lines or a method to make the test sample code more readable.🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added description for tests. |
||
final Tester t = new Tester().foodmart().withEvolve(true); | ||
final String q0 = "select\n" | ||
+ " \"num_children_at_home\" + 12 as \"n12\",\n" | ||
+ " sum(\"num_children_at_home\") as \"n10\",\n" | ||
+ " count(*) as c\n" | ||
+ "from \"customer\"\n" | ||
+ "group by \"num_children_at_home\" + 12"; | ||
t.addQuery(q0); | ||
assertThat(t.s.latticeMap, aMapWithSize(1)); | ||
final Lattice lattice = Iterables.getOnlyElement(t.s.latticeMap.values()); | ||
final String l0 = "customer:[COUNT(), SUM(customer.num_children_at_home)]"; | ||
assertThat(Iterables.getOnlyElement(t.s.latticeMap.keySet()), is(l0)); | ||
ImmutableList<Measure> measures = lattice.defaultMeasures; | ||
assert measures.size() == 2; | ||
Builder groupSetBuilder = ImmutableBitSet.builder(); | ||
measures.forEach(measure -> groupSetBuilder.addAll(measure.argBitSet())); | ||
ImmutableBitSet groupSet = groupSetBuilder.build(); | ||
String sql = "SELECT \"customer\".\"num_children_at_home\", COUNT(*) AS \"m0\", " | ||
+ "SUM(\"customer\".\"num_children_at_home\") AS \"m1\"\n" | ||
+ "FROM \"foodmart\".\"customer\" AS \"customer\"\n" | ||
+ "GROUP BY \"customer\".\"num_children_at_home\""; | ||
assertThat(lattice.sql(groupSet, true, measures), | ||
is(sql)); | ||
} | ||
|
||
/** Test case for measure field involving a complex column operation, | ||
* for example sum("num_children_at_home" + 10). */ | ||
@Test void testExpressionLatticeSql2() throws Exception { | ||
final Tester t = new Tester().foodmart().withEvolve(true); | ||
final String q0 = "select\n" | ||
+ " \"num_children_at_home\" + 12 as \"n12\",\n" | ||
+ " sum(\"num_children_at_home\" + 10) as \"n10\",\n" | ||
+ " sum(\"num_children_at_home\" + 11) as \"n11\",\n" | ||
+ " count(*) as c\n" | ||
+ "from \"customer\"\n" | ||
+ "group by \"num_children_at_home\" + 12"; | ||
t.addQuery(q0); | ||
assertThat(t.s.latticeMap, aMapWithSize(1)); | ||
final Lattice lattice = Iterables.getOnlyElement(t.s.latticeMap.values()); | ||
final String l0 = "customer:[COUNT(), SUM(n10), SUM(n11)]"; | ||
assertThat(Iterables.getOnlyElement(t.s.latticeMap.keySet()), is(l0)); | ||
ImmutableList<Measure> measures = lattice.defaultMeasures; | ||
assert measures.size() == 3; | ||
Builder groupSetBuilder = ImmutableBitSet.builder(); | ||
measures.forEach(measure -> groupSetBuilder.addAll(measure.argBitSet())); | ||
ImmutableBitSet groupSet = groupSetBuilder.build(); | ||
String sql = "SELECT \"num_children_at_home\" + 10 AS \"n10\", " | ||
+ "\"num_children_at_home\" + 11 AS \"n11\", COUNT(*) AS \"m0\", " | ||
+ "SUM(\"num_children_at_home\" + 10) AS \"m1\", " | ||
+ "SUM(\"num_children_at_home\" + 11) AS \"m2\"\n" | ||
+ "FROM \"foodmart\".\"customer\" AS \"customer\"\n" | ||
+ "GROUP BY \"num_children_at_home\" + 10, \"num_children_at_home\" + 11"; | ||
assertThat(lattice.sql(groupSet, true, measures), | ||
is(sql)); | ||
} | ||
|
||
/** Test case for measure field involving a complex column operation with functions, | ||
* for example sum(cast("num_children_at_home" as double) + 11). */ | ||
@Test void testExpressionLatticeSql3() throws Exception { | ||
final Tester t = new Tester().foodmart().withEvolve(true); | ||
final String q0 = "select\n" | ||
+ " \"num_children_at_home\" + 12 as \"n12\",\n" | ||
+ " sum(\"num_children_at_home\" + 10) as \"n10\",\n" | ||
+ " sum(cast(\"num_children_at_home\" as double) + 11) as \"n11\",\n" | ||
+ " count(*) as c\n" | ||
+ "from \"customer\"\n" | ||
+ "group by \"num_children_at_home\" + 12"; | ||
t.addQuery(q0); | ||
assertThat(t.s.latticeMap, aMapWithSize(1)); | ||
final Lattice lattice = Iterables.getOnlyElement(t.s.latticeMap.values()); | ||
final String l0 = "customer:[COUNT(), SUM(n10), SUM(n11)]"; | ||
assertThat(Iterables.getOnlyElement(t.s.latticeMap.keySet()), is(l0)); | ||
ImmutableList<Measure> measures = lattice.defaultMeasures; | ||
assert measures.size() == 3; | ||
Builder groupSetBuilder = ImmutableBitSet.builder(); | ||
measures.forEach(measure -> groupSetBuilder.addAll(measure.argBitSet())); | ||
ImmutableBitSet groupSet = groupSetBuilder.build(); | ||
String sql = "SELECT \"num_children_at_home\" + 10 AS \"n10\", " | ||
+ "CAST(\"num_children_at_home\" AS DOUBLE) + 11 AS \"n11\", " | ||
+ "COUNT(*) AS \"m0\", SUM(\"num_children_at_home\" + 10) AS \"m1\", " | ||
+ "SUM(CAST(\"num_children_at_home\" AS DOUBLE) + 11) AS \"m2\"\n" | ||
+ "FROM \"foodmart\".\"customer\" AS \"customer\"\n" | ||
+ "GROUP BY \"num_children_at_home\" + 10, CAST(\"num_children_at_home\" AS DOUBLE) + 11"; | ||
assertThat(lattice.sql(groupSet, true, measures), | ||
is(sql)); | ||
} | ||
|
||
private void checkFoodmartSimpleJoin(CalciteAssert.SchemaSpec schemaSpec) | ||
throws Exception { | ||
final FrameworkConfig config = Frameworks.newConfigBuilder() | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This probably should have some more descriptive error
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.
+1
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.
It will be called when lattice column generates sql.
Originally, for
DerivedColumn
, theUnsupportedOperationException
will be thrown directly. This PR will obtain the corresponding column based on index and return the field object.Currently there are only two types of columns:
BaseColumn
,DerivedColumn
, theoretically no exception will be thrown. But in order to avoid extreme cases that are not covered, the logic of throwing exceptions before is also retained.