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-6678] Calcite should support dual table query when db provides this feature #4056

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -18,7 +18,6 @@

import org.apache.calcite.DataContext;
import org.apache.calcite.DataContexts;
import org.apache.calcite.adapter.java.AbstractQueryableTable;
import org.apache.calcite.adapter.java.JavaTypeFactory;
import org.apache.calcite.avatica.AvaticaConnection;
import org.apache.calcite.avatica.AvaticaFactory;
Expand All @@ -37,7 +36,6 @@
import org.apache.calcite.linq4j.BaseQueryable;
import org.apache.calcite.linq4j.Enumerable;
import org.apache.calcite.linq4j.Enumerator;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.calcite.linq4j.Ord;
import org.apache.calcite.linq4j.QueryProvider;
import org.apache.calcite.linq4j.Queryable;
Expand All @@ -48,8 +46,6 @@
import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.prepare.CalciteCatalogReader;
import org.apache.calcite.rel.type.DelegatingTypeSystem;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.rel.type.RelDataTypeSystem;
import org.apache.calcite.rel.type.TimeFrameSet;
import org.apache.calcite.rel.type.TimeFrames;
Expand All @@ -59,6 +55,7 @@
import org.apache.calcite.schema.Schemas;
import org.apache.calcite.schema.impl.AbstractSchema;
import org.apache.calcite.schema.impl.LongSchemaVersion;
import org.apache.calcite.schema.impl.ViewTable;
import org.apache.calcite.server.CalciteServer;
import org.apache.calcite.server.CalciteServerStatement;
import org.apache.calcite.sql.advise.SqlAdvisor;
Expand All @@ -80,7 +77,6 @@
import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -156,7 +152,11 @@ protected CalciteConnectionImpl(Driver driver, AvaticaFactory factory,
: CalciteSchema.createRootSchema(true));
// Add dual table metadata when isSupportedDualTable return true
if (cfg.conformance().isSupportedDualTable()) {
this.rootSchema.add("DUAL", new DualTable(String.class));
SchemaPlus schemaPlus = this.rootSchema.plus();
// Dual table contains one row with a value X
schemaPlus.add(
"DUAL", ViewTable.viewMacro(schemaPlus, "VALUES ('X')",
ImmutableList.of(), null, false));
}
checkArgument(this.rootSchema.isRoot(), "must be root schema");
this.properties.put(InternalProperty.CASE_SENSITIVE, cfg.caseSensitive());
Expand Down Expand Up @@ -629,26 +629,4 @@ static class CalciteServerStatementImpl
this.iterator = iterator;
}
}

/** Implementation of {@link AbstractQueryableTable} for dual table. */
private static class DualTable extends AbstractQueryableTable {

DualTable(Class<String> clazz) {
super(clazz);
}

@Override public RelDataType getRowType(RelDataTypeFactory typeFactory) {
return typeFactory.createStructType(
// Dual table has one column DUMMY, and defined to be VARCHAR2(1)
Collections.singletonList(typeFactory.createJavaType(String.class)),
Collections.singletonList("DUMMY"));
}

@SuppressWarnings("unchecked")
@Override public Queryable<String> asQueryable(QueryProvider queryProvider,
SchemaPlus schema, String tableName) {
// Dual table contains one row with a value X
return Linq4j.asEnumerable(Collections.singletonList("X")).asQueryable();
}
}
}
72 changes: 72 additions & 0 deletions core/src/test/resources/sql/dual-table-query.iq
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# dual-table-query.iq - Tests for DUAL table query
#
# 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.
#

# [CALCITE-6678] Support dual table query (enabled in MySQL, Oracle libraries)
!set outputformat mysql
!use scott-mysql

# MySQL supports users to specify the dual table, and also supports users not to specify the dual table.
SELECT 1 + 1 FROM DUAL;
+--------+
| EXPR$0 |
+--------+
| 2 |
+--------+
(1 row)

!ok

SELECT 1 + 1;
+--------+
| EXPR$0 |
+--------+
| 2 |
+--------+
(1 row)

!ok

# Oracle supports users to specify the dual table, but not supports users not to specify the dual table.
!use scott-oracle

SELECT 1 + 1 FROM DUAL;
+--------+
| EXPR$0 |
+--------+
| 2 |
+--------+
(1 row)

!ok

SELECT 1 + 1;
java.sql.SQLException: Error while executing SQL "SELECT 1 + 1": From line 1, column 1 to line 1, column 12: SELECT must have a FROM clause

!error

SELECT * FROM DUAL;
+--------+
| EXPR$0 |
+--------+
| X |
+--------+
(1 row)

!ok

# End dual-table-query.iq
55 changes: 1 addition & 54 deletions core/src/test/resources/sql/dummy.iq
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,5 @@ values 1;
EXPR$0
1
!ok

strongduanmu marked this conversation as resolved.
Show resolved Hide resolved
# [CALCITE-6678] Support dual table query (enabled in MySQL, Oracle libraries)
!set outputformat mysql
!use scott-mysql

# MySQL supports users to specify the dual table, and also supports users not to specify the dual table.
SELECT 1 + 1 FROM DUAL;
+--------+
| EXPR$0 |
+--------+
| 2 |
+--------+
(1 row)

!ok

SELECT 1 + 1;
+--------+
| EXPR$0 |
+--------+
| 2 |
+--------+
(1 row)

!ok

# Oracle supports users to specify the dual table, but not supports users not to specify the dual table.
!use scott-oracle

SELECT 1 + 1 FROM DUAL;
+--------+
| EXPR$0 |
+--------+
| 2 |
+--------+
(1 row)

!ok

SELECT 1 + 1;
java.sql.SQLException: Error while executing SQL "SELECT 1 + 1": From line 1, column 1 to line 1, column 12: SELECT must have a FROM clause

!error

SELECT * FROM DUAL;
+-------+
| DUMMY |
+-------+
| X |
+-------+
(1 row)

!ok

# do not modify this file, this is intended only for temporary debugging code
# End dummy.iq
Loading