Search in sources :

Example 1 with SqlDialect

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);
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) SqlDialect(org.apache.calcite.sql.SqlDialect)

Example 2 with SqlDialect

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());
}
Also used : SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) AnsiSqlDialect(org.apache.calcite.sql.dialect.AnsiSqlDialect) SqlDialect(org.apache.calcite.sql.SqlDialect) SqlString(org.apache.calcite.sql.util.SqlString) SqlString(org.apache.calcite.sql.util.SqlString)

Example 3 with SqlDialect

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%'"));
}
Also used : SqlDialect(org.apache.calcite.sql.SqlDialect) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Example 4 with SqlDialect

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();
}
Also used : JdbcImplementor(org.apache.calcite.adapter.jdbc.JdbcImplementor) SqlDialect(org.apache.calcite.sql.SqlDialect)

Example 5 with SqlDialect

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();
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode) RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) IntPair(org.apache.calcite.util.mapping.IntPair) SqlDialect(org.apache.calcite.sql.SqlDialect)

Aggregations

SqlDialect (org.apache.calcite.sql.SqlDialect)9 JdbcImplementor (org.apache.calcite.adapter.jdbc.JdbcImplementor)3 ArrayList (java.util.ArrayList)2 RelNode (org.apache.calcite.rel.RelNode)2 RexNode (org.apache.calcite.rex.RexNode)2 SqlNode (org.apache.calcite.sql.SqlNode)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 JdbcProject (org.apache.calcite.adapter.jdbc.JdbcRules.JdbcProject)1 NullCollation (org.apache.calcite.config.NullCollation)1 Expression (org.apache.calcite.linq4j.tree.Expression)1 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)1 Project (org.apache.calcite.rel.core.Project)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1 AnsiSqlDialect (org.apache.calcite.sql.dialect.AnsiSqlDialect)1 CalciteSqlDialect (org.apache.calcite.sql.dialect.CalciteSqlDialect)1 HiveSqlDialect (org.apache.calcite.sql.dialect.HiveSqlDialect)1 SqlParser (org.apache.calcite.sql.parser.SqlParser)1 SqlPrettyWriter (org.apache.calcite.sql.pretty.SqlPrettyWriter)1 SqlString (org.apache.calcite.sql.util.SqlString)1