Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-6756] Preserving CAST of STRING operand in binary comparison… #4151

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.rel.type.RelDataTypeSystemImpl;
import org.apache.calcite.rex.RexCall;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexUtil;
import org.apache.calcite.sql.SqlAlienSystemTypeNameSpec;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlCall;
Expand All @@ -40,6 +43,7 @@
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.type.SqlTypeUtil;

import com.google.common.collect.ImmutableList;

Expand Down Expand Up @@ -155,6 +159,17 @@ public PostgresqlSqlDialect(Context context) {
}
}

@Override public boolean supportsImplicitTypeCoercion(RexCall call) {
final RexNode operand0 = call.getOperands().get(0);
RelDataType callType = call.getType();
boolean supportImplicit = super.supportsImplicitTypeCoercion(call);
boolean isNumericType = supportImplicit && SqlTypeUtil.isNumeric(callType);
if (isNumericType) {
return false;
}
return supportImplicit && RexUtil.isLiteral(operand0, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure only literals are a problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Literals don't need the cast, others need it. I add a unit test.

}

@Override public boolean requiresAliasForFromItems() {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7960,6 +7960,28 @@ private void checkLiteral2(String expression, String expected) {
.withBigQuery().ok(expectedBiqquery);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6756">[CALCITE-6756]
* Preserving CAST of STRING operand in binary comparison for PostgreSQL</a>. */
@Test void testImplicitTypeCoercionPostgreSQL() {
final String query = "select \"employee_id\" "
+ "from \"foodmart\".\"employee\" "
+ "where 10 = cast(\"full_name\" as int) and "
+ " \"first_name\" > cast(10 as varchar) and "
+ "\"birth_date\" = cast('1914-02-02' as date) or "
ILuffZhe marked this conversation as resolved.
Show resolved Hide resolved
+ "\"hire_date\" = cast('1996-01-01 '||'00:00:00' as timestamp) or "
+ "\"hire_date\" = '1996-01-01 00:00:00'";
final String expectedPostgresql = "SELECT \"employee_id\"\n"
+ "FROM \"foodmart\".\"employee\"\n"
+ "WHERE 10 = CAST(\"full_name\" AS INTEGER) AND "
+ "\"first_name\" > CAST(10 AS VARCHAR) AND "
+ "\"birth_date\" = '1914-02-02' OR "
+ "\"hire_date\" = CAST('1996-01-01 ' || '00:00:00' AS TIMESTAMP(0)) OR "
+ "\"hire_date\" = '1996-01-01 00:00:00'";
sql(query)
.withPostgresql().ok(expectedPostgresql);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6149">[CALCITE-6149]
* Unparse for CAST Nullable with ClickHouseSqlDialect</a>. */
Expand Down
Loading