Skip to content

Commit

Permalink
Revert "[CALCITE-6519] Non-aggregate query that uses measure in ORDER…
Browse files Browse the repository at this point in the history
… BY"

This reverts commit 27fd583.
  • Loading branch information
olivrlee authored and Anthrino committed Nov 26, 2024
1 parent e3a790d commit ab353e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
import org.apache.calcite.sql.SqlWithItem;
import org.apache.calcite.sql.TableCharacteristic;
import org.apache.calcite.sql.fun.SqlCase;
import org.apache.calcite.sql.fun.SqlInternalOperators;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.AssignableOperandTypeChecker;
Expand Down Expand Up @@ -4869,32 +4868,17 @@ private void validateOrderItem(SqlSelect select, SqlNode orderItem) {
}

@Override public SqlNode expandOrderExpr(SqlSelect select, SqlNode orderExpr) {
final SqlNode orderExpr2 =
final SqlNode newSqlNode =
new OrderExpressionExpander(select, orderExpr).go();
if (orderExpr2 == orderExpr) {
return orderExpr2;
if (newSqlNode != orderExpr) {
final SqlValidatorScope scope = getOrderScope(select);
inferUnknownTypes(unknownType, scope, newSqlNode);
final RelDataType type = deriveType(scope, newSqlNode);
setValidatedNodeType(newSqlNode, type);
}

final SqlValidatorScope scope = getOrderScope(select);
inferUnknownTypes(unknownType, scope, orderExpr2);
final RelDataType type = deriveType(scope, orderExpr2);
setValidatedNodeType(orderExpr2, type);
if (!type.isMeasure()) {
return orderExpr2;
}

final SqlNode orderExpr3 = measureToValue(orderExpr2);
final RelDataType type3 = deriveType(scope, orderExpr3);
setValidatedNodeType(orderExpr3, type3);
return orderExpr3;
return newSqlNode;
}

private static SqlNode measureToValue(SqlNode e) {
if (e.getKind() == SqlKind.V2M) {
return ((SqlCall) e).operand(0);
}
return SqlInternalOperators.M2V.createCall(e.getParserPosition(), e);
}

/**
* Validates the GROUP BY clause of a SELECT statement. This method is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3742,6 +3742,7 @@ protected RelFieldCollation convertOrderItem(
SqlNode orderItem, List<SqlNode> extraExprs,
RelFieldCollation.Direction direction,
RelFieldCollation.NullDirection nullDirection) {
assert select != null;
// Handle DESC keyword, e.g. 'select a, b from t order by a desc'.
switch (orderItem.getKind()) {
case DESCENDING:
Expand Down Expand Up @@ -3769,22 +3770,21 @@ protected RelFieldCollation convertOrderItem(
break;
}

final SqlValidator validator = validator();
SqlNode converted = validator().expandOrderExpr(select, orderItem);

switch (nullDirection) {
case UNSPECIFIED:
nullDirection = validator.config().defaultNullCollation().last(desc(direction))
nullDirection = validator().config().defaultNullCollation().last(desc(direction))
? RelFieldCollation.NullDirection.LAST
: RelFieldCollation.NullDirection.FIRST;
break;
default:
break;
}

SqlNode converted = validator.expandOrderExpr(select, orderItem);

// Scan the select list and order exprs for an identical expression.
final SelectScope selectScope =
requireNonNull(validator.getRawSelectScope(select),
requireNonNull(validator().getRawSelectScope(select),
() -> "getRawSelectScope is not found for " + select);
int ordinal = -1;
List<SqlNode> expandedSelectList = selectScope.getExpandedSelectList();
Expand Down

0 comments on commit ab353e4

Please sign in to comment.