This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new f9560056d23 Add more converter test cases (#37352)
f9560056d23 is described below

commit f9560056d2386fbb2088a1f49b5068dedb9ad174
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Dec 11 22:23:48 2025 +0800

    Add more converter test cases (#37352)
    
    * Add more converter test cases
    
    * Add more converter test cases
    
    * Add more converter test cases
---
 .../segment/projection/DistinctConverterTest.java  |  50 +++++++++
 .../impl/AggregationProjectionConverterTest.java   | 123 +++++++++++++++++++++
 .../impl/ColumnProjectionConverterTest.java        |  75 +++++++++++++
 .../impl/ExpressionProjectionConverterTest.java    |  85 ++++++++++++++
 .../impl/ShorthandProjectionConverterTest.java     |  61 ++++++++++
 .../impl/SubqueryProjectionConverterTest.java      |  91 +++++++++++++++
 6 files changed, 485 insertions(+)

diff --git 
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/DistinctConverterTest.java
 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/DistinctConverterTest.java
new file mode 100644
index 00000000000..efe0f73f87e
--- /dev/null
+++ 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/DistinctConverterTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package 
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.projection;
+
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlSelectKeyword;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionsSegment;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class DistinctConverterTest {
+    
+    @Test
+    void assertConvertReturnsDistinctNodeListWhenFlagPresent() {
+        ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
+        projectionsSegment.setDistinctRow(true);
+        Optional<SqlNodeList> actual = 
DistinctConverter.convert(projectionsSegment);
+        assertTrue(actual.isPresent());
+        SqlNodeList expected = new 
SqlNodeList(Collections.singletonList(SqlSelectKeyword.DISTINCT.symbol(SqlParserPos.ZERO)),
 SqlParserPos.ZERO);
+        assertThat(actual.get(), is(expected));
+    }
+    
+    @Test
+    void assertConvertReturnsEmptyWhenFlagAbsent() {
+        assertFalse(DistinctConverter.convert(new ProjectionsSegment(0, 
0)).isPresent());
+    }
+}
diff --git 
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/AggregationProjectionConverterTest.java
 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/AggregationProjectionConverterTest.java
new file mode 100644
index 00000000000..36f4d17b7f7
--- /dev/null
+++ 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/AggregationProjectionConverterTest.java
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package 
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.projection.impl;
+
+import org.apache.calcite.sql.SqlBasicCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import 
org.apache.shardingsphere.sql.parser.statement.core.enums.AggregationType;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.AggregationDistinctProjectionSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.AggregationProjectionSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.AliasSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import 
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.expression.ExpressionConverter;
+import 
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import 
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(ExpressionConverter.class)
+class AggregationProjectionConverterTest {
+    
+    @Test
+    void assertConvertReturnsEmptyForNullSegment() {
+        assertFalse(AggregationProjectionConverter.convert(null).isPresent());
+    }
+    
+    @Test
+    void assertConvertBuildsDistinctAggregationWithAliasAndStarParameter() {
+        AggregationDistinctProjectionSegment segment = new 
AggregationDistinctProjectionSegment(0, 0, AggregationType.COUNT, "COUNT(*)", 
"COUNT(*)");
+        segment.setAlias(new AliasSegment(0, 0, new IdentifierValue("alias")));
+        Optional<SqlNode> actual = 
AggregationProjectionConverter.convert(segment);
+        assertTrue(actual.isPresent());
+        SqlBasicCall asCall = (SqlBasicCall) actual.orElse(null);
+        assertThat(asCall.getOperator(), is(SqlStdOperatorTable.AS));
+        SqlBasicCall innerCall = (SqlBasicCall) asCall.getOperandList().get(0);
+        assertThat(innerCall.getOperator(), is(SqlStdOperatorTable.COUNT));
+        assertNotNull(innerCall.getFunctionQuantifier());
+        assertThat(innerCall.getOperandList().size(), is(1));
+        assertThat(innerCall.getOperandList().get(0), 
instanceOf(SqlIdentifier.class));
+        SqlIdentifier starIdentifier = (SqlIdentifier) 
innerCall.getOperandList().get(0);
+        assertTrue(starIdentifier.isStar());
+        SqlIdentifier aliasIdentifier = (SqlIdentifier) 
asCall.getOperandList().get(1);
+        assertThat(aliasIdentifier.names, 
is(Collections.singletonList("alias")));
+    }
+    
+    @Test
+    void assertConvertBuildsAggregationWithParametersAndSeparator() {
+        AggregationProjectionSegment segment = new 
AggregationProjectionSegment(0, 0, AggregationType.SUM, "SUM(expr)", "|");
+        ExpressionSegment firstParam = mock(ExpressionSegment.class);
+        ExpressionSegment secondParam = mock(ExpressionSegment.class);
+        segment.getParameters().addAll(Arrays.asList(firstParam, secondParam));
+        SqlNode firstNode = mock(SqlNode.class);
+        SqlNode secondNode = mock(SqlNode.class);
+        
when(ExpressionConverter.convert(firstParam)).thenReturn(Optional.of(firstNode));
+        
when(ExpressionConverter.convert(secondParam)).thenReturn(Optional.of(secondNode));
+        Optional<SqlNode> actual = 
AggregationProjectionConverter.convert(segment);
+        SqlBasicCall sqlBasicCall = (SqlBasicCall) actual.orElse(null);
+        assertNotNull(sqlBasicCall);
+        assertThat(sqlBasicCall.getOperator(), is(SqlStdOperatorTable.SUM));
+        assertThat(sqlBasicCall.getOperandList().size(), is(3));
+        assertThat(sqlBasicCall.getOperandList().get(0), is(firstNode));
+        assertThat(sqlBasicCall.getOperandList().get(1), is(secondNode));
+        assertThat(sqlBasicCall.getOperandList().get(2), 
instanceOf(SqlLiteral.class));
+        assertNull(sqlBasicCall.getFunctionQuantifier());
+    }
+    
+    @Test
+    void assertConvertBuildsAggregationWithParametersWithoutSeparator() {
+        AggregationProjectionSegment segment = new 
AggregationProjectionSegment(0, 0, AggregationType.MAX, "MAX(expr)");
+        ExpressionSegment param = mock(ExpressionSegment.class);
+        segment.getParameters().add(param);
+        SqlNode expectedNode = mock(SqlNode.class);
+        
when(ExpressionConverter.convert(param)).thenReturn(Optional.of(expectedNode));
+        Optional<SqlNode> actual = 
AggregationProjectionConverter.convert(segment);
+        SqlBasicCall sqlBasicCall = (SqlBasicCall) actual.orElse(null);
+        assertNotNull(sqlBasicCall);
+        assertThat(sqlBasicCall.getOperator(), is(SqlStdOperatorTable.MAX));
+        assertThat(sqlBasicCall.getOperandList().size(), is(1));
+        assertThat(sqlBasicCall.getOperandList().get(0), is(expectedNode));
+        assertNull(sqlBasicCall.getFunctionQuantifier());
+    }
+    
+    @Test
+    void assertConvertThrowsExceptionForUnsupportedOperator() {
+        IllegalStateException ex = assertThrows(IllegalStateException.class,
+                () -> AggregationProjectionConverter.convert(new 
AggregationProjectionSegment(0, 0, AggregationType.PRODUCT, "PRODUCT(expr)")));
+        assertThat(ex.getMessage(), is("Unsupported SQL operator: `PRODUCT`"));
+    }
+}
diff --git 
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/ColumnProjectionConverterTest.java
 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/ColumnProjectionConverterTest.java
new file mode 100644
index 00000000000..ab4ab65f1fc
--- /dev/null
+++ 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/ColumnProjectionConverterTest.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package 
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.projection.impl;
+
+import org.apache.calcite.sql.SqlAsOperator;
+import org.apache.calcite.sql.SqlBasicCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ColumnProjectionSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.AliasSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import 
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.expression.impl.ColumnConverter;
+import 
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import 
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(ColumnConverter.class)
+class ColumnProjectionConverterTest {
+    
+    @Test
+    void assertConvertWrapsWithAliasWhenPresent() {
+        ColumnSegment columnSegment = new ColumnSegment(0, 0, new 
IdentifierValue("col"));
+        ColumnProjectionSegment projectionSegment = new 
ColumnProjectionSegment(columnSegment);
+        projectionSegment.setAlias(new AliasSegment(0, 0, new 
IdentifierValue("alias")));
+        SqlIdentifier columnNode = new SqlIdentifier("col", SqlParserPos.ZERO);
+        
when(ColumnConverter.convert(columnSegment)).thenReturn(Optional.of(columnNode));
+        Optional<SqlNode> actual = 
ColumnProjectionConverter.convert(projectionSegment);
+        SqlBasicCall sqlBasicCall = (SqlBasicCall) actual.orElse(null);
+        assertNotNull(sqlBasicCall);
+        assertThat(sqlBasicCall.getOperator(), 
instanceOf(SqlAsOperator.class));
+        assertThat(sqlBasicCall.getOperandList().get(0), is(columnNode));
+        assertThat(((SqlIdentifier) 
sqlBasicCall.getOperandList().get(1)).names, 
is(Collections.singletonList("alias")));
+    }
+    
+    @Test
+    void assertConvertDelegatesWhenAliasAbsent() {
+        ColumnSegment columnSegment = new ColumnSegment(0, 0, new 
IdentifierValue("col"));
+        ColumnProjectionSegment projectionSegment = new 
ColumnProjectionSegment(columnSegment);
+        SqlNode expected = mock(SqlNode.class);
+        
when(ColumnConverter.convert(columnSegment)).thenReturn(Optional.of(expected));
+        Optional<SqlNode> actual = 
ColumnProjectionConverter.convert(projectionSegment);
+        assertTrue(actual.isPresent());
+        assertThat(actual.get(), is(expected));
+    }
+}
diff --git 
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/ExpressionProjectionConverterTest.java
 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/ExpressionProjectionConverterTest.java
new file mode 100644
index 00000000000..8606f58205a
--- /dev/null
+++ 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/ExpressionProjectionConverterTest.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package 
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.projection.impl;
+
+import org.apache.calcite.sql.SqlBasicCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ExpressionProjectionSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.AliasSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import 
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.expression.ExpressionConverter;
+import 
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import 
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(ExpressionConverter.class)
+class ExpressionProjectionConverterTest {
+    
+    @Test
+    void assertConvertReturnsEmptyForNullSegment() {
+        assertFalse(ExpressionProjectionConverter.convert(null).isPresent());
+    }
+    
+    @Test
+    void assertConvertReturnsEmptyWhenExpressionConversionAbsent() {
+        ExpressionSegment expressionSegment = mock(ExpressionSegment.class);
+        
when(ExpressionConverter.convert(expressionSegment)).thenReturn(Optional.empty());
+        assertFalse(ExpressionProjectionConverter.convert(new 
ExpressionProjectionSegment(0, 0, "text", expressionSegment)).isPresent());
+    }
+    
+    @Test
+    void assertConvertWrapsResultWithAlias() {
+        ExpressionSegment expressionSegment = mock(ExpressionSegment.class);
+        ExpressionProjectionSegment projectionSegment = new 
ExpressionProjectionSegment(0, 0, "text", expressionSegment);
+        projectionSegment.setAlias(new AliasSegment(0, 0, new 
IdentifierValue("alias")));
+        SqlNode expected = mock(SqlNode.class);
+        
when(ExpressionConverter.convert(expressionSegment)).thenReturn(Optional.of(expected));
+        SqlBasicCall actual = (SqlBasicCall) 
ExpressionProjectionConverter.convert(projectionSegment).orElse(null);
+        assertNotNull(actual);
+        assertThat(actual.getOperator(), is(SqlStdOperatorTable.AS));
+        assertThat(actual.getOperandList().get(0), is(expected));
+        SqlIdentifier aliasIdentifier = (SqlIdentifier) 
actual.getOperandList().get(1);
+        assertThat(aliasIdentifier.names, 
is(Collections.singletonList("alias")));
+    }
+    
+    @Test
+    void assertConvertReturnsConvertedExpressionWhenAliasAbsent() {
+        ExpressionSegment expressionSegment = mock(ExpressionSegment.class);
+        SqlNode expected = mock(SqlNode.class);
+        
when(ExpressionConverter.convert(expressionSegment)).thenReturn(Optional.of(expected));
+        Optional<SqlNode> actual = ExpressionProjectionConverter.convert(new 
ExpressionProjectionSegment(0, 0, "text", expressionSegment));
+        assertTrue(actual.isPresent());
+        assertThat(actual.orElse(null), is(expected));
+    }
+}
diff --git 
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/ShorthandProjectionConverterTest.java
 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/ShorthandProjectionConverterTest.java
new file mode 100644
index 00000000000..2c5e2ab249c
--- /dev/null
+++ 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/ShorthandProjectionConverterTest.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package 
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.projection.impl;
+
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ShorthandProjectionSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class ShorthandProjectionConverterTest {
+    
+    @Test
+    void assertConvertReturnsEmptyForNullSegment() {
+        assertFalse(ShorthandProjectionConverter.convert(null).isPresent());
+    }
+    
+    @Test
+    void assertConvertReturnsStarWhenNoOwner() {
+        Optional<SqlNode> actual = ShorthandProjectionConverter.convert(new 
ShorthandProjectionSegment(0, 0));
+        assertTrue(actual.isPresent());
+        assertTrue(((SqlIdentifier) actual.get()).isStar());
+    }
+    
+    @Test
+    void assertConvertReturnsQualifiedStarWhenOwnerPresent() {
+        OwnerSegment table = new OwnerSegment(0, 0, new 
IdentifierValue("table"));
+        table.setOwner(new OwnerSegment(0, 0, new IdentifierValue("schema")));
+        ShorthandProjectionSegment projectionSegment = new 
ShorthandProjectionSegment(0, 0);
+        projectionSegment.setOwner(table);
+        Optional<SqlNode> actual = 
ShorthandProjectionConverter.convert(projectionSegment);
+        assertTrue(actual.isPresent());
+        SqlIdentifier identifier = (SqlIdentifier) actual.orElse(null);
+        assertTrue(identifier.isStar());
+        assertThat(identifier.names, is(Arrays.asList("schema", "table", "")));
+    }
+}
diff --git 
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/SubqueryProjectionConverterTest.java
 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/SubqueryProjectionConverterTest.java
new file mode 100644
index 00000000000..78011f7daba
--- /dev/null
+++ 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/segment/projection/impl/SubqueryProjectionConverterTest.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package 
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.segment.projection.impl;
+
+import org.apache.calcite.sql.SqlBasicCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.sql.parser.statement.core.enums.SubqueryType;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubquerySegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.SubqueryProjectionSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.AliasSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.SelectStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import 
org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.statement.type.SelectStatementConverter;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedConstruction;
+
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockConstruction;
+import static org.mockito.Mockito.when;
+
+class SubqueryProjectionConverterTest {
+    
+    private final DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+    
+    @Test
+    void assertConvertReturnsEmptyForNullSegment() {
+        assertFalse(SubqueryProjectionConverter.convert(null).isPresent());
+    }
+    
+    @Test
+    void assertConvertWrapsAliasWhenSubqueryTypeNotExists() {
+        SelectStatement selectStatement = new SelectStatement(databaseType);
+        SubqueryProjectionSegment projectionSegment = new 
SubqueryProjectionSegment(new SubquerySegment(0, 0, selectStatement, "sub"), 
"text");
+        projectionSegment.setAlias(new AliasSegment(0, 0, new 
IdentifierValue("alias")));
+        SqlNode convertedNode = mock(SqlNode.class);
+        try (
+                MockedConstruction<SelectStatementConverter> ignored = 
mockConstruction(SelectStatementConverter.class,
+                        (mock, context) -> 
when(mock.convert(selectStatement)).thenReturn(convertedNode))) {
+            Optional<SqlNode> actual = 
SubqueryProjectionConverter.convert(projectionSegment);
+            assertTrue(actual.isPresent());
+            SqlBasicCall asCall = (SqlBasicCall) actual.orElse(null);
+            assertThat(asCall.getOperator(), is(SqlStdOperatorTable.AS));
+            assertThat(asCall.getOperandList().get(0), is(convertedNode));
+            SqlIdentifier aliasIdentifier = (SqlIdentifier) 
asCall.getOperandList().get(1);
+            assertThat(aliasIdentifier.names, 
is(Collections.singletonList("alias")));
+        }
+    }
+    
+    @Test
+    void assertConvertWrapsExistsWhenSubqueryTypeIsExists() {
+        SelectStatement selectStatement = new SelectStatement(databaseType);
+        selectStatement.setSubqueryType(SubqueryType.EXISTS);
+        SubqueryProjectionSegment projectionSegment = new 
SubqueryProjectionSegment(new SubquerySegment(0, 0, selectStatement, "sub"), 
"text");
+        SqlNode convertedNode = mock(SqlNode.class);
+        try (
+                MockedConstruction<SelectStatementConverter> ignored = 
mockConstruction(SelectStatementConverter.class,
+                        (mock, context) -> 
when(mock.convert(selectStatement)).thenReturn(convertedNode))) {
+            Optional<SqlNode> actual = 
SubqueryProjectionConverter.convert(projectionSegment);
+            assertTrue(actual.isPresent());
+            SqlBasicCall existsCall = (SqlBasicCall) actual.orElse(null);
+            assertThat(existsCall.getOperator(), 
is(SqlStdOperatorTable.EXISTS));
+            assertThat(existsCall.getOperandList().get(0), is(convertedNode));
+        }
+    }
+}

Reply via email to