[ 
https://issues.apache.org/jira/browse/CALCITE-4590?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17894900#comment-17894900
 ] 

Mihai Budiu commented on CALCITE-4590:
--------------------------------------

The bug is actually RexBuilder.makeLiteral. The signature of 
RexBuilder.makeLiteral is:

{code:java}
  public RexNode makeLiteral(@Nullable Object value, RelDataType type,
      boolean allowCast, boolean trim)
{code}

The javadoc for trim is:

{code:java}
   * @param trim      Whether to trim values and type to the shortest equivalent
   *                  value; for example whether to convert CHAR(4) 'foo '
   *                  to CHAR(3) 'foo'
{code}

But in the code, trim is used as follows:

{code:java}
    case CHAR:
      final NlsString nlsString = (NlsString) value;
      if (trim) {
        return makeCharLiteral(nlsString.rtrim());
      } else {
        return makeCharLiteral(padRight(nlsString, type.getPrecision()));
      }
{code}

So, as you see the nlsString is trimmed unconditionally. The size of the type 
should be used to trim only excess spaces.

> Incorrect query result with fixed-length string
> -----------------------------------------------
>
>                 Key: CALCITE-4590
>                 URL: https://issues.apache.org/jira/browse/CALCITE-4590
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.26.0
>            Reporter: Roman Kondakov
>            Assignee: Mihai Budiu
>            Priority: Major
>
> Query may return wrong result when fixed-length strings (CHAR(N)) are used in 
> OR/IN clause
> {code:java}
> @Test void test() {
>     // Passed.
>     CalciteAssert.that()
>         .query("select * from (values (1, 'a'), (2, 'abc')) where EXPR$1 = 
> 'a'")
>         .returns("EXPR$0=1; EXPR$1=a  \n");
>     // Failed. Only "EXPR$0=2; EXPR$1=abc\n" is returned
>     CalciteAssert.that()
>         .query("select * from (values (1, 'a'), (2, 'abc')) where EXPR$1 = 
> 'a' or EXPR$1 = 'abc'")
>         .returns("EXPR$0=1; EXPR$1=a  \n"
>             + "EXPR$0=2; EXPR$1=abc\n");
>   }
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to