private static String applyTimestampFormat(String arg, String format, TimeZone timeZone) { return DruidExpressions.functionCall("timestamp_format", ImmutableList.of(arg, DruidExpressions.stringLiteral(format), DruidExpressions.stringLiteral(timeZone.getID())) ); }
@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))); } }
@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())) ); } }
@Nullable @Override public String toDruidExpression(RexNode rexNode, RelDataType rowType, DruidQuery query ) { final RexCall call = (RexCall) rexNode; if (call.getOperands().size() != 2) { throw new IllegalStateException("date_add/date_sub() requires 2 arguments, got " + call.getOperands().size()); } final String arg0 = DruidExpressions.toDruidExpression(call.getOperands().get(0), rowType, query); final String arg1 = DruidExpressions.toDruidExpression(call.getOperands().get(1), rowType, query); if (arg0 == null || arg1 == null) { return null; } final String steps = direction == -1 ? DruidQuery.format("-( %s )", arg1) : arg1; return DruidExpressions.functionCall( "timestamp_shift", ImmutableList.of( arg0, DruidExpressions.stringLiteral("P1D"), steps, DruidExpressions.stringLiteral(timezoneId(query, call.getOperands().get(0)).getID()))); } }
public static String applyTimeExtract(String timeExpression, String druidUnit, TimeZone timeZone) { return DruidExpressions.functionCall( "timestamp_extract", ImmutableList.of( timeExpression, DruidExpressions.stringLiteral(druidUnit), DruidExpressions.stringLiteral(timeZone.getID()))); } }
public static String applyTimestampFloor( final String input, final String granularity, final String origin, final TimeZone timeZone) { Objects.requireNonNull(input, "input"); Objects.requireNonNull(granularity, "granularity"); return DruidExpressions.functionCall( "timestamp_floor", ImmutableList.of(input, DruidExpressions.stringLiteral(granularity), DruidExpressions.stringLiteral(origin), DruidExpressions.stringLiteral(timeZone.getID()))); }
public static String applyTimestampCeil( final String input, final String granularity, final String origin, final TimeZone timeZone) { Objects.requireNonNull(input, "input"); Objects.requireNonNull(granularity, "granularity"); return DruidExpressions.functionCall( "timestamp_ceil", ImmutableList.of(input, DruidExpressions.stringLiteral(granularity), DruidExpressions.stringLiteral(origin), DruidExpressions.stringLiteral(timeZone.getID()))); }
@Override public String toDruidExpression(RexNode rexNode, RelDataType rowType, DruidQuery druidQuery) { final RexCall call = (RexCall) rexNode; final List<String> druidExpressions = DruidExpressions.toDruidExpressions( druidQuery, rowType, call.getOperands()); if (druidExpressions == null) { return null; } return DruidExpressions.functionCall(druidFunctionName, druidExpressions); } }
private static String castDateTimeToChar( final TimeZone timeZone, final String operand, final SqlTypeName fromType) { return DruidExpressions.functionCall( "timestamp_format", ImmutableList.of( operand, DruidExpressions.stringLiteral(dateTimeFormatString(fromType)), DruidExpressions.stringLiteral(timeZone.getID()))); }
private static String castCharToDateTime( TimeZone timeZone, String operand, final SqlTypeName toType, String format) { // Cast strings to date times by parsing them from SQL format. final String timestampExpression = DruidExpressions.functionCall( "timestamp_parse", ImmutableList.of( operand, DruidExpressions.stringLiteral(format), DruidExpressions.stringLiteral(timeZone.getID()))); if (toType == SqlTypeName.DATE) { // case to date we need to floor to day first return DruidExpressions.applyTimestampFloor( timestampExpression, Period.days(1).toString(), "", timeZone); } else if (toType == SqlTypeName.TIMESTAMP || toType == SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE) { return timestampExpression; } else { throw new IllegalStateException( DruidQuery.format("Unsupported DateTime type[%s]", toType)); } }