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);
}
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);
}
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);
}
}
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;
}
Aggregations