use of org.apache.calcite.sql.SqlWriterConfig in project calcite by apache.
the class PigConverter method pigToSql.
/**
* Converts a Pig script to a list of SQL statements.
*
* @param pigQuery Pig script
* @param sqlDialect Dialect of SQL language
* @throws IOException Exception during parsing or translating Pig
*/
public List<String> pigToSql(String pigQuery, SqlDialect sqlDialect) throws IOException {
final SqlWriterConfig config = SqlPrettyWriter.config().withQuoteAllIdentifiers(false).withAlwaysUseParentheses(false).withSelectListItemsOnSeparateLines(false).withIndentation(2).withDialect(sqlDialect);
final SqlPrettyWriter writer = new SqlPrettyWriter(config);
return pigToSql(pigQuery, writer);
}
use of org.apache.calcite.sql.SqlWriterConfig in project calcite by apache.
the class ServerDdlExecutor method populate.
/**
* 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().prepareStatement(r.rel);
int rowCount = prepare.executeUpdate();
Util.discard(rowCount);
prepare.close();
} catch (SqlParseException | ValidationException | RelConversionException | SQLException e) {
throw Util.throwAsRuntime(e);
}
}
use of org.apache.calcite.sql.SqlWriterConfig in project calcite by apache.
the class CassandraSchema method addMaterializedViews.
/**
* Adds all materialized views defined in the schema to this column family.
*/
private void addMaterializedViews() {
// Close the hook used to get us here
hook.close();
for (ViewMetadata view : getKeyspace().getViews().values()) {
String tableName = view.getBaseTable().asInternal();
StringBuilder queryBuilder = new StringBuilder("SELECT ");
// Add all the selected columns to the query
String columnsList = view.getColumns().values().stream().map(c -> c.getName().asInternal()).collect(Collectors.joining(", "));
queryBuilder.append(columnsList);
queryBuilder.append(" FROM ").append(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().asInternal() + "'";
Row whereClauseRow = Objects.requireNonNull(session.execute(whereQuery).one());
queryBuilder.append(" WHERE ").append(whereClauseRow.getString(0));
// Parse and unparse the view query to get properly quoted field names
String query = queryBuilder.toString();
SqlParser.Config parserConfig = SqlParser.config().withUnquotedCasing(Casing.UNCHANGED);
SqlSelect parsedQuery;
try {
parsedQuery = (SqlSelect) SqlParser.create(query, parserConfig).parseQuery();
} catch (SqlParseException e) {
LOGGER.warn("Could not parse query {} for CQL view {}.{}", query, keyspace, view.getName().asInternal());
continue;
}
final StringBuilder buf = new StringBuilder(query.length());
final SqlWriterConfig config = SqlPrettyWriter.config().withAlwaysUseParentheses(true);
final SqlWriter writer = new SqlPrettyWriter(config, buf);
parsedQuery.unparse(writer, 0, 0);
query = buf.toString();
// Add the view for this query
String viewName = "$" + getTableNames().size();
SchemaPlus schema = parentSchema.getSubSchema(name);
if (schema == null) {
throw new IllegalStateException("Cannot find schema " + name + " in parent schema " + parentSchema.getName());
}
CalciteSchema calciteSchema = CalciteSchema.from(schema);
List<String> viewPath = calciteSchema.path(viewName);
schema.add(viewName, MaterializedViewTable.create(calciteSchema, query, null, viewPath, view.getName().asInternal(), true));
}
}
Aggregations