Java Code Examples for org.apache.calcite.sql.SqlNode#unparse()

The following examples show how to use org.apache.calcite.sql.SqlNode#unparse() . 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: ConvSqlWriter.java    From kylin with Apache License 2.0 6 votes vote down vote up
private void doWriteLimitOffset(SqlNode fetch, SqlNode offset) {
    // Dialect does not support OFFSET/FETCH clause.
    // Assume it uses LIMIT/OFFSET.
    if (fetch != null) {
        this.newlineAndIndent();
        final Frame fetchFrame = this.startList(FrameTypeEnum.FETCH);
        this.keyword("LIMIT");
        fetch.unparse(this, -1, -1);
        this.endList(fetchFrame);
    }
    if (offset != null) {
        this.newlineAndIndent();
        final Frame offsetFrame = this.startList(FrameTypeEnum.OFFSET);
        this.keyword("OFFSET");
        offset.unparse(this, -1, -1);
        this.endList(offsetFrame);
    }
}
 
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: 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");
  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);
  }
  if (query != null) {
    writer.keyword("AS");
    writer.newlineAndIndent();
    query.unparse(writer, 0, 0);
  }
}
 
Example 4
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 5
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 6
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 7
Source File: ClickHouseSqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public void unparseOffsetFetch(SqlWriter writer, SqlNode offset,
    SqlNode fetch) {
  Preconditions.checkArgument(fetch != null);

  writer.newlineAndIndent();
  final SqlWriter.Frame frame =
      writer.startList(SqlWriter.FrameTypeEnum.FETCH);
  writer.keyword("LIMIT");

  if (offset != null) {
    offset.unparse(writer, -1, -1);
    writer.sep(",", true);
  }

  fetch.unparse(writer, -1, -1);
  writer.endList(frame);
}
 
Example 8
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 9
Source File: SqlRollupOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static 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 10
Source File: SqlColumnListConstructor.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) {
  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 11
Source File: SqlTranslate3Function.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.startFunCall("TRANSLATE");
  for (SqlNode sqlNode : call.getOperandList()) {
    writer.sep(",");
    sqlNode.unparse(writer, leftPrec, rightPrec);
  }
  writer.endFunCall(frame);
}
 
Example 12
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 13
Source File: ConvSqlWriter.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public void writeWith(SqlCall call, int leftPrec, int rightPrec) {
    final SqlWith with = (SqlWith) call;
    final SqlWriter.Frame frame = this.startList(SqlWriter.FrameTypeEnum.WITH, "WITH", "");
    for (SqlNode node : with.withList) {
        this.sep(",");
        node.unparse(this, 0, 0);
    }
    with.body.unparse(this, 100, 100);
    this.endList(frame);
}
 
Example 14
Source File: SqlRollupOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void unparseKeyword(SqlWriter writer, SqlCall call, String keyword) {
  final SqlWriter.Frame groupFrame =
      writer.startList(SqlWriter.FrameTypeEnum.GROUP_BY_LIST);
  for (SqlNode operand : call.getOperandList()) {
    writer.sep(",");
    operand.unparse(writer, 2, 3);
  }
  writer.endList(groupFrame);
  writer.keyword(keyword);
}
 
Example 15
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Populates the table called {@code name} by executing {@code query}. */
static void populate(SqlIdentifier name, SqlNode query,
    CalcitePrepare.Context context) {
  // Generate, prepare and execute an "INSERT INTO table query" statement.
  // (It's a bit inefficient that we convert from SqlNode to SQL and back
  // again.)
  final FrameworkConfig config = Frameworks.newConfigBuilder()
      .defaultSchema(context.getRootSchema().plus())
      .build();
  final Planner planner = Frameworks.getPlanner(config);
  try {
    final StringBuilder buf = new StringBuilder();
    final SqlWriterConfig writerConfig =
        SqlPrettyWriter.config().withAlwaysUseParentheses(false);
    final SqlPrettyWriter w = new SqlPrettyWriter(writerConfig, buf);
    buf.append("INSERT INTO ");
    name.unparse(w, 0, 0);
    buf.append(' ');
    query.unparse(w, 0, 0);
    final String sql = buf.toString();
    final SqlNode query1 = planner.parse(sql);
    final SqlNode query2 = planner.validate(query1);
    final RelRoot r = planner.rel(query2);
    final PreparedStatement prepare = context.getRelRunner().prepare(r.rel);
    int rowCount = prepare.executeUpdate();
    Util.discard(rowCount);
    prepare.close();
  } catch (SqlParseException | ValidationException
      | RelConversionException | SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example 16
Source File: SqlBetweenOperator.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.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 17
Source File: SqlTranslate3Function.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("TRANSLATE");
  for (SqlNode sqlNode : call.getOperandList()) {
    writer.sep(",");
    sqlNode.unparse(writer, leftPrec, rightPrec);
  }
  writer.endFunCall(frame);
}
 
Example 18
Source File: SybaseSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void unparseTopN(SqlWriter writer, SqlNode offset,
    SqlNode fetch) {
  // Parentheses are not required, but we use them to be consistent with
  // Microsoft SQL Server, which recommends them but does not require them.
  //
  // Note that "fetch" is ignored.
  writer.keyword("TOP");
  writer.keyword("(");
  fetch.unparse(writer, -1, -1);
  writer.keyword(")");
}
 
Example 19
Source File: ConvSqlWriter.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public void writeWith(SqlCall call, int leftPrec, int rightPrec) {
    final SqlWith with = (SqlWith) call;
    final SqlWriter.Frame frame = this.startList(SqlWriter.FrameTypeEnum.WITH, "WITH", "");
    for (SqlNode node : with.withList) {
        this.sep(",");
        node.unparse(this, 0, 0);
    }
    with.body.unparse(this, 100, 100);
    this.endList(frame);
}
 
Example 20
Source File: UnparseUtil.java    From AthenaX with Apache License 2.0 4 votes vote down vote up
UnparseUtil node(SqlNode n) {
  n.unparse(writer, leftPrec, rightPrec);
  return this;
}