Search in sources :

Example 1 with Temporal

use of com.yahoo.elide.datastores.aggregation.annotation.Temporal in project elide by yahoo.

the class TableType method buildAnnotations.

private static Map<Class<? extends Annotation>, Annotation> buildAnnotations(Dimension dimension) {
    Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<>();
    annotations.put(ColumnMeta.class, new ColumnMeta() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return ColumnMeta.class;
        }

        @Override
        public String friendlyName() {
            return dimension.getFriendlyName();
        }

        @Override
        public String description() {
            return dimension.getDescription();
        }

        @Override
        public String category() {
            return dimension.getCategory();
        }

        @Override
        public TableSource tableSource() {
            return buildTableSource(dimension.getTableSource());
        }

        @Override
        public String[] tags() {
            return dimension.getTags().toArray(new String[0]);
        }

        @Override
        public String[] values() {
            return dimension.getValues().toArray(new String[0]);
        }

        @Override
        public boolean isHidden() {
            return dimension.getHidden() != null && dimension.getHidden();
        }

        @Override
        public String filterTemplate() {
            return dimension.getFilterTemplate();
        }

        @Override
        public CardinalitySize size() {
            if (dimension.getCardinality() == null || dimension.getCardinality().isEmpty()) {
                return CardinalitySize.UNKNOWN;
            }
            return CardinalitySize.valueOf(dimension.getCardinality().toUpperCase(Locale.ENGLISH));
        }
    });
    annotations.put(DimensionFormula.class, new DimensionFormula() {

        @Override
        public ArgumentDefinition[] arguments() {
            return getArgumentDefinitions(dimension.getArguments());
        }

        @Override
        public Class<? extends Annotation> annotationType() {
            return DimensionFormula.class;
        }

        @Override
        public String value() {
            return trimColumnReferences(dimension.getDefinition());
        }
    });
    String readPermission = dimension.getReadAccess();
    if (StringUtils.isNotEmpty(readPermission)) {
        annotations.put(ReadPermission.class, new ReadPermission() {

            @Override
            public Class<? extends Annotation> annotationType() {
                return ReadPermission.class;
            }

            @Override
            public String expression() {
                return readPermission;
            }
        });
    }
    if (dimension.getType().toUpperCase(Locale.ROOT).equals(ENUM_ORDINAL)) {
        annotations.put(Enumerated.class, getEnumeratedAnnotation(EnumType.ORDINAL));
    }
    if (dimension.getType().toUpperCase(Locale.ROOT).equals(TIME)) {
        annotations.put(Temporal.class, new Temporal() {

            @Override
            public Class<? extends Annotation> annotationType() {
                return Temporal.class;
            }

            @Override
            public TimeGrainDefinition[] grains() {
                int numGrains = dimension.getGrains() == null ? 0 : dimension.getGrains().size();
                TimeGrainDefinition[] definitions = new TimeGrainDefinition[numGrains];
                for (int idx = 0; idx < numGrains; idx++) {
                    Grain grain = dimension.getGrains().get(idx);
                    definitions[idx] = new TimeGrainDefinition() {

                        @Override
                        public Class<? extends Annotation> annotationType() {
                            return TimeGrainDefinition.class;
                        }

                        @Override
                        public TimeGrain grain() {
                            if (grain.getType() == null) {
                                return TimeGrain.DAY;
                            }
                            return TimeGrain.valueOf(grain.getType().name());
                        }

                        @Override
                        public String expression() {
                            String sql = grain.getSql();
                            if (StringUtils.isEmpty(sql)) {
                                return "{{$$column.expr}}";
                            }
                            return grain.getSql();
                        }
                    };
                }
                return definitions;
            }

            @Override
            public String timeZone() {
                return "UTC";
            }
        });
    }
    return annotations;
}
Also used : CardinalitySize(com.yahoo.elide.datastores.aggregation.annotation.CardinalitySize) HashMap(java.util.HashMap) Grain(com.yahoo.elide.modelconfig.model.Grain) TimeGrain(com.yahoo.elide.datastores.aggregation.metadata.enums.TimeGrain) Annotation(java.lang.annotation.Annotation) TableSource(com.yahoo.elide.datastores.aggregation.annotation.TableSource) ColumnMeta(com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta) DimensionFormula(com.yahoo.elide.datastores.aggregation.annotation.DimensionFormula) Temporal(com.yahoo.elide.datastores.aggregation.annotation.Temporal) ReadPermission(com.yahoo.elide.annotation.ReadPermission) TimeGrainDefinition(com.yahoo.elide.datastores.aggregation.annotation.TimeGrainDefinition)

Example 2 with Temporal

use of com.yahoo.elide.datastores.aggregation.annotation.Temporal in project elide by yahoo.

the class TableTypeTest method testMultipleTimeDimensionGrains.

@Test
void testMultipleTimeDimensionGrains() throws Exception {
    Table testTable = Table.builder().table("table1").name("Table").dimension(Dimension.builder().type(Type.TIME).definition("{{createdOn}}").name("createdOn").grain(Grain.builder().sql("some sql").type(Grain.GrainType.DAY).build()).grain(Grain.builder().sql("some other sql").type(Grain.GrainType.YEAR).build()).build()).build();
    TableType testType = new TableType(testTable);
    Field field = testType.getDeclaredField("createdOn");
    assertNotNull(field);
    Temporal temporal = field.getAnnotation(Temporal.class);
    assertEquals("UTC", temporal.timeZone());
    assertTrue(temporal.grains().length == 2);
    assertEquals("some sql", temporal.grains()[0].expression());
    assertEquals("some other sql", temporal.grains()[1].expression());
    assertEquals(TimeGrain.DAY, temporal.grains()[0].grain());
    assertEquals(TimeGrain.YEAR, temporal.grains()[1].grain());
}
Also used : Field(com.yahoo.elide.core.type.Field) Table(com.yahoo.elide.modelconfig.model.Table) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) Temporal(com.yahoo.elide.datastores.aggregation.annotation.Temporal) Test(org.junit.jupiter.api.Test)

Example 3 with Temporal

use of com.yahoo.elide.datastores.aggregation.annotation.Temporal in project elide by yahoo.

the class TableTypeTest method testTimeDimensionAnnotations.

@Test
void testTimeDimensionAnnotations() throws Exception {
    Set<String> tags = new HashSet<>(Arrays.asList("tag1", "tag2"));
    Table testTable = Table.builder().table("table1").name("Table").dimension(Dimension.builder().type(Type.TIME).category("category1").definition("{{createdOn  }}").hidden(false).friendlyName("Created On").name("createdOn").readAccess("Admin").description("A time dimension").tags(tags).build()).build();
    TableType testType = new TableType(testTable);
    Field field = testType.getDeclaredField("createdOn");
    assertNotNull(field);
    ReadPermission readPermission = field.getAnnotation(ReadPermission.class);
    assertEquals("Admin", readPermission.expression());
    ColumnMeta columnMeta = field.getAnnotation(ColumnMeta.class);
    assertEquals("A time dimension", columnMeta.description());
    assertEquals("category1", columnMeta.category());
    assertEquals("Created On", columnMeta.friendlyName());
    assertEquals(CardinalitySize.UNKNOWN, columnMeta.size());
    assertEquals(tags, new HashSet<>(Arrays.asList(columnMeta.tags())));
    DimensionFormula dimensionFormula = field.getAnnotation(DimensionFormula.class);
    assertEquals("{{createdOn}}", dimensionFormula.value());
    Temporal temporal = field.getAnnotation(Temporal.class);
    assertEquals("UTC", temporal.timeZone());
    assertTrue(temporal.grains().length == 0);
}
Also used : Field(com.yahoo.elide.core.type.Field) Table(com.yahoo.elide.modelconfig.model.Table) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) ColumnMeta(com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta) DimensionFormula(com.yahoo.elide.datastores.aggregation.annotation.DimensionFormula) Temporal(com.yahoo.elide.datastores.aggregation.annotation.Temporal) ReadPermission(com.yahoo.elide.annotation.ReadPermission) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

Temporal (com.yahoo.elide.datastores.aggregation.annotation.Temporal)3 ReadPermission (com.yahoo.elide.annotation.ReadPermission)2 Field (com.yahoo.elide.core.type.Field)2 ColumnMeta (com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta)2 DimensionFormula (com.yahoo.elide.datastores.aggregation.annotation.DimensionFormula)2 FromTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable)2 Table (com.yahoo.elide.modelconfig.model.Table)2 Test (org.junit.jupiter.api.Test)2 CardinalitySize (com.yahoo.elide.datastores.aggregation.annotation.CardinalitySize)1 TableSource (com.yahoo.elide.datastores.aggregation.annotation.TableSource)1 TimeGrainDefinition (com.yahoo.elide.datastores.aggregation.annotation.TimeGrainDefinition)1 TimeGrain (com.yahoo.elide.datastores.aggregation.metadata.enums.TimeGrain)1 Grain (com.yahoo.elide.modelconfig.model.Grain)1 Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1