Search in sources :

Example 71 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class FilterTranslator method apply.

/**
 * Transforms a filter predicate into a JPQL query fragment.
 * @param filterPredicate The predicate to transform.
 * @param aliasGenerator Function which supplies a HQL fragment which represents the column in the predicate.
 * @return The hql query fragment.
 */
protected String apply(FilterPredicate filterPredicate, Function<Path, String> aliasGenerator) {
    Function<Path, String> removeThisFromAlias = (path) -> {
        String fieldPath = aliasGenerator.apply(path);
        // JPQL doesn't support 'this', but it does support aliases.
        return fieldPath.replaceAll("\\.this", "");
    };
    Path.PathElement last = filterPredicate.getPath().lastElement().get();
    Operator op = filterPredicate.getOperator();
    JPQLPredicateGenerator generator = predicateOverrides.get(Triple.of(op, last.getType(), last.getFieldName()));
    if (generator == null) {
        generator = operatorGenerators.get(op);
    }
    if (generator == null) {
        throw new BadRequestException("Operator not implemented: " + filterPredicate.getOperator());
    }
    return generator.generate(filterPredicate, removeThisFromAlias);
}
Also used : Path(com.yahoo.elide.core.Path) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Path(com.yahoo.elide.core.Path) LE(com.yahoo.elide.core.filter.Operator.LE) FilterOperation(com.yahoo.elide.core.filter.FilterOperation) NOT(com.yahoo.elide.core.filter.Operator.NOT) HashMap(java.util.HashMap) NOTBETWEEN(com.yahoo.elide.core.filter.Operator.NOTBETWEEN) POSTFIX(com.yahoo.elide.core.filter.Operator.POSTFIX) Function(java.util.function.Function) ISNULL(com.yahoo.elide.core.filter.Operator.ISNULL) PREFIX(com.yahoo.elide.core.filter.Operator.PREFIX) BETWEEN(com.yahoo.elide.core.filter.Operator.BETWEEN) IN(com.yahoo.elide.core.filter.Operator.IN) ISEMPTY(com.yahoo.elide.core.filter.Operator.ISEMPTY) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) Map(java.util.Map) GT(com.yahoo.elide.core.filter.Operator.GT) IN_INSENSITIVE(com.yahoo.elide.core.filter.Operator.IN_INSENSITIVE) HASMEMBER(com.yahoo.elide.core.filter.Operator.HASMEMBER) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Triple(org.apache.commons.lang3.tuple.Triple) INFIX_CASE_INSENSITIVE(com.yahoo.elide.core.filter.Operator.INFIX_CASE_INSENSITIVE) FilterParameter(com.yahoo.elide.core.filter.predicates.FilterPredicate.FilterParameter) FilterExpressionVisitor(com.yahoo.elide.core.filter.expression.FilterExpressionVisitor) NOT_INSENSITIVE(com.yahoo.elide.core.filter.Operator.NOT_INSENSITIVE) NOTEMPTY(com.yahoo.elide.core.filter.Operator.NOTEMPTY) TRUE(com.yahoo.elide.core.filter.Operator.TRUE) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) EnumMap(java.util.EnumMap) TypeHelper.getPathAlias(com.yahoo.elide.core.utils.TypeHelper.getPathAlias) GE(com.yahoo.elide.core.filter.Operator.GE) FALSE(com.yahoo.elide.core.filter.Operator.FALSE) Collectors(java.util.stream.Collectors) NOTNULL(com.yahoo.elide.core.filter.Operator.NOTNULL) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) INFIX(com.yahoo.elide.core.filter.Operator.INFIX) PREFIX_CASE_INSENSITIVE(com.yahoo.elide.core.filter.Operator.PREFIX_CASE_INSENSITIVE) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) HASNOMEMBER(com.yahoo.elide.core.filter.Operator.HASNOMEMBER) LT(com.yahoo.elide.core.filter.Operator.LT) Type(com.yahoo.elide.core.type.Type) Operator(com.yahoo.elide.core.filter.Operator) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Preconditions(com.google.common.base.Preconditions) TypeHelper.getFieldAlias(com.yahoo.elide.core.utils.TypeHelper.getFieldAlias) POSTFIX_CASE_INSENSITIVE(com.yahoo.elide.core.filter.Operator.POSTFIX_CASE_INSENSITIVE) Operator(com.yahoo.elide.core.filter.Operator) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException)

Example 72 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class HasMemberJPQLGenerator method getOuterQueryIdField.

private String getOuterQueryIdField(Path path, Function<Path, String> aliasGenerator) {
    Path.PathElement firstElement = path.getPathElements().get(0);
    Type<?> modelType = firstElement.getType();
    String idField = dictionary.getIdFieldName(modelType);
    Type<?> idFieldType = dictionary.getIdType(modelType);
    Path idPath = new Path(Arrays.asList(new Path.PathElement(modelType, idFieldType, idField)));
    return aliasGenerator.apply(idPath);
}
Also used : Path(com.yahoo.elide.core.Path)

Example 73 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class QueryTranslator method visitQuery.

@Override
public NativeQuery.NativeQueryBuilder visitQuery(Query query) {
    NativeQuery.NativeQueryBuilder builder = query.getSource().accept(this);
    if (query.isNested()) {
        NativeQuery innerQuery = builder.build();
        builder = NativeQuery.builder().fromClause(getFromClause("(" + innerQuery + ")", applyQuotes(query.getSource().getAlias()), dialect));
    }
    Set<String> joinExpressions = new LinkedHashSet<>();
    builder.projectionClause(constructProjectionWithReference(query));
    // Handles join for all type of column projects - dimensions, metrics and time dimention
    joinExpressions.addAll(extractJoinExpressions(query));
    Set<ColumnProjection> groupByDimensions = query.getAllDimensionProjections().stream().map(SQLColumnProjection.class::cast).filter(SQLColumnProjection::isProjected).collect(Collectors.toCollection(LinkedHashSet::new));
    if (!groupByDimensions.isEmpty()) {
        if (!query.getMetricProjections().isEmpty()) {
            builder.groupByClause("GROUP BY " + groupByDimensions.stream().map(SQLColumnProjection.class::cast).map((column) -> column.toSQL(query, metaDataStore)).collect(Collectors.joining(", ")));
        }
    }
    if (query.getWhereFilter() != null) {
        builder.whereClause("WHERE " + translateFilterExpression(query.getWhereFilter(), path -> generatePredicatePathReference(path, query)));
        joinExpressions.addAll(extractJoinExpressions(query, query.getWhereFilter()));
    }
    if (query.getHavingFilter() != null) {
        builder.havingClause("HAVING " + translateFilterExpression(query.getHavingFilter(), (path) -> constructHavingClauseWithReference(path, query)));
        joinExpressions.addAll(extractJoinExpressions(query, query.getHavingFilter()));
    }
    if (query.getSorting() != null) {
        Map<Path, Sorting.SortOrder> sortClauses = query.getSorting().getSortingPaths();
        builder.orderByClause(extractOrderBy(sortClauses, query));
        joinExpressions.addAll(extractJoinExpressions(query, sortClauses));
    }
    Pagination pagination = query.getPagination();
    if (pagination != null) {
        builder.offsetLimitClause(dialect.generateOffsetLimitClause(pagination.getOffset(), pagination.getLimit()));
    }
    return builder.joinClause(String.join(" ", joinExpressions));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Path(com.yahoo.elide.core.Path) Function(java.util.function.Function) Argument(com.yahoo.elide.core.request.Argument) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) Map(java.util.Map) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) FilterTranslator(com.yahoo.elide.datastores.jpql.filter.FilterTranslator) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) LinkedHashSet(java.util.LinkedHashSet) QueryVisitor(com.yahoo.elide.datastores.aggregation.query.QueryVisitor) TableContext(com.yahoo.elide.datastores.aggregation.metadata.TableContext) JoinExpressionExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinExpressionExtractor) Sorting(com.yahoo.elide.core.request.Sorting) Queryable(com.yahoo.elide.datastores.aggregation.query.Queryable) FromSubquery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery) ColumnContext(com.yahoo.elide.datastores.aggregation.metadata.ColumnContext) Collection(java.util.Collection) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) Set(java.util.Set) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Stream(java.util.stream.Stream) Pagination(com.yahoo.elide.core.request.Pagination) Type(com.yahoo.elide.core.type.Type) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) ExpressionParser(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser) Reference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference) Path(com.yahoo.elide.core.Path) Pagination(com.yahoo.elide.core.request.Pagination) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection)

Example 74 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class ReferenceExtractor method visitJoinReference.

@Override
public Set<T> visitJoinReference(JoinReference reference) {
    if (referenceType.equals(JoinReference.class)) {
        references.add((T) reference);
    }
    JoinPath path = reference.getPath();
    int pathLimit = (mode == Mode.SAME_QUERY) ? 1 : path.getPathElements().size() - 1;
    for (int idx = 0; idx < pathLimit; idx++) {
        Path.PathElement pathElement = path.getPathElements().get(idx);
        String fieldName = pathElement.getFieldName();
        Type<?> parentClass = pathElement.getType();
        SQLTable table = metaDataStore.getTable(parentClass);
        SQLJoin join = table.getJoin(fieldName);
        if (visitedJoins.contains(join)) {
            continue;
        }
        visitedJoins.add(join);
        parser.parse(table, join.getJoinExpression()).stream().forEach(ref -> ref.accept(this));
    }
    if (mode != Mode.SAME_QUERY) {
        return reference.getReference().accept(this);
    }
    return references;
}
Also used : Path(com.yahoo.elide.core.Path) JoinPath(com.yahoo.elide.datastores.aggregation.core.JoinPath) JoinPath(com.yahoo.elide.datastores.aggregation.core.JoinPath) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) SQLJoin(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLJoin)

Example 75 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class VerifyFieldAccessFilterExpressionVisitorTest method testAccept.

@Test
public void testAccept() throws Exception {
    Path p1Path = new Path(Arrays.asList(new PathElement(Book.class, Author.class, AUTHORS), new PathElement(Author.class, String.class, NAME)));
    FilterPredicate p1 = new InPredicate(p1Path, "foo", "bar");
    Path p2Path = new Path(Arrays.asList(new PathElement(Book.class, String.class, NAME)));
    FilterPredicate p2 = new InPredicate(p2Path, "blah");
    Path p3Path = new Path(Arrays.asList(new PathElement(Book.class, String.class, GENRE)));
    FilterPredicate p3 = new InPredicate(p3Path, SCIFI);
    // P4 is a duplicate of P3
    Path p4Path = new Path(Arrays.asList(new PathElement(Book.class, String.class, GENRE)));
    FilterPredicate p4 = new InPredicate(p4Path, SCIFI);
    OrFilterExpression or = new OrFilterExpression(p2, p3);
    AndFilterExpression and1 = new AndFilterExpression(or, p1);
    AndFilterExpression and2 = new AndFilterExpression(and1, p4);
    NotFilterExpression not = new NotFilterExpression(and2);
    Book book = new Book();
    Author author = new Author();
    book.setAuthors(Collections.singleton(author));
    author.setBooks(Collections.singleton(book));
    PersistentResource<Book> resource = new PersistentResource<>(book, "", scope);
    VerifyFieldAccessFilterExpressionVisitor visitor = new VerifyFieldAccessFilterExpressionVisitor(resource);
    // unrestricted fields
    assertTrue(not.accept(visitor));
    assertTrue(and1.accept(visitor));
    assertTrue(and2.accept(visitor));
    assertTrue(or.accept(visitor));
    assertTrue(p1.accept(visitor));
    assertTrue(p2.accept(visitor));
    assertTrue(p3.accept(visitor));
    assertTrue(p4.accept(visitor));
    PermissionExecutor permissionExecutor = scope.getPermissionExecutor();
    verify(permissionExecutor, times(17)).evaluateFilterJoinUserChecks(any(), any());
    verify(permissionExecutor, times(5)).checkSpecificFieldPermissions(resource, null, ReadPermission.class, NAME);
    verify(permissionExecutor, times(21)).checkUserPermissions(any(), any(), isA(String.class));
    verify(permissionExecutor, never()).handleFilterJoinReject(any(), any(), any());
}
Also used : Path(com.yahoo.elide.core.Path) PersistentResource(com.yahoo.elide.core.PersistentResource) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) PermissionExecutor(com.yahoo.elide.core.security.PermissionExecutor) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) PathElement(com.yahoo.elide.core.Path.PathElement) Book(example.Book) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Test(org.junit.jupiter.api.Test)

Aggregations

Path (com.yahoo.elide.core.Path)88 Test (org.junit.jupiter.api.Test)67 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)51 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)41 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)35 Query (com.yahoo.elide.datastores.aggregation.query.Query)35 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)34 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)33 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)31 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)29 Book (example.Book)29 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)22 Date (java.util.Date)19 Argument (com.yahoo.elide.core.request.Argument)14 EntityProjection (com.yahoo.elide.core.request.EntityProjection)14 GameRevenue (example.GameRevenue)14 HashSet (java.util.HashSet)14 Author (example.Author)12 Publisher (example.Publisher)9 Sorting (com.yahoo.elide.core.request.Sorting)8