org.apache.calcite.sql.SqlPostfixOperator Java Examples

The following examples show how to use org.apache.calcite.sql.SqlPostfixOperator. 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 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 #2
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 #3
Source File: RexNodeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
private RexWindowBound createBound(Expression bound, SqlKind sqlKind) {
	if (bound instanceof CallExpression) {
		CallExpression callExpr = (CallExpression) bound;
		FunctionDefinition func = callExpr.getFunctionDefinition();
		if (BuiltInFunctionDefinitions.UNBOUNDED_ROW.equals(func) || BuiltInFunctionDefinitions.UNBOUNDED_RANGE
				.equals(func)) {
			SqlNode unbounded = sqlKind.equals(SqlKind.PRECEDING) ? SqlWindow
					.createUnboundedPreceding(SqlParserPos.ZERO) :
					SqlWindow.createUnboundedFollowing(SqlParserPos.ZERO);
			return RexWindowBound.create(unbounded, null);
		} else if (BuiltInFunctionDefinitions.CURRENT_ROW.equals(func) || BuiltInFunctionDefinitions.CURRENT_RANGE
				.equals(func)) {
			SqlNode currentRow = SqlWindow.createCurrentRow(SqlParserPos.ZERO);
			return RexWindowBound.create(currentRow, null);
		} else {
			throw new IllegalArgumentException("Unexpected expression: " + bound);
		}
	} else if (bound instanceof ValueLiteralExpression) {
		RelDataType returnType = typeFactory
				.createFieldTypeFromLogicalType(new DecimalType(true, 19, 0));
		SqlOperator sqlOperator = new SqlPostfixOperator(
				sqlKind.name(),
				sqlKind,
				2,
				new OrdinalReturnTypeInference(0),
				null,
				null);
		SqlNode[] operands = new SqlNode[] { SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO) };
		SqlNode node = new SqlBasicCall(sqlOperator, operands, SqlParserPos.ZERO);

		ValueLiteralExpression literalExpr = (ValueLiteralExpression) bound;
		RexNode literalRexNode = literalExpr.getValueAs(Double.class).map(
				v -> relBuilder.literal(BigDecimal.valueOf((Double) v))).orElse(
				relBuilder.literal(extractValue(literalExpr, Object.class)));

		List<RexNode> expressions = new ArrayList<>();
		expressions.add(literalRexNode);
		RexNode rexNode = relBuilder.getRexBuilder().makeCall(returnType, sqlOperator, expressions);
		return RexWindowBound.create(node, rexNode);
	} else {
		throw new TableException("Unexpected expression: " + bound);
	}
}
 
Example #4
Source File: OverConvertRule.java    From flink with Apache License 2.0 4 votes vote down vote up
private RexWindowBound createBound(ConvertContext context, Expression bound, SqlKind sqlKind) {
	if (bound instanceof CallExpression) {
		CallExpression callExpr = (CallExpression) bound;
		FunctionDefinition func = callExpr.getFunctionDefinition();
		if (BuiltInFunctionDefinitions.UNBOUNDED_ROW.equals(func) || BuiltInFunctionDefinitions.UNBOUNDED_RANGE
			.equals(func)) {
			SqlNode unbounded = sqlKind.equals(SqlKind.PRECEDING) ? SqlWindow
				.createUnboundedPreceding(SqlParserPos.ZERO) :
				SqlWindow.createUnboundedFollowing(SqlParserPos.ZERO);
			return RexWindowBound.create(unbounded, null);
		} else if (BuiltInFunctionDefinitions.CURRENT_ROW.equals(func) || BuiltInFunctionDefinitions.CURRENT_RANGE
			.equals(func)) {
			SqlNode currentRow = SqlWindow.createCurrentRow(SqlParserPos.ZERO);
			return RexWindowBound.create(currentRow, null);
		} else {
			throw new IllegalArgumentException("Unexpected expression: " + bound);
		}
	} else if (bound instanceof ValueLiteralExpression) {
		RelDataType returnType = context.getTypeFactory()
			.createFieldTypeFromLogicalType(new DecimalType(true, 19, 0));
		SqlOperator sqlOperator = new SqlPostfixOperator(
			sqlKind.name(),
			sqlKind,
			2,
			new OrdinalReturnTypeInference(0),
			null,
			null);
		SqlNode[] operands = new SqlNode[] { SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO) };
		SqlNode node = new SqlBasicCall(sqlOperator, operands, SqlParserPos.ZERO);

		ValueLiteralExpression literalExpr = (ValueLiteralExpression) bound;
		RexNode literalRexNode = literalExpr.getValueAs(BigDecimal.class)
			.map(v -> context.getRelBuilder().literal(v))
			.orElse(context.getRelBuilder().literal(extractValue(literalExpr, Object.class)));

		List<RexNode> expressions = new ArrayList<>();
		expressions.add(literalRexNode);
		RexNode rexNode = context.getRelBuilder().getRexBuilder().makeCall(
			returnType, sqlOperator, expressions);
		return RexWindowBound.create(node, rexNode);
	} else {
		throw new TableException("Unexpected expression: " + bound);
	}
}