use of org.apache.calcite.rex.RexLiteral in project beam by apache.
the class BigQueryFilter method isSupported.
/**
* Check whether a {@code RexNode} is supported. As of right now BigQuery supports: 1. Complex
* predicates (both conjunction and disjunction). 2. Comparison between a column and a literal.
*
* <p>TODO: Check if comparison between two columns is supported. Also over a boolean field.
*
* @param node A node to check for predicate push-down support.
* @return A pair containing a boolean whether an expression is supported and the number of input
* references used by the expression.
*/
private Pair<Boolean, Integer> isSupported(RexNode node) {
int numberOfInputRefs = 0;
boolean isSupported = true;
if (node instanceof RexCall) {
RexCall compositeNode = (RexCall) node;
// CAST, TRIM? and REVERSE? should be supported as well.
if (!node.getKind().belongsTo(SUPPORTED_OPS)) {
isSupported = false;
} else {
for (RexNode operand : compositeNode.getOperands()) {
// All operands must be supported for a parent node to be supported.
Pair<Boolean, Integer> childSupported = isSupported(operand);
// (OR).
if (!node.getKind().belongsTo(ImmutableSet.of(AND, OR))) {
numberOfInputRefs += childSupported.getRight();
}
// Predicate functions, where more than one field is involved are unsupported.
isSupported = numberOfInputRefs < 2 && childSupported.getLeft();
}
}
} else if (node instanceof RexInputRef) {
numberOfInputRefs = 1;
} else if (node instanceof RexLiteral) {
// RexLiterals are expected, but no action is needed.
} else {
throw new UnsupportedOperationException("Encountered an unexpected node type: " + node.getClass().getSimpleName());
}
return Pair.of(isSupported, numberOfInputRefs);
}
use of org.apache.calcite.rex.RexLiteral in project druid by druid-io.
the class DruidJoinRule method analyzeCondition.
/**
* If this condition is an AND of some combination of (1) literals; (2) equality conditions of the form
* {@code f(LeftRel) = RightColumn}, then return a {@link ConditionAnalysis}.
*/
private Optional<ConditionAnalysis> analyzeCondition(final RexNode condition, final RelDataType leftRowType, DruidRel<?> right) {
final List<RexNode> subConditions = decomposeAnd(condition);
final List<Pair<RexNode, RexInputRef>> equalitySubConditions = new ArrayList<>();
final List<RexLiteral> literalSubConditions = new ArrayList<>();
final int numLeftFields = leftRowType.getFieldCount();
final Set<RexInputRef> rightColumns = new HashSet<>();
for (RexNode subCondition : subConditions) {
if (RexUtil.isLiteral(subCondition, true)) {
if (subCondition.isA(SqlKind.CAST)) {
// This is CAST(literal) which is always OK.
// We know that this is CAST(literal) as it passed the check from RexUtil.isLiteral
RexCall call = (RexCall) subCondition;
// are different, then skipping the cast might change the meaning of the subcondition.
if (call.getType().getSqlTypeName().equals(call.getOperands().get(0).getType().getSqlTypeName())) {
// If the types are the same, unwrap the cast and use the underlying literal.
literalSubConditions.add((RexLiteral) call.getOperands().get(0));
} else {
// If the types are not the same, return Optional.empty() indicating the condition is not supported.
return Optional.empty();
}
} else {
// Literals are always OK.
literalSubConditions.add((RexLiteral) subCondition);
}
continue;
}
if (!subCondition.isA(SqlKind.EQUALS)) {
// If it's not EQUALS, it's not supported.
plannerContext.setPlanningError("SQL requires a join with '%s' condition that is not supported.", subCondition.getKind());
return Optional.empty();
}
final List<RexNode> operands = ((RexCall) subCondition).getOperands();
Preconditions.checkState(operands.size() == 2, "Expected 2 operands, got[%,d]", operands.size());
if (isLeftExpression(operands.get(0), numLeftFields) && isRightInputRef(operands.get(1), numLeftFields)) {
equalitySubConditions.add(Pair.of(operands.get(0), (RexInputRef) operands.get(1)));
rightColumns.add((RexInputRef) operands.get(1));
} else if (isRightInputRef(operands.get(0), numLeftFields) && isLeftExpression(operands.get(1), numLeftFields)) {
equalitySubConditions.add(Pair.of(operands.get(1), (RexInputRef) operands.get(0)));
rightColumns.add((RexInputRef) operands.get(0));
} else {
// Cannot handle this condition.
plannerContext.setPlanningError("SQL is resulting in a join that has unsupported operand types.");
return Optional.empty();
}
}
// thereby allowing join conditions on both k and v columns of the lookup
if (right != null && !DruidJoinQueryRel.computeRightRequiresSubquery(DruidJoinQueryRel.getSomeDruidChild(right)) && right instanceof DruidQueryRel) {
DruidQueryRel druidQueryRel = (DruidQueryRel) right;
if (druidQueryRel.getDruidTable().getDataSource() instanceof LookupDataSource) {
long distinctRightColumns = rightColumns.stream().map(RexSlot::getIndex).distinct().count();
if (distinctRightColumns > 1) {
// it means that the join's right side is lookup and the join condition contains both key and value columns of lookup.
// currently, the lookup datasource in the native engine doesn't support using value column in the join condition.
plannerContext.setPlanningError("SQL is resulting in a join involving lookup where value column is used in the condition.");
return Optional.empty();
}
}
}
return Optional.of(new ConditionAnalysis(numLeftFields, equalitySubConditions, literalSubConditions));
}
use of org.apache.calcite.rex.RexLiteral in project druid by druid-io.
the class TimeParseOperatorConversion method toDruidExpression.
@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode) {
final RexCall call = (RexCall) rexNode;
final RexNode timeArg = call.getOperands().get(0);
final DruidExpression timeExpression = Expressions.toDruidExpression(plannerContext, rowSignature, timeArg);
if (timeExpression == null) {
return null;
}
final String pattern = OperatorConversions.getOperandWithDefault(call.getOperands(), 1, RexLiteral::stringValue, null);
final DateTimeZone timeZone = OperatorConversions.getOperandWithDefault(call.getOperands(), 2, operand -> DateTimes.inferTzFromString(RexLiteral.stringValue(operand)), plannerContext.getTimeZone());
return DruidExpression.ofFunctionCall(Calcites.getColumnTypeForRelDataType(rexNode.getType()), "timestamp_parse", ImmutableList.of(timeExpression, DruidExpression.ofStringLiteral(pattern), DruidExpression.ofStringLiteral(timeZone.getID())));
}
use of org.apache.calcite.rex.RexLiteral in project druid by druid-io.
the class TimeFormatOperatorConversion method toDruidExpression.
@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode) {
final RexCall call = (RexCall) rexNode;
final RexNode timeArg = call.getOperands().get(0);
final DruidExpression timeExpression = Expressions.toDruidExpression(plannerContext, rowSignature, timeArg);
if (timeExpression == null) {
return null;
}
final String pattern = OperatorConversions.getOperandWithDefault(call.getOperands(), 1, RexLiteral::stringValue, DEFAULT_PATTERN);
final DateTimeZone timeZone = OperatorConversions.getOperandWithDefault(call.getOperands(), 2, operand -> {
try {
return DateTimes.inferTzFromString(RexLiteral.stringValue(operand), false);
} catch (IllegalArgumentException e) {
throw new IAE(e.getMessage());
}
}, plannerContext.getTimeZone());
return DruidExpression.ofFunctionCall(Calcites.getColumnTypeForRelDataType(rexNode.getType()), "timestamp_format", ImmutableList.of(timeExpression, DruidExpression.ofStringLiteral(pattern), DruidExpression.ofStringLiteral(timeZone.getID())));
}
use of org.apache.calcite.rex.RexLiteral in project hazelcast by hazelcast.
the class IndexResolver method prepareSingleColumnSearchCandidateComparison.
@SuppressWarnings({ "ConstantConditions", "UnstableApiUsage" })
private static IndexComponentCandidate prepareSingleColumnSearchCandidateComparison(RexNode exp, RexNode operand1, RexNode operand2) {
// SARG is supported only for literals, not for dynamic parameters
if (operand1.getKind() != SqlKind.INPUT_REF || operand2.getKind() != SqlKind.LITERAL) {
return null;
}
int columnIndex = ((RexInputRef) operand1).getIndex();
RexLiteral literal = (RexLiteral) operand2;
QueryDataType hazelcastType = HazelcastTypeUtils.toHazelcastType(literal.getType());
RangeSet<?> rangeSet = RexToExpression.extractRangeFromSearch(literal);
if (rangeSet == null) {
return null;
}
Set<? extends Range<?>> ranges = rangeSet.asRanges();
IndexFilter indexFilter;
if (ranges.size() == 1) {
indexFilter = createIndexFilterForSingleRange(Iterables.getFirst(ranges, null), hazelcastType);
} else if (ranges.stream().allMatch(IndexResolver::isSingletonRange)) {
indexFilter = new IndexInFilter(toList(ranges, range -> createIndexFilterForSingleRange(range, hazelcastType)));
} else {
// No support for IndexInFilter with multiple IndexFilterForSingleRanges
return null;
}
return new IndexComponentCandidate(exp, columnIndex, indexFilter);
}
Aggregations