Search in sources :

Example 1 with ColumnContext

use of com.yahoo.elide.datastores.aggregation.metadata.ColumnContext in project elide by yahoo.

the class JoinExpressionExtractor method visitJoinReference.

@Override
public Set<String> visitJoinReference(JoinReference reference) {
    JoinPath joinPath = reference.getPath();
    List<PathElement> pathElements = joinPath.getPathElements();
    ColumnContext currentCtx = this.context;
    for (int i = 0; i < pathElements.size() - 1; i++) {
        PathElement pathElement = pathElements.get(i);
        Type<?> joinClass = pathElement.getFieldType();
        String joinFieldName = pathElement.getFieldName();
        SQLJoin sqlJoin = currentCtx.getQueryable().getJoin(joinFieldName);
        ColumnContext joinCtx;
        String onClause;
        JoinType joinType;
        String fullExpression;
        if (sqlJoin != null) {
            joinType = sqlJoin.getJoinType();
            joinCtx = (ColumnContext) currentCtx.get(joinFieldName);
            if (joinType.equals(JoinType.CROSS)) {
                onClause = EMPTY;
            } else {
                onClause = ON + currentCtx.resolve(sqlJoin.getJoinExpression());
            }
        } else {
            joinType = JoinType.LEFT;
            SQLTable table = metaDataStore.getTable(joinClass);
            joinCtx = ColumnContext.builder().queryable(table).alias(appendAlias(currentCtx.getAlias(), joinFieldName)).metaDataStore(currentCtx.getMetaDataStore()).column(currentCtx.getColumn()).tableArguments(mergedArgumentMap(table.getArguments(), currentCtx.getTableArguments())).build();
            onClause = ON + String.format("%s.%s = %s.%s", currentCtx.getAlias(), dictionary.getAnnotatedColumnName(pathElement.getType(), joinFieldName), joinCtx.getAlias(), dictionary.getAnnotatedColumnName(joinClass, dictionary.getIdFieldName(joinClass)));
        }
        SQLDialect sqlDialect = currentCtx.getQueryable().getDialect();
        String joinAlias = applyQuotes(joinCtx.getAlias(), sqlDialect);
        String joinKeyword = currentCtx.getQueryable().getDialect().getJoinKeyword(joinType);
        String joinSource = constructTableOrSubselect(joinCtx, joinClass);
        if (sqlDialect.useASBeforeTableAlias()) {
            fullExpression = String.format("%s %s AS %s %s", joinKeyword, joinSource, joinAlias, onClause);
        } else {
            fullExpression = String.format("%s %s %s %s", joinKeyword, joinSource, joinAlias, onClause);
        }
        joinExpressions.add(fullExpression);
        /**
         * If this `for` loop runs more than once, context should be switched to join context.
         */
        currentCtx = joinCtx;
    }
    // If reference within current join reference is of type PhysicalReference, then below visitor doesn't matter.
    // If it is of type LogicalReference, then visitLogicalReference method will recreate visitor with correct
    // value of ColumnProjection in context.
    JoinExpressionExtractor visitor = new JoinExpressionExtractor(currentCtx);
    joinExpressions.addAll(reference.getReference().accept(visitor));
    return joinExpressions;
}
Also used : JoinPath(com.yahoo.elide.datastores.aggregation.core.JoinPath) PathElement(com.yahoo.elide.core.Path.PathElement) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) ColumnContext(com.yahoo.elide.datastores.aggregation.metadata.ColumnContext) SQLJoin(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLJoin) JoinType(com.yahoo.elide.datastores.aggregation.annotation.JoinType)

Example 2 with ColumnContext

use of com.yahoo.elide.datastores.aggregation.metadata.ColumnContext in project elide by yahoo.

the class JoinExpressionExtractor method visitLogicalReference.

@Override
public Set<String> visitLogicalReference(LogicalReference reference) {
    /**
     * For the scenario: col1:{{col2}}, col2:{{col3}}, col3:{{join.col1}}
     * Creating new visitor with new ColumnProjection.
     */
    ColumnProjection newColumn = reference.getColumn();
    ColumnContext newCtx = ColumnContext.builder().queryable(this.context.getQueryable()).alias(this.context.getAlias()).metaDataStore(this.context.getMetaDataStore()).column(newColumn).tableArguments(this.context.getTableArguments()).build();
    JoinExpressionExtractor visitor = new JoinExpressionExtractor(newCtx);
    reference.getReferences().forEach(ref -> {
        joinExpressions.addAll(ref.accept(visitor));
    });
    return joinExpressions;
}
Also used : ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) ColumnContext(com.yahoo.elide.datastores.aggregation.metadata.ColumnContext)

Example 3 with ColumnContext

use of com.yahoo.elide.datastores.aggregation.metadata.ColumnContext in project elide by yahoo.

the class QueryTranslator method extractJoinExpressions.

/**
 * Get required join expressions for given column in given Query.
 * @param column {@link ColumnProjection}
 * @param query Expanded Query.
 * @return A set of Join expressions that capture a relationship traversal.
 */
private Set<String> extractJoinExpressions(ColumnProjection column, Query query) {
    Set<String> joinExpressions = new LinkedHashSet<>();
    ColumnContext context = ColumnContext.builder().queryable(query).alias(query.getSource().getAlias()).metaDataStore(metaDataStore).column(column).tableArguments(query.getArguments()).build();
    JoinExpressionExtractor visitor = new JoinExpressionExtractor(context);
    List<Reference> references = parser.parse(query.getSource(), column);
    references.forEach(ref -> joinExpressions.addAll(ref.accept(visitor)));
    return joinExpressions;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JoinExpressionExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinExpressionExtractor) Reference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference) ColumnContext(com.yahoo.elide.datastores.aggregation.metadata.ColumnContext)

Aggregations

ColumnContext (com.yahoo.elide.datastores.aggregation.metadata.ColumnContext)3 PathElement (com.yahoo.elide.core.Path.PathElement)1 JoinType (com.yahoo.elide.datastores.aggregation.annotation.JoinType)1 JoinPath (com.yahoo.elide.datastores.aggregation.core.JoinPath)1 ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)1 SQLDialect (com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect)1 JoinExpressionExtractor (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinExpressionExtractor)1 Reference (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference)1 SQLJoin (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLJoin)1 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)1 LinkedHashSet (java.util.LinkedHashSet)1