Search in sources :

Example 6 with SqlDialect

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();
}
Also used : Project(org.apache.calcite.rel.core.Project) JdbcProject(org.apache.calcite.adapter.jdbc.JdbcRules.JdbcProject) HiveJdbcImplementor(org.apache.hadoop.hive.ql.optimizer.calcite.rules.jdbc.HiveJdbcImplementor) HiveRelNode(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveRelNode) RelNode(org.apache.calcite.rel.RelNode) JdbcProject(org.apache.calcite.adapter.jdbc.JdbcRules.JdbcProject) ArrayList(java.util.ArrayList) SqlDialect(org.apache.calcite.sql.SqlDialect) RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 7 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)

Example 8 with SqlDialect

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();
}
Also used : CalciteSqlDialect(org.apache.calcite.sql.dialect.CalciteSqlDialect) SqlParser(org.apache.calcite.sql.parser.SqlParser) CalciteSqlDialect(org.apache.calcite.sql.dialect.CalciteSqlDialect) SqlDialect(org.apache.calcite.sql.SqlDialect)

Example 9 with SqlDialect

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