Java Code Examples for org.apache.calcite.sql.SqlLiteral#createCharString()

The following examples show how to use org.apache.calcite.sql.SqlLiteral#createCharString() . 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: SqlValidatorImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
protected SqlNode expandDynamicStar(SqlIdentifier id, SqlIdentifier fqId) {
	if (DynamicRecordType.isDynamicStarColName(Util.last(fqId.names))
		&& !DynamicRecordType.isDynamicStarColName(Util.last(id.names))) {
		// Convert a column ref into ITEM(*, 'col_name')
		// for a dynamic star field in dynTable's rowType.
		SqlNode[] inputs = new SqlNode[2];
		inputs[0] = fqId;
		inputs[1] = SqlLiteral.createCharString(
			Util.last(id.names),
			id.getParserPosition());
		return new SqlBasicCall(
			SqlStdOperatorTable.ITEM,
			inputs,
			id.getParserPosition());
	}
	return fqId;
}
 
Example 2
Source File: RelToSqlConverterUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * For usage of TRIM, LTRIM and RTRIM in Hive, see
 * <a href="https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF">Hive UDF usage</a>.
 */
public static void unparseHiveTrim(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  final SqlLiteral valueToTrim = call.operand(1);
  if (valueToTrim.toValue().matches("\\s+")) {
    unparseTrimWithSpace(writer, call, leftPrec, rightPrec);
  } else {
    // SELECT TRIM(both 'A' from "ABC") -> SELECT REGEXP_REPLACE("ABC", '^(A)*', '')
    final SqlLiteral trimFlag = call.operand(0);
    final SqlCharStringLiteral regexNode =
        createRegexPatternLiteral(call.operand(1), trimFlag);
    final SqlCharStringLiteral blankLiteral =
        SqlLiteral.createCharString("", call.getParserPosition());
    final SqlNode[] trimOperands = new SqlNode[] { call.operand(2), regexNode, blankLiteral };
    final SqlCall regexReplaceCall = REGEXP_REPLACE.createCall(SqlParserPos.ZERO, trimOperands);
    regexReplaceCall.unparse(writer, leftPrec, rightPrec);
  }
}
 
Example 3
Source File: RelToSqlConverterUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates regex pattern based on the TRIM flag.
 *
 * @param call     SqlCall contains the values that need to be trimmed
 * @param trimFlag the trimFlag, either BOTH, LEADING or TRAILING
 * @return the regex pattern of the character to be trimmed
 */
public static SqlCharStringLiteral createRegexPatternLiteral(SqlNode call, SqlLiteral trimFlag) {
  final String regexPattern = ((SqlCharStringLiteral) call).toValue();
  String escaped = escapeSpecialChar(regexPattern);
  final StringBuilder builder = new StringBuilder();
  switch (trimFlag.getValueAs(SqlTrimFunction.Flag.class)) {
  case LEADING:
    builder.append("^(").append(escaped).append(")*");
    break;
  case TRAILING:
    builder.append("(").append(escaped).append(")*$");
    break;
  default:
    builder.append("^(")
        .append(escaped)
        .append(")*|(")
        .append(escaped)
        .append(")*$");
    break;
  }
  return SqlLiteral.createCharString(builder.toString(),
    call.getParserPosition());
}
 
Example 4
Source File: DrillCompoundIdentifier.java    From Bats with Apache License 2.0 5 votes vote down vote up
public SqlNode getNode(SqlNode node) {
  SqlLiteral literal;
  if (isArray) {
    literal = SqlLiteral.createExactNumeric(value, parserPos);
  } else {
    literal = SqlLiteral.createCharString(value, parserPos);
  }
  return new SqlBasicCall(SqlStdOperatorTable.ITEM, new SqlNode[]{node, literal}, parserPos);
}
 
Example 5
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode visit(SqlIdentifier id) {
	// First check for builtin functions which don't have
	// parentheses, like "LOCALTIME".
	SqlCall call =
		SqlUtil.makeCall(
			validator.getOperatorTable(),
			id);
	if (call != null) {
		return call.accept(this);
	}
	final SqlIdentifier fqId = getScope().fullyQualify(id).identifier;
	SqlNode expandedExpr = fqId;
	// Convert a column ref into ITEM(*, 'col_name').
	// select col_name from (select * from dynTable)
	// SqlIdentifier "col_name" would be resolved to a dynamic star field in dynTable's rowType.
	// Expand such SqlIdentifier to ITEM operator.
	if (DynamicRecordType.isDynamicStarColName(Util.last(fqId.names))
		&& !DynamicRecordType.isDynamicStarColName(Util.last(id.names))) {
		SqlNode[] inputs = new SqlNode[2];
		inputs[0] = fqId;
		inputs[1] = SqlLiteral.createCharString(
			Util.last(id.names),
			id.getParserPosition());
		SqlBasicCall item_call = new SqlBasicCall(
			SqlStdOperatorTable.ITEM,
			inputs,
			id.getParserPosition());
		expandedExpr = item_call;
	}
	validator.setOriginal(expandedExpr, id);
	return expandedExpr;
}
 
Example 6
Source File: SqlContains.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static SqlNode getNode(SqlParserPos pos, Map<String, String> fieldMap, String queryString) {
  SqlNode[] operands = new SqlNode[fieldMap.size() + 1];
  for (String field : fieldMap.keySet()) {
    int index = Integer.parseInt(fieldMap.get(field).substring(1));
    operands[index] = getIdentifier(field);
  }
  SqlNode query = SqlLiteral.createCharString(queryString, pos);
  operands[operands.length - 1] = query;
  return new SqlBasicCall(OPERATOR, operands, pos);
}
 
Example 7
Source File: CompoundIdentifier.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public SqlNode getNode(SqlNode node){
  SqlLiteral literal;
  if(isArray){
    literal = SqlLiteral.createExactNumeric(value, parserPos);
  }else{
    literal = SqlLiteral.createCharString(value, parserPos);
  }
  return new SqlBasicCall(SqlStdOperatorTable.ITEM, new SqlNode[]{ node, literal }, parserPos);
}
 
Example 8
Source File: HiveDDLUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public SqlNode visit(SqlLiteral literal) {
	if (literal instanceof SqlCharStringLiteral) {
		SqlCharStringLiteral stringLiteral = (SqlCharStringLiteral) literal;
		String unescaped = StringEscapeUtils.unescapeJava(stringLiteral.getNlsString().getValue());
		return SqlLiteral.createCharString(unescaped, stringLiteral.getParserPosition());
	}
	return literal;
}
 
Example 9
Source File: SqlValidatorUtil.java    From Bats with Apache License 2.0 4 votes vote down vote up
SqlNode createGroupExpr() {
  // TODO: create an expression that could have no other source
  return SqlLiteral.createCharString("xyz" + groupCount++,
      SqlParserPos.ZERO);
}
 
Example 10
Source File: RexToSqlNodeConverterImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
public SqlNode convertLiteral(RexLiteral literal) {
  // Numeric
  if (SqlTypeFamily.EXACT_NUMERIC.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createExactNumeric(
        literal.getValue().toString(),
        SqlParserPos.ZERO);
  }

  if (SqlTypeFamily.APPROXIMATE_NUMERIC.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createApproxNumeric(
        literal.getValue().toString(),
        SqlParserPos.ZERO);
  }

  // Timestamp
  if (SqlTypeFamily.TIMESTAMP.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createTimestamp(
        literal.getValueAs(TimestampString.class),
        0,
        SqlParserPos.ZERO);
  }

  // Date
  if (SqlTypeFamily.DATE.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createDate(
        literal.getValueAs(DateString.class),
        SqlParserPos.ZERO);
  }

  // Time
  if (SqlTypeFamily.TIME.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createTime(
        literal.getValueAs(TimeString.class),
        0,
        SqlParserPos.ZERO);
  }

  // String
  if (SqlTypeFamily.CHARACTER.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createCharString(
        ((NlsString) (literal.getValue())).getValue(),
        SqlParserPos.ZERO);
  }

  // Boolean
  if (SqlTypeFamily.BOOLEAN.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createBoolean(
        (Boolean) literal.getValue(),
        SqlParserPos.ZERO);
  }

  // Null
  if (SqlTypeFamily.NULL == literal.getTypeName().getFamily()) {
    return SqlLiteral.createNull(SqlParserPos.ZERO);
  }

  return null;
}
 
Example 11
Source File: HiveDDLUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
public static SqlTableOption toTableOption(String key, SqlNode value, SqlParserPos pos) {
	return new SqlTableOption(SqlLiteral.createCharString(key, pos), value, pos);
}
 
Example 12
Source File: HiveDDLUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
public static SqlTableOption toTableOption(String key, String value, SqlParserPos pos) {
	return new SqlTableOption(SqlLiteral.createCharString(key, pos), SqlLiteral.createCharString(value, pos), pos);
}
 
Example 13
Source File: SqlValidatorUtil.java    From calcite with Apache License 2.0 4 votes vote down vote up
SqlNode createGroupExpr() {
  // TODO: create an expression that could have no other source
  return SqlLiteral.createCharString("xyz" + groupCount++,
      SqlParserPos.ZERO);
}
 
Example 14
Source File: RexToSqlNodeConverterImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SqlNode convertLiteral(RexLiteral literal) {
  // Numeric
  if (SqlTypeFamily.EXACT_NUMERIC.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createExactNumeric(
        literal.getValue().toString(),
        SqlParserPos.ZERO);
  }

  if (SqlTypeFamily.APPROXIMATE_NUMERIC.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createApproxNumeric(
        literal.getValue().toString(),
        SqlParserPos.ZERO);
  }

  // Timestamp
  if (SqlTypeFamily.TIMESTAMP.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createTimestamp(
        literal.getValueAs(TimestampString.class),
        0,
        SqlParserPos.ZERO);
  }

  // Date
  if (SqlTypeFamily.DATE.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createDate(
        literal.getValueAs(DateString.class),
        SqlParserPos.ZERO);
  }

  // Time
  if (SqlTypeFamily.TIME.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createTime(
        literal.getValueAs(TimeString.class),
        0,
        SqlParserPos.ZERO);
  }

  // String
  if (SqlTypeFamily.CHARACTER.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createCharString(
        ((NlsString) (literal.getValue())).getValue(),
        SqlParserPos.ZERO);
  }

  // Boolean
  if (SqlTypeFamily.BOOLEAN.getTypeNames().contains(
      literal.getTypeName())) {
    return SqlLiteral.createBoolean(
        (Boolean) literal.getValue(),
        SqlParserPos.ZERO);
  }

  // Null
  if (SqlTypeFamily.NULL == literal.getTypeName().getFamily()) {
    return SqlLiteral.createNull(SqlParserPos.ZERO);
  }

  return null;
}
 
Example 15
Source File: SqlFloorFunction.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Copies a {@link SqlCall}, replacing the time unit operand with the given
 * literal.
 *
 * @param call Call
 * @param literal Literal to replace time unit with
 * @param pos Parser position
 * @return Modified call
 */
public static SqlCall replaceTimeUnitOperand(SqlCall call, String literal, SqlParserPos pos) {
  SqlLiteral literalNode = SqlLiteral.createCharString(literal, null, pos);
  return call.getOperator().createCall(call.getFunctionQuantifier(), pos,
      call.getOperandList().get(0), literalNode);
}
 
Example 16
Source File: SqlFloorFunction.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Copies a {@link SqlCall}, replacing the time unit operand with the given
 * literal.
 *
 * @param call Call
 * @param literal Literal to replace time unit with
 * @param pos Parser position
 * @return Modified call
 */
public static SqlCall replaceTimeUnitOperand(SqlCall call, String literal, SqlParserPos pos) {
  SqlLiteral literalNode = SqlLiteral.createCharString(literal, null, pos);
  return call.getOperator().createCall(call.getFunctionQuantifier(), pos,
      call.getOperandList().get(0), literalNode);
}