Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed 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 com.facebook.presto.sql.planner;
 
 
 import java.util.List;
 import java.util.Map;
 
 import static com.facebook.presto.sql.QueryUtil.mangleFieldReference;

Keeps track of fields and expressions and their mapping to symbols in the current plan
 
 {
     // all expressions are rewritten in terms of fields declared by this relation plan
     private final RelationPlan rewriteBase;
     private final Analysis analysis;
 
     // current mappings of underlying field -> symbol for translating direct field references
     private final Symbol[] fieldSymbols;
 
     // current mappings of sub-expressions -> symbol
     private final Map<ExpressionSymbolexpressionMappings = new HashMap<>();
 
     public TranslationMap(RelationPlan rewriteBaseAnalysis analysis)
     {
         this. = rewriteBase;
         this. = analysis;
 
          = new Symbol[rewriteBase.getOutputSymbols().size()];
     }
 
     public RelationPlan getRelationPlan()
     {
         return ;
     }
 
     public void setFieldMappings(List<Symbolsymbols)
     {
         Preconditions.checkArgument(symbols.size() == ."size of symbols list (%s) doesn't match number of expected fields (%s)"symbols.size(), .);
 
         for (int i = 0; i < symbols.size(); i++) {
             this.[i] = symbols.get(i);
         }
     }
 
     public void copyMappingsFrom(TranslationMap other)
     {
         Preconditions.checkArgument(other.fieldSymbols.length == .,
                 "number of fields in other (%s) doesn't match number of expected fields (%s)",
                 other.fieldSymbols.length,
                 .);
 
         .putAll(other.expressionMappings);
         System.arraycopy(other.fieldSymbols, 0, , 0, other.fieldSymbols.length);
     }
 
     public Expression rewrite(Expression expression)
     {
         // first, translate names from sql-land references to plan symbols
         Expression mapped = translateNamesToSymbols(expression);
 
         // then rewrite subexpressions in terms of the current mappings
         return ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Void>()
         {
             @Override
             public Expression rewriteExpression(Expression nodeVoid contextExpressionTreeRewriter<VoidtreeRewriter)
             {
                 // convert expression to qualified name reference (containing a symbol) if rewrite registered
                 Expression rewrittenExpression;
                 Symbol symbol = .get(node);
                 if (symbol != null) {
                     rewrittenExpression = new QualifiedNameReference(symbol.toQualifiedName());
                 }
                else {
                    rewrittenExpression = treeRewriter.defaultRewrite(nodecontext);
                }
                return rewrittenExpression;
            }
        }, mapped);
    }
    public Expression rewrite(FieldOrExpression fieldOrExpression)
    {
        if (fieldOrExpression.isFieldReference()) {
            int fieldIndex = fieldOrExpression.getFieldIndex();
            Symbol symbol = [fieldIndex];
            Preconditions.checkState(symbol != null"No mapping for field '%s'"fieldIndex);
            return new QualifiedNameReference(symbol.toQualifiedName());
        }
        else {
            return rewrite(fieldOrExpression.getExpression());
        }
    }
    public void put(Expression expressionSymbol symbol)
    {
        Expression translated = translateNamesToSymbols(expression);
        .put(translatedsymbol);
        // also update the field mappings if this expression is a simple field reference
        if (expression instanceof QualifiedNameReference) {
            int fieldIndex = .getResolvedNames(expression).get(((QualifiedNameReferenceexpression).getName());
            [fieldIndex] = symbol;
        }
    }
    public void put(FieldOrExpression fieldOrExpressionSymbol symbol)
    {
        if (fieldOrExpression.isFieldReference()) {
            int fieldIndex = fieldOrExpression.getFieldIndex();
            [fieldIndex] = symbol;
            .put(new QualifiedNameReference(.getSymbol(fieldIndex).toQualifiedName()), symbol);
        }
        else {
            put(fieldOrExpression.getExpression(), symbol);
        }
    }
    public Symbol get(Expression expression)
    {
        Expression translated = translateNamesToSymbols(expression);
        Preconditions.checkArgument(.containsKey(translated), "No mapping for expression: %s"expression);
        return .get(translated);
    }
    public Symbol get(FieldOrExpression fieldOrExpression)
    {
        if (fieldOrExpression.isFieldReference()) {
            int field = fieldOrExpression.getFieldIndex();
            Preconditions.checkArgument([field] != null"No mapping for field: %s"field);
            return [field];
        }
        else {
            return get(fieldOrExpression.getExpression());
        }
    }
    private Expression translateNamesToSymbols(Expression expression)
    {
        final Map<QualifiedNameIntegerresolvedNames = .getResolvedNames(expression);
        Preconditions.checkArgument(resolvedNames != null"No resolved names for expression %s"expression);
        return ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Void>()
        {
            @Override
            public Expression rewriteExpression(Expression nodeVoid contextExpressionTreeRewriter<VoidtreeRewriter)
            {
                Expression rewrittenExpression = treeRewriter.defaultRewrite(nodecontext);
                // cast expression if coercion is registered
                Type coercion = .getCoercion(node);
                if (coercion != null) {
                    rewrittenExpression = new Cast(rewrittenExpressioncoercion.getTypeSignature().toString());
                }
                return rewrittenExpression;
            }
            @Override
            public Expression rewriteQualifiedNameReference(QualifiedNameReference nodeVoid contextExpressionTreeRewriter<VoidtreeRewriter)
            {
                QualifiedName name = node.getName();
                Integer fieldIndex = resolvedNames.get(name);
                Preconditions.checkState(fieldIndex != null"No field mapping for name '%s'"name);
                Symbol symbol = .getSymbol(fieldIndex);
                Preconditions.checkState(symbol != null"No symbol mapping for name '%s' (%s)"namefieldIndex);
                Expression rewrittenExpression = new QualifiedNameReference(symbol.toQualifiedName());
                if (.isRowFieldReference(node)) {
                    QualifiedName mangledName = QualifiedName.of(mangleFieldReference(node.getName().getSuffix()));
                    rewrittenExpression = new FunctionCall(mangledName, ImmutableList.of(rewrittenExpression));
                }
                // cast expression if coercion is registered
                Type coercion = .getCoercion(node);
                if (coercion != null) {
                    rewrittenExpression = new Cast(rewrittenExpressioncoercion.getTypeSignature().toString());
                }
                return rewrittenExpression;
            }
        }, expression);
    }
New to GrepCode? Check out our FAQ X