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 static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.BooleanType.BOOLEAN;
 import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
 import static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 import static com.facebook.presto.sql.analyzer.SemanticErrorCode.TYPE_MISMATCH;
 import static com.facebook.presto.type.UnknownType.UNKNOWN;
 import static com.facebook.presto.util.DateTimeUtils.parseDayTimeInterval;
 import static com.facebook.presto.util.DateTimeUtils.parseTime;
 import static com.facebook.presto.util.DateTimeUtils.parseTimestamp;
 import static com.facebook.presto.util.DateTimeUtils.parseYearMonthInterval;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static io.airlift.slice.Slices.utf8Slice;
 import static java.nio.charset.StandardCharsets.UTF_8;
 
 public final class LiteralInterpreter
 {
     private LiteralInterpreter() {}
 
     public static Object evaluate(Metadata metadataConnectorSession sessionExpression node)
     {
         if (!(node instanceof Literal)) {
             throw new IllegalArgumentException("node must be a Literal");
         }
         return new LiteralVisitor(metadata).process(nodesession);
     }
 
     public static List<ExpressiontoExpressions(List<?> objectsList<? extends Typetypes)
     {
         checkNotNull(objects"objects is null");
         checkNotNull(types"types is null");
         checkArgument(objects.size() == types.size(), "objects and types do not have the same size");
 
         ImmutableList.Builder<Expressionexpressions = ImmutableList.builder();
         for (int i = 0; i < objects.size(); i++) {
             Object object = objects.get(i);
             Type type = types.get(i);
             expressions.add(toExpression(objecttype));
         }
         return expressions.build();
     }
 
     public static Expression toExpression(Object objectType type)
     {
         if (object instanceof Expression) {
             return (Expressionobject;
         }
 
         if (object == null) {
             if (type.equals()) {
                 return new NullLiteral();
             }
             return new Cast(new NullLiteral(), type.getTypeSignature().toString());
        }
        if (type.equals()) {
            return new LongLiteral(object.toString());
        }
        if (type.equals()) {
            Double value = (Doubleobject;
            // WARNING: the ORC predicate code depends on NaN and infinity not appearing in a tuple domain, so
            // if you remove this, you will need to update the TupleDomainOrcPredicate
            if (value.isNaN()) {
                return new FunctionCall(new QualifiedName("nan"), ImmutableList.<Expression>of());
            }
            else if (value.equals(.)) {
                return ArithmeticUnaryExpression.negative(new FunctionCall(new QualifiedName("infinity"), ImmutableList.<Expression>of()));
            }
            else if (value.equals(.)) {
                return new FunctionCall(new QualifiedName("infinity"), ImmutableList.<Expression>of());
            }
            else {
                return new DoubleLiteral(object.toString());
            }
        }
        if (type.equals()) {
            if (object instanceof Slice) {
                return new StringLiteral(((Sliceobject).toString());
            }
            if (object instanceof String) {
                return new StringLiteral((Stringobject);
            }
        }
        if (type.equals()) {
            return new BooleanLiteral(object.toString());
        }
        if (object instanceof Slice && !type.equals()) {
            // HACK: we need to serialize VARBINARY in a format that can be embedded in an expression to be
            // able to encode it in the plan that gets sent to workers.
            // We do this by transforming the in-memory varbinary into a call to from_base64(<base64-encoded value>)
            FunctionCall fromBase64 = new FunctionCall(new QualifiedName("from_base64"), ImmutableList.of(new StringLiteral(VarbinaryFunctions.toBase64((Sliceobject).toStringUtf8())));
            Signature signature = FunctionRegistry.getMagicLiteralFunctionSignature(type);
            return new FunctionCall(new QualifiedName(signature.getName()), ImmutableList.of(fromBase64));
        }
        Signature signature = FunctionRegistry.getMagicLiteralFunctionSignature(type);
        Expression rawLiteral = toExpression(object, FunctionRegistry.type(type.getJavaType()));
        return new FunctionCall(new QualifiedName(signature.getName()), ImmutableList.of(rawLiteral));
    }
    private static class LiteralVisitor
            extends AstVisitor<ObjectConnectorSession>
    {
        private final Metadata metadata;
        private LiteralVisitor(Metadata metadata)
        {
            this. = metadata;
        }
        @Override
        protected Object visitLiteral(Literal nodeConnectorSession session)
        {
            throw new UnsupportedOperationException("Unhandled literal type: " + node);
        }
        @Override
        protected Object visitBooleanLiteral(BooleanLiteral nodeConnectorSession session)
        {
            return node.getValue();
        }
        @Override
        protected Long visitLongLiteral(LongLiteral nodeConnectorSession session)
        {
            return node.getValue();
        }
        @Override
        protected Double visitDoubleLiteral(DoubleLiteral nodeConnectorSession session)
        {
            return node.getValue();
        }
        @Override
        protected Slice visitStringLiteral(StringLiteral nodeConnectorSession session)
        {
            return node.getSlice();
        }
        @Override
        protected Object visitGenericLiteral(GenericLiteral nodeConnectorSession session)
        {
            Type type = .getType(parseTypeSignature(node.getType()));
            if (type == null) {
                throw new SemanticException(node"Unknown type: " + node.getType());
            }
            FunctionInfo operator;
            try {
                operator = .getFunctionRegistry().getCoercion(type);
            }
            catch (IllegalArgumentException e) {
                throw new SemanticException(node"No literal form for type %s"type);
            }
            try {
                return ExpressionInterpreter.invoke(sessionoperator.getMethodHandle(), ImmutableList.<Object>of(utf8Slice(node.getValue())));
            }
            catch (Throwable throwable) {
                throw Throwables.propagate(throwable);
            }
        }
        @Override
        protected Long visitTimeLiteral(TimeLiteral nodeConnectorSession session)
        {
            return parseTime(session.getTimeZoneKey(), node.getValue());
        }
        @Override
        protected Long visitTimestampLiteral(TimestampLiteral nodeConnectorSession session)
        {
            return parseTimestamp(session.getTimeZoneKey(), node.getValue());
        }
        @Override
        protected Long visitIntervalLiteral(IntervalLiteral nodeConnectorSession session)
        {
            if (node.isYearToMonth()) {
                return node.getSign().multiplier() * parseYearMonthInterval(node.getValue(), node.getStartField(), node.getEndField());
            }
            else {
                return node.getSign().multiplier() * parseDayTimeInterval(node.getValue(), node.getStartField(), node.getEndField());
            }
        }
        @Override
        protected Object visitNullLiteral(NullLiteral nodeConnectorSession session)
        {
            return null;
        }
    }
New to GrepCode? Check out our FAQ X