Skip to content

Commit

Permalink
refactor: simplify sql permission (DataLinkDC#1845)
Browse files Browse the repository at this point in the history
* refactor: simplify sql permission

* Spotless Apply

---------

Co-authored-by: leechor <leechor@users.noreply.github.com>
  • Loading branch information
leechor and leechor authored Apr 4, 2023
1 parent fbf46ae commit d364c43
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 284 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.dinky.context.CustomTableEnvironmentContext;
import org.dinky.context.RowLevelPermissionsContext;
import org.dinky.executor.ExtendedParser;

import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.validate.SqlValidator;
Expand Down Expand Up @@ -134,11 +135,16 @@ private SqlNode addPermission(SqlNode where, String tableName, String tableAlias
if (permissionsMap != null) {
String permissionsStatement = permissionsMap.get(tableName);
if (permissionsStatement != null && !"".equals(permissionsStatement)) {
permissions =
(SqlBasicCall)
CustomTableEnvironmentContext.get()
.getParser()
.parseExpression(permissionsStatement);
if (CustomTableEnvironmentContext.get().getParser() instanceof ExtendedParser) {
ExtendedParser extendedParser =
(ExtendedParser) CustomTableEnvironmentContext.get().getParser();
permissions =
(SqlBasicCall)
(extendedParser.getCustomParser())
.parseExpression(permissionsStatement);
} else {
throw new RuntimeException("CustomParser is not set");
}
}
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public Planner getPlanner() {

@Override
public void injectParser(CustomParser parser) {
ReflectUtil.setFieldValue(
getPlanner(), "parser", new ParserWrapper(getPlanner().getParser(), parser));
ReflectUtil.setFieldValue(getPlanner(), "parser", new ParserWrapper(parser));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
*
* 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.dinky.executor;

import org.apache.flink.table.delegation.Parser;

/** */
public interface ExtendedParser extends Parser {
CustomParser getCustomParser();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

package org.dinky.executor;

import org.apache.calcite.sql.SqlNode;
import org.apache.flink.table.catalog.UnresolvedIdentifier;
import org.apache.flink.table.delegation.Parser;
import org.apache.flink.table.expressions.ResolvedExpression;
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.types.logical.LogicalType;
Expand All @@ -31,13 +29,11 @@

import javax.annotation.Nullable;

public class ParserWrapper implements Parser {
public class ParserWrapper implements ExtendedParser {

private Parser parser;
private CustomParser customParser;

public ParserWrapper(Parser parser, CustomParser customParser) {
this.parser = parser;
public ParserWrapper(CustomParser customParser) {
this.customParser = customParser;
}

Expand All @@ -47,27 +43,28 @@ public List<Operation> parse(String statement) {
if (result != null) {
return result;
}
return parser.parse(statement);

return customParser.getParser().parse(statement);
}

@Override
public UnresolvedIdentifier parseIdentifier(String identifier) {
return parser.parseIdentifier(identifier);
return customParser.getParser().parseIdentifier(identifier);
}

@Override
public ResolvedExpression parseSqlExpression(
String sqlExpression, RowType inputRowType, @Nullable LogicalType outputType) {
return parser.parseSqlExpression(sqlExpression, inputRowType, outputType);
return customParser.getParser().parseSqlExpression(sqlExpression, inputRowType, outputType);
}

@Override
public String[] getCompletionHints(String statement, int position) {
return parser.getCompletionHints(statement, position);
return customParser.getParser().getCompletionHints(statement, position);
}

@Override
public SqlNode parseExpression(String sqlExpression) {
return parser.parseExpression(sqlExpression);
public CustomParser getCustomParser() {
return customParser;
}
}
Loading

0 comments on commit d364c43

Please sign in to comment.