Search in sources :

Example 1 with HiveSqlDialect

use of org.apache.calcite.sql.dialect.HiveSqlDialect in project calcite by apache.

the class RelToSqlConverterTest method testHiveSelectQueryWithOrderByDescAndHighNullsWithVersion20.

@Test
public void testHiveSelectQueryWithOrderByDescAndHighNullsWithVersion20() {
    final HiveSqlDialect hive2_1_0_Dialect = new HiveSqlDialect(SqlDialect.EMPTY_CONTEXT.withDatabaseMajorVersion(2).withDatabaseMinorVersion(0).withNullCollation(NullCollation.LOW));
    final String query = "select \"product_id\" from \"product\"\n" + "order by \"product_id\" desc nulls first";
    final String expected = "SELECT product_id\n" + "FROM foodmart.product\n" + "ORDER BY product_id IS NULL DESC, product_id DESC";
    sql(query).dialect(hive2_1_0_Dialect).ok(expected);
}
Also used : HiveSqlDialect(org.apache.calcite.sql.dialect.HiveSqlDialect) Test(org.junit.Test)

Example 2 with HiveSqlDialect

use of org.apache.calcite.sql.dialect.HiveSqlDialect in project calcite by apache.

the class RelToSqlConverterTest method testHiveSelectQueryWithOrderByDescAndHighNullsWithVersionGreaterThanOrEq21.

@Test
public void testHiveSelectQueryWithOrderByDescAndHighNullsWithVersionGreaterThanOrEq21() {
    final HiveSqlDialect hive2_1Dialect = new HiveSqlDialect(SqlDialect.EMPTY_CONTEXT.withDatabaseMajorVersion(2).withDatabaseMinorVersion(1).withNullCollation(NullCollation.LOW));
    final HiveSqlDialect hive2_2_Dialect = new HiveSqlDialect(SqlDialect.EMPTY_CONTEXT.withDatabaseMajorVersion(2).withDatabaseMinorVersion(2).withNullCollation(NullCollation.LOW));
    final String query = "select \"product_id\" from \"product\"\n" + "order by \"product_id\" desc nulls first";
    final String expected = "SELECT product_id\n" + "FROM foodmart.product\n" + "ORDER BY product_id DESC NULLS FIRST";
    sql(query).dialect(hive2_1Dialect).ok(expected);
    sql(query).dialect(hive2_2_Dialect).ok(expected);
}
Also used : HiveSqlDialect(org.apache.calcite.sql.dialect.HiveSqlDialect) Test(org.junit.Test)

Example 3 with HiveSqlDialect

use of org.apache.calcite.sql.dialect.HiveSqlDialect in project calcite by apache.

the class SqlDialectFactoryImpl method create.

public SqlDialect create(DatabaseMetaData databaseMetaData) {
    String databaseProductName;
    int databaseMajorVersion;
    int databaseMinorVersion;
    String databaseVersion;
    try {
        databaseProductName = databaseMetaData.getDatabaseProductName();
        databaseMajorVersion = databaseMetaData.getDatabaseMajorVersion();
        databaseMinorVersion = databaseMetaData.getDatabaseMinorVersion();
        databaseVersion = databaseMetaData.getDatabaseProductVersion();
    } catch (SQLException e) {
        throw new RuntimeException("while detecting database product", e);
    }
    final String upperProductName = databaseProductName.toUpperCase(Locale.ROOT).trim();
    final String quoteString = getIdentifierQuoteString(databaseMetaData);
    final NullCollation nullCollation = getNullCollation(databaseMetaData);
    final SqlDialect.Context c = SqlDialect.EMPTY_CONTEXT.withDatabaseProductName(databaseProductName).withDatabaseMajorVersion(databaseMajorVersion).withDatabaseMinorVersion(databaseMinorVersion).withDatabaseVersion(databaseVersion).withIdentifierQuoteString(quoteString).withNullCollation(nullCollation);
    switch(upperProductName) {
        case "ACCESS":
            return new AccessSqlDialect(c);
        case "APACHE DERBY":
            return new DerbySqlDialect(c);
        case "DBMS:CLOUDSCAPE":
            return new DerbySqlDialect(c);
        case "HIVE":
            return new HiveSqlDialect(c);
        case "INGRES":
            return new IngresSqlDialect(c);
        case "INTERBASE":
            return new InterbaseSqlDialect(c);
        case "JETHRODATA":
            return new JethroDataSqlDialect(c.withJethroInfo(jethroCache.get(databaseMetaData)));
        case "LUCIDDB":
            return new LucidDbSqlDialect(c);
        case "ORACLE":
            return new OracleSqlDialect(c);
        case "PHOENIX":
            return new PhoenixSqlDialect(c);
        case "MYSQL (INFOBRIGHT)":
            return new InfobrightSqlDialect(c);
        case "MYSQL":
            return new MysqlSqlDialect(c);
        case "REDSHIFT":
            return new RedshiftSqlDialect(c);
    }
    // Now the fuzzy matches.
    if (databaseProductName.startsWith("DB2")) {
        return new Db2SqlDialect(c);
    } else if (upperProductName.contains("FIREBIRD")) {
        return new FirebirdSqlDialect(c);
    } else if (databaseProductName.startsWith("Informix")) {
        return new InformixSqlDialect(c);
    } else if (upperProductName.contains("NETEZZA")) {
        return new NetezzaSqlDialect(c);
    } else if (upperProductName.contains("PARACCEL")) {
        return new ParaccelSqlDialect(c);
    } else if (databaseProductName.startsWith("HP Neoview")) {
        return new NeoviewSqlDialect(c);
    } else if (upperProductName.contains("POSTGRE")) {
        return new PostgresqlSqlDialect(c);
    } else if (upperProductName.contains("SQL SERVER")) {
        return new MssqlSqlDialect(c);
    } else if (upperProductName.contains("SYBASE")) {
        return new SybaseSqlDialect(c);
    } else if (upperProductName.contains("TERADATA")) {
        return new TeradataSqlDialect(c);
    } else if (upperProductName.contains("HSQL")) {
        return new HsqldbSqlDialect(c);
    } else if (upperProductName.contains("H2")) {
        return new H2SqlDialect(c);
    } else if (upperProductName.contains("VERTICA")) {
        return new VerticaSqlDialect(c);
    } else {
        return new AnsiSqlDialect(c);
    }
}
Also used : H2SqlDialect(org.apache.calcite.sql.dialect.H2SqlDialect) SQLException(java.sql.SQLException) NeoviewSqlDialect(org.apache.calcite.sql.dialect.NeoviewSqlDialect) InformixSqlDialect(org.apache.calcite.sql.dialect.InformixSqlDialect) TeradataSqlDialect(org.apache.calcite.sql.dialect.TeradataSqlDialect) HsqldbSqlDialect(org.apache.calcite.sql.dialect.HsqldbSqlDialect) OracleSqlDialect(org.apache.calcite.sql.dialect.OracleSqlDialect) InterbaseSqlDialect(org.apache.calcite.sql.dialect.InterbaseSqlDialect) AccessSqlDialect(org.apache.calcite.sql.dialect.AccessSqlDialect) SybaseSqlDialect(org.apache.calcite.sql.dialect.SybaseSqlDialect) LucidDbSqlDialect(org.apache.calcite.sql.dialect.LucidDbSqlDialect) IngresSqlDialect(org.apache.calcite.sql.dialect.IngresSqlDialect) JethroDataSqlDialect(org.apache.calcite.sql.dialect.JethroDataSqlDialect) AnsiSqlDialect(org.apache.calcite.sql.dialect.AnsiSqlDialect) MssqlSqlDialect(org.apache.calcite.sql.dialect.MssqlSqlDialect) RedshiftSqlDialect(org.apache.calcite.sql.dialect.RedshiftSqlDialect) VerticaSqlDialect(org.apache.calcite.sql.dialect.VerticaSqlDialect) FirebirdSqlDialect(org.apache.calcite.sql.dialect.FirebirdSqlDialect) InfobrightSqlDialect(org.apache.calcite.sql.dialect.InfobrightSqlDialect) MysqlSqlDialect(org.apache.calcite.sql.dialect.MysqlSqlDialect) NetezzaSqlDialect(org.apache.calcite.sql.dialect.NetezzaSqlDialect) ParaccelSqlDialect(org.apache.calcite.sql.dialect.ParaccelSqlDialect) PostgresqlSqlDialect(org.apache.calcite.sql.dialect.PostgresqlSqlDialect) HiveSqlDialect(org.apache.calcite.sql.dialect.HiveSqlDialect) Db2SqlDialect(org.apache.calcite.sql.dialect.Db2SqlDialect) AnsiSqlDialect(org.apache.calcite.sql.dialect.AnsiSqlDialect) H2SqlDialect(org.apache.calcite.sql.dialect.H2SqlDialect) DerbySqlDialect(org.apache.calcite.sql.dialect.DerbySqlDialect) SybaseSqlDialect(org.apache.calcite.sql.dialect.SybaseSqlDialect) TeradataSqlDialect(org.apache.calcite.sql.dialect.TeradataSqlDialect) OracleSqlDialect(org.apache.calcite.sql.dialect.OracleSqlDialect) AccessSqlDialect(org.apache.calcite.sql.dialect.AccessSqlDialect) VerticaSqlDialect(org.apache.calcite.sql.dialect.VerticaSqlDialect) CalciteSqlDialect(org.apache.calcite.sql.dialect.CalciteSqlDialect) PhoenixSqlDialect(org.apache.calcite.sql.dialect.PhoenixSqlDialect) HsqldbSqlDialect(org.apache.calcite.sql.dialect.HsqldbSqlDialect) IngresSqlDialect(org.apache.calcite.sql.dialect.IngresSqlDialect) ParaccelSqlDialect(org.apache.calcite.sql.dialect.ParaccelSqlDialect) InterbaseSqlDialect(org.apache.calcite.sql.dialect.InterbaseSqlDialect) MssqlSqlDialect(org.apache.calcite.sql.dialect.MssqlSqlDialect) NetezzaSqlDialect(org.apache.calcite.sql.dialect.NetezzaSqlDialect) InfobrightSqlDialect(org.apache.calcite.sql.dialect.InfobrightSqlDialect) PostgresqlSqlDialect(org.apache.calcite.sql.dialect.PostgresqlSqlDialect) RedshiftSqlDialect(org.apache.calcite.sql.dialect.RedshiftSqlDialect) FirebirdSqlDialect(org.apache.calcite.sql.dialect.FirebirdSqlDialect) MysqlSqlDialect(org.apache.calcite.sql.dialect.MysqlSqlDialect) Db2SqlDialect(org.apache.calcite.sql.dialect.Db2SqlDialect) JethroDataSqlDialect(org.apache.calcite.sql.dialect.JethroDataSqlDialect) NeoviewSqlDialect(org.apache.calcite.sql.dialect.NeoviewSqlDialect) HiveSqlDialect(org.apache.calcite.sql.dialect.HiveSqlDialect) InformixSqlDialect(org.apache.calcite.sql.dialect.InformixSqlDialect) LucidDbSqlDialect(org.apache.calcite.sql.dialect.LucidDbSqlDialect) NullCollation(org.apache.calcite.config.NullCollation) DerbySqlDialect(org.apache.calcite.sql.dialect.DerbySqlDialect) PhoenixSqlDialect(org.apache.calcite.sql.dialect.PhoenixSqlDialect)

Example 4 with HiveSqlDialect

use of org.apache.calcite.sql.dialect.HiveSqlDialect 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

HiveSqlDialect (org.apache.calcite.sql.dialect.HiveSqlDialect)4 NullCollation (org.apache.calcite.config.NullCollation)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SQLException (java.sql.SQLException)1 JdbcImplementor (org.apache.calcite.adapter.jdbc.JdbcImplementor)1 SqlDialect (org.apache.calcite.sql.SqlDialect)1 AccessSqlDialect (org.apache.calcite.sql.dialect.AccessSqlDialect)1 AnsiSqlDialect (org.apache.calcite.sql.dialect.AnsiSqlDialect)1 CalciteSqlDialect (org.apache.calcite.sql.dialect.CalciteSqlDialect)1 Db2SqlDialect (org.apache.calcite.sql.dialect.Db2SqlDialect)1 DerbySqlDialect (org.apache.calcite.sql.dialect.DerbySqlDialect)1 FirebirdSqlDialect (org.apache.calcite.sql.dialect.FirebirdSqlDialect)1 H2SqlDialect (org.apache.calcite.sql.dialect.H2SqlDialect)1 HsqldbSqlDialect (org.apache.calcite.sql.dialect.HsqldbSqlDialect)1 InfobrightSqlDialect (org.apache.calcite.sql.dialect.InfobrightSqlDialect)1 InformixSqlDialect (org.apache.calcite.sql.dialect.InformixSqlDialect)1 IngresSqlDialect (org.apache.calcite.sql.dialect.IngresSqlDialect)1 InterbaseSqlDialect (org.apache.calcite.sql.dialect.InterbaseSqlDialect)1