Skip to content

Commit

Permalink
Guard against null JetSqlBackend (#19147)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Gierlach committed Jul 22, 2021
1 parent 25c604b commit 741fccd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.apache.calcite.tools.RuleSet;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

/**
Expand Down Expand Up @@ -96,7 +97,7 @@ public static OptimizerContext create(
List<Object> arguments,
int memberCount,
@Nonnull SqlBackend sqlBackend,
@Nonnull SqlBackend jetSqlBackend
@Nullable SqlBackend jetSqlBackend
) {
// Resolve tables.
HazelcastSchema rootSchema = HazelcastSchemaUtils.createRootSchema(schema);
Expand All @@ -110,7 +111,7 @@ public static OptimizerContext create(
List<Object> arguments,
int memberCount,
@Nonnull SqlBackend sqlBackend,
@Nonnull SqlBackend jetSqlBackend
@Nullable SqlBackend jetSqlBackend
) {
DistributionTraitDef distributionTraitDef = new DistributionTraitDef(memberCount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.calcite.sql.validate.SqlConformance;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

/**
Expand All @@ -59,7 +60,7 @@ public QueryParser(
SqlConformance jetConformance,
List<Object> arguments,
@Nonnull SqlBackend sqlBackend,
@Nonnull SqlBackend jetSqlBackend
@Nullable SqlBackend jetSqlBackend
) {
this.typeFactory = typeFactory;
this.catalogReader = catalogReader;
Expand All @@ -76,7 +77,12 @@ public QueryParseResult parse(String sql) {
try {
return parse(sql, sqlBackend, conformance);
} catch (Exception e) {
return parse(sql, jetSqlBackend, jetConformance);
// TODO: once IMDG engine is removed, move the check (and fail fast) to SqlServiceImpl?
if (jetSqlBackend != null) {
return parse(sql, jetSqlBackend, jetConformance);
} else {
throw e;
}
}
} catch (Exception e) {
String message;
Expand Down
41 changes: 41 additions & 0 deletions hazelcast-sql/src/test/java/com/hazelcast/jet/sql/SqlTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2021 Hazelcast Inc.
*
* Licensed under the Hazelcast Community License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://hazelcast.com/hazelcast-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hazelcast.jet.sql;

import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.jet.core.JetTestSupport;
import com.hazelcast.sql.HazelcastSqlException;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class SqlTest extends JetTestSupport {

@Test
public void when_jetDisabled_and_usingClient_then_sqlThrowsException() {
Config config = smallInstanceConfig();
config.getJetConfig().setEnabled(false);
createHazelcastInstance(config);

HazelcastInstance client = createHazelcastClient();

assertThatThrownBy(() -> client.getSql().execute("SELECT * FROM TABLE(GENERATE_SERIES(1, 1))").iterator().next())
.isInstanceOf(HazelcastSqlException.class)
.hasMessageContaining("Function 'TABLE' does not exist");
}
}

0 comments on commit 741fccd

Please sign in to comment.