Search in sources :

Example 1 with JdbcImplementor

use of org.apache.calcite.adapter.jdbc.JdbcImplementor in project calcite by apache.

the class JdbcToSparkConverter method generateSql.

private String generateSql(SqlDialect dialect) {
    final JdbcImplementor jdbcImplementor = new JdbcImplementor(dialect, (JavaTypeFactory) getCluster().getTypeFactory());
    final JdbcImplementor.Result result = jdbcImplementor.visitChild(0, getInput());
    return result.asStatement().toSqlString(dialect).getSql();
}
Also used : JdbcImplementor(org.apache.calcite.adapter.jdbc.JdbcImplementor)

Example 2 with JdbcImplementor

use of org.apache.calcite.adapter.jdbc.JdbcImplementor 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 3 with JdbcImplementor

use of org.apache.calcite.adapter.jdbc.JdbcImplementor 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)

Example 4 with JdbcImplementor

use of org.apache.calcite.adapter.jdbc.JdbcImplementor in project hive by apache.

the class CalcitePlanner method getOptimizedSql.

/**
 * Get SQL rewrite for a Calcite logical plan
 *
 * @return Optimized SQL text (or null, if failed)
 */
public String getOptimizedSql(RelNode optimizedOptiqPlan) {
    boolean nullsLast = HiveConf.getBoolVar(conf, ConfVars.HIVE_DEFAULT_NULLS_LAST);
    NullCollation nullCollation = nullsLast ? NullCollation.LAST : NullCollation.LOW;
    SqlDialect dialect = new HiveSqlDialect(SqlDialect.EMPTY_CONTEXT.withDatabaseProduct(SqlDialect.DatabaseProduct.HIVE).withDatabaseMajorVersion(// TODO: should not be hardcoded
    4).withDatabaseMinorVersion(0).withIdentifierQuoteString("`").withDataTypeSystem(new HiveTypeSystemImpl()).withNullCollation(nullCollation)) {

        @Override
        protected boolean allowsAs() {
            return true;
        }

        @Override
        public boolean supportsCharSet() {
            return false;
        }
    };
    try {
        final JdbcImplementor jdbcImplementor = new JdbcImplementor(dialect, (JavaTypeFactory) optimizedOptiqPlan.getCluster().getTypeFactory());
        final JdbcImplementor.Result result = jdbcImplementor.visitRoot(optimizedOptiqPlan);
        String sql = result.asStatement().toSqlString(dialect).getSql();
        // VARCHAR(INTEGER.MAX) -> STRING
        sql = PATTERN_VARCHAR.matcher(sql).replaceAll("STRING");
        // TIMESTAMP(9) -> TIMESTAMP
        sql = PATTERN_TIMESTAMP.matcher(sql).replaceAll("TIMESTAMP");
        return sql;
    } catch (Error | Exception e) {
        // We play it safe here. If we get an error or exception,
        // we will simply not print the optimized SQL.
        LOG.warn("Rel2SQL Rewrite threw error", e);
    }
    return null;
}
Also used : HiveSqlDialect(org.apache.calcite.sql.dialect.HiveSqlDialect) JdbcImplementor(org.apache.calcite.adapter.jdbc.JdbcImplementor) SqlDialect(org.apache.calcite.sql.SqlDialect) HiveSqlDialect(org.apache.calcite.sql.dialect.HiveSqlDialect) NullCollation(org.apache.calcite.config.NullCollation) CalciteSubquerySemanticException(org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSubquerySemanticException) IOException(java.io.IOException) CalciteSemanticException(org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException) CalciteViewSemanticException(org.apache.hadoop.hive.ql.optimizer.calcite.CalciteViewSemanticException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ReCompileException(org.apache.hadoop.hive.ql.reexec.ReCompileException) HiveTypeSystemImpl(org.apache.hadoop.hive.ql.optimizer.calcite.HiveTypeSystemImpl)

Aggregations

JdbcImplementor (org.apache.calcite.adapter.jdbc.JdbcImplementor)4 SqlDialect (org.apache.calcite.sql.SqlDialect)3 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 NullCollation (org.apache.calcite.config.NullCollation)1 HiveSqlDialect (org.apache.calcite.sql.dialect.HiveSqlDialect)1 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)1 CalciteSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)1 CalciteSubquerySemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSubquerySemanticException)1 CalciteViewSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteViewSemanticException)1 HiveTypeSystemImpl (org.apache.hadoop.hive.ql.optimizer.calcite.HiveTypeSystemImpl)1 ReCompileException (org.apache.hadoop.hive.ql.reexec.ReCompileException)1