Skip to content

Commit

Permalink
KE-11534 [follow up] remove some inappropriate changes
Browse files Browse the repository at this point in the history
support ifnull
  • Loading branch information
pfzhan committed Dec 12, 2023
1 parent 11b9679 commit 93199da
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 676 deletions.
122 changes: 0 additions & 122 deletions core/src/main/java/org/apache/calcite/plan/RelOptUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2363,7 +2363,6 @@ private static RexNode isDistinctFromInternal(
RexNode y,
boolean neg) {

// see https://olapio.atlassian.net/browse/KE-42039
if (neg) {
// x is not distinct from y
// x=y IS TRUE or ((x is null) and (y is null)),
Expand Down Expand Up @@ -2894,127 +2893,6 @@ public static boolean classifyFilters(
return !filtersToRemove.isEmpty();
}

/**
* see https://olapio.atlassian.net/browse/KE-42039
* Calcite 1.30 copy from following method and add new parameter - shiftedMapping
* @see RelOptUtil#classifyFilters(RelNode, List, boolean, boolean, boolean, List, List, List)
*
* Classifies filters according to where they should be processed. They
* either stay where they are, are pushed to the join (if they originated
* from above the join), or are pushed to one of the children. Filters that
* are pushed are added to list passed in as input parameters.
*
* @param joinRel join node
* @param filters filters to be classified
* @param pushInto whether filters can be pushed into the join
* @param pushLeft true if filters can be pushed to the left
* @param pushRight true if filters can be pushed to the right
* @param joinFilters list of filters to push to the join
* @param leftFilters list of filters to push to the left child
* @param rightFilters list of filters to push to the right child
* @param shiftedMapping list of leftFilters or rightFilters to push to the child
* @return whether at least one filter was pushed
*/
public static boolean classifyFilters(
RelNode joinRel,
List<RexNode> filters,
boolean pushInto,
boolean pushLeft,
boolean pushRight,
List<RexNode> joinFilters,
List<RexNode> leftFilters,
List<RexNode> rightFilters,
Map<RexNode, RexNode> shiftedMapping) {
RexBuilder rexBuilder = joinRel.getCluster().getRexBuilder();
List<RelDataTypeField> joinFields = joinRel.getRowType().getFieldList();
final int nSysFields = 0; // joinRel.getSystemFieldList().size();
final List<RelDataTypeField> leftFields =
joinRel.getInputs().get(0).getRowType().getFieldList();
final int nFieldsLeft = leftFields.size();
final List<RelDataTypeField> rightFields =
joinRel.getInputs().get(1).getRowType().getFieldList();
final int nFieldsRight = rightFields.size();
final int nTotalFields = nFieldsLeft + nFieldsRight;

// set the reference bitmaps for the left and right children
ImmutableBitSet leftBitmap =
ImmutableBitSet.range(nSysFields, nSysFields + nFieldsLeft);
ImmutableBitSet rightBitmap =
ImmutableBitSet.range(nSysFields + nFieldsLeft, nTotalFields);

final List<RexNode> filtersToRemove = new ArrayList<>();
for (RexNode filter : filters) {
final InputFinder inputFinder = InputFinder.analyze(filter);
final ImmutableBitSet inputBits = inputFinder.build();

// REVIEW - are there any expressions that need special handling
// and therefore cannot be pushed?

if (pushLeft && leftBitmap.contains(inputBits)) {
// ignore filters that always evaluate to true
if (!filter.isAlwaysTrue()) {
// adjust the field references in the filter to reflect
// that fields in the left now shift over by the number
// of system fields
final RexNode shiftedFilter =
shiftFilter(
nSysFields,
nSysFields + nFieldsLeft,
-nSysFields,
rexBuilder,
joinFields,
nTotalFields,
leftFields,
filter);

leftFilters.add(shiftedFilter);
shiftedMapping.put(shiftedFilter, filter);
}
filtersToRemove.add(filter);
} else if (pushRight && rightBitmap.contains(inputBits)) {
if (!filter.isAlwaysTrue()) {
// adjust the field references in the filter to reflect
// that fields in the right now shift over to the left
final RexNode shiftedFilter =
shiftFilter(
nSysFields + nFieldsLeft,
nTotalFields,
-(nSysFields + nFieldsLeft),
rexBuilder,
joinFields,
nTotalFields,
rightFields,
filter);
rightFilters.add(shiftedFilter);
shiftedMapping.put(shiftedFilter, filter);
}
filtersToRemove.add(filter);

} else {
// If the filter can't be pushed to either child, we may push them into the join
if (pushInto) {
// Calcite 1.30 do not do the acutal push into
// remove above filters only if the filter is within the filter condition
/*if (!joinFilters.contains(filter)) {
joinFilters.add(filter);
}
filtersToRemove.add(filter);*/
if (joinFilters.stream().anyMatch(filter::equals)) {
filtersToRemove.add(filter);
}
}
}
}

// Remove filters after the loop, to prevent concurrent modification.
if (!filtersToRemove.isEmpty()) {
filters.removeAll(filtersToRemove);
}

// Did anything change?
return !filtersToRemove.isEmpty();
}

/**
* Classifies filters according to where they should be processed. They
* either stay where they are, are pushed to the join (if they originated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.calcite.rel.core.Filter;
import org.apache.calcite.rel.core.Join;
import org.apache.calcite.rel.core.Project;
import org.apache.calcite.rel.core.Values;
import org.apache.calcite.rel.core.Window;
import org.apache.calcite.rel.logical.LogicalCalc;
import org.apache.calcite.rel.logical.LogicalFilter;
Expand Down Expand Up @@ -180,8 +179,7 @@ public FilterReduceExpressionsRule(Class<? extends Filter> filterClass,
filter.getInput());
} else if (newConditionExp instanceof RexLiteral
|| RexUtil.isNullLiteral(newConditionExp, true)) {
Values values = (Values) createEmptyRelOrEquivalent(call, filter);
call.transformTo(values.copy(call.rel(0).getTraitSet(), values.getInputs()));
call.transformTo(createEmptyRelOrEquivalent(call, filter));
} else if (reduced) {
call.transformTo(call.builder()
.push(filter.getInput())
Expand Down
18 changes: 18 additions & 0 deletions core/src/main/java/org/apache/calcite/rex/RexBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.calcite.rel.core.CorrelationId;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeFactoryImpl;
import org.apache.calcite.rel.type.RelDataTypeField;
import org.apache.calcite.runtime.FlatLists;
import org.apache.calcite.runtime.Geometries;
Expand Down Expand Up @@ -642,6 +643,11 @@ protected static TimeUnit baseUnit(SqlTypeName unit) {

boolean canRemoveCastFromLiteral(RelDataType toType, @Nullable Comparable value,
SqlTypeName fromTypeName) {
if (value == null) {
return true;
} else if (isKylinUdfObjectType(toType)) {
return false;
}
final SqlTypeName sqlType = toType.getSqlTypeName();
if (!RexLiteral.valueMatchesType(value, sqlType, false)) {
return false;
Expand Down Expand Up @@ -681,6 +687,15 @@ boolean canRemoveCastFromLiteral(RelDataType toType, @Nullable Comparable value,
return true;
}

private static boolean isKylinUdfObjectType(RelDataType toType) {
if (toType instanceof RelDataTypeFactoryImpl.JavaType) {
// kylin udf to support null, very dirty to remove
RelDataTypeFactoryImpl.JavaType javaType = (RelDataTypeFactoryImpl.JavaType) toType;
return javaType.getJavaClass() == Object.class;
}
return false;
}

private RexNode makeCastExactToBoolean(RelDataType toType, RexNode exp) {
return makeCall(toType,
SqlStdOperatorTable.NOT_EQUALS,
Expand Down Expand Up @@ -783,6 +798,9 @@ public RexNode decodeIntervalOrDecimal(RexNode node) {
public RexNode makeAbstractCast(
RelDataType type,
RexNode exp) {
if (isKylinUdfObjectType(type)) {
type = exp.getType();
}
return new RexCall(
type,
SqlStdOperatorTable.CAST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ private SqlLibraryOperators() {
.andThen(SqlTypeTransforms.TO_NULLABLE_ALL),
null, OperandTypes.SAME_SAME, SqlFunctionCategory.SYSTEM);

/** The "NVL(value, value)" function. */
@LibraryOperator(libraries = {BIG_QUERY, HIVE, SPARK})
public static final SqlFunction IFNULL =
new SqlFunction("IFNULL", SqlKind.NVL,
ReturnTypes.LEAST_RESTRICTIVE
.andThen(SqlTypeTransforms.TO_NULLABLE_ALL),
null, OperandTypes.SAME_SAME, SqlFunctionCategory.SYSTEM);

/** The "LTRIM(string)" function. */
@LibraryOperator(libraries = {ORACLE})
public static final SqlFunction LTRIM =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ protected void addAlias(
"call to wrong operator");
final SqlCall newCall =
target.createCall(SqlParserPos.ZERO, call.getOperandList());
cx.getValidator().setValidatedNodeType(newCall,
cx.getValidator().getValidatedNodeType(call));
return cx.convertExpression(newCall);
});
}
Expand Down
Loading

0 comments on commit 93199da

Please sign in to comment.