Skip to content

Commit

Permalink
[Enhancement] support mysql key words in where caluse
Browse files Browse the repository at this point in the history
Signed-off-by: zombee0 <[email protected]>
  • Loading branch information
zombee0 committed Nov 27, 2023
1 parent f32ef10 commit 19f3274
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
15 changes: 9 additions & 6 deletions fe/fe-core/src/main/java/com/starrocks/planner/JDBCScanNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,20 @@ private void createJDBCTableFilters() {
List<SlotRef> slotRefs = Lists.newArrayList();
Expr.collectList(conjuncts, SlotRef.class, slotRefs);
ExprSubstitutionMap sMap = new ExprSubstitutionMap();
for (SlotRef slotRef : slotRefs) {
SlotRef tmpRef = (SlotRef) slotRef.clone();
tmpRef.setTblName(null);

sMap.put(slotRef, tmpRef);
}
JDBCResource resource = (JDBCResource) GlobalStateMgr.getCurrentState().getResourceMgr()
.getResource(table.getResourceName());
// Compatible with jdbc catalog
String jdbcURI = resource != null ? resource.getProperty(JDBCResource.URI) : table.getProperty(JDBCResource.URI);
boolean isMySQL = jdbcURI.startsWith("jdbc:mysql");
for (SlotRef slotRef : slotRefs) {
SlotRef tmpRef = (SlotRef) slotRef.clone();
tmpRef.setTblName(null);
if (isMySQL && tmpRef.getLabel() != null && !tmpRef.getLabel().startsWith("`")) {
tmpRef.setLabel("`" + tmpRef.getLabel() + "`");
}
sMap.put(slotRef, tmpRef);
}

ArrayList<Expr> mysqlConjuncts = Expr.cloneList(conjuncts, sMap);
for (Expr p : mysqlConjuncts) {
filters.add(p.toJDBCSQL(isMySQL));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed 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
//
// https://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.

package com.starrocks.sql.plan;

import com.starrocks.common.FeConstants;
import com.starrocks.utframe.StarRocksAssert;
import org.junit.BeforeClass;
import org.junit.Test;

public class JDBCTablePlanTest extends PlanTestBase {

@BeforeClass
public static void beforeClass() throws Exception {
PlanTestBase.beforeClass();
StarRocksAssert starRocksAssert = new StarRocksAssert(connectContext);
FeConstants.runningUnitTest = true;
starRocksAssert.withTable("create external table test.jdbc_key_words_test\n" +
"(a int, `schema` varchar(20))\n" +
"ENGINE=jdbc\n" +
"PROPERTIES (\n" +
"\"resource\"=\"jdbc_test\",\n" +
"\"table\"=\"test_table\"\n" +
");");
FeConstants.runningUnitTest = false;
}

@Test
public void testStruct() throws Exception {
String sql = "select * from test.jdbc_key_words_test where `schema` = \"test\"";
String plan = getFragmentPlan(sql);
assertContains(plan, "`schema` = 'test'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ public static void beforeClass() throws Exception {
"\"password\"=\"test_passwd\",\n" +
"\"driver_url\"=\"test_driver_url\",\n" +
"\"driver_class\"=\"test.driver.class\",\n" +
"\"jdbc_uri\"=\"test_uri\"\n" +
"\"jdbc_uri\"=\"jdbc:mysql://127.0.0.1:3306\"\n" +
");")
.withTable("create external table test.jdbc_test\n" +
"(a int, b varchar(20), c float)\n" +
Expand Down

0 comments on commit 19f3274

Please sign in to comment.