Skip to content
Merged
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 @@ -28,6 +28,7 @@
import org.apache.calcite.schema.FunctionParameter;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.schema.Table;
import org.apache.calcite.schema.TableMacro;
import org.apache.calcite.schema.impl.AbstractSchema;
import org.apache.calcite.tools.FrameworkConfig;
import org.apache.ignite.IgniteException;
Expand All @@ -37,6 +38,12 @@
* Ignite schema.
*/
public class IgniteSchema extends AbstractSchema {
/** */
private static final String DUAL_TBL_NAME = "DUAL";

/** */
private static final String DUAL_TBL_VIEW = "SELECT * FROM (VALUES ('X')) AS T(DUMMY)";

/** */
private final String schemaName;

Expand Down Expand Up @@ -143,6 +150,17 @@ public SchemaPlus register(SchemaPlus parent, FrameworkConfig frameworkCfg) {

viewMap.forEach((name, sql) -> newSchema.add(name, new ViewTableMacroImpl(sql, newSchema, frameworkCfg)));

registerDualTableIfSupported(newSchema, frameworkCfg);

return newSchema;
}

/** */
private static void registerDualTableIfSupported(SchemaPlus schema, FrameworkConfig frameworkCfg) {
if (frameworkCfg != null && frameworkCfg.getSqlValidatorConfig().conformance().isSupportedDualTable()
&& schema.tables().get(DUAL_TBL_NAME) == null
&& schema.getFunctions(DUAL_TBL_NAME).stream().noneMatch(TableMacro.class::isInstance)) {
schema.add(DUAL_TBL_NAME, new ViewTableMacroImpl(DUAL_TBL_VIEW, schema, frameworkCfg));
Comment thread
tkalkirill marked this conversation as resolved.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.util.ReflectiveSqlOperatorTable;
import org.apache.calcite.sql.util.SqlOperatorTables;
import org.apache.calcite.sql.validate.SqlConformance;
import org.apache.calcite.sql.validate.SqlDelegatingConformance;
import org.apache.calcite.sql.validate.SqlValidator;
import org.apache.calcite.sql2rel.SqlRexContext;
import org.apache.calcite.sql2rel.SqlRexConvertlet;
import org.apache.calcite.tools.FrameworkConfig;
import org.apache.calcite.tools.Frameworks;
import org.apache.calcite.util.BuiltInMethod;
import org.apache.calcite.util.Optionality;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.IgniteEx;
import org.apache.ignite.internal.processors.query.IgniteSQLException;
import org.apache.ignite.internal.processors.query.calcite.CalciteQueryProcessor;
import org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
Expand All @@ -79,8 +84,22 @@
* Tests SQL engine extension with plugin.
*/
public class OperatorsExtensionIntegrationTest extends AbstractBasicIntegrationTest {
/** */
private static final SqlConformance TEST_CONFORMANCE = new SqlDelegatingConformance(
CalciteQueryProcessor.FRAMEWORK_CONFIG.getParserConfig().conformance()) {
/** {@inheritDoc} */
@Override public boolean isSupportedDualTable() {
return true;
}
};

/** {@inheritDoc} */
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
return getConfiguration(igniteInstanceName, TEST_CONFORMANCE);
}

/** */
private IgniteConfiguration getConfiguration(String igniteInstanceName, SqlConformance conformance) throws Exception {
return super.getConfiguration(igniteInstanceName)
.setPluginProviders(new AbstractTestPluginProvider() {
@Override public String name() {
Expand All @@ -90,12 +109,15 @@ public class OperatorsExtensionIntegrationTest extends AbstractBasicIntegrationT
@Override public <T> @Nullable T createComponent(PluginContext ctx, Class<T> cls) {
if (FrameworkConfig.class.equals(cls)) {
FrameworkConfig cfg = Frameworks.newConfigBuilder(CalciteQueryProcessor.FRAMEWORK_CONFIG)
.parserConfig(CalciteQueryProcessor.FRAMEWORK_CONFIG.getParserConfig()
.withConformance(conformance))
.convertletTable(new ConvertletTable())
.operatorTable(SqlOperatorTables.chain(
new OperatorTable().init(), CalciteQueryProcessor.FRAMEWORK_CONFIG.getOperatorTable()))
.sqlValidatorConfig(
((IgniteSqlValidator.Config)CalciteQueryProcessor.FRAMEWORK_CONFIG.getSqlValidatorConfig())
.withSqlNodeRewriter(new SqlRewriter()))
.withSqlNodeRewriter(new SqlRewriter())
.withConformance(conformance))
.context(Contexts.chain(
CalciteQueryProcessor.FRAMEWORK_CONFIG.getContext(),
Contexts.of(IgniteSqlSemantics.builder()
Expand Down Expand Up @@ -238,6 +260,74 @@ public void testPaginationRoundingPolicy() {
.check();
}

/** */
@Test
public void testDualTable() {
assertQuery("SELECT 1 + 1 FROM dual").returns(2).check();

assertQuery("SELECT * FROM DUAL")
.columnNames("DUMMY")
.returns("X")
.check();

assertQuery("SELECT DUMMY FROM DUAL")
.columnNames("DUMMY")
.returns("X")
.check();

assertQuery("SELECT LAG(rate, 1, rate) OVER (ORDER BY period) FROM "
+ "(SELECT 1 AS rate, 1 AS period FROM dual)")
.returns(1)
.check();
}

/** */
@Test
public void testDualWithisFromRequired() throws Exception {
SqlConformance conformance = new SqlDelegatingConformance(TEST_CONFORMANCE) {
/** {@inheritDoc} */
@Override public boolean isFromRequired() {
return true;
}
};

try (IgniteEx c = startClientGrid(getConfiguration("from-required-client", conformance))) {
assertThrows(c, "SELECT 1", IgniteSQLException.class, "SELECT must have a FROM clause");

assertQuery(c, "SELECT 1 + 1 FROM dual").returns(2).check();
}
}

/** */
@Test
public void testDualTableInNewSchema() {
client.getOrCreateCache(new CacheConfiguration<Integer, Integer>()
.setName("CUSTOM_SCHEMA_MARKER")
.setSqlSchema("CUSTOM_SCHEMA")
.setIndexedTypes(Integer.class, Integer.class));

assertQuery("SELECT DUMMY FROM CUSTOM_SCHEMA.DUAL")
.columnNames("DUMMY")
.returns("X")
.check();
}

/** */
@Test
public void testUserDefinedDualView() {
sql("CREATE VIEW PUBLIC.DUAL AS SELECT 'USER' AS DUMMY");

try {
assertQuery("SELECT DUMMY FROM PUBLIC.DUAL")
.columnNames("DUMMY")
.returns("USER")
.check();
}
finally {
sql("DROP VIEW IF EXISTS PUBLIC.DUAL");
}
}

/** Rewrites LTRIM with 2 parameters. */
public static SqlCall rewriteLtrim(SqlValidator validator, SqlCall call) {
if (call.operandCount() != 2)
Expand Down
Loading