Java Code Examples for org.apache.calcite.sql.SqlKind#FLOOR
The following examples show how to use
org.apache.calcite.sql.SqlKind#FLOOR .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StandardConvertletTable.java From Bats with Apache License 2.0 | 5 votes |
protected RexNode convertFloorCeil(SqlRexContext cx, SqlCall call) { final boolean floor = call.getKind() == SqlKind.FLOOR; // Rewrite floor, ceil of interval if (call.operandCount() == 1 && call.operand(0) instanceof SqlIntervalLiteral) { final SqlIntervalLiteral literal = call.operand(0); SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue(); BigDecimal val = interval.getIntervalQualifier().getStartUnit().multiplier; RexNode rexInterval = cx.convertExpression(literal); final RexBuilder rexBuilder = cx.getRexBuilder(); RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.valueOf(0)); RexNode cond = ge(rexBuilder, rexInterval, zero); RexNode pad = rexBuilder.makeExactLiteral(val.subtract(BigDecimal.ONE)); RexNode cast = rexBuilder.makeReinterpretCast( rexInterval.getType(), pad, rexBuilder.makeLiteral(false)); RexNode sum = floor ? minus(rexBuilder, rexInterval, cast) : plus(rexBuilder, rexInterval, cast); RexNode kase = floor ? case_(rexBuilder, rexInterval, cond, sum) : case_(rexBuilder, sum, cond, rexInterval); RexNode factor = rexBuilder.makeExactLiteral(val); RexNode div = divideInt(rexBuilder, kase, factor); return multiply(rexBuilder, div, factor); } // normal floor, ceil function return convertFunction(cx, (SqlFunction) call.getOperator(), call); }
Example 2
Source File: TimeExtractionFunction.java From calcite with Apache License 2.0 | 5 votes |
/** * Returns whether the RexCall contains a valid FLOOR unit that we can * serialize to Druid. * * @param rexNode Extract expression * * @return true if the extract unit is valid */ public static boolean isValidTimeFloor(RexNode rexNode) { if (rexNode.getKind() != SqlKind.FLOOR) { return false; } final RexCall call = (RexCall) rexNode; if (call.operands.size() != 2) { return false; } final RexLiteral flag = (RexLiteral) call.operands.get(1); final TimeUnitRange timeUnit = (TimeUnitRange) flag.getValue(); return timeUnit != null && VALID_TIME_FLOOR.contains(timeUnit); }
Example 3
Source File: StandardConvertletTable.java From calcite with Apache License 2.0 | 5 votes |
protected RexNode convertFloorCeil(SqlRexContext cx, SqlCall call) { final boolean floor = call.getKind() == SqlKind.FLOOR; // Rewrite floor, ceil of interval if (call.operandCount() == 1 && call.operand(0) instanceof SqlIntervalLiteral) { final SqlIntervalLiteral literal = call.operand(0); SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) literal.getValue(); BigDecimal val = interval.getIntervalQualifier().getStartUnit().multiplier; RexNode rexInterval = cx.convertExpression(literal); final RexBuilder rexBuilder = cx.getRexBuilder(); RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.valueOf(0)); RexNode cond = ge(rexBuilder, rexInterval, zero); RexNode pad = rexBuilder.makeExactLiteral(val.subtract(BigDecimal.ONE)); RexNode cast = rexBuilder.makeReinterpretCast( rexInterval.getType(), pad, rexBuilder.makeLiteral(false)); RexNode sum = floor ? minus(rexBuilder, rexInterval, cast) : plus(rexBuilder, rexInterval, cast); RexNode kase = floor ? case_(rexBuilder, rexInterval, cond, sum) : case_(rexBuilder, sum, cond, rexInterval); RexNode factor = rexBuilder.makeExactLiteral(val); RexNode div = divideInt(rexBuilder, kase, factor); return multiply(rexBuilder, div, factor); } // normal floor, ceil function return convertFunction(cx, (SqlFunction) call.getOperator(), call); }
Example 4
Source File: DruidQuery.java From calcite with Apache License 2.0 | 4 votes |
/** * Computes the project group set. * * @param projectNode Project under the Aggregates if any * @param groupSet Ids of grouping keys as they are listed in {@code projects} list * @param inputRowType Input row type under the project * @param druidQuery Druid Query * * @return A list of {@link DimensionSpec} containing the group by dimensions, * and a list of {@link VirtualColumn} containing Druid virtual column * projections; or null, if translation is not possible. * Note that the size of lists can be different. */ @Nullable protected static Pair<List<DimensionSpec>, List<VirtualColumn>> computeProjectGroupSet( @Nullable Project projectNode, ImmutableBitSet groupSet, RelDataType inputRowType, DruidQuery druidQuery) { final List<DimensionSpec> dimensionSpecList = new ArrayList<>(); final List<VirtualColumn> virtualColumnList = new ArrayList<>(); final Set<String> usedFieldNames = new HashSet<>(); for (int groupKey : groupSet) { final DimensionSpec dimensionSpec; final RexNode project; if (projectNode == null) { project = RexInputRef.of(groupKey, inputRowType); } else { project = projectNode.getProjects().get(groupKey); } Pair<String, ExtractionFunction> druidColumn = toDruidColumn(project, inputRowType, druidQuery); if (druidColumn.left != null && druidColumn.right == null) { // SIMPLE INPUT REF dimensionSpec = new DefaultDimensionSpec(druidColumn.left, druidColumn.left, DruidExpressions.EXPRESSION_TYPES.get(project.getType().getSqlTypeName())); usedFieldNames.add(druidColumn.left); } else if (druidColumn.left != null && druidColumn.right != null) { // CASE it is an extraction Dimension final String columnPrefix; //@TODO Remove it! if else statement is not really needed it is here to make tests pass. if (project.getKind() == SqlKind.EXTRACT) { columnPrefix = EXTRACT_COLUMN_NAME_PREFIX + "_" + Objects .requireNonNull(DruidDateTimeUtils .extractGranularity(project, druidQuery.getConnectionConfig().timeZone()) .getType().lowerName); } else if (project.getKind() == SqlKind.FLOOR) { columnPrefix = FLOOR_COLUMN_NAME_PREFIX + "_" + Objects .requireNonNull(DruidDateTimeUtils .extractGranularity(project, druidQuery.getConnectionConfig().timeZone()) .getType().lowerName); } else { columnPrefix = "extract"; } final String uniqueExtractColumnName = SqlValidatorUtil .uniquify(columnPrefix, usedFieldNames, SqlValidatorUtil.EXPR_SUGGESTER); dimensionSpec = new ExtractionDimensionSpec(druidColumn.left, druidColumn.right, uniqueExtractColumnName); usedFieldNames.add(uniqueExtractColumnName); } else { // CASE it is Expression final String expression = DruidExpressions .toDruidExpression(project, inputRowType, druidQuery); if (Strings.isNullOrEmpty(expression)) { return null; } final String name = SqlValidatorUtil .uniquify("vc", usedFieldNames, SqlValidatorUtil.EXPR_SUGGESTER); VirtualColumn vc = new VirtualColumn(name, expression, DruidExpressions.EXPRESSION_TYPES.get(project.getType().getSqlTypeName())); virtualColumnList.add(vc); dimensionSpec = new DefaultDimensionSpec(name, name, DruidExpressions.EXPRESSION_TYPES.get(project.getType().getSqlTypeName())); usedFieldNames.add(name); } dimensionSpecList.add(dimensionSpec); } return Pair.of(dimensionSpecList, virtualColumnList); }