Java Code Examples for org.apache.calcite.util.Util#swallow()

The following examples show how to use org.apache.calcite.util.Util#swallow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RexUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any {link RexTableInputRef} nodes.
 *
 * @param node a RexNode tree
 * @return first such node found or null if it there is no such node
 */
public static RexTableInputRef containsTableInputRef(RexNode node) {
  try {
    RexVisitor<Void> visitor =
        new RexVisitorImpl<Void>(true) {
          public Void visitTableInputRef(RexTableInputRef inputRef) {
            throw new Util.FoundOne(inputRef);
          }
        };
    node.accept(visitor);
    return null;
  } catch (Util.FoundOne e) {
    Util.swallow(e, null);
    return (RexTableInputRef) e.getNode();
  }
}
 
Example 2
Source File: RexUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any {link RexInputRef} nodes.
 *
 * @param node a RexNode tree
 */
public static boolean containsInputRef(
    RexNode node) {
  try {
    RexVisitor<Void> visitor =
        new RexVisitorImpl<Void>(true) {
          public Void visitInputRef(RexInputRef inputRef) {
            throw new Util.FoundOne(inputRef);
          }
        };
    node.accept(visitor);
    return false;
  } catch (Util.FoundOne e) {
    Util.swallow(e, null);
    return true;
  }
}
 
Example 3
Source File: RexUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any {link RexTableInputRef} nodes.
 *
 * @param node a RexNode tree
 * @return first such node found or null if it there is no such node
 */
public static RexTableInputRef containsTableInputRef(RexNode node) {
    try {
        RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
            @Override
            public Void visitTableInputRef(RexTableInputRef inputRef) {
                throw new Util.FoundOne(inputRef);
            }
        };
        node.accept(visitor);
        return null;
    } catch (Util.FoundOne e) {
        Util.swallow(e, null);
        return (RexTableInputRef) e.getNode();
    }
}
 
Example 4
Source File: DrillRelMdSelectivity.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static RexInputRef findRexInputRef(final RexNode node) {
  try {
    RexVisitor<Void> visitor =
        new RexVisitorImpl<Void>(true) {
          public Void visitCall(RexCall call) {
            for (RexNode child : call.getOperands()) {
              child.accept(this);
            }
            return super.visitCall(call);
          }

          public Void visitInputRef(RexInputRef inputRef) {
            throw new Util.FoundOne(inputRef);
          }
        };
    node.accept(visitor);
    return null;
  } catch (Util.FoundOne e) {
    Util.swallow(e, null);
    return (RexInputRef) e.getNode();
  }
}
 
Example 5
Source File: RexUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any
 * {@link org.apache.calcite.rex.RexFieldAccess} nodes.
 *
 * @param node a RexNode tree
 */
public static boolean containsFieldAccess(RexNode node) {
    try {
        RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
            @Override
            public Void visitFieldAccess(RexFieldAccess fieldAccess) {
                throw new Util.FoundOne(fieldAccess);
            }
        };
        node.accept(visitor);
        return false;
    } catch (Util.FoundOne e) {
        Util.swallow(e, null);
        return true;
    }
}
 
Example 6
Source File: AbstractMaterializedViewRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces all the input references by the position in the
 * input column set. If a reference index cannot be found in
 * the input set, then we return null.
 */
private static RexNode shuttleReferences(final RexBuilder rexBuilder, final RexNode node, final Mapping mapping) {
    try {
        RexShuttle visitor = new RexShuttle() {
            @Override
            public RexNode visitInputRef(RexInputRef inputRef) {
                int pos = mapping.getTargetOpt(inputRef.getIndex());
                if (pos != -1) {
                    // Found it
                    return rexBuilder.makeInputRef(inputRef.getType(), pos);
                }
                throw Util.FoundOne.NULL;
            }
        };
        return visitor.apply(node);
    } catch (Util.FoundOne ex) {
        Util.swallow(ex, null);
        return null;
    }
}
 
Example 7
Source File: DrillRelOptUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Find whether the given project rel has unknown output schema. This would happen if the
 * project has CONVERT_FROMJSON which can only derive the schema after evaluation is performed
 * @param project : The project rel
 * @return : Return true if the project output schema is unknown. Otherwise, false.
 */
public static boolean isProjectOutputSchemaUnknown(Project project) {
    try {
        RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
            @Override
            public Void visitCall(RexCall call) {
                if ("convert_fromjson".equals(call.getOperator().getName().toLowerCase())) {
                    throw new Util.FoundOne(call); /* throw exception to interrupt tree walk (this is similar to
                                                   other utility methods in RexUtil.java */
                }
                return super.visitCall(call);
            }
        };
        for (RexNode rex : project.getProjects()) {
            rex.accept(visitor);
        }
    } catch (Util.FoundOne e) {
        Util.swallow(e, null);
        return true;
    }
    return false;
}
 
Example 8
Source File: RexUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any {link RexInputRef} nodes.
 *
 * @param node a RexNode tree
 */
public static boolean containsInputRef(RexNode node) {
    try {
        RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
            @Override
            public Void visitInputRef(RexInputRef inputRef) {
                throw new Util.FoundOne(inputRef);
            }
        };
        node.accept(visitor);
        return false;
    } catch (Util.FoundOne e) {
        Util.swallow(e, null);
        return true;
    }
}
 
Example 9
Source File: AggFinder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SqlCall findAgg(List<SqlNode> nodes) {
  try {
    for (SqlNode node : nodes) {
      node.accept(this);
    }
    return null;
  } catch (Util.FoundOne e) {
    Util.swallow(e, null);
    return (SqlCall) e.getNode();
  }
}
 
Example 10
Source File: RexOver.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether a program contains an OVER clause.
 */
public static boolean containsOver(RexProgram program) {
    try {
        RexUtil.apply(FINDER, program.getExprList(), null);
        return false;
    } catch (OverFound e) {
        Util.swallow(e, null);
        return true;
    }
}
 
Example 11
Source File: RexOver.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether an expression contains an OVER clause.
 */
public static boolean containsOver(RexNode expr) {
    try {
        expr.accept(FINDER);
        return false;
    } catch (OverFound e) {
        Util.swallow(e, null);
        return true;
    }
}
 
Example 12
Source File: SqlAdvisorValidator.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the identifier and its scope into a map keyed by ParserPosition.
 */
public void validateIdentifier(SqlIdentifier id, SqlValidatorScope scope) {
  registerId(id, scope);
  try {
    super.validateIdentifier(id, scope);
  } catch (CalciteException e) {
    Util.swallow(e, TRACER);
  }
}
 
Example 13
Source File: RexUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether an array of expressions has any common sub-expressions.
 */
public static boolean containNoCommonExprs(List<RexNode> exprs, Litmus litmus) {
    final ExpressionNormalizer visitor = new ExpressionNormalizer(false);
    for (RexNode expr : exprs) {
        try {
            expr.accept(visitor);
        } catch (ExpressionNormalizer.SubExprExistsException e) {
            Util.swallow(e, null);
            return litmus.fail(null);
        }
    }
    return litmus.succeed();
}
 
Example 14
Source File: RexOver.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether an expression contains an OVER clause.
 */
public static boolean containsOver(RexNode expr) {
  try {
    expr.accept(FINDER);
    return false;
  } catch (OverFound e) {
    Util.swallow(e, null);
    return true;
  }
}
 
Example 15
Source File: RexUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether an array of expressions has any common sub-expressions.
 */
public static boolean containNoCommonExprs(List<RexNode> exprs,
    Litmus litmus) {
  final ExpressionNormalizer visitor = new ExpressionNormalizer(false);
  for (RexNode expr : exprs) {
    try {
      expr.accept(visitor);
    } catch (ExpressionNormalizer.SubExprExistsException e) {
      Util.swallow(e, null);
      return litmus.fail(null);
    }
  }
  return litmus.succeed();
}
 
Example 16
Source File: SqlAdvisorValidator.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Calls the parent class method and masks Farrago exception thrown.
 */
protected void validateHavingClause(SqlSelect select) {
  try {
    super.validateHavingClause(select);
  } catch (CalciteException e) {
    Util.swallow(e, TRACER);
  }
}
 
Example 17
Source File: RexUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RexNode toCnf(RexNode rex) {
  try {
    this.currentCount = 0;
    return toCnf2(rex);
  } catch (OverflowError e) {
    Util.swallow(e, null);
    return rex;
  }
}
 
Example 18
Source File: SqlAdvisorValidator.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected void validateFrom(
    SqlNode node,
    RelDataType targetRowType,
    SqlValidatorScope scope) {
  try {
    super.validateFrom(node, targetRowType, scope);
  } catch (CalciteException e) {
    Util.swallow(e, TRACER);
  }
}
 
Example 19
Source File: CalcRelSplitter.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether this <code>RelType</code> can implement a given
 * expression.
 *
 * @param expr      Expression
 * @param condition Whether expression is a condition
 * @return Whether this <code>RelType</code> can implement a given
 * expression.
 */
public boolean canImplement(RexNode expr, boolean condition) {
    if (condition && !supportsCondition()) {
        return false;
    }
    try {
        expr.accept(new ImplementTester(this));
        return true;
    } catch (CannotImplement e) {
        Util.swallow(e, null);
        return false;
    }
}
 
Example 20
Source File: SqlAdvisor.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Gets completion hints for a syntactically correct sql statement with dummy
 * SqlIdentifier
 *
 * @param sql A syntactically correct sql statement for which to retrieve
 *            completion hints
 * @param pos to indicate the line and column position in the query at which
 *            completion hints need to be retrieved. For example, "select
 *            a.ename, b.deptno from sales.emp a join sales.dept b "on
 *            a.deptno=b.deptno where empno=1"; setting pos to 'Line 1, Column
 *            17' returns all the possible column names that can be selected
 *            from sales.dept table setting pos to 'Line 1, Column 31' returns
 *            all the possible table names in 'sales' schema
 * @return an array of hints ({@link SqlMoniker}) that can fill in at the
 * indicated position
 */
public List<SqlMoniker> getCompletionHints(String sql, SqlParserPos pos) {
  // First try the statement they gave us. If this fails, just return
  // the tokens which were expected at the failure point.
  List<SqlMoniker> hintList = new ArrayList<>();
  SqlNode sqlNode = tryParse(sql, hintList);
  if (sqlNode == null) {
    return hintList;
  }

  // Now construct a statement which is bound to fail. (Character 7 BEL
  // is not legal in any SQL statement.)
  final int x = pos.getColumnNum() - 1;
  sql = sql.substring(0, x)
      + " \07"
      + sql.substring(x);
  tryParse(sql, hintList);

  final SqlMoniker star =
      new SqlMonikerImpl(ImmutableList.of("*"), SqlMonikerType.KEYWORD);
  String hintToken =
      parserConfig.unquotedCasing() == Casing.TO_UPPER ? UPPER_HINT_TOKEN : HINT_TOKEN;
  if (hintList.contains(star) && !isSelectListItem(sqlNode, pos, hintToken)) {
    hintList.remove(star);
  }

  // Add the identifiers which are expected at the point of interest.
  try {
    validator.validate(sqlNode);
  } catch (Exception e) {
    // mask any exception that is thrown during the validation, i.e.
    // try to continue even if the sql is invalid. we are doing a best
    // effort here to try to come up with the requested completion
    // hints
    Util.swallow(e, LOGGER);
  }
  final List<SqlMoniker> validatorHints =
      validator.lookupHints(sqlNode, pos);
  hintList.addAll(validatorHints);
  return hintList;
}