private RexNode useStructIfNeeded(List<? extends RexNode> columns) { // Create STRUCT clause if (columns.size() == 1) { return columns.get(0); } else { return rexBuilder.makeCall(SqlStdOperatorTable.ROW, columns); } }
private RexNode makeAnd(final List<RexNode> conditions) { if (conditions.size() == 1) { return Iterables.getOnlyElement(conditions); } else { return getCluster().getRexBuilder().makeCall(SqlStdOperatorTable.AND, conditions); } }
private RexNode makeOr(final List<RexNode> conditions) { if (conditions.size() == 1) { return Iterables.getOnlyElement(conditions); } else { return getCluster().getRexBuilder().makeCall(SqlStdOperatorTable.OR, conditions); } } }
/** Creates a call to a scalar operator. */ public RexNode call(SqlOperator operator, Iterable<? extends RexNode> operands) { return cluster.getRexBuilder().makeCall(operator, ImmutableList.copyOf(operands)); }
private static List<RexNode> getNotNullConditions(RelOptCluster cluster, RexBuilder rexBuilder, List<RexNode> inputJoinExprs, Set<String> pushedPredicates) { final List<RexNode> newConditions = Lists.newArrayList(); for (RexNode rexNode : inputJoinExprs) { RexNode cond = rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, rexNode); String digest = cond.toString(); if (pushedPredicates.add(digest)) { newConditions.add(cond); } } return newConditions; }
private List<RexNode> rewriteInClauseChildren(SqlOperator op, List<RexNode> childRexNodeLst) throws SemanticException { assert op.getKind() == SqlKind.IN; RexNode firstPred = childRexNodeLst.get(0); List<RexNode> newChildRexNodeLst = new ArrayList<RexNode>(); for (int i = 1; i < childRexNodeLst.size(); i++) { newChildRexNodeLst.add( cluster.getRexBuilder().makeCall( SqlStdOperatorTable.EQUALS, firstPred, childRexNodeLst.get(i))); } return newChildRexNodeLst; }
/** Creates a call to a scalar operator. */ public RexNode call(SqlOperator operator, RexNode... operands) { final RexBuilder builder = cluster.getRexBuilder(); final List<RexNode> operandList = ImmutableList.copyOf(operands); final RelDataType type = builder.deriveReturnType(operator, operandList); if (type == null) { throw new IllegalArgumentException("cannot derive type: " + operator + "; operands: " + Lists.transform(operandList, FN_TYPE)); } return builder.makeCall(type, operator, operandList); }
private RexNode buildInFor(Set<RexNodeRef> set, Collection<ConstraintGroup> value) throws SemanticException { List<RexNodeRef> columns = new ArrayList<>(); columns.addAll(set); columns.sort(RexNodeRef.COMPARATOR); List<RexNode >operands = new ArrayList<>(); List<RexNode> columnNodes = columns.stream().map(n -> n.getRexNode()).collect(Collectors.toList()); operands.add(useStructIfNeeded(columnNodes)); for (ConstraintGroup node : value) { List<RexNode> values = node.getValuesInOrder(columns); operands.add(useStructIfNeeded(values)); } return rexBuilder.makeCall(HiveIn.INSTANCE, operands); }
@Test public void testCharacterLength() { testExpression( rexBuilder.makeCall( SqlStdOperatorTable.CHARACTER_LENGTH, inputRef("s") ), DruidExpression.fromExpression("strlen(\"s\")"), 3L ); }
@Test public void testTimePlusDayTimeInterval() { final Period period = new Period("P1DT1H1M"); testExpression( rexBuilder.makeCall( SqlStdOperatorTable.DATETIME_PLUS, inputRef("t"), rexBuilder.makeIntervalLiteral( new BigDecimal(period.toStandardDuration().getMillis()), // DAY-TIME literals value is millis new SqlIntervalQualifier(TimeUnit.DAY, TimeUnit.MINUTE, SqlParserPos.ZERO) ) ), DruidExpression.of( null, "(\"t\" + 90060000)" ), DateTimes.of("2000-02-03T04:05:06").plus(period).getMillis() ); }
@Test public void testTimePlusYearMonthInterval() { final Period period = new Period("P1Y1M"); testExpression( rexBuilder.makeCall( SqlStdOperatorTable.DATETIME_PLUS, inputRef("t"), rexBuilder.makeIntervalLiteral( new BigDecimal(13), // YEAR-MONTH literals value is months new SqlIntervalQualifier(TimeUnit.YEAR, TimeUnit.MONTH, SqlParserPos.ZERO) ) ), DruidExpression.of( null, "timestamp_shift(\"t\",concat('P', 13, 'M'),1)" ), DateTimes.of("2000-02-03T04:05:06").plus(period).getMillis() ); }
@Test public void testOtherTimeFloor() { // FLOOR(__time TO unit) testExpression( rexBuilder.makeCall( SqlStdOperatorTable.FLOOR, inputRef("t"), rexBuilder.makeFlag(TimeUnitRange.YEAR) ), DruidExpression.fromExpression("timestamp_floor(\"t\",'P1Y',null,'UTC')"), DateTimes.of("2000").getMillis() ); }
@Test public void testOtherTimeCeil() { // CEIL(__time TO unit) testExpression( rexBuilder.makeCall( SqlStdOperatorTable.CEIL, inputRef("t"), rexBuilder.makeFlag(TimeUnitRange.YEAR) ), DruidExpression.fromExpression("timestamp_ceil(\"t\",'P1Y',null,'UTC')"), DateTimes.of("2001").getMillis() ); }
@Test public void testTimeMinusDayTimeInterval() { final Period period = new Period("P1DT1H1M"); testExpression( rexBuilder.makeCall( typeFactory.createSqlType(SqlTypeName.TIMESTAMP), SqlStdOperatorTable.MINUS_DATE, ImmutableList.of( inputRef("t"), rexBuilder.makeIntervalLiteral( new BigDecimal(period.toStandardDuration().getMillis()), // DAY-TIME literals value is millis new SqlIntervalQualifier(TimeUnit.DAY, TimeUnit.MINUTE, SqlParserPos.ZERO) ) ) ), DruidExpression.of( null, "(\"t\" - 90060000)" ), DateTimes.of("2000-02-03T04:05:06").minus(period).getMillis() ); }
@Test public void testPower() { testExpression( rexBuilder.makeCall(SqlStdOperatorTable.POWER, inputRef("a"), integerLiteral(2)), DruidExpression.fromExpression("pow(\"a\",2)"), 100.0 ); }
@Test public void testConcat() { testExpression( rexBuilder.makeCall( typeFactory.createSqlType(SqlTypeName.VARCHAR), SqlStdOperatorTable.CONCAT, ImmutableList.of( inputRef("s"), rexBuilder.makeLiteral("bar") ) ), DruidExpression.fromExpression("concat(\"s\",'bar')"), "foobar" ); }
@Test public void testExtract() { testExpression( rexBuilder.makeCall( SqlStdOperatorTable.EXTRACT, rexBuilder.makeFlag(TimeUnitRange.QUARTER), inputRef("t") ), DruidExpression.fromExpression("timestamp_extract(\"t\",'QUARTER','UTC')"), 1L ); testExpression( rexBuilder.makeCall( SqlStdOperatorTable.EXTRACT, rexBuilder.makeFlag(TimeUnitRange.DAY), inputRef("t") ), DruidExpression.fromExpression("timestamp_extract(\"t\",'DAY','UTC')"), 3L ); }
@Test public void testTimeShift() { testExpression( rexBuilder.makeCall( new TimeShiftOperatorConversion().calciteOperator(), inputRef("t"), rexBuilder.makeLiteral("PT2H"), rexBuilder.makeLiteral(-3, typeFactory.createSqlType(SqlTypeName.INTEGER), true) ), DruidExpression.fromExpression("timestamp_shift(\"t\",'PT2H',-3)"), DateTimes.of("2000-02-02T22:05:06").getMillis() ); }
@Test public void testDateTrunc() { testExpression( rexBuilder.makeCall( new DateTruncOperatorConversion().calciteOperator(), rexBuilder.makeLiteral("hour"), timestampLiteral(DateTimes.of("2000-02-03T04:05:06Z")) ), DruidExpression.fromExpression("timestamp_floor(949550706000,'PT1H',null,'UTC')"), DateTimes.of("2000-02-03T04:00:00").getMillis() ); testExpression( rexBuilder.makeCall( new DateTruncOperatorConversion().calciteOperator(), rexBuilder.makeLiteral("DAY"), timestampLiteral(DateTimes.of("2000-02-03T04:05:06Z")) ), DruidExpression.fromExpression("timestamp_floor(949550706000,'P1D',null,'UTC')"), DateTimes.of("2000-02-03T00:00:00").getMillis() ); }
@Test public void testTimeExtract() { testExpression( rexBuilder.makeCall( new TimeExtractOperatorConversion().calciteOperator(), inputRef("t"), rexBuilder.makeLiteral("QUARTER") ), DruidExpression.fromExpression("timestamp_extract(\"t\",'QUARTER','UTC')"), 1L ); testExpression( rexBuilder.makeCall( new TimeExtractOperatorConversion().calciteOperator(), inputRef("t"), rexBuilder.makeLiteral("DAY"), rexBuilder.makeLiteral("America/Los_Angeles") ), DruidExpression.fromExpression("timestamp_extract(\"t\",'DAY','America/Los_Angeles')"), 2L ); }