Skip to content

Commit

Permalink
[CALCITE-6220] Rewrite MIN/MAX(bool) as BOOL_AND/BOOL_OR for Postgres…
Browse files Browse the repository at this point in the history
…, Redshift
  • Loading branch information
tanclary committed Jan 25, 2024
1 parent 91a8ab8 commit 26b5e9b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.apache.calcite.sql.SqlUpdate;
import org.apache.calcite.sql.SqlUtil;
import org.apache.calcite.sql.fun.SqlInternalOperators;
import org.apache.calcite.sql.fun.SqlMinMaxAggFunction;
import org.apache.calcite.sql.fun.SqlSingleValueAggFunction;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
Expand Down Expand Up @@ -577,6 +578,8 @@ protected Builder buildAggregate(Aggregate e, Builder builder,
RelDataType aggCallRelDataType = aggCall.getType();
if (aggCall.getAggregation() instanceof SqlSingleValueAggFunction) {
aggCallSqlNode = dialect.rewriteSingleValueExpr(aggCallSqlNode, aggCallRelDataType);
} else if (aggCall.getAggregation() instanceof SqlMinMaxAggFunction) {
aggCallSqlNode = dialect.rewriteMaxMinExpr(aggCallSqlNode, aggCallRelDataType);
}
addSelect(selectList, aggCallSqlNode, e.getRowType());
}
Expand Down
34 changes: 33 additions & 1 deletion core/src/main/java/org/apache/calcite/sql/SqlDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.dialect.JethroDataSqlDialect;
import org.apache.calcite.sql.fun.SqlInternalOperators;
import org.apache.calcite.sql.fun.SqlLibraryOperators;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.AbstractSqlType;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.type.SqlTypeUtil;
import org.apache.calcite.sql.validate.SqlConformance;
import org.apache.calcite.sql.validate.SqlConformanceEnum;
Expand Down Expand Up @@ -864,14 +866,44 @@ public boolean supportsDataType(RelDataType type) {
return SqlTypeUtil.convertTypeToSpec(type);
}

/** Rewrite SINGLE_VALUE into expression based on database variants
/** Rewrites SINGLE_VALUE into expression based on database variants
* E.g. HSQLDB, MYSQL, ORACLE, etc.
*/
public SqlNode rewriteSingleValueExpr(SqlNode aggCall, RelDataType relDataType) {
LOGGER.debug("SINGLE_VALUE rewrite not supported for {}", databaseProduct);
return aggCall;
}

/**
* Rewrites MAX(x)/MIN(x) as BOOL_OR(x)/BOOL_AND(x) for certain
* database variants (Postgres and Redshift, currently).
*
* @see #rewriteMaxMin(SqlNode, RelDataType)
*/
public SqlNode rewriteMaxMinExpr(SqlNode aggCall, RelDataType relDataType) {
return aggCall;
}

/**
* Helper for rewrites of MAX/MIN.
* Some dialects (e.g. Postgres and Redshift), rewrite as
* BOOL_OR/BOOL_AND if the return type is BOOLEAN.
*/
protected static SqlNode rewriteMaxMin(SqlNode aggCall, RelDataType relDataType) {
// The behavior of this method depends on the argument type,
// and whether it is MIN/MAX
final SqlTypeName type = relDataType.getSqlTypeName();
final boolean isMax = aggCall.getKind() == SqlKind.MAX;
// If the type is BOOLEAN, create a new call to the correct operator
if (type == SqlTypeName.BOOLEAN) {
final SqlOperator op = isMax ? SqlLibraryOperators.BOOL_OR : SqlLibraryOperators.BOOL_AND;
final SqlNode operand = ((SqlBasicCall) aggCall).operand(0);
return op.createCall(SqlParserPos.ZERO, operand);
}
// Otherwise, just return as it arrived
return aggCall;
}

/**
* Returns the SqlNode for emulating the null direction for the given field
* or <code>null</code> if no emulation needs to be done.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,15 @@ public PostgresqlSqlDialect(Context context) {
timeUnitNode.getParserPosition());
SqlFloorFunction.unparseDatetimeFunction(writer, call2, "DATE_TRUNC", false);
break;

default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
}

@Override public SqlNode rewriteMaxMinExpr(SqlNode aggCall, RelDataType relDataType) {
return rewriteMaxMin(aggCall, relDataType);
}

@Override public boolean supportsGroupByLiteral() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public RedshiftSqlDialect(Context context) {
SqlParserPos.ZERO);
}

@Override public SqlNode rewriteMaxMinExpr(SqlNode aggCall, RelDataType relDataType) {
return rewriteMaxMin(aggCall, relDataType);
}

@Override public boolean supportsGroupByLiteral() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6683,6 +6683,37 @@ private void checkLiteral2(String expression, String expected) {
sql(query).withLibrary(SqlLibrary.SNOWFLAKE).withSnowflake().ok(expectedSnowflake);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6220">[CALCITE-6220]
* Rewrite MIN/MAX(bool) as BOOL_AND/BOOL_OR for Postgres, Redshift</a>. */
@Test void testMaxMinOnBooleanColumn() {
final String query = "select max(\"brand_name\" = 'a'), "
+ "min(\"brand_name\" = 'a'), "
+ "min(\"brand_name\")\n"
+ "from \"product\"";
final String expected = "SELECT MAX(\"brand_name\" = 'a'), "
+ "MIN(\"brand_name\" = 'a'), "
+ "MIN(\"brand_name\")\n"
+ "FROM \"foodmart\".\"product\"";
final String expectedBigQuery = "SELECT MAX(brand_name = 'a'), "
+ "MIN(brand_name = 'a'), "
+ "MIN(brand_name)\n"
+ "FROM foodmart.product";
final String expectedPostgres = "SELECT BOOL_OR(\"brand_name\" = 'a'), "
+ "BOOL_AND(\"brand_name\" = 'a'), "
+ "MIN(\"brand_name\")\n"
+ "FROM \"foodmart\".\"product\"";
final String expectedRedshift = "SELECT BOOL_OR(\"brand_name\" = 'a'), "
+ "BOOL_AND(\"brand_name\" = 'a'), "
+ "MIN(\"brand_name\")\n"
+ "FROM \"foodmart\".\"product\"";
sql(query)
.ok(expected)
.withBigQuery().ok(expectedBigQuery)
.withPostgresql().ok(expectedPostgres)
.withRedshift().ok(expectedPostgres);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6156">[CALCITE-6156]
* Add ENDSWITH, STARTSWITH functions (enabled in Postgres, Snowflake libraries)</a>. */
Expand Down

0 comments on commit 26b5e9b

Please sign in to comment.