Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@
* @param program Required only if {@code rex} contains {@link RexLocalRef}
* @param rex Expression to convert
*/
public SqlNode toSql(@Nullable RexProgram program, RexNode rex) {

Check warning on line 697 in core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 178 to 64, Complexity from 39 to 14, Nesting Level from 3 to 2, Number of Variables from 46 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_calcite&issues=AZ-DVjKSxYjh3jIVuw1g&open=AZ-DVjKSxYjh3jIVuw1g&pullRequest=5081
rex = dialect.prepareUnparse(rex);
final RexSubQuery subQuery;
final SqlNode sqlSubQuery;
Expand Down Expand Up @@ -1077,7 +1077,8 @@
partitionKeys.add(this.field(partition));
}
for (RelFieldCollation collation : group.orderKeys.getFieldCollations()) {
this.addOrderItem(orderByKeys, collation);
// A window ORDER BY has no ordinal notion; resolve via field().
this.addOrderItem(orderByKeys, collation, false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems this branch isn't covered by tests. Could you show the execution results for the case where the value is false?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

didn't get what you mean by that, can you elaborate please?

This is exactly the case covered by testWindowOrderByExpression in this PR

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, I see—regarding the addOrderItem method, you added a parameter called allowsOrdinal. The case statement seems to cover the scenario where allowsOrdinal is true; if I’m not mistaken, shouldn't there also be a case for when it is false?

}
SqlLiteral isRows = SqlLiteral.createBoolean(group.isRows, POS);
SqlNode lowerBound = null;
Expand Down Expand Up @@ -1290,6 +1291,15 @@
}

void addOrderItem(List<SqlNode> orderByList, RelFieldCollation field) {
addOrderItem(orderByList, field, true);
}

/** Adds an ORDER BY item, optionally allowing an expression key to be
* emitted as a positional ordinal. Ordinals are valid only in a
* statement-level ORDER BY; callers such as a window's ORDER BY must pass
* {@code allowsOrdinal = false}. */
void addOrderItem(List<SqlNode> orderByList, RelFieldCollation field,
boolean allowsOrdinal) {
if (field.nullDirection != RelFieldCollation.NullDirection.UNSPECIFIED) {
final boolean first =
field.nullDirection == RelFieldCollation.NullDirection.FIRST;
Expand All @@ -1303,7 +1313,7 @@
RelFieldCollation.NullDirection.UNSPECIFIED);
}
}
orderByList.add(toSql(field));
orderByList.add(toSql(field, allowsOrdinal));
}

/** Converts a RexFieldCollation to an ORDER BY item. */
Expand Down Expand Up @@ -1426,7 +1436,15 @@

/** Converts a collation to an ORDER BY item. */
public SqlNode toSql(RelFieldCollation collation) {
SqlNode node = orderField(collation.getFieldIndex());
return toSql(collation, true);
}

/** Converts a collation to an ORDER BY item; see
* {@link #addOrderItem(List, RelFieldCollation, boolean)}. */
public SqlNode toSql(RelFieldCollation collation, boolean allowsOrdinal) {
SqlNode node = allowsOrdinal
? orderField(collation.getFieldIndex())
: field(collation.getFieldIndex());
switch (collation.getDirection()) {
case DESCENDING:
case STRICTLY_DESCENDING:
Expand Down Expand Up @@ -1550,7 +1568,7 @@
}

/** Converts a {@link RexLiteral} to a {@link SqlLiteral}. */
public static SqlNode toSql(RexLiteral literal) {

Check warning on line 1571 in core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 91 to 64, Complexity from 25 to 14, Nesting Level from 3 to 2, Number of Variables from 13 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_calcite&issues=AZ-DVjKSxYjh3jIVuw1h&open=AZ-DVjKSxYjh3jIVuw1h&pullRequest=5081
SqlTypeName typeName = literal.getTypeName();
switch (typeName) {
case SYMBOL:
Expand Down Expand Up @@ -2094,7 +2112,7 @@
}

/** Returns whether a new sub-query is required. */
private boolean needNewSubQuery(

Check warning on line 2115 in core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 73 to 64, Complexity from 34 to 14, Nesting Level from 3 to 2, Number of Variables from 15 to 6.

See more on https://sonarcloud.io/project/issues?id=apache_calcite&issues=AZ-DVjKSxYjh3jIVuw1i&open=AZ-DVjKSxYjh3jIVuw1i&pullRequest=5081
@UnknownInitialization Result this,
RelNode rel, List<Clause> clauses,
Set<Clause> expectedClauses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5984,6 +5984,32 @@ private void checkLiteral2(String expression, String expected) {
sql(query8).optimize(rules, hepPlanner).ok(expected8);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7644">[CALCITE-7644]
* Window's ORDER BY expression is unparsed as positional ordinal</a>. */
@Test void testWindowOrderByExpression() {
// A window ORDER BY expression must not be unparsed as a positional ordinal.
final String query = "SELECT \"employee_id\", \"salary\", \"hire_date\", "
+ "SUM(\"salary\") OVER ("
+ "PARTITION BY \"employee_id\" "
+ "ORDER BY CASE WHEN \"salary\" > 1000 THEN 1 ELSE 0 END "
+ "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS \"sum_val\"\n"
+ "FROM \"employee\"";
final String expected = "SELECT \"employee_id\", \"salary\", \"hire_date\", "
+ "SUM(\"salary\") OVER (PARTITION BY \"employee_id\" "
+ "ORDER BY CASE WHEN CAST(\"salary\" AS DECIMAL(14, 4)) > 1000.0000 "
+ "THEN 1 ELSE 0 END "
+ "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS \"sum_val\"\n"
+ "FROM \"foodmart\".\"employee\"";

final HepProgramBuilder builder = new HepProgramBuilder();
builder.addRuleClass(ProjectToWindowRule.class);
final HepPlanner hepPlanner = new HepPlanner(builder.build());
final RuleSet rules =
RuleSets.ofList(CoreRules.PROJECT_TO_LOGICAL_PROJECT_AND_WINDOW);
sql(query).optimize(rules, hepPlanner).ok(expected);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6475">[CALCITE-6475]
* RelToSql converter fails when the IN-list contains NULL
Expand Down
Loading