forked from apache/kylin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KYLIN-4554] validate filter condition on model saving
verify filter condition using the following login 1 select * from tableName where {filterCondition} pass calcite parse 2 all column in filter condition must be model table column
- Loading branch information
1 parent
b577521
commit 9589802
Showing
4 changed files
with
150 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
core-metadata/src/main/java/org/apache/kylin/metadata/util/ModelUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* http://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 org.apache.kylin.metadata.util; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import org.apache.calcite.sql.SqlCall; | ||
import org.apache.calcite.sql.SqlIdentifier; | ||
import org.apache.calcite.sql.SqlNode; | ||
import org.apache.calcite.sql.SqlSelect; | ||
import org.apache.calcite.sql.parser.SqlParser; | ||
import org.apache.calcite.sql.util.SqlBasicVisitor; | ||
import org.apache.kylin.metadata.model.TableDesc; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
public class ModelUtil { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ModelUtil.class); | ||
|
||
public static void verifyFilterCondition(String factTableName, String filterCondition, TableDesc tableDesc) | ||
throws Exception { | ||
StringBuilder checkSql = new StringBuilder(); | ||
checkSql.append("select * from ").append(factTableName).append(" where ").append(filterCondition); | ||
|
||
SqlCall inputToNode = (SqlCall) parse(doubleQuoteKeywordDefault(checkSql.toString())); | ||
SqlVerify sqlVerify = new SqlVerify(tableDesc); | ||
sqlVerify.visit(inputToNode); | ||
|
||
} | ||
|
||
public static SqlNode parse(String sql) throws Exception { | ||
SqlParser.ConfigBuilder parserBuilder = SqlParser.configBuilder().setIdentifierMaxLength(300); | ||
SqlParser sqlParser = SqlParser.create(sql, parserBuilder.build()); | ||
return sqlParser.parseQuery(); | ||
} | ||
|
||
private static class SqlVerify extends SqlBasicVisitor { | ||
|
||
private TableDesc tableDesc; | ||
|
||
SqlVerify(TableDesc tableDesc) { | ||
this.tableDesc = tableDesc; | ||
} | ||
|
||
@Override | ||
public Object visit(SqlCall call) { | ||
SqlSelect select = (SqlSelect) call; | ||
WhereColumnVerify.verify(select.getWhere(), tableDesc); | ||
return null; | ||
} | ||
} | ||
|
||
private static class WhereColumnVerify extends SqlBasicVisitor { | ||
|
||
private List<String> allSqlIdentifier = Lists.newArrayList(); | ||
|
||
static void verify(SqlNode whereNode, TableDesc tableDesc) { | ||
WhereColumnVerify whereColumnVerify = new WhereColumnVerify(); | ||
whereNode.accept(whereColumnVerify); | ||
whereColumnVerify.allSqlIdentifier.stream().forEach(col -> { | ||
if (tableDesc.findColumnByName(col) == null) { | ||
String verifyError = String.format(Locale.ROOT, | ||
"filter condition col: %s is not a column in the table ", col); | ||
logger.error(verifyError); | ||
throw new IllegalArgumentException(verifyError); | ||
} | ||
}); | ||
} | ||
|
||
public Object visit(SqlIdentifier id) { | ||
allSqlIdentifier.add(id.names.get(0)); | ||
return null; | ||
} | ||
} | ||
|
||
public static String doubleQuoteKeywordDefault(String sql) { | ||
sql = sql.replaceAll("(?i)default\\.", "\"DEFAULT\"."); | ||
sql = sql.replace("defaultCatalog.", ""); | ||
sql = sql.replace("\"defaultCatalog\".", ""); | ||
return sql; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters