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

The following examples show how to use org.apache.calcite.sql.SqlWriter#sep() . 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: 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 2
Source File: MssqlSqlDialect.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public void unparseSqlDatetimeArithmetic(SqlWriter writer,
    SqlCall call, SqlKind sqlKind, int leftPrec, int rightPrec) {

  final SqlWriter.Frame frame = writer.startFunCall("DATEADD");
  SqlNode operand = call.operand(1);
  if (operand instanceof SqlIntervalLiteral) {
    //There is no DATESUB method available, so change the sign.
    unparseSqlIntervalLiteralMssql(
        writer, (SqlIntervalLiteral) operand, sqlKind == SqlKind.MINUS ? -1 : 1);
  } else {
    operand.unparse(writer, leftPrec, rightPrec);
  }
  writer.sep(",", true);

  call.operand(0).unparse(writer, leftPrec, rightPrec);
  writer.endList(frame);
}
 
Example 3
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 4
Source File: SqlRollupOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
private void unparseCube(SqlWriter writer, SqlCall call) {
  writer.keyword(call.getOperator().getName());
  final SqlWriter.Frame frame =
      writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")");
  for (SqlNode operand : call.getOperandList()) {
    writer.sep(",");
    if (operand.getKind() == SqlKind.ROW) {
      final SqlWriter.Frame frame2 =
          writer.startList(SqlWriter.FrameTypeEnum.SIMPLE, "(", ")");
      for (SqlNode operand2 : ((SqlCall) operand).getOperandList()) {
        writer.sep(",");
        operand2.unparse(writer, 0, 0);
      }
      writer.endList(frame2);
    } else if (operand instanceof SqlNodeList
        && ((SqlNodeList) operand).size() == 0) {
      writer.keyword("()");
    } else {
      operand.unparse(writer, 0, 0);
    }
  }
  writer.endList(frame);
}
 
Example 5
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 6
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 7
Source File: SqlAlterQuarkDataSource.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("DATASOURCE");
  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 8
Source File: SqlCheckConstraint.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  if (name != null) {
    writer.keyword("CONSTRAINT");
    name.unparse(writer, 0, 0);
  }
  writer.keyword("CHECK");
  if (writer.isAlwaysUseParentheses()) {
    expression.unparse(writer, 0, 0);
  } else {
    writer.sep("(");
    expression.unparse(writer, 0, 0);
    writer.sep(")");
  }
}
 
Example 9
Source File: SqlDotOperator.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.startList(SqlWriter.FrameTypeEnum.IDENTIFIER);
  call.operand(0).unparse(writer, leftPrec, 0);
  writer.sep(".");
  call.operand(1).unparse(writer, 0, 0);
  writer.endList(frame);
}
 
Example 10
Source File: SqlConvertFunction.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("USING");
  call.operand(1).unparse(writer, leftPrec, rightPrec);
  writer.endFunCall(frame);
}
 
Example 11
Source File: SqlJsonQueryFunction.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, 0, 0);
  writer.sep(",", true);
  call.operand(1).unparse(writer, 0, 0);
  final SqlJsonQueryWrapperBehavior wrapperBehavior =
      getEnumValue(call.operand(2));
  switch (wrapperBehavior) {
  case WITHOUT_ARRAY:
    writer.keyword("WITHOUT ARRAY");
    break;
  case WITH_CONDITIONAL_ARRAY:
    writer.keyword("WITH CONDITIONAL ARRAY");
    break;
  case WITH_UNCONDITIONAL_ARRAY:
    writer.keyword("WITH UNCONDITIONAL ARRAY");
    break;
  default:
    throw new IllegalStateException("unreachable code");
  }
  writer.keyword("WRAPPER");
  unparseEmptyOrErrorBehavior(writer, getEnumValue(call.operand(3)));
  writer.keyword("ON EMPTY");
  unparseEmptyOrErrorBehavior(writer, getEnumValue(call.operand(4)));
  writer.keyword("ON ERROR");
  writer.endFunCall(frame);
}
 
Example 12
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 13
Source File: SqlColumnDeclaration.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void exp(SqlWriter writer) {
  if (writer.isAlwaysUseParentheses()) {
    expression.unparse(writer, 0, 0);
  } else {
    writer.sep("(");
    expression.unparse(writer, 0, 0);
    writer.sep(")");
  }
}
 
Example 14
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 15
Source File: SqlBetweenOperator.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) {
  final SqlWriter.Frame frame =
      writer.startList(FRAME_TYPE, "", "");
  call.operand(VALUE_OPERAND).unparse(writer, getLeftPrec(), 0);
  writer.sep(super.getName());
  writer.sep(flag.name());

  // If the expression for the lower bound contains a call to an AND
  // operator, we need to wrap the expression in parentheses to prevent
  // the AND from associating with BETWEEN. For example, we should
  // unparse
  //    a BETWEEN b OR (c AND d) OR e AND f
  // as
  //    a BETWEEN (b OR c AND d) OR e) AND f
  // If it were unparsed as
  //    a BETWEEN b OR c AND d OR e AND f
  // then it would be interpreted as
  //    (a BETWEEN (b OR c) AND d) OR (e AND f)
  // which would be wrong.
  final SqlNode lower = call.operand(LOWER_OPERAND);
  final SqlNode upper = call.operand(UPPER_OPERAND);
  int lowerPrec = new AndFinder().containsAnd(lower) ? 100 : 0;
  lower.unparse(writer, lowerPrec, lowerPrec);
  writer.sep("AND");
  upper.unparse(writer, 0, getRightPrec());
  writer.endList(frame);
}
 
Example 16
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 17
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 18
Source File: ConvSqlWriter.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
    writer.sep("");
}
 
Example 19
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);
  }
}
 
Example 20
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("  ");
}