JingsongLi commented on a change in pull request #10381: [FLINK-14513][hive]
Implement listPartitionsByFilter to HiveCatalog
URL: https://github.com/apache/flink/pull/10381#discussion_r355248962
##########
File path:
flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/util/HiveTableUtil.java
##########
@@ -175,4 +186,93 @@ public static boolean requireRelyConstraint(byte trait) {
return (trait & HIVE_CONSTRAINT_RELY) != 0;
}
+ /**
+ * Generates a filter string for partition columns from the given
filter expressions.
+ *
+ * @param partColOffset The number of non-partition columns -- used to
shift field reference index
+ * @param partColNames The names of all partition columns
+ * @param expressions The filter expressions in CNF form
+ * @return an Optional filter string equivalent to the expressions,
which is empty if the expressions can't be handled
+ */
+ public static Optional<String> makePartitionFilter(int partColOffset,
List<String> partColNames, List<Expression> expressions) {
+ List<String> filters = new ArrayList<>(expressions.size());
+ ExpressionExtractor extractor = new
ExpressionExtractor(partColOffset, partColNames);
+ for (Expression expression : expressions) {
+ String str = expression.accept(extractor);
+ if (str == null) {
+ return Optional.empty();
+ }
+ filters.add(str);
+ }
+ return Optional.of(String.join(" and ", filters));
+ }
+
+ private static class ExpressionExtractor implements
ExpressionVisitor<String> {
+
+ private static final Map<FunctionDefinition, String>
FUNC_TO_STR = new HashMap<>();
+
+ static {
+ FUNC_TO_STR.put(BuiltInFunctionDefinitions.EQUALS, "=");
+ FUNC_TO_STR.put(BuiltInFunctionDefinitions.NOT_EQUALS,
"<>");
+
FUNC_TO_STR.put(BuiltInFunctionDefinitions.GREATER_THAN, ">");
+
FUNC_TO_STR.put(BuiltInFunctionDefinitions.GREATER_THAN_OR_EQUAL, ">=");
+ FUNC_TO_STR.put(BuiltInFunctionDefinitions.LESS_THAN,
"<");
+
FUNC_TO_STR.put(BuiltInFunctionDefinitions.LESS_THAN_OR_EQUAL, "<=");
+ }
+
+ // used to shift field reference index
+ private final int partColOffset;
+ private final List<String> partColNames;
+
+ ExpressionExtractor(int partColOffset, List<String>
partColNames) {
+ this.partColOffset = partColOffset;
+ this.partColNames = partColNames;
+ }
+
+ private String funcToString(BuiltInFunctionDefinition funcDef) {
+ return FUNC_TO_STR.containsKey(funcDef) ?
FUNC_TO_STR.get(funcDef) : funcDef.getName();
+ }
+
+ @Override
+ public String visit(CallExpression call) {
+ FunctionDefinition funcDef =
call.getFunctionDefinition();
+ if (funcDef instanceof BuiltInFunctionDefinition) {
+ List<String> operands = new ArrayList<>();
+ for (Expression child : call.getChildren()) {
+ String operand = child.accept(this);
+ if (operand == null) {
+ return null;
+ }
+ operands.add(operand);
+ }
+ if (funcDef == BuiltInFunctionDefinitions.CAST)
{
+ return String.format("cast(%s as %s)",
operands.get(0), operands.get(1));
+ }
+ return "(" + String.join(" " +
funcToString((BuiltInFunctionDefinition) funcDef) + " ", operands) + ")";
Review comment:
It is very dangerous here, maybe hive not support our built-in functions.
So I prefer use white list to support instead of throwing runtime exceptions.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services