use of org.apache.calcite.sql.SqlDialect in project calcite by apache.
the class JdbcSchema method create.
public static JdbcSchema create(SchemaPlus parentSchema, String name, DataSource dataSource, SqlDialectFactory dialectFactory, String catalog, String schema) {
final Expression expression = Schemas.subSchemaExpression(parentSchema, name, JdbcSchema.class);
final SqlDialect dialect = createDialect(dialectFactory, dataSource);
final JdbcConvention convention = JdbcConvention.of(dialect, expression, name);
return new JdbcSchema(dataSource, dialect, convention, catalog, schema);
}
use of org.apache.calcite.sql.SqlDialect in project calcite by apache.
the class IntervalSqlType method generateTypeString.
// ~ Methods ----------------------------------------------------------------
protected void generateTypeString(StringBuilder sb, boolean withDetail) {
sb.append("INTERVAL ");
final SqlDialect dialect = AnsiSqlDialect.DEFAULT;
final SqlPrettyWriter writer = new SqlPrettyWriter(dialect);
writer.setAlwaysUseParentheses(false);
writer.setSelectListItemsOnSeparateLines(false);
writer.setIndentation(0);
intervalQualifier.unparse(writer, 0, 0);
final String sql = writer.toString();
sb.append(new SqlString(dialect, sql).getSql());
}
use of org.apache.calcite.sql.SqlDialect in project calcite by apache.
the class PlannerTest method testHiveDialect.
/**
* Tests that Hive dialect does not generate "AS".
*/
@Test
public void testHiveDialect() throws SqlParseException {
Planner planner = getPlanner(null);
SqlNode parse = planner.parse("select * from (select * from \"emps\") as t\n" + "where \"name\" like '%e%'");
final SqlDialect hiveDialect = SqlDialect.DatabaseProduct.HIVE.getDialect();
assertThat(Util.toLinux(parse.toSqlString(hiveDialect).getSql()), equalTo("SELECT *\n" + "FROM (SELECT *\n" + "FROM emps) T\n" + "WHERE name LIKE '%e%'"));
}
use of org.apache.calcite.sql.SqlDialect in project drill by apache.
the class ClickhouseJdbcDialect method generateSql.
@Override
public String generateSql(RelOptCluster cluster, RelNode input) {
final SqlDialect dialect = plugin.getDialect();
final JdbcImplementor jdbcImplementor = new ClickhouseJdbcImplementor(dialect, (JavaTypeFactory) cluster.getTypeFactory());
final JdbcImplementor.Result result = jdbcImplementor.visitChild(0, input.accept(SubsetRemover.INSTANCE));
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();
}
Aggregations