-
Notifications
You must be signed in to change notification settings - Fork 74
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): support expressions in Sort #322
Open
yassram
wants to merge
4
commits into
substrait-io:main
Choose a base branch
from
yassram:yr/isthmus-sort-expressions-192
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3338960
fix(isthmus): support expressions in Sort
yassram 318f831
test(isthmus): additional tests for substrait to Calcite with Sort
yassram 060b960
test(isthmus): test (ComplexSortTest) adjustments
yassram 711828b
test(isthmus): add docstring to CollationRelWriter - pr suggestion
yassram 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
205 changes: 205 additions & 0 deletions
205
isthmus/src/test/java/io/substrait/isthmus/ComplexSortTest.java
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 |
---|---|---|
@@ -0,0 +1,205 @@ | ||
package io.substrait.isthmus; | ||
|
||
import static io.substrait.isthmus.SqlConverterBase.EXTENSION_COLLECTION; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import io.substrait.dsl.SubstraitBuilder; | ||
import io.substrait.expression.Expression; | ||
import io.substrait.relation.Rel; | ||
import io.substrait.type.TypeCreator; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.List; | ||
import org.apache.calcite.rel.RelNode; | ||
import org.apache.calcite.rel.externalize.RelWriterImpl; | ||
import org.apache.calcite.sql.SqlExplainLevel; | ||
import org.apache.calcite.util.Pair; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ComplexSortTest extends PlanTestBase { | ||
|
||
final TypeCreator R = TypeCreator.of(false); | ||
SubstraitBuilder b = new SubstraitBuilder(extensions); | ||
|
||
final SubstraitToCalcite substraitToCalcite = | ||
new SubstraitToCalcite(EXTENSION_COLLECTION, typeFactory); | ||
|
||
public static class CollationRelWriter extends RelWriterImpl { | ||
public CollationRelWriter(StringWriter sw) { | ||
super(new PrintWriter(sw), SqlExplainLevel.EXPPLAN_ATTRIBUTES, false); | ||
} | ||
|
||
@Override | ||
protected void explain_(RelNode rel, List<Pair<String, @Nullable Object>> values) { | ||
var collation = rel.getTraitSet().getCollation(); | ||
if (!collation.isDefault()) { | ||
StringBuilder s = new StringBuilder(); | ||
spacer.spaces(s); | ||
s.append("Collation: ").append(collation.toString()); | ||
pw.println(s); | ||
} | ||
super.explain_(rel, values); | ||
} | ||
} | ||
|
||
@Test | ||
void handleInputReferenceSort() { | ||
// CREATE TABLE example (a VARCHAR) | ||
// SELECT a FROM example ORDER BY a | ||
|
||
Rel rel = | ||
b.project( | ||
input -> b.fieldReferences(input, 0), | ||
b.remap(1), | ||
b.sort( | ||
input -> | ||
List.of( | ||
b.sortField( | ||
b.fieldReference(input, 0), Expression.SortDirection.ASC_NULLS_LAST)), | ||
b.namedScan(List.of("example"), List.of("a"), List.of(R.STRING)))); | ||
|
||
String expected = | ||
""" | ||
Collation: [0] | ||
LogicalSort(sort0=[$0], dir0=[ASC]) | ||
LogicalTableScan(table=[[example]]) | ||
"""; | ||
|
||
RelNode relReturned = substraitToCalcite.convert(rel); | ||
var sw = new StringWriter(); | ||
relReturned.explain(new CollationRelWriter(sw)); | ||
assertEquals(expected, sw.toString()); | ||
} | ||
|
||
@Test | ||
void handleCastExpressionSort() { | ||
// CREATE TABLE example (a VARCHAR) | ||
// SELECT a FROM example ORDER BY a::INT | ||
|
||
Rel rel = | ||
b.project( | ||
input -> b.fieldReferences(input, 0), | ||
b.remap(1), | ||
b.sort( | ||
input -> | ||
List.of( | ||
b.sortField( | ||
b.cast(b.fieldReference(input, 0), R.I32), | ||
Expression.SortDirection.ASC_NULLS_LAST)), | ||
b.namedScan(List.of("example"), List.of("a"), List.of(R.STRING)))); | ||
|
||
String expected = | ||
""" | ||
LogicalProject(a0=[$0]) | ||
Collation: [1] | ||
LogicalSort(sort0=[$1], dir0=[ASC]) | ||
LogicalProject(a=[$0], a0=[CAST($0):INTEGER NOT NULL]) | ||
LogicalTableScan(table=[[example]]) | ||
"""; | ||
|
||
RelNode relReturned = substraitToCalcite.convert(rel); | ||
var sw = new StringWriter(); | ||
relReturned.explain(new CollationRelWriter(sw)); | ||
assertEquals(expected, sw.toString()); | ||
vbarua marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Test | ||
void handleCastProjectAndSortWithSortDirection() { | ||
// CREATE TABLE example (a VARCHAR) | ||
// SELECT a::INT FROM example ORDER BY a::INT DESC NULLS LAST | ||
|
||
Rel rel = | ||
b.project( | ||
input -> List.of(b.cast(b.fieldReference(input, 0), R.I32)), | ||
b.remap(1), | ||
b.sort( | ||
input -> | ||
List.of( | ||
b.sortField( | ||
b.cast(b.fieldReference(input, 0), R.I32), | ||
Expression.SortDirection.DESC_NULLS_LAST)), | ||
b.namedScan(List.of("example"), List.of("a"), List.of(R.STRING)))); | ||
|
||
String expected = | ||
""" | ||
LogicalProject(a0=[CAST($0):INTEGER NOT NULL]) | ||
Collation: [1 DESC-nulls-last] | ||
LogicalSort(sort0=[$1], dir0=[DESC-nulls-last]) | ||
LogicalProject(a=[$0], a0=[CAST($0):INTEGER NOT NULL]) | ||
LogicalTableScan(table=[[example]]) | ||
"""; | ||
|
||
RelNode relReturned = substraitToCalcite.convert(rel); | ||
var sw = new StringWriter(); | ||
relReturned.explain(new CollationRelWriter(sw)); | ||
assertEquals(expected, sw.toString()); | ||
} | ||
|
||
@Test | ||
void handleCastSortToOriginalType() { | ||
// CREATE TABLE example (a VARCHAR) | ||
// SELECT a FROM example ORDER BY a::VARCHAR | ||
|
||
Rel rel = | ||
b.project( | ||
input -> List.of(b.fieldReference(input, 0)), | ||
b.remap(1), | ||
b.sort( | ||
input -> | ||
List.of( | ||
b.sortField( | ||
b.cast(b.fieldReference(input, 0), R.STRING), | ||
Expression.SortDirection.DESC_NULLS_LAST)), | ||
b.namedScan(List.of("example"), List.of("a"), List.of(R.STRING)))); | ||
|
||
String expected = | ||
""" | ||
LogicalProject(a0=[$0]) | ||
Collation: [1 DESC-nulls-last] | ||
LogicalSort(sort0=[$1], dir0=[DESC-nulls-last]) | ||
LogicalProject(a=[$0], a0=[$0]) | ||
LogicalTableScan(table=[[example]]) | ||
"""; | ||
|
||
RelNode relReturned = substraitToCalcite.convert(rel); | ||
var sw = new StringWriter(); | ||
relReturned.explain(new CollationRelWriter(sw)); | ||
assertEquals(expected, sw.toString()); | ||
} | ||
|
||
@Test | ||
void handleComplex2ExpressionSort() { | ||
// CREATE TABLE example (a VARCHAR, b INT) | ||
// SELECT b, a FROM example ORDER BY a::INT DESC, -b + 42 ASC NULLS LAST | ||
|
||
Rel rel = | ||
b.project( | ||
input -> List.of(b.fieldReference(input, 0), b.fieldReference(input, 1)), | ||
b.remap(2, 3), | ||
b.sort( | ||
input -> | ||
List.of( | ||
b.sortField( | ||
b.cast(b.fieldReference(input, 0), R.I32), | ||
Expression.SortDirection.DESC_NULLS_FIRST), | ||
b.sortField( | ||
b.add(b.negate(b.fieldReference(input, 1)), b.i32(42)), | ||
Expression.SortDirection.ASC_NULLS_LAST)), | ||
b.namedScan(List.of("example"), List.of("a", "b"), List.of(R.STRING, R.I32)))); | ||
|
||
String expected = | ||
""" | ||
LogicalProject(a0=[$0], b0=[$1]) | ||
Collation: [2 DESC, 3] | ||
LogicalSort(sort0=[$2], sort1=[$3], dir0=[DESC], dir1=[ASC]) | ||
LogicalProject(a=[$0], b=[$1], a0=[CAST($0):INTEGER NOT NULL], $f3=[+(-($1), 42)]) | ||
LogicalTableScan(table=[[example]]) | ||
"""; | ||
|
||
RelNode relReturned = substraitToCalcite.convert(rel); | ||
var sw = new StringWriter(); | ||
relReturned.explain(new CollationRelWriter(sw)); | ||
assertEquals(expected, sw.toString()); | ||
} | ||
} |
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.
Nice find! It's really helpful that we can rely on Calcite's machinery to do this for us.