Java Code Examples for org.apache.calcite.sql.SqlWriter#print()

The following examples show how to use org.apache.calcite.sql.SqlWriter#print() . 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: SqlPosixRegexOperator.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.startList("", "");
  call.operand(0).unparse(writer, getLeftPrec(), getRightPrec());

  if (this.negated) {
    writer.print("!");
  }
  writer.print("~");
  if (!this.caseSensitive) {
    writer.print("*");
  }
  writer.print(" ");

  call.operand(1).unparse(writer, getLeftPrec(), getRightPrec());
  writer.endList(frame);
}
 
Example 2
Source File: BigQuerySqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** BigQuery interval syntax: INTERVAL int64 time_unit. */
@Override public void unparseSqlIntervalLiteral(
        SqlWriter writer, SqlIntervalLiteral literal, int leftPrec, int rightPrec) {
  SqlIntervalLiteral.IntervalValue interval =
          (SqlIntervalLiteral.IntervalValue) literal.getValue();
  writer.keyword("INTERVAL");
  if (interval.getSign() == -1) {
    writer.print("-");
  }
  Long intervalValueInLong;
  try {
    intervalValueInLong = Long.parseLong(literal.getValue().toString());
  } catch (NumberFormatException e) {
    throw new RuntimeException("Only INT64 is supported as the interval value for BigQuery.");
  }
  writer.literal(intervalValueInLong.toString());
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
          RelDataTypeSystem.DEFAULT);
}
 
Example 3
Source File: SqlCreateHiveTable.java    From flink with Apache License 2.0 6 votes vote down vote up
private void unparseColumns(HiveTableCreationContext context, SqlNodeList columns,
		SqlWriter writer, int leftPrec, int rightPrec) {
	List<SqlHiveConstraintTrait> notNullTraits = context.notNullTraits;
	int traitIndex = 0;
	for (SqlNode node : columns) {
		printIndent(writer);
		SqlTableColumn column = (SqlTableColumn) node;
		column.getName().unparse(writer, leftPrec, rightPrec);
		writer.print(" ");
		column.getType().unparse(writer, leftPrec, rightPrec);
		if (column.getType().getNullable() != null && !column.getType().getNullable()) {
			writer.keyword("NOT NULL");
			notNullTraits.get(traitIndex++).unparse(writer, leftPrec, rightPrec);
		}
		column.getComment().ifPresent(c -> {
			writer.keyword("COMMENT");
			c.unparse(writer, leftPrec, rightPrec);
		});
	}
}
 
Example 4
Source File: Db2SqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void unparseSqlIntervalLiteral(SqlWriter writer,
    SqlIntervalLiteral literal, int leftPrec, int rightPrec) {
  // A duration is a positive or negative number representing an interval of time.
  // If one operand is a date, the other labeled duration of YEARS, MONTHS, or DAYS.
  // If one operand is a time, the other must be labeled duration of HOURS, MINUTES, or SECONDS.
  // If one operand is a timestamp, the other operand can be any of teh duration.

  SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) literal.getValue();
  if (interval.getSign() == -1) {
    writer.print("-");
  }
  writer.literal(literal.getValue().toString());
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
      RelDataTypeSystem.DEFAULT);
}
 
Example 5
Source File: SqlRowType.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	writer.print("ROW");
	if (getFieldNames().size() == 0) {
		writer.print("<>");
	} else {
		SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "<", ">");
		int i = 0;
		for (Pair<SqlIdentifier, SqlDataTypeSpec> p : Pair.zip(this.fieldNames, this.fieldTypes)) {
			writer.sep(",", false);
			p.left.unparse(writer, 0, 0);
			ExtendedSqlType.unparseType(p.right, writer, leftPrec, rightPrec);
			if (comments.get(i) != null) {
				comments.get(i).unparse(writer, leftPrec, rightPrec);
			}
			i += 1;
		}
		writer.endList(frame);
	}
}
 
Example 6
Source File: Db2SqlDialect.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public void unparseSqlIntervalLiteral(SqlWriter writer,
    SqlIntervalLiteral literal, int leftPrec, int rightPrec) {
  // A duration is a positive or negative number representing an interval of time.
  // If one operand is a date, the other labeled duration of YEARS, MONTHS, or DAYS.
  // If one operand is a time, the other must be labeled duration of HOURS, MINUTES, or SECONDS.
  // If one operand is a timestamp, the other operand can be any of teh duration.

  SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) literal.getValue();
  if (interval.getSign() == -1) {
    writer.print("-");
  }
  writer.literal(literal.getValue().toString());
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
      RelDataTypeSystem.DEFAULT);
}
 
Example 7
Source File: MssqlSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void unparseFloorWithUnit(SqlWriter writer, SqlCall call, int charLen,
    String offset) {
  writer.print("CONVERT");
  SqlWriter.Frame frame = writer.startList("(", ")");
  writer.print("DATETIME, CONVERT(VARCHAR(" + charLen + "), ");
  call.operand(0).unparse(writer, 0, 0);
  writer.print(", 126)");

  if (offset.length() > 0) {
    writer.print("+'" + offset + "'");
  }
  writer.endList(frame);
}
 
Example 8
Source File: SqlStdOperatorTable.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) {
  writer.keyword("PERMUTE");
  SqlWriter.Frame frame = writer.startList("(", ")");
  for (int i = 0; i < call.getOperandList().size(); i++) {
    SqlNode pattern = call.getOperandList().get(i);
    pattern.unparse(writer, 0, 0);
    if (i != call.getOperandList().size() - 1) {
      writer.print(",");
    }
  }
  writer.endList(frame);
}
 
Example 9
Source File: MssqlSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void unparseSqlIntervalLiteralMssql(
    SqlWriter writer, SqlIntervalLiteral literal, int sign) {
  final SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) literal.getValue();
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
      RelDataTypeSystem.DEFAULT);
  writer.sep(",", true);
  if (interval.getSign() * sign == -1) {
    writer.print("-");
  }
  writer.literal(literal.getValue().toString());
}
 
Example 10
Source File: SqlTimestampType.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	writer.keyword(SqlTypeName.TIMESTAMP.name());
	if (this.precision >= 0) {
		final SqlWriter.Frame frame =
			writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")");
		writer.print(precision);
		writer.endList(frame);
	}
	if (this.withLocalTimeZone) {
		writer.keyword("WITH LOCAL TIME ZONE");
	}
}
 
Example 11
Source File: SqlTimeType.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	writer.keyword(SqlTypeName.TIME.name());
	if (this.precision >= 0) {
		final SqlWriter.Frame frame =
			writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")");
		writer.print(precision);
		writer.endList(frame);
	}
	if (this.withLocalTimeZone) {
		writer.keyword("WITH LOCAL TIME ZONE");
	}
}
 
Example 12
Source File: SqlStdOperatorTable.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) {
  writer.keyword("PERMUTE");
  SqlWriter.Frame frame = writer.startList("(", ")");
  for (int i = 0; i < call.getOperandList().size(); i++) {
    SqlNode pattern = call.getOperandList().get(i);
    pattern.unparse(writer, 0, 0);
    if (i != call.getOperandList().size() - 1) {
      writer.print(",");
    }
  }
  writer.endList(frame);
}
 
Example 13
Source File: MssqlSqlDialect.java    From Bats with Apache License 2.0 5 votes vote down vote up
private void unparseFloorWithUnit(SqlWriter writer, SqlCall call, int charLen,
    String offset) {
  writer.print("CONVERT");
  SqlWriter.Frame frame = writer.startList("(", ")");
  writer.print("DATETIME, CONVERT(VARCHAR(" + charLen + "), ");
  call.operand(0).unparse(writer, 0, 0);
  writer.print(", 126)");

  if (offset.length() > 0) {
    writer.print("+'" + offset + "'");
  }
  writer.endList(frame);
}
 
Example 14
Source File: MssqlSqlDialect.java    From Bats with Apache License 2.0 5 votes vote down vote up
private void unparseSqlIntervalLiteralMssql(
    SqlWriter writer, SqlIntervalLiteral literal, int sign) {
  final SqlIntervalLiteral.IntervalValue interval =
      (SqlIntervalLiteral.IntervalValue) literal.getValue();
  unparseSqlIntervalQualifier(writer, interval.getIntervalQualifier(),
      RelDataTypeSystem.DEFAULT);
  writer.sep(",", true);
  if (interval.getSign() * sign == -1) {
    writer.print("-");
  }
  writer.literal(literal.getValue().toString());
}
 
Example 15
Source File: ClickHouseSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Unparses datetime floor for ClickHouse.
 *
 * @param writer Writer
 * @param call Call
 */
private void unparseFloor(SqlWriter writer, SqlCall call) {
  final SqlLiteral timeUnitNode = call.operand(1);
  TimeUnitRange unit = (TimeUnitRange) timeUnitNode.getValue();

  String funName;
  switch (unit) {
  case YEAR:
    funName = "toStartOfYear";
    break;
  case MONTH:
    funName = "toStartOfMonth";
    break;
  case WEEK:
    funName = "toMonday";
    break;
  case DAY:
    funName = "toDate";
    break;
  case HOUR:
    funName = "toStartOfHour";
    break;
  case MINUTE:
    funName = "toStartOfMinute";
    break;
  default:
    throw new RuntimeException("ClickHouse does not support FLOOR for time unit: "
        + unit);
  }

  writer.print(funName);
  SqlWriter.Frame frame = writer.startList("(", ")");
  call.operand(0).unparse(writer, 0, 0);
  writer.endList(frame);
}
 
Example 16
Source File: SqlCreateView.java    From flink with Apache License 2.0 4 votes vote down vote up
private void printIndent(SqlWriter writer) {
	writer.sep(",", false);
	writer.newlineAndIndent();
	writer.print("  ");
}
 
Example 17
Source File: SqlAddReplaceColumns.java    From flink with Apache License 2.0 4 votes vote down vote up
protected void printIndent(SqlWriter writer) {
	writer.sep(",", false);
	writer.newlineAndIndent();
	writer.print("  ");
}
 
Example 18
Source File: SqlAlterDatabase.java    From flink with Apache License 2.0 4 votes vote down vote up
protected void printIndent(SqlWriter writer) {
	writer.sep(",", false);
	writer.newlineAndIndent();
	writer.print("  ");
}
 
Example 19
Source File: SqlAlterTableProperties.java    From flink with Apache License 2.0 4 votes vote down vote up
protected void printIndent(SqlWriter writer) {
	writer.sep(",", false);
	writer.newlineAndIndent();
	writer.print("  ");
}
 
Example 20
Source File: SqlCreateDatabase.java    From flink with Apache License 2.0 4 votes vote down vote up
protected void printIndent(SqlWriter writer) {
	writer.sep(",", false);
	writer.newlineAndIndent();
	writer.print("  ");
}