public boolean isStorageTableCandidateForRange(String storageTableName, String fromDate, String toDate) throws HiveException, LensException { Date now = new Date(); return isStorageTableCandidateForRange(storageTableName, resolveDate(fromDate, now), resolveDate(toDate, now)); }
public static String relativeToAbsolute(String relative, Date now) throws LensException { if (RELDATE_VALIDATOR.matcher(relative).matches()) { return formatAbsDate(resolveRelativeDate(relative, now)); } else { return relative; } }
public static Date resolveAbsoluteDate(final String str) throws LensException { try { return stringToDateCache.get(str, () -> ABSDATE_PARSER.get().parse(getAbsDateFormatString(str))); } catch (Exception e) { log.error("Invalid date format. expected only {} date provided:{}", ABSDATE_FMT, str, e); throw new LensException(LensCubeErrorCode.WRONG_TIME_RANGE_FORMAT.getLensErrorInfo(), ABSDATE_FMT, str); } }
static CoveringInfo getCoveringInfo(Date from, Date to, UpdatePeriod interval) { switch (interval) { case SECONDLY: case CONTINUOUS: return getMilliSecondCoveringInfo(from, to, 1000, interval); case MINUTELY: case HOURLY: case DAILY: return getMilliSecondCoveringInfo(from, to, interval.weight(), interval); case WEEKLY: return getWeeklyCoveringInfo(from, to); case MONTHLY: return getMonthlyCoveringInfo(from, to); case QUARTERLY: return getQuarterlyCoveringInfo(from, to); case YEARLY: return getYearlyCoveringInfo(from, to); default: return new CoveringInfo(0, false); } }
@Test public void testRelativeToAbsolute() throws LensException { Date now = new Date(); Date nowDay = DateUtils.truncate(now, DAY_OF_MONTH); Date nowDayMinus2Days = DateUtils.add(nowDay, DAY_OF_MONTH, -2); assertEquals(relativeToAbsolute("now", now), DateUtil.formatAbsDate(now)); assertEquals(relativeToAbsolute("now.day", now), DateUtil.formatAbsDate(nowDay)); assertEquals(relativeToAbsolute("now.day - 2 days", now), DateUtil.formatAbsDate(nowDayMinus2Days)); assertEquals(relativeToAbsolute("now.day - 2 day", now), DateUtil.formatAbsDate(nowDayMinus2Days)); assertEquals(relativeToAbsolute("now.day - 2day", now), DateUtil.formatAbsDate(nowDayMinus2Days)); assertEquals(relativeToAbsolute("now.day -2 day", now), DateUtil.formatAbsDate(nowDayMinus2Days)); assertEquals(relativeToAbsolute("now.day -2 days", now), DateUtil.formatAbsDate(nowDayMinus2Days)); } @Test
Date ceilFromDate = DateUtil.getCeilDate(timeRangeStart.after(maxIntervalStorageTableStartDate) ? timeRangeStart : maxIntervalStorageTableStartDate, maxInterval); Date floorToDate = DateUtil.getFloorDate(timeRangeEnd.before(maxIntervalStorageTableEndDate) ? timeRangeEnd : maxIntervalStorageTableEndDate, maxInterval); if (ceilFromDate.equals(floorToDate) || floorToDate.before(ceilFromDate)) {
@DataProvider(name = "properties") public Object[][] factProperties() throws LensException { String minus1DaysRelative = "now.day -1 days"; String minus2DaysRelative = "now.day -2 days"; String plus1DaysRelative = "now.day +1 days"; String plus2DaysRelative = "now.day +2 days"; String minus1DaysAbsolute = DateUtil.relativeToAbsolute(minus1DaysRelative, now); String minus2DaysAbsolute = DateUtil.relativeToAbsolute(minus2DaysRelative, now); String plus1DaysAbsolute = DateUtil.relativeToAbsolute(plus1DaysRelative, now); String plus2DaysAbsolute = DateUtil.relativeToAbsolute(plus2DaysRelative, now); Date minus1DaysDate = DateUtil.resolveRelativeDate(minus1DaysRelative, now); Date minus2DaysDate = DateUtil.resolveRelativeDate(minus2DaysRelative, now); Date plus1DaysDate = DateUtil.resolveRelativeDate(plus1DaysRelative, now); Date plus2DaysDate = DateUtil.resolveRelativeDate(plus2DaysRelative, now); return new Object[][]{ {null, null, null, null, new Date(Long.MIN_VALUE), new Date(Long.MAX_VALUE)}, {null, minus2DaysRelative, null, plus2DaysRelative, minus2DaysDate, plus2DaysDate}, {minus2DaysAbsolute, null, plus2DaysAbsolute, null, minus2DaysDate, plus2DaysDate}, {minus1DaysAbsolute, minus2DaysRelative, plus1DaysAbsolute, plus2DaysRelative, minus1DaysDate, plus1DaysDate}, {minus2DaysAbsolute, minus1DaysRelative, plus2DaysAbsolute, plus1DaysRelative, minus1DaysDate, plus1DaysDate}, }; } private Cube getMockCube(Map<String, String> properties) {
public static CoveringInfo getYearlyCoveringInfo(Date from, Date to) { CoveringInfo monthlyCoveringInfo = getMonthlyCoveringInfo(from, to); if (monthlyCoveringInfo.getCountBetween() < 12) { return new CoveringInfo(0, false); } boolean coverable = monthlyCoveringInfo.isCoverable(); if (!from.equals(DateUtils.truncate(from, MONTH))) { from = DateUtils.addMonths(DateUtils.truncate(from, MONTH), 1); coverable = false; } Calendar cal = Calendar.getInstance(); cal.setTime(from); int fromMonth = cal.get(MONTH); int beginOffset = (12 - fromMonth % 12) % 12; int endOffset = (monthlyCoveringInfo.getCountBetween() - beginOffset) % 12; if (beginOffset > 0 || endOffset > 0) { coverable = false; } return new CoveringInfo((monthlyCoveringInfo.getCountBetween() - beginOffset - endOffset) / 12, coverable); }
@Test public void testFloorDate() throws ParseException { Date date = ABSDATE_PARSER.get().parse("2015-01-01-00:00:00,000"); Date curDate = date; for (int i = 0; i < 284; i++) { assertEquals(getFloorDate(curDate, YEARLY), date); curDate = addMilliseconds(curDate, 111111111); } assertEquals(getFloorDate(curDate, YEARLY), DateUtils.addYears(date, 1)); assertEquals(getFloorDate(date, WEEKLY), ABSDATE_PARSER.get().parse("2014-12-28-00:00:00,000")); }
public static String relativeToAbsolute(String relative) throws LensException { return relativeToAbsolute(relative, new Date()); }
if (astNode.getParent() == queriedTimeRange.getAstNode()) { if (astNode.getChildIndex() == 2) { return Pair.of(getStringLiteralAST(formatAbsDate(timeRange.getFromDate())), false); } else if (astNode.getChildIndex() == 3) { return Pair.of(getStringLiteralAST(formatAbsDate(timeRange.getToDate())), false);
@Test public void testCeilDate() throws ParseException { Date date = ABSDATE_PARSER.get().parse("2015-12-26-06:30:15,040"); assertEquals(getCeilDate(date, YEARLY), ABSDATE_PARSER.get().parse("2016-01-01-00:00:00,000")); assertEquals(getCeilDate(date, MONTHLY), ABSDATE_PARSER.get().parse("2016-01-01-00:00:00,000")); assertEquals(getCeilDate(date, DAILY), ABSDATE_PARSER.get().parse("2015-12-27-00:00:00,000")); assertEquals(getCeilDate(date, HOURLY), ABSDATE_PARSER.get().parse("2015-12-26-07:00:00,000")); assertEquals(getCeilDate(date, MINUTELY), ABSDATE_PARSER.get().parse("2015-12-26-06:31:00,000")); assertEquals(getCeilDate(date, SECONDLY), ABSDATE_PARSER.get().parse("2015-12-26-06:30:16,000")); assertEquals(getCeilDate(date, WEEKLY), ABSDATE_PARSER.get().parse("2015-12-27-00:00:00,000")); }
@Test public void testWeeksBetween() throws Exception { CoveringInfo weeks; weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-26"), DATE_FMT.parse("2013-Jun-2")); assertEquals(weeks, new CoveringInfo(1, true), "2013-May-26 to 2013-Jun-2"); weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-27"), DATE_FMT.parse("2013-Jun-3")); assertEquals(weeks, new CoveringInfo(0, false), "2013-May-26 to 2013-Jun-2"); weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-27"), DATE_FMT.parse("2013-Jun-9")); assertEquals(weeks, new CoveringInfo(1, false), "2013-May-26 to 2013-Jun-2"); weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-27"), DATE_FMT.parse("2013-Jun-1")); assertEquals(weeks, new CoveringInfo(0, false), "2013-May-27 to 2013-Jun-1"); weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-25"), DATE_FMT.parse("2013-Jun-2")); assertEquals(weeks, new CoveringInfo(1, false), "2013-May-25 to 2013-Jun-1"); weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-26"), DATE_FMT.parse("2013-Jun-9")); assertEquals(weeks, new CoveringInfo(2, true), "2013-May-26 to 2013-Jun-8"); weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2013-May-26"), DATE_FMT.parse("2013-Jun-10")); assertEquals(weeks, new CoveringInfo(2, false), "2013-May-26 to 2013-Jun-10"); weeks = getWeeklyCoveringInfo(DATE_FMT.parse("2015-Dec-27"), DATE_FMT.parse("2016-Jan-03")); assertEquals(weeks, new CoveringInfo(1, true), "2015-Dec-27 to 2016-Jan-03"); }
@Test public void testQuartersBetween() throws Exception { int i = 0; assertEquals(getQuarterlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(0, false), "2013-Jan-01 to 2013-Jan-31"); i += 2; assertEquals(getQuarterlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(1, false), "2013-Jan-01 to 2013-May-31"); i += 2; assertEquals(getQuarterlyCoveringInfo(pairs[i], DateUtils.round(pairs[i + 1], MONTH)), new CoveringInfo(4, true), "2013-Jan-01 to 2013-Dec-31"); i += 2; assertEquals(getQuarterlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(0, false), "2013-Feb-01 to 2013-Apr-25"); i += 2; assertEquals(getQuarterlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(3, false), "2012-Feb-01 to 2013-Feb-01"); i += 2; assertEquals(getQuarterlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(7, false), "2011-Feb-01 to 2013-Feb-01"); }
@Test public void testYearsBetween() throws Exception { int i = 0; assertEquals(getYearlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(0, false), "" + pairs[i] + "->" + pairs[i + 1]); i += 2; assertEquals(getYearlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(0, false), "" + pairs[i] + "->" + pairs[i + 1]); i += 2; assertEquals(getYearlyCoveringInfo(pairs[i], DateUtils.round(pairs[i + 1], MONTH)), new CoveringInfo(1, true), "" + pairs[i] + "->" + pairs[i + 1]); i += 2; assertEquals(getYearlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(0, false), "" + pairs[i] + "->" + pairs[i + 1]); i += 2; assertEquals(getYearlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(0, false), "" + pairs[i] + "->" + pairs[i + 1]); i += 2; assertEquals(getYearlyCoveringInfo(pairs[i], pairs[i + 1]), new CoveringInfo(1, false), "" + pairs[i] + "->" + pairs[i + 1]); }
static CoveringInfo getCoveringInfo(Date from, Date to, UpdatePeriod interval) { switch (interval) { case SECONDLY: case CONTINUOUS: return getMilliSecondCoveringInfo(from, to, 1000, interval); case MINUTELY: case HOURLY: case DAILY: return getMilliSecondCoveringInfo(from, to, interval.weight(), interval); case WEEKLY: return getWeeklyCoveringInfo(from, to); case MONTHLY: return getMonthlyCoveringInfo(from, to); case QUARTERLY: return getQuarterlyCoveringInfo(from, to); case YEARLY: return getYearlyCoveringInfo(from, to); default: return new CoveringInfo(0, false); } }
Date ceilFromDate = DateUtil.getCeilDate(timeRangeStart.after(maxIntervalStorageTableStartDate) ? timeRangeStart : maxIntervalStorageTableStartDate, maxInterval); Date floorToDate = DateUtil.getFloorDate(timeRangeEnd.before(maxIntervalStorageTableEndDate) ? timeRangeEnd : maxIntervalStorageTableEndDate, maxInterval); if (ceilFromDate.equals(floorToDate) || floorToDate.before(ceilFromDate)) {
@DataProvider(name = "properties") public Object[][] factProperties() throws LensException { String minus1DaysRelative = "now.day -1 days"; String minus2DaysRelative = "now.day -2 days"; String plus1DaysRelative = "now.day +1 days"; String plus2DaysRelative = "now.day +2 days"; String minus1DaysAbsolute = DateUtil.relativeToAbsolute(minus1DaysRelative, now); String minus2DaysAbsolute = DateUtil.relativeToAbsolute(minus2DaysRelative, now); String plus1DaysAbsolute = DateUtil.relativeToAbsolute(plus1DaysRelative, now); String plus2DaysAbsolute = DateUtil.relativeToAbsolute(plus2DaysRelative, now); Date minus1DaysDate = DateUtil.resolveRelativeDate(minus1DaysRelative, now); Date minus2DaysDate = DateUtil.resolveRelativeDate(minus2DaysRelative, now); Date plus1DaysDate = DateUtil.resolveRelativeDate(plus1DaysRelative, now); Date plus2DaysDate = DateUtil.resolveRelativeDate(plus2DaysRelative, now); return new Object[][]{ {null, null, null, null, new Date(Long.MIN_VALUE), new Date(Long.MAX_VALUE)}, {null, minus2DaysRelative, null, plus2DaysRelative, minus2DaysDate, plus2DaysDate}, {minus2DaysAbsolute, null, plus2DaysAbsolute, null, minus2DaysDate, plus2DaysDate}, {minus1DaysAbsolute, minus2DaysRelative, plus1DaysAbsolute, plus2DaysRelative, minus1DaysDate, plus1DaysDate}, {minus2DaysAbsolute, minus1DaysRelative, plus2DaysAbsolute, plus1DaysRelative, minus1DaysDate, plus1DaysDate}, }; }
public static CoveringInfo getYearlyCoveringInfo(Date from, Date to) { CoveringInfo monthlyCoveringInfo = getMonthlyCoveringInfo(from, to); if (monthlyCoveringInfo.getCountBetween() < 12) { return new CoveringInfo(0, false); } boolean coverable = monthlyCoveringInfo.isCoverable(); if (!from.equals(DateUtils.truncate(from, MONTH))) { from = DateUtils.addMonths(DateUtils.truncate(from, MONTH), 1); coverable = false; } Calendar cal = Calendar.getInstance(); cal.setTime(from); int fromMonth = cal.get(MONTH); int beginOffset = (12 - fromMonth % 12) % 12; int endOffset = (monthlyCoveringInfo.getCountBetween() - beginOffset) % 12; if (beginOffset > 0 || endOffset > 0) { coverable = false; } return new CoveringInfo((monthlyCoveringInfo.getCountBetween() - beginOffset - endOffset) / 12, coverable); }
public static Map<String, String> getWhereForMonthly2months(String monthlyTable) { Map<String, String> storageTableToWhereClause = new LinkedHashMap<String, String>(); List<String> parts = new ArrayList<String>(); addParts(parts, MONTHLY, TWO_MONTHS_BACK, DateUtil.getFloorDate(NOW, MONTHLY)); storageTableToWhereClause.put(getDbName() + monthlyTable, StorageUtil.getWherePartClause("dt", TEST_CUBE_NAME, parts)); return storageTableToWhereClause; }