Skip to content
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 @@ -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 'X' AS 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.getParserConfig().conformance().isSupportedDualTable()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be more appropriate to use the validator conformance here?

&& 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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure whether we really need this, but Calcite supports this property. If we decide to support it, let’s implement it the same way as Calcite does, because its implementation also works with grammars that do not allow SELECT without a FROM clause.
VALUES ('X')

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@
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.processors.query.calcite.CalciteQueryProcessor;
import org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
Expand All @@ -79,6 +82,15 @@
* 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 super.getConfiguration(igniteInstanceName)
Expand All @@ -90,12 +102,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(TEST_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(TEST_CONFORMANCE))
.context(Contexts.chain(
CalciteQueryProcessor.FRAMEWORK_CONFIG.getContext(),
Contexts.of(IgniteSqlSemantics.builder()
Expand Down Expand Up @@ -238,6 +253,57 @@ 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 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