Skip to content
Merged
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 @@ -123,7 +123,6 @@
import org.apache.calcite.util.trace.CalciteTrace;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

Expand Down Expand Up @@ -568,8 +567,8 @@
return false;
}

private static Map<String, String> getFieldAliases(final SelectScope scope) {
final ImmutableMap.Builder<String, String> fieldAliases = new ImmutableMap.Builder<>();
private static ImmutableSet<String> getFieldsAliased(final SelectScope scope) {

Check warning on line 570 in core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move this method into "Expander".

See more on https://sonarcloud.io/project/issues?id=apache_calcite&issues=AZ30pOWQUrAl3R5f4zSj&open=AZ30pOWQUrAl3R5f4zSj&pullRequest=4917
final ImmutableSet.Builder<String> result = new ImmutableSet.Builder<>();

for (SqlNode selectItem : scope.getNode().getSelectList()) {
if (selectItem instanceof SqlCall) {
Expand All @@ -580,12 +579,11 @@
}

final SqlIdentifier fieldIdentifier = call.operand(0);
fieldAliases.put(fieldIdentifier.getSimple(),
((SqlIdentifier) call.operand(1)).getSimple());
result.add(fieldIdentifier.names.get(fieldIdentifier.names.size() - 1));
}
}

return fieldAliases.build();
return result.build();
}

/** Returns the set of field names in the join condition specified by USING
Expand Down Expand Up @@ -7520,7 +7518,7 @@
}

final SqlNameMatcher matcher = validator.getCatalogReader().nameMatcher();
final Map<String, String> fieldAliases = getFieldAliases(scope);
final Set<String> fieldAliases = getFieldsAliased(scope);

for (String name : commonColumnNames) {
if (matcher.matches(identifier.getSimple(), name)) {
Expand All @@ -7537,13 +7535,12 @@

assert qualifiedNode.size() == 2;

// If there is an alias for the column, no need to wrap the coalesce with an AS operator
boolean haveAlias = fieldAliases.containsKey(name);

final SqlCall coalesceCall =
SqlStdOperatorTable.COALESCE.createCall(SqlParserPos.ZERO, qualifiedNode.get(0),
qualifiedNode.get(1));

// If there is an alias for the column, no need to wrap the coalesce with an AS operator
boolean haveAlias = fieldAliases.contains(name);
if (haveAlias) {
return coalesceCall;
} else {
Expand Down
33 changes: 32 additions & 1 deletion core/src/test/resources/sql/planner.iq
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,40 @@ EnumerableCalc(expr#0..2=[{inputs}], $f0=[$t1], $f1=[$t2])
EnumerableValues(tuples=[[{ 10 }, { 10 }, { 20 }, { 30 }, { 30 }, { 50 }, { 50 }, { 60 }, { null }]])
!plan
!set planner-rules original
!use blank

# Test case for [CALCITE-7501] Assertion error in alias expansion for LEFT JOIN USING
CREATE TABLE D(sk_cid INT, dt DATE, dm_sym VARCHAR, fhd DATE);
(0 rows modified)

!update

CREATE TABLE F(sk_cid INT);
(0 rows modified)

!update

CREATE TABLE S(sk_sid INT, sym VARCHAR);
(0 rows modified)

!update

SELECT
d.dt as dtn,
fhd as sk_fhd
FROM D
JOIN S
ON S.sym = D.dm_sym
LEFT JOIN F USING (sk_cid);
+-----+--------+
| DTN | SK_FHD |
+-----+--------+
+-----+--------+
(0 rows)

!ok

# Add tests for [CALCITE-6985] to verify AggregateMinMaxToLimitRule handles empty tables correctly
!use blank
create table t_empty (id int);
(0 rows modified)

Expand Down
Loading