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
16 changes: 16 additions & 0 deletions core/src/main/java/org/apache/calcite/rex/RexSimplify.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,22 @@ RexNode isFalse(RexNode e) {
private RexNode simplifyGenericNode(RexCall e) {
final List<RexNode> operands = new ArrayList<>(e.operands);
simplifyList(operands, UNKNOWN);

// Simplify CARDINALITY(MAP_KEYS(m)) -> CARDINALITY(m)
// and CARDINALITY(MAP_VALUES(m)) -> CARDINALITY(m)
if (e.getOperator().getName().equals("CARDINALITY")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you expect people write such programs?
There's potentially an infinite number of such optimizations we can consider, but are they useful in practice?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You are right. This is indeed an overly restrictive optimization, with little value; I currently share this view. Perhaps the value of Jira needs further discussion.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I had moved the discussion to jira.

&& operands.size() == 1
&& operands.get(0) instanceof RexCall) {
final RexCall innerCall = (RexCall) operands.get(0);
final SqlKind innerKind = innerCall.getKind();
if (innerKind == SqlKind.MAP_KEYS || innerKind == SqlKind.MAP_VALUES) {
// CARDINALITY(MAP_KEYS(m)) -> CARDINALITY(m)
// CARDINALITY(MAP_VALUES(m)) -> CARDINALITY(m)
final RexNode map = innerCall.getOperands().get(0);
return rexBuilder.makeCall(e.getParserPosition(), e.getOperator(), map);
}
}

if (e.operands.equals(operands)) {
return e;
}
Expand Down
60 changes: 60 additions & 0 deletions core/src/test/java/org/apache/calcite/rex/RexBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1524,4 +1524,64 @@ private static class UDT extends RelDataTypeImpl {
literal, literal, flag),
notNullValue());
}

@Test void testCardinalityMapKeysSimplification() {
final RexImplicationCheckerFixtures.Fixture f =
new RexImplicationCheckerFixtures.Fixture();
final RexBuilder rexBuilder = f.rexBuilder;

// Create map literal: map['foo', 1, 'bar', 2]
final RexLiteral fooKey = rexBuilder.makeLiteral("foo");
final RexLiteral oneVal = rexBuilder.makeExactLiteral(new java.math.BigDecimal(1));
final RexLiteral barKey = rexBuilder.makeLiteral("bar");
final RexLiteral twoVal = rexBuilder.makeExactLiteral(new java.math.BigDecimal(2));
final RexNode mapLiteral =
rexBuilder.makeCall(SqlStdOperatorTable.MAP_VALUE_CONSTRUCTOR,
fooKey, oneVal, barKey, twoVal);

// Create CARDINALITY(MAP_KEYS(map))
final RexNode mapKeys =
rexBuilder.makeCall(SqlLibraryOperators.MAP_KEYS, mapLiteral);
final RexNode cardinalityMapKeys =
rexBuilder.makeCall(SqlStdOperatorTable.CARDINALITY, mapKeys);

// Simplify
final RexNode simplified = f.simplify.simplify(cardinalityMapKeys);

// Expected: CARDINALITY(map)
final RexNode expectedCardinalityMap =
rexBuilder.makeCall(SqlStdOperatorTable.CARDINALITY, mapLiteral);

assertThat(simplified, hasToString(expectedCardinalityMap.toString()));
}

@Test void testCardinalityMapValuesSimplification() {
final RexImplicationCheckerFixtures.Fixture f =
new RexImplicationCheckerFixtures.Fixture();
final RexBuilder rexBuilder = f.rexBuilder;

// Create map literal: map['foo', 1, 'bar', 2]
final RexLiteral fooKey = rexBuilder.makeLiteral("foo");
final RexLiteral oneVal = rexBuilder.makeExactLiteral(new java.math.BigDecimal(1));
final RexLiteral barKey = rexBuilder.makeLiteral("bar");
final RexLiteral twoVal = rexBuilder.makeExactLiteral(new java.math.BigDecimal(2));
final RexNode mapLiteral =
rexBuilder.makeCall(SqlStdOperatorTable.MAP_VALUE_CONSTRUCTOR,
fooKey, oneVal, barKey, twoVal);

// Create CARDINALITY(MAP_VALUES(map))
final RexNode mapValues =
rexBuilder.makeCall(SqlLibraryOperators.MAP_VALUES, mapLiteral);
final RexNode cardinalityMapValues =
rexBuilder.makeCall(SqlStdOperatorTable.CARDINALITY, mapValues);

// Simplify
final RexNode simplified = f.simplify.simplify(cardinalityMapValues);

// Expected: CARDINALITY(map)
final RexNode expectedCardinalityMap =
rexBuilder.makeCall(SqlStdOperatorTable.CARDINALITY, mapLiteral);

assertThat(simplified, hasToString(expectedCardinalityMap.toString()));
}
}
Loading