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

Christophe Le Saec commented on CALCITE-5326:
---------------------------------------------

I just tried to add a test in RelToSqlConverterTest with same principle as 
"[void 
testFloorWithGroupBy()|https://github.com/apache/calcite/blob/main/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java#L4016]";,
 but get the error 
java.lang.AssertionError
at 
org.apache.calcite.sql.validate.SqlValidatorImpl.validateSelect([SqlValidatorImpl.java:3664|https://github.com/apache/calcite/blob/main/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java#L3664])
at 
org.apache.calcite.sql.validate.SelectNamespace.validateImpl(SelectNamespace.java:64)
at 
org.apache.calcite.sql.validate.AbstractNamespace.validate(AbstractNamespace.java:89)

due to a [strange code in method 
SqlValidatorImpl.rewriteMerge|https://github.com/apache/calcite/blob/main/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java#L1575]
 :
{code:java}
      final SqlNode insertSource = SqlNode.clone(sourceTableRef);
      select =
          new SqlSelect(SqlParserPos.ZERO, null, selectList, insertSource, null,
              null, null, null, null, null, null, null);
      insertCall.setSource(select);
{code}
And as i can't find a unit test to cover this method ({_}i tried step by step 
test with breakpoint{_}), i don't know the aim of this code.
All i see, it transforms the "WHEN NOT MATCHED" part of the valid SQLCall from
{code:sql}
MERGE INTO `foodmart`.`product` AS `P`
USING (SELECT `employee_id`, `first_name`
FROM `foodmart`.`employee`
WHERE `first_name` IS NOT NULL) AS `T`
ON `P`.`product_name` = `T`.`employee_id`
WHEN MATCHED THEN UPDATE SET `brand_name` = `T`.`FIRST_NAME`
WHEN NOT MATCHED THEN INSERT (`product_name`, `brand_name`) VALUES 
ROW(`T`.`EMPLOYEE_ID`, `T`.`FIRST_NAME`)
{code}
to
{code:sql}
WHEN NOT MATCHED THEN INSERT (`product_name`, `brand_name`) SELECT 
`T`.`EMPLOYEE_ID`, `T`.`FIRST_NAME`
FROM (SELECT `employee_id`, `first_name`
FROM `foodmart`.`employee`
WHERE `first_name` IS NOT NULL) AS `T`
{code}
which seems to be not valid ?

> SqlMerge generate extra bracket on toSqlString
> ----------------------------------------------
>
>                 Key: CALCITE-5326
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5326
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>            Reporter: Christophe Le Saec
>            Priority: Major
>              Labels: pull-requests-available
>         Attachments: SqlMergeTest.java
>
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> Following this [mails 
> exchange|https://lists.apache.org/thread/pgycm37ld6v92llv1t6bdmk80wylcspj], i 
> build this JIRA about SQL Merge.
> Using SQLMerge on this unit test [^SqlMergeTest.java] i tried to generate 
> [SQL Merge SQL code|https://en.wikipedia.org/wiki/Merge_(SQL)].
> this generate
> {code:sql}
> MERGE INTO "OUT_TABLE" AS "Target"
> USING "IN_TABLE" AS "Source"
> ON "TARGET"."ID" = "SOURCE"."IDENT"
> WHEN MATCHED THEN UPDATE SET "TARGET"."FIELD1" = "SOURCE"."F1", 
> "TARGET"."FIELD2" = "SOURCE"."F2"
> WHEN NOT MATCHED THEN INSERT ("TARGET"."ID", "TARGET"."FIELD1", 
> "TARGET"."FIELD2") (VALUES ("SOURCE"."IDENT", "SOURCE"."F1", "SOURCE"."F2"))
> {code}
> that doesn't work because of the bracket before VALUES on last line.
> just removing brackets
> {code:sql}
> WHEN NOT MATCHED THEN INSERT ("TARGET"."ID", "TARGET"."FIELD1", 
> "TARGET"."FIELD2") VALUES ("SOURCE"."IDENT", "SOURCE"."F1", "SOURCE"."F2")
> {code}
> make the query work.



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

Reply via email to