Skip to content

Commit f016170

Browse files
IGNITE-27105 SQL: Add JDBC local query property - Fixes #12519.
Signed-off-by: Aleksey Plekhanov <[email protected]>
1 parent a7b6d1d commit f016170

File tree

17 files changed

+276
-56
lines changed

17 files changed

+276
-56
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ignite.internal.processors.query.calcite.jdbc;
19+
20+
import java.sql.Connection;
21+
import java.sql.ResultSet;
22+
import java.sql.SQLException;
23+
import java.sql.Statement;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import org.apache.ignite.IgniteException;
27+
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
28+
29+
/**
30+
* Abstract JDBC test.
31+
*/
32+
public abstract class AbstractJdbcTest extends GridCommonAbstractTest {
33+
/** URL. */
34+
protected static final String URL = "jdbc:ignite:thin://127.0.0.1";
35+
36+
/** */
37+
protected void execute(Statement stmt, String sql) {
38+
try {
39+
stmt.execute(sql);
40+
}
41+
catch (SQLException e) {
42+
throw new IgniteException(e.getMessage(), e);
43+
}
44+
}
45+
46+
/** */
47+
protected List<List<Object>> executeQuery(Connection conn, String sql) throws SQLException {
48+
return executeQuery(conn.createStatement(), sql);
49+
}
50+
51+
/** */
52+
protected List<List<Object>> executeQuery(Statement stmt, String sql) {
53+
try (ResultSet rs = stmt.executeQuery(sql)) {
54+
List<List<Object>> res = new ArrayList<>();
55+
while (rs.next()) {
56+
List<Object> row = new ArrayList<>();
57+
58+
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++)
59+
row.add(rs.getObject(i));
60+
61+
res.add(row);
62+
}
63+
64+
return res;
65+
}
66+
catch (SQLException e) {
67+
throw new IgniteException(e.getMessage(), e);
68+
}
69+
}
70+
}

modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/jdbc/JdbcCrossEngineTest.java

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,23 @@
2121
import java.sql.Connection;
2222
import java.sql.Date;
2323
import java.sql.DriverManager;
24-
import java.sql.ResultSet;
25-
import java.sql.SQLException;
2624
import java.sql.Statement;
2725
import java.sql.Time;
2826
import java.sql.Timestamp;
29-
import java.util.ArrayList;
3027
import java.util.List;
3128
import java.util.Objects;
3229
import java.util.UUID;
3330
import java.util.function.IntConsumer;
34-
import org.apache.ignite.IgniteException;
3531
import org.apache.ignite.calcite.CalciteQueryEngineConfiguration;
3632
import org.apache.ignite.configuration.IgniteConfiguration;
3733
import org.apache.ignite.configuration.SqlConfiguration;
3834
import org.apache.ignite.indexing.IndexingQueryEngineConfiguration;
39-
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
4035
import org.junit.Test;
4136

4237
/**
4338
* Cross check queries on experimental and non-experimental SQL engines.
4439
*/
45-
public class JdbcCrossEngineTest extends GridCommonAbstractTest {
46-
/** URL. */
47-
private static final String url = "jdbc:ignite:thin://127.0.0.1";
48-
40+
public class JdbcCrossEngineTest extends AbstractJdbcTest {
4941
/** Nodes count. */
5042
private static final int nodesCnt = 3;
5143

@@ -76,7 +68,7 @@ public class JdbcCrossEngineTest extends GridCommonAbstractTest {
7668
startGrids(nodesCnt);
7769

7870
for (int i = 0; i < engineNames.length; i++) {
79-
conns[i] = DriverManager.getConnection(url + "?queryEngine=" + engineNames[i]);
71+
conns[i] = DriverManager.getConnection(URL + "?queryEngine=" + engineNames[i]);
8072
conns[i].setSchema("PUBLIC");
8173
stmts[i] = conns[i].createStatement();
8274

@@ -203,36 +195,6 @@ private void checkInsertDefaultValue(String sqlType, String sqlVal, Object expec
203195
);
204196
}
205197

206-
/** */
207-
private void execute(Statement stmt, String sql) {
208-
try {
209-
stmt.execute(sql);
210-
}
211-
catch (SQLException e) {
212-
throw new IgniteException(e.getMessage(), e);
213-
}
214-
}
215-
216-
/** */
217-
private List<List<Object>> executeQuery(Statement stmt, String sql) {
218-
try (ResultSet rs = stmt.executeQuery(sql)) {
219-
List<List<Object>> res = new ArrayList<>();
220-
while (rs.next()) {
221-
List<Object> row = new ArrayList<>();
222-
223-
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++)
224-
row.add(rs.getObject(i));
225-
226-
res.add(row);
227-
}
228-
229-
return res;
230-
}
231-
catch (SQLException e) {
232-
throw new IgniteException(e.getMessage(), e);
233-
}
234-
}
235-
236198
/** */
237199
private void crossCheck(IntConsumer consumer1, IntConsumer consumer2) {
238200
// Execute consumer1 on indexing engine, consumer2 on calcite engine.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ignite.internal.processors.query.calcite.jdbc;
19+
20+
import java.sql.Connection;
21+
import java.sql.DriverManager;
22+
import java.util.List;
23+
import org.apache.ignite.cache.query.SqlFieldsQuery;
24+
import org.apache.ignite.calcite.CalciteQueryEngineConfiguration;
25+
import org.apache.ignite.configuration.IgniteConfiguration;
26+
import org.apache.ignite.configuration.SqlConfiguration;
27+
import org.apache.ignite.internal.IgniteEx;
28+
import org.junit.Test;
29+
30+
/** */
31+
public class JdbcLocalFlagTest extends AbstractJdbcTest {
32+
/** {@inheritDoc} */
33+
@Override protected IgniteConfiguration getConfiguration(String instanceName) throws Exception {
34+
IgniteConfiguration cfg = super.getConfiguration(instanceName);
35+
36+
cfg.setSqlConfiguration(new SqlConfiguration()
37+
.setQueryEnginesConfiguration(new CalciteQueryEngineConfiguration().setDefault(true)));
38+
39+
return cfg;
40+
}
41+
42+
/** {@inheritDoc} */
43+
@Override protected void afterTest() {
44+
stopAllGrids();
45+
}
46+
47+
/** */
48+
@Test
49+
public void testLocalFlag() throws Exception {
50+
IgniteEx ignite = startGrids(3);
51+
52+
int keys = 100;
53+
54+
sql(ignite, "CREATE TABLE test(id INT PRIMARY KEY, val VARCHAR)");
55+
56+
for (int i = 0; i < keys; i++)
57+
sql(ignite, "INSERT INTO test(id, val) VALUES (?, ?)", i, "val" + i);
58+
59+
try (Connection conn = DriverManager.getConnection(URL)) {
60+
List<List<Object>> res = executeQuery(conn, "SELECT * FROM test");
61+
62+
assertEquals(keys, res.size());
63+
}
64+
65+
try (Connection conn = DriverManager.getConnection(URL + "?local=true")) {
66+
List<List<Object>> res = executeQuery(conn, "SELECT * FROM test");
67+
68+
assertTrue(keys > res.size());
69+
}
70+
}
71+
72+
/** */
73+
private List<List<?>> sql(IgniteEx ignite, String sql, Object... args) {
74+
return ignite.context().query().querySqlFields(new SqlFieldsQuery(sql).setArgs(args), false).getAll();
75+
}
76+
}

modules/calcite/src/test/java/org/apache/ignite/testsuites/IgniteCalciteTestSuite.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
import org.apache.ignite.internal.processors.query.calcite.exec.LogicalRelImplementorTest;
2121
import org.apache.ignite.internal.processors.query.calcite.exec.NumericTypesPrecisionsTest;
22-
import org.apache.ignite.internal.processors.query.calcite.jdbc.JdbcConnectionEnabledPropertyTest;
23-
import org.apache.ignite.internal.processors.query.calcite.jdbc.JdbcSetClientInfoTest;
24-
import org.apache.ignite.internal.processors.query.calcite.jdbc.JdbcThinTransactionalSelfTest;
2522
import org.apache.ignite.internal.processors.query.calcite.message.CalciteCommunicationMessageSerializationTest;
2623
import org.apache.ignite.internal.processors.query.calcite.sql.SqlCustomParserTest;
2724
import org.apache.ignite.internal.processors.query.calcite.sql.SqlReservedWordsTest;
@@ -39,6 +36,7 @@
3936
ExecutionTestSuite.class,
4037
IntegrationTestSuite.class,
4138
UtilTestSuite.class,
39+
JdbcTestSuite.class,
4240

4341
SqlCustomParserTest.class,
4442
SqlReservedWordsTest.class,
@@ -51,10 +49,6 @@
5149

5250
SqlTransactionsIsolationTest.class,
5351
SqlTransactionsUnsupportedModesTest.class,
54-
55-
JdbcThinTransactionalSelfTest.class,
56-
JdbcSetClientInfoTest.class,
57-
JdbcConnectionEnabledPropertyTest.class
5852
})
5953
public class IgniteCalciteTestSuite {
6054
}

modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@
8585
import org.apache.ignite.internal.processors.query.calcite.integration.tpch.TpchScale001Test;
8686
import org.apache.ignite.internal.processors.query.calcite.integration.tpch.TpchScale010Test;
8787
import org.apache.ignite.internal.processors.query.calcite.integration.tpch.TpchScale100Test;
88-
import org.apache.ignite.internal.processors.query.calcite.jdbc.JdbcCrossEngineTest;
89-
import org.apache.ignite.internal.processors.query.calcite.jdbc.JdbcQueryTest;
9088
import org.apache.ignite.internal.processors.query.calcite.rules.JoinCommuteRulesTest;
9189
import org.apache.ignite.internal.processors.query.calcite.rules.JoinOrderOptimizationTest;
9290
import org.apache.ignite.internal.processors.query.calcite.rules.OrToUnionRuleTest;
@@ -104,8 +102,6 @@
104102
ProjectScanMergeRuleTest.class,
105103
CalciteQueryProcessorTest.class,
106104
CalciteErrorHandlilngIntegrationTest.class,
107-
JdbcQueryTest.class,
108-
JdbcCrossEngineTest.class,
109105
CalciteBasicSecondaryIndexIntegrationTest.class,
110106
CancelTest.class,
111107
DateTimeTest.class,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ignite.testsuites;
19+
20+
import org.apache.ignite.internal.processors.query.calcite.jdbc.JdbcConnectionEnabledPropertyTest;
21+
import org.apache.ignite.internal.processors.query.calcite.jdbc.JdbcCrossEngineTest;
22+
import org.apache.ignite.internal.processors.query.calcite.jdbc.JdbcLocalFlagTest;
23+
import org.apache.ignite.internal.processors.query.calcite.jdbc.JdbcQueryTest;
24+
import org.apache.ignite.internal.processors.query.calcite.jdbc.JdbcSetClientInfoTest;
25+
import org.apache.ignite.internal.processors.query.calcite.jdbc.JdbcThinTransactionalSelfTest;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.Suite;
28+
29+
/**
30+
* Calcite JDBC tests.
31+
*/
32+
@RunWith(Suite.class)
33+
@Suite.SuiteClasses({
34+
JdbcQueryTest.class,
35+
JdbcCrossEngineTest.class,
36+
JdbcThinTransactionalSelfTest.class,
37+
JdbcSetClientInfoTest.class,
38+
JdbcConnectionEnabledPropertyTest.class,
39+
JdbcLocalFlagTest.class,
40+
})
41+
public class JdbcTestSuite {
42+
}

modules/clients/src/test/java/org/apache/ignite/jdbc/thin/JdbcThinDataSourceSelfTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
@SuppressWarnings("ThrowableNotThrown")
5353
public class JdbcThinDataSourceSelfTest extends JdbcThinAbstractSelfTest {
5454
/** {@inheritDoc} */
55-
@SuppressWarnings("deprecation")
5655
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
5756
IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
5857

@@ -141,6 +140,10 @@ public void testResetUrl() throws Exception {
141140
assertNull(ids.getSchema());
142141
assertEquals(DFLT_LAZY, ids.isLazy());
143142
assertTrue(ids.isCollocated());
143+
144+
ids.setUrl("jdbc:ignite:thin://127.0.0.1:10800?local=true");
145+
146+
assertTrue(ids.isLocal());
144147
}
145148

146149
/**
@@ -161,6 +164,7 @@ public void testSqlHints() throws Exception {
161164
assertEquals(DFLT_LAZY, io.connectionProperties().isLazy());
162165
assertFalse(io.connectionProperties().isDistributedJoins());
163166
assertFalse(io.connectionProperties().isReplicatedOnly());
167+
assertFalse(io.connectionProperties().isLocal());
164168
}
165169
}
166170

@@ -170,6 +174,7 @@ public void testSqlHints() throws Exception {
170174
ids.setLazy(!DFLT_LAZY);
171175
ids.setDistributedJoins(true);
172176
ids.setReplicatedOnly(true);
177+
ids.setLocal(true);
173178

174179
try (Connection conn = ids.getConnection()) {
175180

@@ -180,6 +185,7 @@ public void testSqlHints() throws Exception {
180185
assertEquals(!DFLT_LAZY, io.connectionProperties().isLazy());
181186
assertTrue(io.connectionProperties().isDistributedJoins());
182187
assertTrue(io.connectionProperties().isReplicatedOnly());
188+
assertTrue(io.connectionProperties().isLocal());
183189
}
184190
}
185191
}

modules/core/src/main/java/org/apache/ignite/IgniteJdbcThinDataSource.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,20 @@ public void setLazy(boolean lazy) {
325325
props.setLazy(lazy);
326326
}
327327

328+
/**
329+
* @return Local query flag.
330+
*/
331+
public boolean isLocal() {
332+
return props.isLocal();
333+
}
334+
335+
/**
336+
* @param loc Local query flag.
337+
*/
338+
public void setLocal(boolean loc) {
339+
props.setLocal(loc);
340+
}
341+
328342
/**
329343
* @return Skip reducer on update flag.
330344
*/

modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/ConnectionProperties.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,4 +597,14 @@ public void setPartitionAwarenessPartitionDistributionsCacheSize(
597597
* @param transactionLabel Transaction label.
598598
*/
599599
public void setTransactionLabel(String transactionLabel);
600+
601+
/**
602+
* @return Local flag.
603+
*/
604+
public boolean isLocal();
605+
606+
/**
607+
* @param loc Local flag.
608+
*/
609+
public void setLocal(boolean loc);
600610
}

0 commit comments

Comments
 (0)