use of org.apache.calcite.sql.SqlDialect in project hive by apache.
the class HiveJdbcConverter method generateSql.
public String generateSql() {
SqlDialect dialect = getJdbcDialect();
final HiveJdbcImplementor jdbcImplementor = new HiveJdbcImplementor(dialect, (JavaTypeFactory) getCluster().getTypeFactory());
Project topProject;
if (getInput() instanceof Project) {
topProject = (Project) getInput();
} else {
// If it is not a project operator, we add it on top of the input
// to force generating the column names instead of * while
// translating to SQL
RelNode nodeToTranslate = getInput();
RexBuilder builder = getCluster().getRexBuilder();
List<RexNode> projects = new ArrayList<>(nodeToTranslate.getRowType().getFieldList().size());
for (int i = 0; i < nodeToTranslate.getRowType().getFieldCount(); i++) {
projects.add(builder.makeInputRef(nodeToTranslate, i));
}
topProject = new JdbcProject(nodeToTranslate.getCluster(), nodeToTranslate.getTraitSet(), nodeToTranslate, projects, nodeToTranslate.getRowType());
}
final HiveJdbcImplementor.Result result = jdbcImplementor.visitRoot(topProject);
return result.asStatement().toSqlString(dialect).getSql();
}
use of org.apache.calcite.sql.SqlDialect in project calcite by apache.
the class Lattice method sql.
/**
* Generates a SQL query to populate a tile of the lattice specified by a
* given set of columns and measures, optionally grouping.
*/
public String sql(ImmutableBitSet groupSet, boolean group, List<Measure> aggCallList) {
final List<Node> usedNodes = new ArrayList<>();
if (group) {
final ImmutableBitSet.Builder columnSetBuilder = groupSet.rebuild();
for (Measure call : aggCallList) {
for (Column arg : call.args) {
columnSetBuilder.set(arg.ordinal);
}
}
final ImmutableBitSet columnSet = columnSetBuilder.build();
// or if has a child whose columns are used.
for (Node node : nodes) {
if (ImmutableBitSet.range(node.startCol, node.endCol).intersects(columnSet)) {
use(usedNodes, node);
}
}
if (usedNodes.isEmpty()) {
usedNodes.add(nodes.get(0));
}
} else {
usedNodes.addAll(nodes);
}
final SqlDialect dialect = SqlDialect.DatabaseProduct.CALCITE.getDialect();
final StringBuilder buf = new StringBuilder("SELECT ");
final StringBuilder groupBuf = new StringBuilder("\nGROUP BY ");
int k = 0;
final Set<String> columnNames = Sets.newHashSet();
if (groupSet != null) {
for (int i : groupSet) {
if (k++ > 0) {
buf.append(", ");
groupBuf.append(", ");
}
final Column column = columns.get(i);
dialect.quoteIdentifier(buf, column.identifiers());
dialect.quoteIdentifier(groupBuf, column.identifiers());
final String fieldName = uniqueColumnNames.get(i);
columnNames.add(fieldName);
if (!column.alias.equals(fieldName)) {
buf.append(" AS ");
dialect.quoteIdentifier(buf, fieldName);
}
}
if (groupSet.isEmpty()) {
groupBuf.append("()");
}
int m = 0;
for (Measure measure : aggCallList) {
if (k++ > 0) {
buf.append(", ");
}
buf.append(measure.agg.getName()).append("(");
if (measure.args.isEmpty()) {
buf.append("*");
} else {
int z = 0;
for (Column arg : measure.args) {
if (z++ > 0) {
buf.append(", ");
}
dialect.quoteIdentifier(buf, arg.identifiers());
}
}
buf.append(") AS ");
String measureName;
while (!columnNames.add(measureName = "m" + m)) {
++m;
}
dialect.quoteIdentifier(buf, measureName);
}
} else {
buf.append("*");
}
buf.append("\nFROM ");
for (Node node : usedNodes) {
if (node.parent != null) {
buf.append("\nJOIN ");
}
dialect.quoteIdentifier(buf, node.scan.getTable().getQualifiedName());
buf.append(" AS ");
dialect.quoteIdentifier(buf, node.alias);
if (node.parent != null) {
buf.append(" ON ");
k = 0;
for (IntPair pair : node.link) {
if (k++ > 0) {
buf.append(" AND ");
}
final Column left = columns.get(node.parent.startCol + pair.source);
dialect.quoteIdentifier(buf, left.identifiers());
buf.append(" = ");
final Column right = columns.get(node.startCol + pair.target);
dialect.quoteIdentifier(buf, right.identifiers());
}
}
}
if (CalcitePrepareImpl.DEBUG) {
System.out.println("Lattice SQL:\n" + buf);
}
if (group) {
buf.append(groupBuf);
}
return buf.toString();
}
use of org.apache.calcite.sql.SqlDialect in project flink by apache.
the class SqlToOperationConverter method getQuotedSqlString.
private String getQuotedSqlString(SqlNode sqlNode) {
SqlParser.Config parserConfig = flinkPlanner.config().getParserConfig();
SqlDialect dialect = new CalciteSqlDialect(SqlDialect.EMPTY_CONTEXT.withQuotedCasing(parserConfig.unquotedCasing()).withConformance(parserConfig.conformance()).withUnquotedCasing(parserConfig.unquotedCasing()).withIdentifierQuoteString(parserConfig.quoting().string));
return sqlNode.toSqlString(dialect).getSql();
}
use of org.apache.calcite.sql.SqlDialect in project drill by apache.
the class DefaultJdbcDialect method generateSql.
@Override
public String generateSql(RelOptCluster cluster, RelNode input) {
final SqlDialect dialect = plugin.getDialect();
final JdbcImplementor jdbcImplementor = new JdbcImplementor(dialect, (JavaTypeFactory) cluster.getTypeFactory());
final JdbcImplementor.Result result = jdbcImplementor.visitChild(0, input.accept(SubsetRemover.INSTANCE));
return result.asStatement().toSqlString(dialect).getSql();
}
Aggregations