Tabnine Logo
IfExpression
Code IndexAdd Tabnine to your IDE (free)

How to use
IfExpression
in
com.facebook.presto.sql.tree

Best Java code snippets using com.facebook.presto.sql.tree.IfExpression (Showing top 20 results out of 315)

origin: prestodb/presto

@Override
protected Boolean visitIfExpression(IfExpression node, Void context)
{
  ImmutableList.Builder<Expression> expressions = ImmutableList.<Expression>builder()
      .add(node.getCondition())
      .add(node.getTrueValue());
  if (node.getFalseValue().isPresent()) {
    expressions.add(node.getFalseValue().get());
  }
  return expressions.build().stream().allMatch(expression -> process(expression, context));
}
origin: prestodb/presto

@Override
protected Object visitIfExpression(IfExpression node, Object context)
{
  Object trueValue = processWithExceptionHandling(node.getTrueValue(), context);
  Object falseValue = processWithExceptionHandling(node.getFalseValue().orElse(null), context);
  Object condition = processWithExceptionHandling(node.getCondition(), context);
  if (condition instanceof Expression) {
    Expression falseValueExpression = (falseValue == null) ? null : toExpression(falseValue, type(node.getFalseValue().get()));
    return new IfExpression(
        toExpression(condition, type(node.getCondition())),
        toExpression(trueValue, type(node.getTrueValue())),
        falseValueExpression);
  }
  else if (Boolean.TRUE.equals(condition)) {
    return trueValue;
  }
  else {
    return falseValue;
  }
}
origin: prestodb/presto

  private static IfExpression createIfExpression(Expression left, Expression right, ComparisonExpression.Operator operator, Expression result, Type trueValueType)
  {
    return new IfExpression(
        new ComparisonExpression(operator, left, right),
        result,
        new Cast(new NullLiteral(), trueValueType.getTypeSignature().toString()));
  }
}
origin: prestodb/presto

return new IfExpression(
    getLocation(context),
    (Expression) visit(context.expression(0)),
origin: prestodb/presto

@Override
protected R visitIfExpression(IfExpression node, C context)
{
  process(node.getCondition(), context);
  process(node.getTrueValue(), context);
  if (node.getFalseValue().isPresent()) {
    process(node.getFalseValue().get(), context);
  }
  return null;
}
origin: prestodb/presto

@Test
public void testIf()
{
  assertExpression("if(true, 1, 0)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("1"), new LongLiteral("0")));
  assertExpression("if(true, 3, null)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("3"), new NullLiteral()));
  assertExpression("if(false, null, 4)", new IfExpression(new BooleanLiteral("false"), new NullLiteral(), new LongLiteral("4")));
  assertExpression("if(false, null, null)", new IfExpression(new BooleanLiteral("false"), new NullLiteral(), new NullLiteral()));
  assertExpression("if(true, 3)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("3"), null));
  assertInvalidExpression("IF(true)", "Invalid number of arguments for 'if' function");
  assertInvalidExpression("IF(true, 1, 0) FILTER (WHERE true)", "FILTER not valid for 'if' function");
  assertInvalidExpression("IF(true, 1, 0) OVER()", "OVER clause not valid for 'if' function");
}
origin: prestodb/presto

@Override
protected Type visitIfExpression(IfExpression node, StackableAstVisitorContext<Context> context)
{
  coerceType(context, node.getCondition(), BOOLEAN, "IF condition");
  Type type;
  if (node.getFalseValue().isPresent()) {
    type = coerceToSingleType(context, node, "Result types for IF must be the same: %s vs %s", node.getTrueValue(), node.getFalseValue().get());
  }
  else {
    type = process(node.getTrueValue(), context);
  }
  return setExpressionType(node, type);
}
origin: prestodb/presto

@Test
public void testExpressionsThatMayReturnNullOnNonNullInput()
{
  List<Expression> candidates = ImmutableList.of(
      new Cast(nameReference("b"), "BIGINT", true), // try_cast
      new FunctionCall(QualifiedName.of("try"), ImmutableList.of(nameReference("b"))),
      new NullIfExpression(nameReference("b"), number(1)),
      new IfExpression(nameReference("b"), number(1), new NullLiteral()),
      new DereferenceExpression(nameReference("b"), identifier("x")),
      new InPredicate(nameReference("b"), new InListExpression(ImmutableList.of(new NullLiteral()))),
      new SearchedCaseExpression(ImmutableList.of(new WhenClause(new IsNotNullPredicate(nameReference("b")), new NullLiteral())), Optional.empty()),
      new SimpleCaseExpression(nameReference("b"), ImmutableList.of(new WhenClause(number(1), new NullLiteral())), Optional.empty()),
      new SubscriptExpression(new ArrayConstructor(ImmutableList.of(new NullLiteral())), nameReference("b")));
  for (Expression candidate : candidates) {
    EqualityInference.Builder builder = new EqualityInference.Builder();
    builder.extractInferenceCandidates(equals(nameReference("b"), nameReference("x")));
    builder.extractInferenceCandidates(equals(nameReference("a"), candidate));
    EqualityInference inference = builder.build();
    List<Expression> equalities = inference.generateEqualitiesPartitionedBy(matchesSymbols("b")).getScopeStraddlingEqualities();
    assertEquals(equalities.size(), 1);
    assertTrue(equalities.get(0).equals(equals(nameReference("x"), nameReference("b"))) || equalities.get(0).equals(equals(nameReference("b"), nameReference("x"))));
  }
}
origin: prestodb/presto

@Override
protected String visitIfExpression(IfExpression node, Void context)
{
  StringBuilder builder = new StringBuilder();
  builder.append("IF(")
      .append(process(node.getCondition(), context))
      .append(", ")
      .append(process(node.getTrueValue(), context));
  if (node.getFalseValue().isPresent()) {
    builder.append(", ")
        .append(process(node.getFalseValue().get(), context));
  }
  builder.append(")");
  return builder.toString();
}
origin: com.facebook.presto/presto-parser

return new IfExpression(
    getLocation(context),
    (Expression) visit(context.expression(0)),
origin: prestodb/presto

@Override
public Expression rewriteIfExpression(IfExpression node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
{
  Expression condition = treeRewriter.rewrite(node.getCondition(), context);
  Expression trueValue = treeRewriter.rewrite(node.getTrueValue(), context);
  Optional<Expression> falseValue = node.getFalseValue().map((value) -> treeRewriter.rewrite(value, context));
  return new SearchedCaseExpression(ImmutableList.of(new WhenClause(condition, trueValue)), falseValue);
}
origin: uk.co.nichesolutions.presto/presto-parser

return new IfExpression(
    getLocation(context),
    (Expression) visit(context.expression(0)),
origin: prestodb/presto

@Override
protected RowExpression visitIfExpression(IfExpression node, Void context)
{
  ImmutableList.Builder<RowExpression> arguments = ImmutableList.builder();
  arguments.add(process(node.getCondition(), context))
      .add(process(node.getTrueValue(), context));
  if (node.getFalseValue().isPresent()) {
    arguments.add(process(node.getFalseValue().get(), context));
  }
  else {
    arguments.add(constantNull(getType(node)));
  }
  return call(Signatures.ifSignature(getType(node)), getType(node), arguments.build());
}
origin: com.facebook.presto/presto-parser

@Test
public void testIf()
{
  assertExpression("if(true, 1, 0)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("1"), new LongLiteral("0")));
  assertExpression("if(true, 3, null)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("3"), new NullLiteral()));
  assertExpression("if(false, null, 4)", new IfExpression(new BooleanLiteral("false"), new NullLiteral(), new LongLiteral("4")));
  assertExpression("if(false, null, null)", new IfExpression(new BooleanLiteral("false"), new NullLiteral(), new NullLiteral()));
  assertExpression("if(true, 3)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("3"), null));
  assertInvalidExpression("IF(true)", "Invalid number of arguments for 'if' function");
  assertInvalidExpression("IF(true, 1, 0) FILTER (WHERE true)", "FILTER not valid for 'if' function");
  assertInvalidExpression("IF(true, 1, 0) OVER()", "OVER clause not valid for 'if' function");
}
origin: rakam-io/rakam

@Override
protected String visitIfExpression(IfExpression node, Void context) {
  StringBuilder builder = new StringBuilder();
  builder.append("IF(")
      .append(process(node.getCondition(), context))
      .append(", ")
      .append(process(node.getTrueValue(), context));
  if (node.getFalseValue().isPresent()) {
    builder.append(", ")
        .append(process(node.getFalseValue().get(), context));
  }
  builder.append(")");
  return builder.toString();
}
origin: uk.co.nichesolutions.presto/presto-main

@Override
protected Boolean visitIfExpression(IfExpression node, Void context)
{
  ImmutableList.Builder<Expression> expressions = ImmutableList.<Expression>builder()
      .add(node.getCondition())
      .add(node.getTrueValue());
  if (node.getFalseValue().isPresent()) {
    expressions.add(node.getFalseValue().get());
  }
  return expressions.build().stream().allMatch(expression -> process(expression, context));
}
origin: uk.co.nichesolutions.presto/presto-parser

@Override
protected R visitIfExpression(IfExpression node, C context)
{
  process(node.getCondition(), context);
  process(node.getTrueValue(), context);
  if (node.getFalseValue().isPresent()) {
    process(node.getFalseValue().get(), context);
  }
  return null;
}
origin: com.facebook.presto/presto-parser

@Override
protected R visitIfExpression(IfExpression node, C context)
{
  process(node.getCondition(), context);
  process(node.getTrueValue(), context);
  if (node.getFalseValue().isPresent()) {
    process(node.getFalseValue().get(), context);
  }
  return null;
}
origin: uk.co.nichesolutions.presto/presto-main

@Override
protected Type visitIfExpression(IfExpression node, StackableAstVisitorContext<AnalysisContext> context)
{
  coerceType(context, node.getCondition(), BOOLEAN, "IF condition");
  Type type;
  if (node.getFalseValue().isPresent()) {
    type = coerceToSingleType(context, node, "Result types for IF must be the same: %s vs %s", node.getTrueValue(), node.getFalseValue().get());
  }
  else {
    type = process(node.getTrueValue(), context);
  }
  expressionTypes.put(node, type);
  return type;
}
origin: vqtran/EchoQuery

@Override
protected String visitIfExpression(IfExpression node, Boolean unmangleNames)
{
  StringBuilder builder = new StringBuilder();
  builder.append("IF(")
      .append(process(node.getCondition(), unmangleNames))
      .append(", ")
      .append(process(node.getTrueValue(), unmangleNames));
  if (node.getFalseValue().isPresent()) {
    builder.append(", ")
        .append(process(node.getFalseValue().get(), unmangleNames));
  }
  builder.append(")");
  return builder.toString();
}
com.facebook.presto.sql.treeIfExpression

Javadoc

IF(v1,v2[,v3]): CASE WHEN v1 THEN v2 [ELSE v3] END

Most used methods

  • getCondition
  • getFalseValue
  • getTrueValue
  • <init>

Popular in Java

  • Parsing JSON documents to java classes using gson
  • getResourceAsStream (ClassLoader)
  • addToBackStack (FragmentTransaction)
  • onRequestPermissionsResult (Fragment)
  • BufferedImage (java.awt.image)
    The BufferedImage subclass describes an java.awt.Image with an accessible buffer of image data. All
  • SocketTimeoutException (java.net)
    This exception is thrown when a timeout expired on a socket read or accept operation.
  • Timer (java.util)
    Timers schedule one-shot or recurring TimerTask for execution. Prefer java.util.concurrent.Scheduled
  • Executors (java.util.concurrent)
    Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, ThreadFactory,
  • Annotation (javassist.bytecode.annotation)
    The annotation structure.An instance of this class is returned bygetAnnotations() in AnnotationsAttr
  • BoxLayout (javax.swing)
  • Github Copilot alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now