org.apache.calcite.sql.SqlSpecialOperator Java Examples

The following examples show how to use org.apache.calcite.sql.SqlSpecialOperator. 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: SqlParserUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a list of {expression, operator, expression, ...} into a tree,
 * taking operator precedence and associativity into account.
 *
 * @param list        List of operands and operators. This list is modified as
 *                    expressions are reduced.
 * @param start       Position of first operand in the list. Anything to the
 *                    left of this (besides the immediately preceding operand)
 *                    is ignored. Generally use value 1.
 * @param minPrec     Minimum precedence to consider. If the method encounters
 *                    an operator of lower precedence, it doesn't reduce any
 *                    further.
 * @param stopperKind If not {@link SqlKind#OTHER}, stop reading the list if
 *                    we encounter a token of this kind.
 * @return the root node of the tree which the list condenses into
 */
public static SqlNode toTreeEx(SqlSpecialOperator.TokenSequence list,
    int start, final int minPrec, final SqlKind stopperKind) {
  PrecedenceClimbingParser parser = list.parser(start,
      token -> {
        if (token instanceof PrecedenceClimbingParser.Op) {
          final SqlOperator op = ((ToTreeListItem) token.o).op;
          return stopperKind != SqlKind.OTHER
              && op.kind == stopperKind
              || minPrec > 0
              && op.getLeftPrec() < minPrec;
        } else {
          return false;
        }
      });
  final int beforeSize = parser.all().size();
  parser.partialParse();
  final int afterSize = parser.all().size();
  final SqlNode node = convert(parser.all().get(0));
  list.replaceSublist(start, start + beforeSize - afterSize + 1, node);
  return node;
}
 
Example #2
Source File: SqlParserUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a list of {expression, operator, expression, ...} into a tree,
 * taking operator precedence and associativity into account.
 *
 * @param list        List of operands and operators. This list is modified as
 *                    expressions are reduced.
 * @param start       Position of first operand in the list. Anything to the
 *                    left of this (besides the immediately preceding operand)
 *                    is ignored. Generally use value 1.
 * @param minPrec     Minimum precedence to consider. If the method encounters
 *                    an operator of lower precedence, it doesn't reduce any
 *                    further.
 * @param stopperKind If not {@link SqlKind#OTHER}, stop reading the list if
 *                    we encounter a token of this kind.
 * @return the root node of the tree which the list condenses into
 */
public static SqlNode toTreeEx(SqlSpecialOperator.TokenSequence list,
    int start, final int minPrec, final SqlKind stopperKind) {
  PrecedenceClimbingParser parser = list.parser(start,
      token -> {
        if (token instanceof PrecedenceClimbingParser.Op) {
          final SqlOperator op = ((ToTreeListItem) token.o).op;
          return stopperKind != SqlKind.OTHER
              && op.kind == stopperKind
              || minPrec > 0
              && op.getLeftPrec() < minPrec;
        } else {
          return false;
        }
      });
  final int beforeSize = parser.all().size();
  parser.partialParse();
  final int afterSize = parser.all().size();
  final SqlNode node = convert(parser.all().get(0));
  list.replaceSublist(start, start + beforeSize - afterSize + 1, node);
  return node;
}
 
Example #3
Source File: RelToSqlConverterUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Returns a {@link SqlSpecialOperator} with given operator name, mainly used for
 * unparse override. */
public static SqlSpecialOperator specialOperatorByName(String opName) {
  return new SqlSpecialOperator(opName, SqlKind.OTHER_FUNCTION) {
    public void unparse(
        SqlWriter writer,
        SqlCall call,
        int leftPrec,
        int rightPrec) {
      writer.print(getName());
      final SqlWriter.Frame frame =
          writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")");
      for (SqlNode operand : call.getOperandList()) {
        writer.sep(",");
        operand.unparse(writer, 0, 0);
      }
      writer.endList(frame);
    }
  };
}
 
Example #4
Source File: DocumentationTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private void addOperators(Map<String, PatternOp> map, String prefix,
    List<SqlOperator> operatorList) {
  for (SqlOperator op : operatorList) {
    final String name = op.getName().equals("TRANSLATE3") ? "TRANSLATE"
        : op.getName();
    if (op instanceof SqlSpecialOperator
        || !name.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) {
      continue;
    }
    final String regex;
    if (op instanceof SqlOverlapsOperator) {
      regex = "[ ]*<td>period1 " + name + " period2</td>";
    } else if (op instanceof SqlFunction
        && (op.getOperandTypeChecker() == null
            || op.getOperandTypeChecker().getOperandCountRange().getMin()
                != 0)) {
      regex = prefix + "\\| .*" + name + "\\(.*";
    } else {
      regex = prefix + "\\| .*" + name + ".*";
    }
    map.put(regex, new PatternOp(Pattern.compile(regex), name));
  }
}
 
Example #5
Source File: RexProgramTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testIsDeterministic() {
  SqlOperator ndc = new SqlSpecialOperator(
          "NDC",
          SqlKind.OTHER_FUNCTION,
          0,
          false,
          ReturnTypes.BOOLEAN,
          null, null) {
    @Override public boolean isDeterministic() {
      return false;
    }
  };
  RexNode n = rexBuilder.makeCall(ndc);
  assertFalse(RexUtil.isDeterministic(n));
  assertEquals(0,
          RexUtil.retainDeterministic(RelOptUtil.conjunctions(n)).size());
}
 
Example #6
Source File: RexProgramTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testSimplifyNonDeterministicFunction() {
  final SqlOperator ndc = new SqlSpecialOperator(
      "NDC",
      SqlKind.OTHER_FUNCTION,
      0,
      false,
      ReturnTypes.BOOLEAN,
      null, null) {
    @Override public boolean isDeterministic() {
      return false;
    }
  };
  final RexNode call1 = rexBuilder.makeCall(ndc);
  final RexNode call2 = rexBuilder.makeCall(ndc);
  final RexNode expr = eq(call1, call2);
  checkSimplifyUnchanged(expr);
}
 
Example #7
Source File: SqlParserUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public PrecedenceClimbingParser parser(int start,
    Predicate<PrecedenceClimbingParser.Token> predicate) {
  final PrecedenceClimbingParser.Builder builder =
      new PrecedenceClimbingParser.Builder();
  for (Object o : Util.skip(list, start)) {
    if (o instanceof ToTreeListItem) {
      final ToTreeListItem item = (ToTreeListItem) o;
      final SqlOperator op = item.getOperator();
      if (op instanceof SqlPrefixOperator) {
        builder.prefix(item, op.getLeftPrec());
      } else if (op instanceof SqlPostfixOperator) {
        builder.postfix(item, op.getRightPrec());
      } else if (op instanceof SqlBinaryOperator) {
        builder.infix(item, op.getLeftPrec(),
            op.getLeftPrec() < op.getRightPrec());
      } else if (op instanceof SqlSpecialOperator) {
        builder.special(item, op.getLeftPrec(), op.getRightPrec(),
            (parser, op2) -> {
              final List<PrecedenceClimbingParser.Token> tokens =
                  parser.all();
              final SqlSpecialOperator op1 =
                  (SqlSpecialOperator) ((ToTreeListItem) op2.o).op;
              SqlSpecialOperator.ReduceResult r =
                  op1.reduceExpr(tokens.indexOf(op2),
                      new TokenSequenceImpl(parser));
              return new PrecedenceClimbingParser.Result(
                  tokens.get(r.startOrdinal),
                  tokens.get(r.endOrdinal - 1),
                  parser.atom(r.node));
            });
      } else {
        throw new AssertionError();
      }
    } else {
      builder.atom(o);
    }
  }
  return builder.build();
}
 
Example #8
Source File: ExpressionGenerator.java    From streamline with Apache License 2.0 5 votes vote down vote up
@Override
public Expression visit(SqlCall call) {
    SqlOperator sqlOperator = call.getOperator();
    if (sqlOperator instanceof SqlBinaryOperator) {
        return visitBinaryOperator((SqlBinaryOperator) sqlOperator, call.getOperandList().get(0),
                call.getOperandList().get(1));
    } else if (sqlOperator instanceof SqlSpecialOperator) {
        return visitSqlSpecialOperator((SqlSpecialOperator) sqlOperator, call.getOperandList());
    } else if (sqlOperator instanceof SqlFunction) {
        SqlFunction sqlFunction = (SqlFunction) sqlOperator;
        if (sqlFunction instanceof SqlAggFunction) {
            return visitAggregateFunction(sqlFunction.getName(), call.getOperandList());
        } else if (sqlFunction instanceof SqlUnresolvedFunction) {
            String udfName = sqlFunction.getName().toUpperCase();
            if (catalogUdfs.containsKey(udfName)) {
                Udf udfInfo = catalogUdfs.get(udfName);
                if (udfInfo.isAggregate()) {
                    return visitUserDefinedAggregateFunction(udfInfo, call.getOperandList());
                } else {
                    return visitUserDefinedFunction(udfInfo, call.getOperandList());
                }
            } else {
                throw new UnsupportedOperationException("Unknown built-in or User defined function '" + udfName + "'");
            }
        } else {
            return visitFunction(sqlFunction.getName(), call.getOperandList());
        }
    } else {
        throw new UnsupportedOperationException("Operator " + sqlOperator.getName() + " is not supported");
    }
}
 
Example #9
Source File: SqlShowViews.java    From quark with Apache License 2.0 5 votes vote down vote up
public SqlShowViews(
    SqlParserPos pos,
    SqlNode likePattern) {
  super(pos, likePattern);
  this.likePattern = likePattern;
  operator = new SqlSpecialOperator("SHOW_VIEW", SqlKind.OTHER_DDL);
  operatorString = "SHOW VIEW";
}
 
Example #10
Source File: SqlAlterQuarkDataSource.java    From quark with Apache License 2.0 5 votes vote down vote up
public SqlAlterQuarkDataSource(SqlParserPos pos,
                     SqlNodeList targetColumnList,
                     SqlNodeList sourceExpressionList,
                     SqlIdentifier identifier) {
  super(pos, targetColumnList, sourceExpressionList, identifier);
  operator = new SqlSpecialOperator("ALTER_DATASOURCE", SqlKind.OTHER_DDL);
  operatorString = "ALTER DATASOURCE";
}
 
Example #11
Source File: SqlAlterQuarkView.java    From quark with Apache License 2.0 5 votes vote down vote up
public SqlAlterQuarkView(SqlParserPos pos,
                     SqlNodeList targetColumnList,
                     SqlNodeList sourceExpressionList,
                     SqlIdentifier identifier) {
  super(pos, targetColumnList, sourceExpressionList, identifier);
  operator = new SqlSpecialOperator("ALTER_VIEW", SqlKind.OTHER_DDL);
  operatorString = "ALTER VIEW";
}
 
Example #12
Source File: SqlCreateQuarkDataSource.java    From quark with Apache License 2.0 5 votes vote down vote up
public SqlCreateQuarkDataSource(SqlParserPos pos,
                     SqlNodeList targetColumnList,
                     SqlNodeList sourceExpressionList,
                     SqlIdentifier identifier) {
  super(pos, targetColumnList, sourceExpressionList, identifier);
  operator = new SqlSpecialOperator("CREATE_DATASOURCE", SqlKind.OTHER_DDL);
  operatorString = "CREATE DATASOURCE";
}
 
Example #13
Source File: SqlParserUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public PrecedenceClimbingParser parser(int start,
    Predicate<PrecedenceClimbingParser.Token> predicate) {
  final PrecedenceClimbingParser.Builder builder =
      new PrecedenceClimbingParser.Builder();
  for (Object o : Util.skip(list, start)) {
    if (o instanceof ToTreeListItem) {
      final ToTreeListItem item = (ToTreeListItem) o;
      final SqlOperator op = item.getOperator();
      if (op instanceof SqlPrefixOperator) {
        builder.prefix(item, op.getLeftPrec());
      } else if (op instanceof SqlPostfixOperator) {
        builder.postfix(item, op.getRightPrec());
      } else if (op instanceof SqlBinaryOperator) {
        builder.infix(item, op.getLeftPrec(),
            op.getLeftPrec() < op.getRightPrec());
      } else if (op instanceof SqlSpecialOperator) {
        builder.special(item, op.getLeftPrec(), op.getRightPrec(),
            (parser, op2) -> {
              final List<PrecedenceClimbingParser.Token> tokens =
                  parser.all();
              final SqlSpecialOperator op1 =
                  (SqlSpecialOperator) ((ToTreeListItem) op2.o).op;
              SqlSpecialOperator.ReduceResult r =
                  op1.reduceExpr(tokens.indexOf(op2),
                      new TokenSequenceImpl(parser));
              return new PrecedenceClimbingParser.Result(
                  tokens.get(r.startOrdinal),
                  tokens.get(r.endOrdinal - 1),
                  parser.atom(r.node));
            });
      } else {
        throw new AssertionError();
      }
    } else {
      builder.atom(o);
    }
  }
  return builder.build();
}
 
Example #14
Source File: SqlDropQuarkView.java    From quark with Apache License 2.0 4 votes vote down vote up
public SqlDropQuarkView(
    SqlParserPos pos,
    SqlIdentifier identifier) {
  super(pos, identifier);
  operator = new SqlSpecialOperator("DROP_VIEW", SqlKind.OTHER_DDL);
}
 
Example #15
Source File: SqlDropQuarkDataSource.java    From quark with Apache License 2.0 4 votes vote down vote up
public SqlDropQuarkDataSource(
    SqlParserPos pos,
    SqlIdentifier identifier) {
  super(pos, identifier);
  operator = new SqlSpecialOperator("DROP_DATASOURCE", SqlKind.OTHER_DDL);
}
 
Example #16
Source File: SqlShowQuark.java    From quark with Apache License 2.0 4 votes vote down vote up
public SqlShowQuark(SqlParserPos pos, SqlNode likePattern) {
  super(pos);
  operator = new SqlSpecialOperator("SHOW_DATASOURCE", SqlKind.OTHER_DDL);
  this.likePattern = likePattern;
}