use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter in project hazelcast by hazelcast.
the class HazelcastJsonQueryFunction method unparse.
@Override
public void unparse(final SqlWriter writer, final SqlCall call, final int leftPrec, final int rightPrec) {
final SqlWriter.Frame frame = writer.startFunCall(this.getName());
call.operand(0).unparse(writer, leftPrec, rightPrec);
writer.sep(",", true);
call.operand(1).unparse(writer, leftPrec, rightPrec);
final SqlJsonQueryWrapperBehavior wrapperBehavior = (SqlJsonQueryWrapperBehavior) ((SqlLiteral) call.operand(2)).getValue();
final SqlJsonQueryEmptyOrErrorBehavior onEmpty = (SqlJsonQueryEmptyOrErrorBehavior) ((SqlLiteral) call.operand(3)).getValue();
final SqlJsonQueryEmptyOrErrorBehavior onError = (SqlJsonQueryEmptyOrErrorBehavior) ((SqlLiteral) call.operand(4)).getValue();
unparseWrapperBehavior(wrapperBehavior, writer);
unparseEmptyOrErrorBehavior(onEmpty, writer);
writer.keyword("ON EMPTY");
unparseEmptyOrErrorBehavior(onError, writer);
writer.keyword("ON ERROR");
writer.endFunCall(frame);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter in project hazelcast by hazelcast.
the class HazelcastCaseOperator method unparse.
@Override
public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
assert call instanceof HazelcastSqlCase;
final SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.CASE, "CASE", "END");
HazelcastSqlCase sqlCase = (HazelcastSqlCase) call;
SqlNodeList whenList = sqlCase.getWhenOperands();
SqlNodeList thenList = sqlCase.getThenOperands();
assert whenList.size() == thenList.size();
for (Pair<SqlNode, SqlNode> pair : Pair.zip(whenList, thenList)) {
writer.sep("WHEN");
pair.left.unparse(writer, 0, 0);
writer.sep("THEN");
pair.right.unparse(writer, 0, 0);
}
writer.sep("ELSE");
SqlNode elseExpr = sqlCase.getElseOperand();
elseExpr.unparse(writer, 0, 0);
writer.endList(frame);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter in project beam by apache.
the class BeamBigQuerySqlDialect method unparseExtractFunctions.
private void unparseExtractFunctions(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
String funName = call.getOperator().getName();
int operandCount = call.operandCount();
SqlNode tz = null;
final SqlWriter.Frame frame = writer.startFunCall("EXTRACT");
if (!funName.equals("$extract") && (operandCount == 1 || operandCount == 2)) {
// EXTRACT(DATE/TIME/DATETIME FROM timestamp_expression [AT TIME ZONE tz])
// operand0: timestamp_expression
// operand1: tz (optional)
writer.literal(EXTRACT_FUNCTIONS.get(funName));
if (operandCount == 2) {
tz = call.operand(1);
}
} else if (funName.equals("$extract") && (operandCount == 2 || operandCount == 3)) {
// EXTRACT(date_part FROM timestamp_expression [AT TIME ZONE tz])
// operand0: timestamp_expression
// operand1: date_part
// operand2: tz (optional)
call.operand(1).unparse(writer, leftPrec, rightPrec);
if (operandCount == 3) {
tz = call.operand(2);
}
} else {
throw new IllegalArgumentException(String.format("Unable to unparse %s with %d operands.", funName, operandCount));
}
writer.literal("FROM");
call.operand(0).unparse(writer, leftPrec, rightPrec);
if (tz != null) {
writer.literal("AT TIME ZONE");
tz.unparse(writer, leftPrec, rightPrec);
}
writer.endFunCall(frame);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter in project calcite by apache.
the class CassandraSchema method addMaterializedViews.
/**
* Add all materialized views defined in the schema to this column family
*/
private void addMaterializedViews() {
// Close the hook use to get us here
hook.close();
for (MaterializedViewMetadata view : getKeyspace().getMaterializedViews()) {
String tableName = view.getBaseTable().getName();
StringBuilder queryBuilder = new StringBuilder("SELECT ");
// Add all the selected columns to the query
List<String> columnNames = new ArrayList<String>();
for (ColumnMetadata column : view.getColumns()) {
columnNames.add("\"" + column.getName() + "\"");
}
queryBuilder.append(Util.toString(columnNames, "", ", ", ""));
queryBuilder.append(" FROM \"" + tableName + "\"");
// Get the where clause from the system schema
String whereQuery = "SELECT where_clause from system_schema.views " + "WHERE keyspace_name='" + keyspace + "' AND view_name='" + view.getName() + "'";
queryBuilder.append(" WHERE " + session.execute(whereQuery).one().getString(0));
// Parse and unparse the view query to get properly quoted field names
String query = queryBuilder.toString();
SqlParser.ConfigBuilder configBuilder = SqlParser.configBuilder();
configBuilder.setUnquotedCasing(Casing.UNCHANGED);
SqlSelect parsedQuery;
try {
parsedQuery = (SqlSelect) SqlParser.create(query, configBuilder.build()).parseQuery();
} catch (SqlParseException e) {
LOGGER.warn("Could not parse query {} for CQL view {}.{}", query, keyspace, view.getName());
continue;
}
StringWriter stringWriter = new StringWriter(query.length());
PrintWriter printWriter = new PrintWriter(stringWriter);
SqlWriter writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, true, printWriter);
parsedQuery.unparse(writer, 0, 0);
query = stringWriter.toString();
// Add the view for this query
String viewName = "$" + getTableNames().size();
SchemaPlus schema = parentSchema.getSubSchema(name);
CalciteSchema calciteSchema = CalciteSchema.from(schema);
List<String> viewPath = calciteSchema.path(viewName);
schema.add(viewName, MaterializedViewTable.create(calciteSchema, query, null, viewPath, view.getName(), true));
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlWriter in project calcite by apache.
the class SqlCreateTable method unparse.
@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);
}
}
Aggregations