@Nullable @Override public String toDruidExpression(RexNode rexNode, RelDataType rowType, DruidQuery query ) { final RexCall call = (RexCall) rexNode; if (call.getOperands().size() < 1 || call.getOperands().size() > 2) { throw new IllegalStateException("form_unixtime() requires 1 or 2 argument, got " + call.getOperands().size()); } final String arg = DruidExpressions.toDruidExpression(call.getOperands().get(0), rowType, query); if (arg == null) { return null; } final String numMillis = DruidQuery.format("(%s * '1000')", arg); final String format = call.getOperands().size() == 1 ? DruidExpressions.stringLiteral(DEFAULT_TS_FORMAT) : DruidExpressions .toDruidExpression(call.getOperands().get(1), rowType, query); return DruidExpressions.functionCall("timestamp_format", ImmutableList.of(numMillis, format, DruidExpressions.stringLiteral(TimeZone.getTimeZone("UTC").getID())) ); } }
if (call.getKind() == SqlKind.EXTRACT) { args.add(call.operands.get(1).accept(this)); } else if (call.getKind() == SqlKind.FLOOR && call.operands.size() == 2) { args.add(call.operands.get(0).accept(this)); } else { for (RexNode operand : call.operands) { args.add(operand.accept(this)); if (call.isA(SqlKind.CAST) && (call.operands.size() == 1) && SqlTypeUtil.equalSansNullability(dTFactory, call.getType(), call.operands.get(0).getType())) { return args.get(0); } else { GenericUDF hiveUdf = SqlFunctionConverter.getHiveUDF(call.getOperator(), call.getType(), args.size()); if (hiveUdf == null) { throw new RuntimeException("Cannot find UDF for " + call.getType() + " " + call.getOperator() + "[" + call.getOperator().getKind() + "]/" + args.size());
private BinaryTupleExpression getBinaryTupleExpression(RexCall call, TupleExpression.ExpressionOperatorEnum op) { assert call.operands.size() == 2; TupleExpression left = call.operands.get(0).accept(this); TupleExpression right = call.operands.get(1).accept(this); BinaryTupleExpression tuple = new BinaryTupleExpression(op, Lists.newArrayList(left, right)); tuple.setDigest(call.toString()); return tuple; }
@Override public Void visitCall(RexCall call) { if (call.getOperator().getKind() == SqlKind.EQUALS) { int lPos = pos(call.getOperands().get(0)); int rPos = pos(call.getOperands().get(1)); if (lPos != -1 && rPos != -1) { JoinConditionBasedPredicateInference.this.equivalent(lPos, rPos); JoinConditionBasedPredicateInference.this.equalityPredicates .add(call.toString()); } } return null; } }
private SqlKind getOp(RexCall call) { SqlKind op = call.getKind(); if (call.getKind().equals(SqlKind.OTHER_FUNCTION) && SqlTypeUtil.inBooleanFamily(call.getType())) { SqlOperator sqlOp = call.getOperator(); String opName = (sqlOp != null) ? sqlOp.getName() : ""; if (opName.equalsIgnoreCase("in")) { op = SqlKind.IN; } } return op; }
@Override public Boolean visitCall(RexCall call) { // Constant if operator is deterministic and all operands are // constant. return call.getOperator().isDeterministic() && RexVisitorImpl.visitArrayAnd(this, call.getOperands()); }
List<RexNode> inputRefs = Lists.transform(inputRel.getRowType().getFieldList(), input -> new RexInputRef(input.getIndex(), input.getType())); inputRefsTypes.add(inputRefs.get(i).getType()); List<String> outputFieldNames = new ArrayList<>(inputRel.getRowType().getFieldNames()); RelDataType valuesRowType = ((ArraySqlType) convertedOriginalValuesExpr.getType()).getComponentType(); List<RexNode> newStructExprs = new ArrayList<>(); for (RexNode structExpr : convertedOriginalValuesExpr.getOperands()) { RexCall structCall = (RexCall) structExpr; List<RexNode> exprs = new ArrayList<>(inputRefs); exprs.addAll(structCall.getOperands()); newStructExprs.add(rexBuilder.makeCall(structCall.op, exprs)); rexBuilder.makeCall(convertedOriginalValuesExpr.op, newStructExprs); valuesRowType, JoinRelType.INNER, dtFactory, null, ImmutableList.of()); SqlOperator calciteOp = SqlFunctionConverter.getCalciteOperator( inlineFunctionName, inlineFunctionInfo.getGenericUDTF(), ImmutableList.copyOf(inputRefsTypes), retType); ImmutableList.of(inputRel), rexBuilder.makeCall(calciteOp, convertedFinalValuesExpr), null, retType, null); columnAliases.size() != valuesRowType.getFieldCount()) {
final RexNode timeArg = call.getOperands().get(0); final DruidExpression timeExpression = Expressions.toDruidExpression(plannerContext, rowSignature, timeArg); if (timeExpression == null) { final String pattern = call.getOperands().size() > 1 && !RexLiteral.isNullLiteral(call.getOperands().get(1)) ? RexLiteral.stringValue(call.getOperands().get(1)) : "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"; final DateTimeZone timeZone = call.getOperands().size() > 2 && !RexLiteral.isNullLiteral(call.getOperands().get(2)) ? DateTimes.inferTzFromString(RexLiteral.stringValue(call.getOperands().get(2))) : plannerContext.getTimeZone(); ImmutableList.of( timeExpression.getExpression(), DruidExpression.stringLiteral(pattern), DruidExpression.stringLiteral(timeZone.getID()) ).stream().map(DruidExpression::fromExpression).collect(Collectors.toList()) );
@Nullable @Override public String toDruidExpression(RexNode rexNode, RelDataType rowType, DruidQuery query ) { final RexCall call = (RexCall) rexNode; final String arg0 = DruidExpressions.toDruidExpression(call.getOperands().get(0), rowType, query); if (arg0 == null) { return null; } if (SqlTypeUtil.isDatetime((call.getOperands().get(0).getType()))) { // Timestamp is represented as long internally no need to any thing here return DruidExpressions.functionCall("div", ImmutableList.of(arg0, DruidExpressions.numberLiteral(1000))); } // dealing with String type final String format = call.getOperands().size() == 2 ? DruidExpressions .toDruidExpression(call.getOperands().get(1), rowType, query) : DEFAULT_TS_FORMAT; return DruidExpressions .functionCall("unix_timestamp", ImmutableList.of(arg0, DruidExpressions.stringLiteral(format))); } }
newCasts.add(rexBuilder.makeInputRef(project.getChildExps().get(fieldNumber).getType(), fieldNumber)); final boolean flip = RexLiteral.isNullLiteral(caseCall.getOperands().get(1)) && !RexLiteral.isNullLiteral(caseCall.getOperands().get(2)); final RexNode arg1 = caseCall.getOperands().get(flip ? 2 : 1); final RexNode arg2 = caseCall.getOperands().get(flip ? 1 : 2); final RexNode filterFromCase = rexBuilder.makeCall( booleanType, flip ? SqlStdOperatorTable.IS_FALSE : SqlStdOperatorTable.IS_TRUE, ImmutableList.of(caseCall.getOperands().get(0)) ); if (aggregateCall.getAggregation().getKind() == SqlKind.COUNT && RexLiteral.isNullLiteral(arg2)) { newProjects.add(arg1); newProjects.add(filter); && arg1.isA(SqlKind.LITERAL) && !RexLiteral.isNullLiteral(arg1) && RexLiteral.isNullLiteral(arg2)) { final RelDataType oldType = aggregate.getRowType().getFieldList().get(i).getType(); if (newCall == null) { newCasts.add(rexBuilder.makeInputRef(oldType, i));
private List<RexNode> extractFilterPreds(Filter filterOp) { List<RexNode> conjs = new ArrayList<>(); for (RexNode r : HiveRelOptUtil.conjunctions(filterOp.getCondition())) { if (r.getKind() == SqlKind.IS_NOT_NULL) { RexCall isNotNullNode = (RexCall) r; if (RexUtil.isReferenceOrAccess(isNotNullNode.getOperands().get(0), true)) { ImmutableBitSet ref = RelOptUtil.InputFinder.bits(isNotNullNode); RelColumnOrigin co = mq.getColumnOrigin(filterOp, ref.nextSetBit(0)); if (co == null) { // We add it back conjs.add(r); continue; } RelOptHiveTable table = (RelOptHiveTable) co.getOriginTable(); List<ColStatistics> colStats = table.getColStat(ImmutableList.of(co.getOriginColumnOrdinal()), true); if (colStats == null || colStats.isEmpty() || colStats.get(0).getNumNulls() != 0) { // We add it back conjs.add(r); } } } else { conjs.add(r); } } return conjs; }
needed.add(fc.getFieldIndex()); final RexNode node = project.getProjects().get(map.getTarget(fc.getFieldIndex())); if (node.isA(SqlKind.CAST)) { ImmutableList.of(RexUtil.apply(map, sort.getCollation()))); if (cast.getOperator().getMonotonicity(binding) == SqlMonotonicity.NOT_MONOTONIC) { return; ImmutableList.<RelNode>of(sort.getInput())); final HiveSortLimit newSort = sort.copy(newProject.getTraitSet(), newProject, newCollation, sort.offset, sort.fetch);
Builder<RelDataType> argTypeBldr = ImmutableList.<RelDataType> builder(); SqlOperator calciteOp = SqlFunctionConverter.getCalciteOperator(func.getFuncText(), func.getGenericUDF(), argTypeBldr.build(), retType); if (calciteOp.getKind() == SqlKind.CASE) { } else if (calciteOp.getKind() == SqlKind.IN && isAllPrimitive) { if (childRexNodeLst.size() == 2) { calciteOp = SqlStdOperatorTable.OR; } else if (calciteOp.getKind() == SqlKind.COALESCE && childRexNodeLst.size() > 1 ) { childRexNodeLst = rewriteToDateChildren(childRexNodeLst); expr = cluster.getRexBuilder().makeCall(retType, calciteOp, childRexNodeLst); } else { retType = expr.getType(); && !(((RexCall) expr).getOperator() instanceof SqlCastFunction)) { RexCall call = (RexCall) expr; expr = cluster.getRexBuilder().makeCall(retType, call.getOperator(), RexUtil.flatten(call.getOperands(), call.getOperator()));
private static RexCall adjustOBSchema(RexCall obyExpr, Project obChild, List<FieldSchema> resultSchema) { int a = -1; List<RexNode> operands = new ArrayList<>(); for (int k = 0; k < obyExpr.operands.size(); k++) { RexNode rn = obyExpr.operands.get(k); for (int j = 0; j < resultSchema.size(); j++) { if( obChild.getChildExps().get(j).toString().equals(rn.toString())) { a = j; break; } } if (a != -1) { operands.add(new RexInputRef(a, rn.getType())); } else { if (rn instanceof RexCall) { operands.add(adjustOBSchema((RexCall)rn, obChild, resultSchema)); } else { operands.add(rn); } } a = -1; } return (RexCall) obChild.getCluster().getRexBuilder().makeCall( obyExpr.getType(), obyExpr.getOperator(), operands); }
private RexNode transformIntoInClauseCondition(RexBuilder rexBuilder, RexNode condition, int minNumORClauses) throws SemanticException { assert condition.getKind() == SqlKind.OR; ImmutableList<RexNode> operands = RexUtil.flattenOr(((RexCall) condition).getOperands()); if (operands.size() < minNumORClauses) { for (int i = 0; i < operands.size(); i++) { ConstraintGroup m = new ConstraintGroup(operands.get(i)); allNodes.add(m); return ops.get(0); } else { return rexBuilder.makeCall(SqlStdOperatorTable.OR, ops);
private static List<RexNode> extractCommonOperands(RexBuilder rexBuilder, RelNode input, RexNode condition, int maxCNFNodeCount) { assert condition.getKind() == SqlKind.OR; Multimap<String, RexNode> reductionCondition = LinkedHashMultimap.create(); ImmutableList<RexNode> operands = RexUtil.flattenOr(((RexCall) condition).getOperands()); for (int i = 0; i < operands.size(); i++) { final RexNode operand = operands.get(i); RexNode ref = rexBuilder.makeInputRef(input, refs.iterator().next()); String stringRef = ref.toString(); reductionCondition.put(stringRef, conjCall); refsInCurrentOperand.add(stringRef);
private CaseTupleExpression getCaseTupleExpression(RexCall call) { List<Pair<TupleFilter, TupleExpression>> whenList = Lists .newArrayListWithExpectedSize(call.operands.size() / 2); TupleExpression elseExpr = null; for (int i = 0; i < call.operands.size() - 1; i += 2) { if (call.operands.get(i) instanceof RexCall) { RexCall whenCall = (RexCall) call.operands.get(i); CompareTupleFilter.CompareResultType compareResultType = RexUtil.getCompareResultType(whenCall); if (compareResultType == CompareTupleFilter.CompareResultType.AlwaysTrue) { elseExpr = call.operands.get(i + 1).accept(this); break; } else if (compareResultType == CompareTupleFilter.CompareResultType.AlwaysFalse) { continue; TupleFilter whenFilter = whenCall.accept(filterVistor); whenFilter = new FilterOptimizeTransformer().transform(whenFilter); TupleExpression thenExpr = call.operands.get(i + 1).accept(this); whenList.add(new Pair<>(whenFilter, thenExpr)); if (elseExpr == null && call.operands.size() % 2 == 1) { RexNode elseNode = call.operands.get(call.operands.size() - 1); if (!(elseNode instanceof RexLiteral && ((RexLiteral) elseNode).getValue() == null)) { elseExpr = elseNode.accept(this); tuple.setDigest(call.toString()); return tuple;
hiveUDF = SqlFunctionConverter.getHiveUDF(call.getOperator(), call.getType(), call.operands.size()); if (hiveUDF != null && !FunctionRegistry.isConsistentWithinQuery(hiveUDF)) { RexNode n = operand.accept(this); if (n != null) { args.add(n); if (call.getOperator() != SqlStdOperatorTable.AND) { return argsPruned ? null : call; } else { return args.get(0); } else { return cluster.getRexBuilder().makeCall(call.getOperator(), args);
switch (conjCall.getOperator().getKind()) { case EQUALS: if (!(RexUtil.isConstant(conjCall.operands.get(0))) && RexUtil.isConstant(conjCall.operands.get(1))) { conditions.put(conjCall.operands.get(0).toString(), conjCall.operands.get(1)); } else if (!(RexUtil.isConstant(conjCall.operands.get(1))) && RexUtil.isConstant(conjCall.operands.get(0))) { conditions.put(conjCall.operands.get(1).toString(), conjCall.operands.get(0)); conditions.put(conjCall.operands.get(0).toString(), relBuilder.getRexBuilder().makeNullLiteral( conjCall.operands.get(0).getType().getSqlTypeName()));
public static boolean isFlat(RexCall call) { boolean flat = false; if (call.operands != null && call.operands.size() > 2) { SqlOperator op = call.getOperator(); if (op.getKind() == SqlKind.AND || op.getKind() == SqlKind.OR) { flat = true; } } return flat; }