Repository: lens Updated Branches: refs/heads/master bfba3a1eb -> 13dc803c7
LENS-820 : Allow specifying relative end time for facts Project: http://git-wip-us.apache.org/repos/asf/lens/repo Commit: http://git-wip-us.apache.org/repos/asf/lens/commit/13dc803c Tree: http://git-wip-us.apache.org/repos/asf/lens/tree/13dc803c Diff: http://git-wip-us.apache.org/repos/asf/lens/diff/13dc803c Branch: refs/heads/master Commit: 13dc803c7b4fec1abb525ecd8de69cec94764fdc Parents: bfba3a1 Author: Rajat Khandelwal <[email protected]> Authored: Tue Oct 20 11:34:38 2015 +0530 Committer: Amareshwari Sriramadasu <[email protected]> Committed: Tue Oct 20 11:34:38 2015 +0530 ---------------------------------------------------------------------- .../lens/cube/metadata/CubeFactTable.java | 20 +++- .../lens/cube/metadata/MetastoreConstants.java | 1 + .../lens/cube/metadata/CubeFactTableTest.java | 105 +++++++++++++++++++ 3 files changed, 122 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lens/blob/13dc803c/lens-cube/src/main/java/org/apache/lens/cube/metadata/CubeFactTable.java ---------------------------------------------------------------------- diff --git a/lens-cube/src/main/java/org/apache/lens/cube/metadata/CubeFactTable.java b/lens-cube/src/main/java/org/apache/lens/cube/metadata/CubeFactTable.java index a6cff41..a7a5bb0 100644 --- a/lens-cube/src/main/java/org/apache/lens/cube/metadata/CubeFactTable.java +++ b/lens-cube/src/main/java/org/apache/lens/cube/metadata/CubeFactTable.java @@ -30,7 +30,7 @@ import org.apache.hadoop.hive.ql.metadata.Table; import com.google.common.collect.Lists; -public final class CubeFactTable extends AbstractCubeTable { +public class CubeFactTable extends AbstractCubeTable { private String cubeName; private final Map<String, Set<UpdatePeriod>> storageUpdatePeriods; @@ -222,7 +222,7 @@ public final class CubeFactTable extends AbstractCubeTable { */ public List<String> getValidColumns() { String validColsStr = - MetastoreUtil.getNamedStringValue(getProperties(), MetastoreUtil.getValidColumnsKey(getName())); + MetastoreUtil.getNamedStringValue(getProperties(), MetastoreUtil.getValidColumnsKey(getName())); return validColsStr == null ? null : Arrays.asList(StringUtils.split(validColsStr.toLowerCase(), ',')); } @@ -332,7 +332,7 @@ public final class CubeFactTable extends AbstractCubeTable { public Date getRelativeStartTime() { try { - return DateUtil.resolveRelativeDate(getProperties().get(MetastoreConstants.FACT_RELATIVE_START_TIME), new Date()); + return DateUtil.resolveRelativeDate(getProperties().get(MetastoreConstants.FACT_RELATIVE_START_TIME), now()); } catch (Exception e) { return new Date(Long.MIN_VALUE); } @@ -350,7 +350,19 @@ public final class CubeFactTable extends AbstractCubeTable { } } + public Date getRelativeEndTime() { + try { + return DateUtil.resolveRelativeDate(getProperties().get(MetastoreConstants.FACT_RELATIVE_END_TIME), now()); + } catch (Exception e) { + return new Date(Long.MAX_VALUE); + } + } + public Date getEndTime() { - return getAbsoluteEndTime(); + return Collections.min(Lists.newArrayList(getRelativeEndTime(), getAbsoluteEndTime())); + } + + public Date now() { + return new Date(); } } http://git-wip-us.apache.org/repos/asf/lens/blob/13dc803c/lens-cube/src/main/java/org/apache/lens/cube/metadata/MetastoreConstants.java ---------------------------------------------------------------------- diff --git a/lens-cube/src/main/java/org/apache/lens/cube/metadata/MetastoreConstants.java b/lens-cube/src/main/java/org/apache/lens/cube/metadata/MetastoreConstants.java index da47fa5..ed78d74 100644 --- a/lens-cube/src/main/java/org/apache/lens/cube/metadata/MetastoreConstants.java +++ b/lens-cube/src/main/java/org/apache/lens/cube/metadata/MetastoreConstants.java @@ -54,6 +54,7 @@ public final class MetastoreConstants { public static final String FACT_ABSOLUTE_START_TIME = "cube.fact.absolute.start.time"; public static final String FACT_RELATIVE_START_TIME = "cube.fact.relative.start.time"; public static final String FACT_ABSOLUTE_END_TIME = "cube.fact.absolute.end.time"; + public static final String FACT_RELATIVE_END_TIME = "cube.fact.relative.end.time"; // dim table constants // TODO: remove this and move to "dimtable." http://git-wip-us.apache.org/repos/asf/lens/blob/13dc803c/lens-cube/src/test/java/org/apache/lens/cube/metadata/CubeFactTableTest.java ---------------------------------------------------------------------- diff --git a/lens-cube/src/test/java/org/apache/lens/cube/metadata/CubeFactTableTest.java b/lens-cube/src/test/java/org/apache/lens/cube/metadata/CubeFactTableTest.java new file mode 100644 index 0000000..2abc6d0 --- /dev/null +++ b/lens-cube/src/test/java/org/apache/lens/cube/metadata/CubeFactTableTest.java @@ -0,0 +1,105 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.lens.cube.metadata; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import org.apache.lens.cube.parse.DateUtil; +import org.apache.lens.server.api.error.LensException; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +public class CubeFactTableTest { + Date now = new Date(); + + @DataProvider(name = "properties") + public Object[][] factProperties() throws LensException { + String minus1DaysRelative = "now -1 days"; + String minus2DaysRelative = "now -2 days"; + String plus1DaysRelative = "now +1 days"; + String plus2DaysRelative = "now +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 CubeFactTable getMockCubeFactTable(Map<String, String> properties) { + CubeFactTable cubeFactTable = mock(CubeFactTable.class); + when(cubeFactTable.now()).thenReturn(now); + + when(cubeFactTable.getProperties()).thenReturn(properties); + + when(cubeFactTable.getRelativeStartTime()).thenCallRealMethod(); + when(cubeFactTable.getAbsoluteStartTime()).thenCallRealMethod(); + + when(cubeFactTable.getRelativeEndTime()).thenCallRealMethod(); + when(cubeFactTable.getAbsoluteEndTime()).thenCallRealMethod(); + + when(cubeFactTable.getStartTime()).thenCallRealMethod(); + when(cubeFactTable.getEndTime()).thenCallRealMethod(); + + return cubeFactTable; + } + + @Test(dataProvider = "properties") + public void testStartAndEndTime(String absoluteStartProperty, String relativeStartProperty, + String absoluteEndProperty, String relativeEndProperty, + Date expectedStartTime, Date expectedEndTime) throws Exception { + + Map<String, String> properties = new HashMap<>(); + if (absoluteStartProperty != null) { + properties.put(MetastoreConstants.FACT_ABSOLUTE_START_TIME, absoluteStartProperty); + } + if (relativeStartProperty != null) { + properties.put(MetastoreConstants.FACT_RELATIVE_START_TIME, relativeStartProperty); + } + if (absoluteEndProperty != null) { + properties.put(MetastoreConstants.FACT_ABSOLUTE_END_TIME, absoluteEndProperty); + } + if (relativeEndProperty != null) { + properties.put(MetastoreConstants.FACT_RELATIVE_END_TIME, relativeEndProperty); + } + CubeFactTable cubeFactTable = getMockCubeFactTable(properties); + + assertEquals(cubeFactTable.getStartTime(), expectedStartTime); + assertEquals(cubeFactTable.getEndTime(), expectedEndTime); + } +}
