use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by axbaretto.
the class RewriteCombineBinaryOperators method visitCall.
@Override
public RexNode visitCall(RexCall call) {
SqlOperator op = call.getOperator();
SqlKind kind = op.getKind();
RelDataType type = call.getType();
if (kind == SqlKind.AND) {
List<RexNode> conjuncts = Lists.newArrayList();
for (RexNode child : call.getOperands()) {
conjuncts.addAll(RelOptUtil.conjunctions(child.accept(this)));
}
return RexUtil.composeConjunction(builder, conjuncts, true);
}
if (kind == SqlKind.OR) {
List<RexNode> disjuncts = Lists.newArrayList();
for (RexNode child : call.getOperands()) {
disjuncts.addAll(RelOptUtil.disjunctions(child.accept(this)));
}
return RexUtil.composeDisjunction(builder, disjuncts, true);
}
return builder.makeCall(type, op, visitChildren(call));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by apache.
the class MetadataAggregateHelper method addColumnAggregateCalls.
/**
* Adds aggregate calls to calculate column metadata either from column data itself
* or from previously calculated metadata.
*
* @param fieldRef field reference
* @param fieldName field name
*/
private void addColumnAggregateCalls(FieldReference fieldRef, String fieldName) {
List<SchemaPath> interestingColumns = context.interestingColumns();
if (createNewAggregations()) {
if (interestingColumns == null || interestingColumns.contains(fieldRef)) {
// collect statistics for all or only interesting columns if they are specified
AnalyzeColumnUtils.COLUMN_STATISTICS_FUNCTIONS.forEach((statisticsKind, sqlKind) -> {
// constructs "case when projectMetadataColumn is null then column1 else null end" call
// to avoid using default values for required columns when data for empty result is obtained
LogicalExpression caseExpr = IfExpression.newBuilder().setIfCondition(new IfExpression.IfCondition(new FunctionCall("isnull", Collections.singletonList(FieldReference.getWithQuotedRef(columnNamesOptions.projectMetadataColumn())), ExpressionPosition.UNKNOWN), fieldRef)).setElse(NullExpression.INSTANCE).build();
LogicalExpression call = new FunctionCall(sqlKind.name(), Collections.singletonList(caseExpr), ExpressionPosition.UNKNOWN);
valueExpressions.add(new NamedExpression(call, FieldReference.getWithQuotedRef(AnalyzeColumnUtils.getColumnStatisticsFieldName(fieldName, statisticsKind))));
});
}
} else if (AnalyzeColumnUtils.isColumnStatisticsField(fieldName) || AnalyzeColumnUtils.isMetadataStatisticsField(fieldName)) {
SqlKind function = AnalyzeColumnUtils.COLUMN_STATISTICS_FUNCTIONS.get(AnalyzeColumnUtils.getStatisticsKind(fieldName));
if (function == SqlKind.COUNT) {
// for the case when aggregation was done, call SUM function for the results of COUNT aggregate call
function = SqlKind.SUM;
}
LogicalExpression functionCall = new FunctionCall(function.name(), Collections.singletonList(fieldRef), ExpressionPosition.UNKNOWN);
valueExpressions.add(new NamedExpression(functionCall, fieldRef));
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by apache.
the class DrillReduceAggregatesRule method reduceAgg.
private RexNode reduceAgg(Aggregate oldAggRel, AggregateCall oldCall, List<AggregateCall> newCalls, Map<AggregateCall, RexNode> aggCallMapping, List<RexNode> inputExprs) {
final SqlAggFunction sqlAggFunction = DrillCalciteWrapperUtility.extractSqlOperatorFromWrapper(oldCall.getAggregation());
if (sqlAggFunction instanceof SqlSumAggFunction) {
// case COUNT(x) when 0 then null else SUM0(x) end
return reduceSum(oldAggRel, oldCall, newCalls, aggCallMapping);
}
if (sqlAggFunction instanceof SqlAvgAggFunction) {
// causes the loss of the scale
if (oldCall.getType().getSqlTypeName() == SqlTypeName.DECIMAL) {
return oldAggRel.getCluster().getRexBuilder().addAggCall(oldCall, oldAggRel.getGroupCount(), newCalls, aggCallMapping, ImmutableList.of(getFieldType(oldAggRel.getInput(), oldCall.getArgList().get(0))));
}
final SqlKind subtype = sqlAggFunction.getKind();
switch(subtype) {
case AVG:
// replace original AVG(x) with SUM(x) / COUNT(x)
return reduceAvg(oldAggRel, oldCall, newCalls, aggCallMapping);
case STDDEV_POP:
// / COUNT(x))
return reduceStddev(oldAggRel, oldCall, true, true, newCalls, aggCallMapping, inputExprs);
case STDDEV_SAMP:
// / CASE COUNT(x) WHEN 1 THEN NULL ELSE COUNT(x) - 1 END)
return reduceStddev(oldAggRel, oldCall, false, true, newCalls, aggCallMapping, inputExprs);
case VAR_POP:
// / COUNT(x)
return reduceStddev(oldAggRel, oldCall, true, false, newCalls, aggCallMapping, inputExprs);
case VAR_SAMP:
// / CASE COUNT(x) WHEN 1 THEN NULL ELSE COUNT(x) - 1 END
return reduceStddev(oldAggRel, oldCall, false, false, newCalls, aggCallMapping, inputExprs);
default:
throw Util.unexpected(subtype);
}
} else {
// anything else: preserve original call
RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder();
final int nGroups = oldAggRel.getGroupCount();
List<RelDataType> oldArgTypes = new ArrayList<>();
List<Integer> ordinals = oldCall.getArgList();
assert ordinals.size() <= inputExprs.size();
for (int ordinal : ordinals) {
oldArgTypes.add(inputExprs.get(ordinal).getType());
}
// different RelDataTypes
if (aggCallMapping.containsKey(oldCall) && !aggCallMapping.get(oldCall).getType().equals(oldCall.getType())) {
int index = newCalls.size() + nGroups;
newCalls.add(oldCall);
return rexBuilder.makeInputRef(oldCall.getType(), index);
}
return rexBuilder.addAggCall(oldCall, nGroups, newCalls, aggCallMapping, oldArgTypes);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by apache.
the class IndexableExprMarker method operandsAreIndexable.
public boolean operandsAreIndexable(RexCall call) {
SqlKind kind = call.getKind();
boolean kindIsRight = (SqlKind.COMPARISON.contains(kind) || kind == SqlKind.LIKE || kind == SqlKind.SIMILAR);
if (!kindIsRight) {
return false;
}
int inputReference = 0;
for (RexNode operand : call.operands) {
// a.b = a.c, instead of a.b ='hello', so this cannot apply index
if (containInputRef(operand)) {
inputReference++;
if (inputReference >= 2) {
return false;
}
}
}
return true;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by apache.
the class RewriteAsBinaryOperators method visitCall.
@Override
public RexNode visitCall(RexCall call) {
SqlOperator op = call.getOperator();
SqlKind kind = op.getKind();
RelDataType type = call.getType();
if (kind == SqlKind.OR || kind == SqlKind.AND) {
if (call.getOperands().size() > 2) {
List<RexNode> children = new ArrayList<>(call.getOperands());
RexNode left = children.remove(0).accept(this);
RexNode right = builder.makeCall(type, op, children).accept(this);
return builder.makeCall(type, op, ImmutableList.of(left, right));
}
}
return builder.makeCall(type, op, visitChildren(call));
}
Aggregations