org.apache.calcite.sql.SqlWriter Java Examples

The following examples show how to use org.apache.calcite.sql.SqlWriter. 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: SqlAlterTableExtension.java    From kareldb with Apache License 2.0 6 votes vote down vote up
@Override
protected void unparseAlterOperation(SqlWriter writer, int leftPrec, int rightPrec) {
    if (ifExists) {
        writer.keyword("IF NOT EXISTS");
    }
    name.unparse(writer, leftPrec, rightPrec);
    if (columnList != null) {
        SqlWriter.Frame frame = writer.startList("", "");
        for (int i = 0; i < columnList.size(); i++) {
            Action a = actions.get(i);
            SqlNode c = columnList.get(i);
            writer.sep(",");
            writer.keyword(a.name());
            c.unparse(writer, 0, 0);
        }
        writer.endList(frame);
    }
}
 
Example #2
Source File: SqlOverlayFunction.java    From Bats 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("PLACING");
  call.operand(1).unparse(writer, leftPrec, rightPrec);
  writer.sep("FROM");
  call.operand(2).unparse(writer, leftPrec, rightPrec);
  if (4 == call.operandCount()) {
    writer.sep("FOR");
    call.operand(3).unparse(writer, leftPrec, rightPrec);
  }
  writer.endFunCall(frame);
}
 
Example #3
Source File: SqlJsonValueFunction.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
  assert call.operandCount() == 5 || call.operandCount() == 6;
  final SqlWriter.Frame frame = writer.startFunCall(getName());
  call.operand(0).unparse(writer, 0, 0);
  if (!returnAny) {
    writer.keyword("RETURNING");
    call.operand(5).unparse(writer, 0, 0);
  }
  unparseEnum(writer, call.operand(1));
  if (isDefaultLiteral(call.operand(1))) {
    call.operand(2).unparse(writer, 0, 0);
  }
  writer.keyword("ON");
  writer.keyword("EMPTY");
  unparseEnum(writer, call.operand(3));
  if (isDefaultLiteral(call.operand(3))) {
    call.operand(4).unparse(writer, 0, 0);
  }
  writer.keyword("ON");
  writer.keyword("ERROR");
  writer.endFunCall(frame);
}
 
Example #4
Source File: SqlOverlayFunction.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("PLACING");
  call.operand(1).unparse(writer, leftPrec, rightPrec);
  writer.sep("FROM");
  call.operand(2).unparse(writer, leftPrec, rightPrec);
  if (4 == call.operandCount()) {
    writer.sep("FOR");
    call.operand(3).unparse(writer, leftPrec, rightPrec);
  }
  writer.endFunCall(frame);
}
 
Example #5
Source File: SqlTableConstraint.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	if (this.constraintName != null) {
		writer.keyword("CONSTRAINT");
		this.constraintName.unparse(writer, leftPrec, rightPrec);
	}
	this.uniqueSpec.unparse(writer, leftPrec, rightPrec);
	if (isTableConstraint) {
		SqlWriter.Frame frame = writer.startList("(", ")");
		for (SqlNode column : this.columns) {
			writer.sep(",", false);
			column.unparse(writer, leftPrec, rightPrec);
		}
		writer.endList(frame);
	}
	if (this.enforcement != null) {
		this.enforcement.unparse(writer, leftPrec, rightPrec);
	}
}
 
Example #6
Source File: SqlJsonArrayFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void unparse(SqlWriter writer, SqlCall call, int leftPrec,
    int rightPrec) {
  assert call.operandCount() >= 1;
  final SqlWriter.Frame frame = writer.startFunCall(getName());
  SqlWriter.Frame listFrame = writer.startList("", "");
  for (int i = 1; i < call.operandCount(); i++) {
    writer.sep(",");
    call.operand(i).unparse(writer, leftPrec, rightPrec);
  }
  writer.endList(listFrame);

  SqlJsonConstructorNullClause nullClause = getEnumValue(call.operand(0));
  switch (nullClause) {
  case ABSENT_ON_NULL:
    writer.keyword("ABSENT ON NULL");
    break;
  case NULL_ON_NULL:
    writer.keyword("NULL ON NULL");
    break;
  default:
    throw new IllegalStateException("unreachable code");
  }
  writer.endFunCall(frame);
}
 
Example #7
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 #8
Source File: SqlFloorFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Most dialects that natively support datetime floor will use this.
 * In those cases the call will look like TRUNC(datetime, 'year').
 *
 * @param writer SqlWriter
 * @param call SqlCall
 * @param funName Name of the sql function to call
 * @param datetimeFirst Specify the order of the datetime &amp; timeUnit
 * arguments
 */
public static void unparseDatetimeFunction(SqlWriter writer, SqlCall call,
    String funName, Boolean datetimeFirst) {
  SqlFunction func = new SqlFunction(funName, SqlKind.OTHER_FUNCTION,
      ReturnTypes.ARG0_NULLABLE_VARYING, null, null,
      SqlFunctionCategory.STRING);

  SqlCall call1;
  if (datetimeFirst) {
    call1 = call;
  } else {
    // switch order of operands
    SqlNode op1 = call.operand(0);
    SqlNode op2 = call.operand(1);

    call1 = call.getOperator().createCall(call.getParserPosition(), op2, op1);
  }

  SqlUtil.unparseFunctionSyntax(func, writer, call1);
}
 
Example #9
Source File: SqlAlterHiveTableSerDe.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	super.unparse(writer, leftPrec, rightPrec);
	writer.keyword("SET");
	if (serdeLib != null) {
		writer.keyword("SERDE");
		serdeLib.unparse(writer, leftPrec, rightPrec);
	}
	if (origSerDeProps != null && origSerDeProps.size() > 0) {
		if (serdeLib == null) {
			writer.keyword("SERDEPROPERTIES");
		} else {
			writer.keyword("WITH SERDEPROPERTIES");
		}
		SqlWriter.Frame withFrame = writer.startList("(", ")");
		for (SqlNode property : origSerDeProps) {
			printIndent(writer);
			property.unparse(writer, leftPrec, rightPrec);
		}
		writer.newlineAndIndent();
		writer.endList(withFrame);
	}
}
 
Example #10
Source File: SqlAlterHiveTableChangeColumn.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.keyword("ALTER TABLE");
	tableIdentifier.unparse(writer, leftPrec, rightPrec);
	SqlNodeList partitionSpec = getPartitionSpec();
	if (partitionSpec != null && partitionSpec.size() > 0) {
		writer.keyword("PARTITION");
		partitionSpec.unparse(writer, getOperator().getLeftPrec(), getOperator().getRightPrec());
	}
	writer.keyword("CHANGE COLUMN");
	getOldName().unparse(writer, leftPrec, rightPrec);
	origNewColumn.unparse(writer, leftPrec, rightPrec);
	if (isFirst()) {
		writer.keyword("FIRST");
	}
	if (getAfter() != null) {
		writer.keyword("AFTER");
		getAfter().unparse(writer, leftPrec, rightPrec);
	}
	if (cascade) {
		writer.keyword("CASCADE");
	} else {
		writer.keyword("RESTRICT");
	}
}
 
Example #11
Source File: SqlCreateView.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  writer.keyword("CREATE");
  switch (SqlCreateType.valueOf(createType.toValue())) {
    case SIMPLE:
      writer.keyword("VIEW");
      break;
    case OR_REPLACE:
      writer.keyword("OR");
      writer.keyword("REPLACE");
      writer.keyword("VIEW");
      break;
    case IF_NOT_EXISTS:
      writer.keyword("VIEW");
      writer.keyword("IF");
      writer.keyword("NOT");
      writer.keyword("EXISTS");
      break;
  }
  viewName.unparse(writer, leftPrec, rightPrec);
  if (fieldList.size() > 0) {
    SqlHandlerUtil.unparseSqlNodeList(writer, leftPrec, rightPrec, fieldList);
  }
  writer.keyword("AS");
  query.unparse(writer, leftPrec, rightPrec);
}
 
Example #12
Source File: HsqldbSqlDialect.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);

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

  default:
    super.unparseCall(writer, call, leftPrec, rightPrec);
  }
}
 
Example #13
Source File: SqlColumnDeclaration.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  name.unparse(writer, 0, 0);
  dataType.unparse(writer, 0, 0);
  if (dataType.getNullable() != null && !dataType.getNullable()) {
    writer.keyword("NOT NULL");
  }
  if (expression != null) {
    switch (strategy) {
      case VIRTUAL:
      case STORED:
        writer.keyword("AS");
        exp(writer);
        writer.keyword(strategy.name());
        break;
      case DEFAULT:
        writer.keyword("DEFAULT");
        exp(writer);
        break;
      default:
        throw new AssertionError("unexpected: " + strategy);
    }
  }
}
 
Example #14
Source File: SqlAlterFunction.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.keyword("ALTER");
	if (isTemporary) {
		writer.keyword("TEMPORARY");
	}
	if (isSystemFunction) {
		writer.keyword("SYSTEM");
	}
	writer.keyword("FUNCTION");
	if (ifExists) {
		writer.keyword("IF EXISTS");
	}
	functionIdentifier.unparse(writer, leftPrec, rightPrec);
	writer.keyword("AS");
	functionClassName.unparse(writer, leftPrec, rightPrec);
	if (functionLanguage != null) {
		writer.keyword("LANGUAGE");
		writer.keyword(functionLanguage);
	}
}
 
Example #15
Source File: SqlExtendOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public void unparse(SqlWriter writer, SqlCall call, int leftPrec,
    int rightPrec) {
  final SqlOperator operator = call.getOperator();
  assert call.operandCount() == 2;
  final SqlWriter.Frame frame =
      writer.startList(SqlWriter.FrameTypeEnum.SIMPLE);
  call.operand(0).unparse(writer, leftPrec, operator.getLeftPrec());
  writer.setNeedWhitespace(true);
  writer.sep(operator.getName());
  final SqlNodeList list = call.operand(1);
  final SqlWriter.Frame frame2 = writer.startList("(", ")");
  for (Ord<SqlNode> node2 : Ord.zip(list)) {
    if (node2.i > 0 && node2.i % 2 == 0) {
      writer.sep(",");
    }
    node2.e.unparse(writer, 2, 3);
  }
  writer.endList(frame2);
  writer.endList(frame);
}
 
Example #16
Source File: SqlDropView.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  writer.keyword("DROP");
  writer.keyword("VIEW");
  if (viewExistenceCheck) {
    writer.keyword("IF");
    writer.keyword("EXISTS");
  }
  viewName.unparse(writer, leftPrec, rightPrec);
}
 
Example #17
Source File: SqlAlterQuarkView.java    From quark with Apache License 2.0 5 votes vote down vote up
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  writer.keyword("ALTER");
  writer.keyword("VIEW");
  identifier.unparse(writer, leftPrec, rightPrec);
  writer.keyword("SET");
  for (Pair<SqlNode, SqlNode> pair
      : Pair.zip(getTargetColumnList(), getSourceExpressionList())) {
    writer.sep(",");
    SqlIdentifier id = (SqlIdentifier) pair.left;
    id.unparse(writer, leftPrec, rightPrec);
    writer.keyword("=");
    SqlNode sourceExp = pair.right;
    sourceExp.unparse(writer, leftPrec, rightPrec);
  }
}
 
Example #18
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) {
  SqlWriter.Frame frame = writer.startList("{-", "-}");
  SqlNode node = call.getOperandList().get(0);
  node.unparse(writer, 0, 0);
  writer.endList(frame);
}
 
Example #19
Source File: SqlColumnListConstructor.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void unparse(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  writer.keyword("ROW");
  final SqlWriter.Frame frame = writer.startList("(", ")");
  for (SqlNode operand : call.getOperandList()) {
    writer.sep(",");
    operand.unparse(writer, leftPrec, rightPrec);
  }
  writer.endList(frame);
}
 
Example #20
Source File: SqlDotOperator.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.startList(SqlWriter.FrameTypeEnum.IDENTIFIER);
  call.operand(0).unparse(writer, leftPrec, 0);
  writer.sep(".");
  call.operand(1).unparse(writer, 0, 0);
  writer.endList(frame);
}
 
Example #21
Source File: SqlAlterDatabase.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("ALTER DATABASE");
	databaseName.unparse(writer, leftPrec, rightPrec);
	writer.keyword("SET");
	SqlWriter.Frame withFrame = writer.startList("(", ")");
	for (SqlNode property : propertyList) {
		printIndent(writer);
		property.unparse(writer, leftPrec, rightPrec);
	}
	writer.newlineAndIndent();
	writer.endList(withFrame);
}
 
Example #22
Source File: SqlInsertTable.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  writer.keyword("INSERT");
  writer.keyword("INTO");
  tblName.unparse(writer, leftPrec, rightPrec);
  if (insertFields.size() > 0) {
    SqlHandlerUtil.unparseSqlNodeList(writer, leftPrec, rightPrec, insertFields);
  }
  query.unparse(writer, leftPrec, rightPrec);
}
 
Example #23
Source File: SqlCastFunction.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) {
  assert call.operandCount() == 2;
  final SqlWriter.Frame frame = writer.startFunCall(getName());
  call.operand(0).unparse(writer, 0, 0);
  writer.sep("AS");
  if (call.operand(1) instanceof SqlIntervalQualifier) {
    writer.sep("INTERVAL");
  }
  call.operand(1).unparse(writer, 0, 0);
  writer.endFunCall(frame);
}
 
Example #24
Source File: SqlAlterTable.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("ALTER TABLE");
	tableIdentifier.unparse(writer, leftPrec, rightPrec);
	SqlNodeList partitionSpec = getPartitionSpec();
	if (partitionSpec != null && partitionSpec.size() > 0) {
		writer.keyword("PARTITION");
		partitionSpec.unparse(writer, getOperator().getLeftPrec(), getOperator().getRightPrec());
	}
}
 
Example #25
Source File: SqlMapType.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("MAP");
	SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "<", ">");
	writer.sep(",");
	ExtendedSqlType.unparseType(keyType, writer, leftPrec, rightPrec);
	writer.sep(",");
	ExtendedSqlType.unparseType(valType, writer, leftPrec, rightPrec);
	writer.endList(frame);
}
 
Example #26
Source File: PigConverter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a Pig script to a list of SQL statements.
 *
 * @param pigQuery Pig script
 * @param writer The SQL writer to decide dialect and format of SQL statements
 * @throws IOException Exception during parsing or translating Pig
 */
private List<String> pigToSql(String pigQuery, SqlWriter writer)
    throws IOException {
  final RelToSqlConverter sqlConverter =
      new PigRelToSqlConverter(writer.getDialect());
  final List<RelNode> finalRels = pigQuery2Rel(pigQuery);
  final List<String> sqlStatements = new ArrayList<>();
  for (RelNode rel : finalRels) {
    final SqlNode sqlNode = sqlConverter.visitChild(0, rel).asStatement();
    sqlNode.unparse(writer, 0, 0);
    sqlStatements.add(writer.toString());
  }
  return sqlStatements;
}
 
Example #27
Source File: SqlJsonArrayAggAggFunction.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) {
  assert call.operandCount() == 1;
  final SqlWriter.Frame frame = writer.startFunCall("JSON_ARRAYAGG");
  call.operand(0).unparse(writer, leftPrec, rightPrec);
  writer.keyword(nullClause.sql);
  writer.endFunCall(frame);
}
 
Example #28
Source File: SqlJsonValueFunction.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());
  call.operand(0).unparse(writer, leftPrec, rightPrec);
  writer.sep(",", true);
  for (int i = 1; i < call.operandCount(); i++) {
    call.operand(i).unparse(writer, leftPrec, rightPrec);
  }
  writer.endFunCall(frame);
}
 
Example #29
Source File: SqlRowOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void unparse(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  SqlUtil.unparseFunctionSyntax(this, writer, call);
}
 
Example #30
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);
}