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

The following examples show how to use org.apache.calcite.sql.SqlWriter#newlineAndIndent() . 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: SqlCreateView.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  if (getReplace()) {
    writer.keyword("CREATE OR REPLACE");
  } else {
    writer.keyword("CREATE");
  }
  writer.keyword("VIEW");
  name.unparse(writer, leftPrec, rightPrec);
  if (columnList != null) {
    SqlWriter.Frame frame = writer.startList("(", ")");
    for (SqlNode c : columnList) {
      writer.sep(",");
      c.unparse(writer, 0, 0);
    }
    writer.endList(frame);
  }
  writer.keyword("AS");
  writer.newlineAndIndent();
  query.unparse(writer, 0, 0);
}
 
Example 2
Source File: SqlAddReplaceColumns.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);
	if (replace) {
		writer.keyword("REPLACE");
	} else {
		writer.keyword("ADD");
	}
	writer.keyword("COLUMNS");
	SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.create("sds"), "(", ")");
	for (SqlNode column : newColumns) {
		printIndent(writer);
		column.unparse(writer, leftPrec, rightPrec);
	}
	writer.newlineAndIndent();
	writer.endList(frame);
}
 
Example 3
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 4
Source File: SqlCreateCatalog.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("CREATE CATALOG");
	catalogName.unparse(writer, leftPrec, rightPrec);

	if (this.propertyList.size() > 0) {
		writer.keyword("WITH");
		SqlWriter.Frame withFrame = writer.startList("(", ")");
		for (SqlNode property : propertyList) {
			printIndent(writer);
			property.unparse(writer, leftPrec, rightPrec);
		}
		writer.newlineAndIndent();
		writer.endList(withFrame);
	}
}
 
Example 5
Source File: SqlCreateMaterializedView.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  writer.keyword("CREATE");
  writer.keyword("MATERIALIZED VIEW");
  if (ifNotExists) {
    writer.keyword("IF NOT EXISTS");
  }
  name.unparse(writer, leftPrec, rightPrec);
  if (columnList != null) {
    SqlWriter.Frame frame = writer.startList("(", ")");
    for (SqlNode c : columnList) {
      writer.sep(",");
      c.unparse(writer, 0, 0);
    }
    writer.endList(frame);
  }
  writer.keyword("AS");
  writer.newlineAndIndent();
  query.unparse(writer, 0, 0);
}
 
Example 6
Source File: SqlAddHivePartitions.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);
	writer.newlineAndIndent();
	writer.keyword("ADD");
	if (ifNotExists()) {
		writer.keyword("IF NOT EXISTS");
	}
	int opLeftPrec = getOperator().getLeftPrec();
	int opRightPrec = getOperator().getRightPrec();
	for (int i = 0; i < getPartSpecs().size(); i++) {
		writer.newlineAndIndent();
		SqlNodeList partSpec = getPartSpecs().get(i);
		writer.keyword("PARTITION");
		partSpec.unparse(writer, opLeftPrec, opRightPrec);
		SqlCharStringLiteral location = partLocations.get(i);
		if (location != null) {
			writer.keyword("LOCATION");
			location.unparse(writer, opLeftPrec, opRightPrec);
		}
	}
}
 
Example 7
Source File: SqlCreateTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  writer.keyword("CREATE");
  writer.keyword("TABLE");
  name.unparse(writer, leftPrec, rightPrec);
  if (columnList != null) {
    SqlWriter.Frame frame = writer.startList("(", ")");
    forEachNameType((name, typeSpec) -> {
      writer.sep(",");
      name.unparse(writer, leftPrec, rightPrec);
      typeSpec.unparse(writer, leftPrec, rightPrec);
      if (Boolean.FALSE.equals(typeSpec.getNullable())) {
        writer.keyword("NOT NULL");
      }
    });
    writer.endList(frame);
  }
  if (query != null) {
    writer.keyword("AS");
    writer.newlineAndIndent();
    query.unparse(writer, 0, 0);
  }
}
 
Example 8
Source File: SqlAlterHivePartitionRename.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	super.unparse(writer, leftPrec, rightPrec);
	writer.newlineAndIndent();
	writer.keyword("RENAME TO");
	writer.newlineAndIndent();
	writer.keyword("PARTITION");
	newPartSpec.unparse(writer, getOperator().getLeftPrec(), getOperator().getRightPrec());
}
 
Example 9
Source File: SqlAlterTableProperties.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	super.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 10
Source File: SqlCreateHiveTable.java    From flink with Apache License 2.0 5 votes vote down vote up
private void unparseStoredAs(SqlWriter writer, int leftPrec, int rightPrec) {
	if (storedAs == null) {
		return;
	}
	writer.newlineAndIndent();
	writer.keyword("STORED AS");
	if (storedAs.fileFormat != null) {
		storedAs.fileFormat.unparse(writer, leftPrec, rightPrec);
	} else {
		writer.keyword("INPUTFORMAT");
		storedAs.intputFormat.unparse(writer, leftPrec, rightPrec);
		writer.keyword("OUTPUTFORMAT");
		storedAs.outputFormat.unparse(writer, leftPrec, rightPrec);
	}
}
 
Example 11
Source File: SqlCreateHiveTable.java    From flink with Apache License 2.0 5 votes vote down vote up
private void unparsePropList(SqlNodeList propList, SqlWriter writer, int leftPrec, int rightPrec) {
	SqlWriter.Frame withFrame = writer.startList("(", ")");
	for (SqlNode property : propList) {
		printIndent(writer);
		property.unparse(writer, leftPrec, rightPrec);
	}
	writer.newlineAndIndent();
	writer.endList(withFrame);
}
 
Example 12
Source File: SqlAlterHiveViewProperties.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 VIEW");
	viewIdentifier.unparse(writer, leftPrec, rightPrec);
	writer.keyword("SET TBLPROPERTIES");
	SqlWriter.Frame withFrame = writer.startList("(", ")");
	for (SqlNode property : getPropertyList()) {
		printIndent(writer);
		property.unparse(writer, leftPrec, rightPrec);
	}
	writer.newlineAndIndent();
	writer.endList(withFrame);
}
 
Example 13
Source File: SqlAlterHiveTableProps.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	super.unparse(writer, leftPrec, rightPrec);
	writer.keyword("SET TBLPROPERTIES");
	SqlWriter.Frame withFrame = writer.startList("(", ")");
	for (SqlNode property : origProps) {
		printIndent(writer);
		property.unparse(writer, leftPrec, rightPrec);
	}
	writer.newlineAndIndent();
	writer.endList(withFrame);
}
 
Example 14
Source File: SqlCreateCatalog.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 15
Source File: SqlLiteralChainOperator.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void unparse(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  final SqlWriter.Frame frame = writer.startList("", "");
  SqlCollation collation = null;
  for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
    SqlLiteral rand = (SqlLiteral) operand.e;
    if (operand.i > 0) {
      // SQL:2003 says there must be a newline between string
      // fragments.
      writer.newlineAndIndent();
    }
    if (rand instanceof SqlCharStringLiteral) {
      NlsString nls = ((SqlCharStringLiteral) rand).getNlsString();
      if (operand.i == 0) {
        collation = nls.getCollation();

        // print with prefix
        writer.literal(nls.asSql(true, false));
      } else {
        // print without prefix
        writer.literal(nls.asSql(false, false));
      }
    } else if (operand.i == 0) {
      // print with prefix
      rand.unparse(writer, leftPrec, rightPrec);
    } else {
      // print without prefix
      if (rand.getTypeName() == SqlTypeName.BINARY) {
        BitString bs = (BitString) rand.getValue();
        writer.literal("'" + bs.toHexString() + "'");
      } else {
        writer.literal("'" + rand.toValue() + "'");
      }
    }
  }
  if (collation != null) {
    collation.unparse(writer, 0, 0);
  }
  writer.endList(frame);
}
 
Example 16
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 17
Source File: SqlLiteralChainOperator.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void unparse(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  final SqlWriter.Frame frame = writer.startList("", "");
  SqlCollation collation = null;
  for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
    SqlLiteral rand = (SqlLiteral) operand.e;
    if (operand.i > 0) {
      // SQL:2003 says there must be a newline between string
      // fragments.
      writer.newlineAndIndent();
    }
    if (rand instanceof SqlCharStringLiteral) {
      NlsString nls = ((SqlCharStringLiteral) rand).getNlsString();
      if (operand.i == 0) {
        collation = nls.getCollation();

        // print with prefix
        writer.literal(nls.asSql(true, false, writer.getDialect()));
      } else {
        // print without prefix
        writer.literal(nls.asSql(false, false, writer.getDialect()));
      }
    } else if (operand.i == 0) {
      // print with prefix
      rand.unparse(writer, leftPrec, rightPrec);
    } else {
      // print without prefix
      if (rand.getTypeName() == SqlTypeName.BINARY) {
        BitString bs = (BitString) rand.getValue();
        writer.literal("'" + bs.toHexString() + "'");
      } else {
        writer.literal("'" + rand.toValue() + "'");
      }
    }
  }
  if (collation != null) {
    collation.unparse(writer);
  }
  writer.endList(frame);
}
 
Example 18
Source File: SqlCreateTable.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void unparse(
		SqlWriter writer,
		int leftPrec,
		int rightPrec) {
	writer.keyword("CREATE");
	if (isTemporary()) {
		writer.keyword("TEMPORARY");
	}
	writer.keyword("TABLE");
	tableName.unparse(writer, leftPrec, rightPrec);
	SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.create("sds"), "(", ")");
	for (SqlNode column : columnList) {
		printIndent(writer);
		if (column instanceof SqlBasicCall) {
			SqlCall call = (SqlCall) column;
			SqlCall newCall = call.getOperator().createCall(
				SqlParserPos.ZERO,
				call.operand(1),
				call.operand(0));
			newCall.unparse(writer, leftPrec, rightPrec);
		} else {
			column.unparse(writer, leftPrec, rightPrec);
		}
	}
	if (tableConstraints.size() > 0) {
		for (SqlTableConstraint constraint : tableConstraints) {
			printIndent(writer);
			constraint.unparse(writer, leftPrec, rightPrec);
		}
	}
	if (watermark != null) {
		printIndent(writer);
		watermark.unparse(writer, leftPrec, rightPrec);
	}

	writer.newlineAndIndent();
	writer.endList(frame);

	if (comment != null) {
		writer.newlineAndIndent();
		writer.keyword("COMMENT");
		comment.unparse(writer, leftPrec, rightPrec);
	}

	if (this.partitionKeyList.size() > 0) {
		writer.newlineAndIndent();
		writer.keyword("PARTITIONED BY");
		SqlWriter.Frame partitionedByFrame = writer.startList("(", ")");
		this.partitionKeyList.unparse(writer, leftPrec, rightPrec);
		writer.endList(partitionedByFrame);
		writer.newlineAndIndent();
	}

	if (this.propertyList.size() > 0) {
		writer.keyword("WITH");
		SqlWriter.Frame withFrame = writer.startList("(", ")");
		for (SqlNode property : propertyList) {
			printIndent(writer);
			property.unparse(writer, leftPrec, rightPrec);
		}
		writer.newlineAndIndent();
		writer.endList(withFrame);
	}

	if (this.tableLike != null) {
		writer.newlineAndIndent();
		this.tableLike.unparse(writer, leftPrec, rightPrec);
	}
}
 
Example 19
Source File: SqlCreateTable.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: SqlAlterViewProperties.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("  ");
}