Java Code Examples for org.apache.calcite.sql.SqlCall#operandCount()

The following examples show how to use org.apache.calcite.sql.SqlCall#operandCount() . 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: OracleSqlDialect.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public void unparseCall(SqlWriter writer, SqlCall call,
    int leftPrec, int rightPrec) {
  if (call.getOperator() == SqlStdOperatorTable.SUBSTRING) {
    SqlUtil.unparseFunctionSyntax(OracleSqlOperatorTable.SUBSTR, writer, call);
  } else {
    switch (call.getKind()) {
    case FLOOR:
      if (call.operandCount() != 2) {
        super.unparseCall(writer, call, leftPrec, rightPrec);
        return;
      }

      final SqlLiteral timeUnitNode = call.operand(1);
      final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class);

      SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(),
          timeUnitNode.getParserPosition());
      SqlFloorFunction.unparseDatetimeFunction(writer, call2, "TRUNC", true);
      break;

    default:
      super.unparseCall(writer, call, leftPrec, rightPrec);
    }
  }
}
 
Example 2
Source File: PostgresqlSqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void unparseCall(SqlWriter writer, SqlCall call,
    int leftPrec, int rightPrec) {
  switch (call.getKind()) {
  case FLOOR:
    if (call.operandCount() != 2) {
      super.unparseCall(writer, call, leftPrec, rightPrec);
      return;
    }

    final SqlLiteral timeUnitNode = call.operand(1);
    final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class);

    SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(),
        timeUnitNode.getParserPosition());
    SqlFloorFunction.unparseDatetimeFunction(writer, call2, "DATE_TRUNC", false);
    break;

  default:
    super.unparseCall(writer, call, leftPrec, rightPrec);
  }
}
 
Example 3
Source File: PostgresqlSqlDialect.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public void unparseCall(SqlWriter writer, SqlCall call,
    int leftPrec, int rightPrec) {
  switch (call.getKind()) {
  case FLOOR:
    if (call.operandCount() != 2) {
      super.unparseCall(writer, call, leftPrec, rightPrec);
      return;
    }

    final SqlLiteral timeUnitNode = call.operand(1);
    final TimeUnitRange timeUnit = timeUnitNode.getValueAs(TimeUnitRange.class);

    SqlCall call2 = SqlFloorFunction.replaceTimeUnitOperand(call, timeUnit.name(),
        timeUnitNode.getParserPosition());
    SqlFloorFunction.unparseDatetimeFunction(writer, call2, "DATE_TRUNC", false);
    break;

  default:
    super.unparseCall(writer, call, leftPrec, rightPrec);
  }
}
 
Example 4
Source File: MysqlSqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void unparseCall(SqlWriter writer, SqlCall call,
    int leftPrec, int rightPrec) {
  switch (call.getKind()) {
  case FLOOR:
    if (call.operandCount() != 2) {
      super.unparseCall(writer, call, leftPrec, rightPrec);
      return;
    }

    unparseFloor(writer, call);
    break;

  default:
    super.unparseCall(writer, call, leftPrec, rightPrec);
  }
}
 
Example 5
Source File: AbstractSqlTester.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void checkIntervalConv(String sql, String expected) {
  SqlValidator validator = getValidator();
  final SqlCall n = (SqlCall) parseAndValidate(validator, sql);

  SqlNode node = null;
  for (int i = 0; i < n.operandCount(); i++) {
    node = stripAs(n.operand(i));
    if (node instanceof SqlCall) {
      node = ((SqlCall) node).operand(0);
      break;
    }
  }

  assertNotNull(node);
  SqlIntervalLiteral intervalLiteral = (SqlIntervalLiteral) node;
  SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) intervalLiteral.getValue();
  long l =
      interval.getIntervalQualifier().isYearMonth()
          ? SqlParserUtil.intervalToMonths(interval)
          : SqlParserUtil.intervalToMillis(interval);
  String actual = l + "";
  assertEquals(expected, actual);
}
 
Example 6
Source File: SqlSubstringFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void unparse(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  final SqlWriter.Frame frame = writer.startFunCall(getName());
  call.operand(0).unparse(writer, leftPrec, rightPrec);
  writer.sep("FROM");
  call.operand(1).unparse(writer, leftPrec, rightPrec);

  if (3 == call.operandCount()) {
    writer.sep("FOR");
    call.operand(2).unparse(writer, leftPrec, rightPrec);
  }

  writer.endFunCall(frame);
}
 
Example 7
Source File: MssqlSqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void unparseCall(SqlWriter writer, SqlCall call,
    int leftPrec, int rightPrec) {
  if (call.getOperator() == SqlStdOperatorTable.SUBSTRING) {
    if (call.operandCount() != 3) {
      throw new IllegalArgumentException("MSSQL SUBSTRING requires FROM and FOR arguments");
    }
    SqlUtil.unparseFunctionSyntax(MSSQL_SUBSTRING, writer, call);
  } else {
    switch (call.getKind()) {
    case FLOOR:
      if (call.operandCount() != 2) {
        super.unparseCall(writer, call, leftPrec, rightPrec);
        return;
      }
      unparseFloor(writer, call);
      break;

    default:
      super.unparseCall(writer, call, leftPrec, rightPrec);
    }
  }
}
 
Example 8
Source File: SqlFloorFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public void unparse(SqlWriter writer, SqlCall call, int leftPrec,
    int rightPrec) {
  final SqlWriter.Frame frame = writer.startFunCall(getName());
  if (call.operandCount() == 2) {
    call.operand(0).unparse(writer, 0, 100);
    writer.sep("TO");
    call.operand(1).unparse(writer, 100, 0);
  } else {
    call.operand(0).unparse(writer, 0, 0);
  }
  writer.endFunCall(frame);
}
 
Example 9
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
protected void validateUnnest(SqlCall call, SqlValidatorScope scope, RelDataType targetRowType) {
	for (int i = 0; i < call.operandCount(); i++) {
		SqlNode expandedItem = expand(call.operand(i), scope);
		call.setOperand(i, expandedItem);
	}
	validateQuery(call, scope, targetRowType);
}
 
Example 10
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
private void registerSubQueries(
	SqlValidatorScope parentScope,
	SqlNode node) {
	if (node == null) {
		return;
	}
	if (node.getKind().belongsTo(SqlKind.QUERY)
		|| node.getKind() == SqlKind.MULTISET_QUERY_CONSTRUCTOR
		|| node.getKind() == SqlKind.MULTISET_VALUE_CONSTRUCTOR) {
		registerQuery(parentScope, null, node, node, null, false);
	} else if (node instanceof SqlCall) {
		validateNodeFeature(node);
		SqlCall call = (SqlCall) node;
		for (int i = 0; i < call.operandCount(); i++) {
			registerOperandSubQueries(parentScope, call, i);
		}
	} else if (node instanceof SqlNodeList) {
		SqlNodeList list = (SqlNodeList) node;
		for (int i = 0, count = list.size(); i < count; i++) {
			SqlNode listNode = list.get(i);
			if (listNode.getKind().belongsTo(SqlKind.QUERY)) {
				listNode =
					SqlStdOperatorTable.SCALAR_QUERY.createCall(
						listNode.getParserPosition(),
						listNode);
				list.set(i, listNode);
			}
			registerSubQueries(parentScope, listNode);
		}
	} else {
		// atomic node -- can be ignored
	}
}
 
Example 11
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public void validateCall(
	SqlCall call,
	SqlValidatorScope scope) {
	final SqlOperator operator = call.getOperator();
	if ((call.operandCount() == 0)
		&& (operator.getSyntax() == SqlSyntax.FUNCTION_ID)
		&& !call.isExpanded()
		&& !conformance.allowNiladicParentheses()) {
		// For example, "LOCALTIME()" is illegal. (It should be
		// "LOCALTIME", which would have been handled as a
		// SqlIdentifier.)
		throw handleUnresolvedFunction(call, (SqlFunction) operator,
			ImmutableList.of(), null);
	}

	SqlValidatorScope operandScope = scope.getOperandScope(call);

	if (operator instanceof SqlFunction
		&& ((SqlFunction) operator).getFunctionType()
		== SqlFunctionCategory.MATCH_RECOGNIZE
		&& !(operandScope instanceof MatchRecognizeScope)) {
		throw newValidationError(call,
			Static.RESOURCE.functionMatchRecognizeOnly(call.toString()));
	}
	// Delegate validation to the operator.
	operator.validateCall(call, this, scope, operandScope);
}
 
Example 12
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void registerSubQueries(
	SqlValidatorScope parentScope,
	SqlNode node) {
	if (node == null) {
		return;
	}
	if (node.getKind().belongsTo(SqlKind.QUERY)
		|| node.getKind() == SqlKind.MULTISET_QUERY_CONSTRUCTOR
		|| node.getKind() == SqlKind.MULTISET_VALUE_CONSTRUCTOR) {
		registerQuery(parentScope, null, node, node, null, false);
	} else if (node instanceof SqlCall) {
		validateNodeFeature(node);
		SqlCall call = (SqlCall) node;
		for (int i = 0; i < call.operandCount(); i++) {
			registerOperandSubQueries(parentScope, call, i);
		}
	} else if (node instanceof SqlNodeList) {
		SqlNodeList list = (SqlNodeList) node;
		for (int i = 0, count = list.size(); i < count; i++) {
			SqlNode listNode = list.get(i);
			if (listNode.getKind().belongsTo(SqlKind.QUERY)) {
				listNode =
					SqlStdOperatorTable.SCALAR_QUERY.createCall(
						listNode.getParserPosition(),
						listNode);
				list.set(i, listNode);
			}
			registerSubQueries(parentScope, listNode);
		}
	} else {
		// atomic node -- can be ignored
	}
}
 
Example 13
Source File: SqlJsonExistsFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public void unparse(SqlWriter writer, SqlCall call, int leftPrec,
    int rightPrec) {
  final SqlWriter.Frame frame = writer.startFunCall(getName());
  call.operand(0).unparse(writer, 0, 0);
  if (call.operandCount() == 2) {
    call.operand(1).unparse(writer, 0, 0);
    writer.keyword("ON ERROR");
  }
  writer.endFunCall(frame);
}
 
Example 14
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
public void validateCall(
	SqlCall call,
	SqlValidatorScope scope) {
	final SqlOperator operator = call.getOperator();
	if ((call.operandCount() == 0)
		&& (operator.getSyntax() == SqlSyntax.FUNCTION_ID)
		&& !call.isExpanded()
		&& !conformance.allowNiladicParentheses()) {
		// For example, "LOCALTIME()" is illegal. (It should be
		// "LOCALTIME", which would have been handled as a
		// SqlIdentifier.)
		throw handleUnresolvedFunction(call, (SqlFunction) operator,
			ImmutableList.of(), null);
	}

	SqlValidatorScope operandScope = scope.getOperandScope(call);

	if (operator instanceof SqlFunction
		&& ((SqlFunction) operator).getFunctionType()
		== SqlFunctionCategory.MATCH_RECOGNIZE
		&& !(operandScope instanceof MatchRecognizeScope)) {
		throw newValidationError(call,
			Static.RESOURCE.functionMatchRecognizeOnly(call.toString()));
	}
	// Delegate validation to the operator.
	operator.validateCall(call, this, scope, operandScope);
}
 
Example 15
Source File: SqlPositionFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void unparse(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  final SqlWriter.Frame frame = writer.startFunCall(getName());
  call.operand(0).unparse(writer, leftPrec, rightPrec);
  writer.sep("IN");
  call.operand(1).unparse(writer, leftPrec, rightPrec);
  if (3 == call.operandCount()) {
    writer.sep("FROM");
    call.operand(2).unparse(writer, leftPrec, rightPrec);
  }
  writer.endFunCall(frame);
}
 
Example 16
Source File: HiveSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void unparseCall(final SqlWriter writer, final SqlCall call,
    final int leftPrec, final int rightPrec) {
  switch (call.getKind()) {
  case POSITION:
    final SqlWriter.Frame frame = writer.startFunCall("INSTR");
    writer.sep(",");
    call.operand(1).unparse(writer, leftPrec, rightPrec);
    writer.sep(",");
    call.operand(0).unparse(writer, leftPrec, rightPrec);
    if (3 == call.operandCount()) {
      throw new RuntimeException("3rd operand Not Supported for Function INSTR in Hive");
    }
    writer.endFunCall(frame);
    break;
  case MOD:
    SqlOperator op = SqlStdOperatorTable.PERCENT_REMAINDER;
    SqlSyntax.BINARY.unparse(writer, op, call, leftPrec, rightPrec);
    break;
  case TRIM:
    RelToSqlConverterUtil.unparseHiveTrim(writer, call, leftPrec, rightPrec);
    break;
  case OTHER_FUNCTION:
    if (call.getOperator() instanceof SqlSubstringFunction) {
      final SqlWriter.Frame funCallFrame = writer.startFunCall(call.getOperator().getName());
      call.operand(0).unparse(writer, leftPrec, rightPrec);
      writer.sep(",", true);
      call.operand(1).unparse(writer, leftPrec, rightPrec);
      if (3 == call.operandCount()) {
        writer.sep(",", true);
        call.operand(2).unparse(writer, leftPrec, rightPrec);
      }
      writer.endFunCall(funCallFrame);
    } else {
      super.unparseCall(writer, call, leftPrec, rightPrec);
    }
    break;
  default:
    super.unparseCall(writer, call, leftPrec, rightPrec);
  }
}
 
Example 17
Source File: StandardConvertletTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected RexNode convertFloorCeil(SqlRexContext cx, SqlCall call) {
  final boolean floor = call.getKind() == SqlKind.FLOOR;
  // Rewrite floor, ceil of interval
  if (call.operandCount() == 1
      && call.operand(0) instanceof SqlIntervalLiteral) {
    final SqlIntervalLiteral literal = call.operand(0);
    SqlIntervalLiteral.IntervalValue interval =
        (SqlIntervalLiteral.IntervalValue) literal.getValue();
    BigDecimal val =
        interval.getIntervalQualifier().getStartUnit().multiplier;
    RexNode rexInterval = cx.convertExpression(literal);

    final RexBuilder rexBuilder = cx.getRexBuilder();
    RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.valueOf(0));
    RexNode cond = ge(rexBuilder, rexInterval, zero);

    RexNode pad =
        rexBuilder.makeExactLiteral(val.subtract(BigDecimal.ONE));
    RexNode cast = rexBuilder.makeReinterpretCast(
        rexInterval.getType(), pad, rexBuilder.makeLiteral(false));
    RexNode sum = floor
        ? minus(rexBuilder, rexInterval, cast)
        : plus(rexBuilder, rexInterval, cast);

    RexNode kase = floor
        ? case_(rexBuilder, rexInterval, cond, sum)
        : case_(rexBuilder, sum, cond, rexInterval);

    RexNode factor = rexBuilder.makeExactLiteral(val);
    RexNode div = divideInt(rexBuilder, kase, factor);
    return multiply(rexBuilder, div, factor);
  }

  // normal floor, ceil function
  return convertFunction(cx, (SqlFunction) call.getOperator(), call);
}
 
Example 18
Source File: SqlFloorFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void unparse(SqlWriter writer, SqlCall call, int leftPrec,
    int rightPrec) {
  final SqlWriter.Frame frame = writer.startFunCall(getName());
  if (call.operandCount() == 2) {
    call.operand(0).unparse(writer, 0, 100);
    writer.sep("TO");
    call.operand(1).unparse(writer, 100, 0);
  } else {
    call.operand(0).unparse(writer, 0, 0);
  }
  writer.endFunCall(frame);
}
 
Example 19
Source File: BigQuerySqlDialect.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public void unparseCall(final SqlWriter writer, final SqlCall call, final int leftPrec,
    final int rightPrec) {
  switch (call.getKind()) {
  case POSITION:
    final SqlWriter.Frame frame = writer.startFunCall("STRPOS");
    writer.sep(",");
    call.operand(1).unparse(writer, leftPrec, rightPrec);
    writer.sep(",");
    call.operand(0).unparse(writer, leftPrec, rightPrec);
    if (3 == call.operandCount()) {
      throw new RuntimeException("3rd operand Not Supported for Function STRPOS in Big Query");
    }
    writer.endFunCall(frame);
    break;
  case UNION:
    if (!((SqlSetOperator) call.getOperator()).isAll()) {
      SqlSyntax.BINARY.unparse(writer, UNION_DISTINCT, call, leftPrec, rightPrec);
    }
    break;
  case EXCEPT:
    if (!((SqlSetOperator) call.getOperator()).isAll()) {
      SqlSyntax.BINARY.unparse(writer, EXCEPT_DISTINCT, call, leftPrec, rightPrec);
    }
    break;
  case INTERSECT:
    if (!((SqlSetOperator) call.getOperator()).isAll()) {
      SqlSyntax.BINARY.unparse(writer, INTERSECT_DISTINCT, call, leftPrec, rightPrec);
    }
    break;
  default:
    super.unparseCall(writer, call, leftPrec, rightPrec);
  }
}
 
Example 20
Source File: BigQuerySqlDialect.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public void unparseCall(final SqlWriter writer, final SqlCall call, final int leftPrec,
    final int rightPrec) {
  switch (call.getKind()) {
  case POSITION:
    final SqlWriter.Frame frame = writer.startFunCall("STRPOS");
    writer.sep(",");
    call.operand(1).unparse(writer, leftPrec, rightPrec);
    writer.sep(",");
    call.operand(0).unparse(writer, leftPrec, rightPrec);
    if (3 == call.operandCount()) {
      throw new RuntimeException("3rd operand Not Supported for Function STRPOS in Big Query");
    }
    writer.endFunCall(frame);
    break;
  case UNION:
    if (((SqlSetOperator) call.getOperator()).isAll()) {
      super.unparseCall(writer, call, leftPrec, rightPrec);
    } else {
      SqlSyntax.BINARY.unparse(writer, UNION_DISTINCT, call, leftPrec,
          rightPrec);
    }
    break;
  case EXCEPT:
    if (((SqlSetOperator) call.getOperator()).isAll()) {
      throw new RuntimeException("BigQuery does not support EXCEPT ALL");
    }
    SqlSyntax.BINARY.unparse(writer, EXCEPT_DISTINCT, call, leftPrec,
        rightPrec);
    break;
  case INTERSECT:
    if (((SqlSetOperator) call.getOperator()).isAll()) {
      throw new RuntimeException("BigQuery does not support INTERSECT ALL");
    }
    SqlSyntax.BINARY.unparse(writer, INTERSECT_DISTINCT, call, leftPrec,
        rightPrec);
    break;
  case TRIM:
    unparseTrim(writer, call, leftPrec, rightPrec);
    break;
  default:
    super.unparseCall(writer, call, leftPrec, rightPrec);
  }
}