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_r355107756
##########
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 numNonPartCol 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 numNonPartCol,
List<String> partColNames, List<Expression> expressions) {
+ List<String> filters = new ArrayList<>(expressions.size());
+ ExpressionExtractor extractor = new
ExpressionExtractor(numNonPartCol, 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> funcToStr
= new HashMap<>();
+
+ static {
+ funcToStr.put(BuiltInFunctionDefinitions.EQUALS, "=");
+ funcToStr.put(BuiltInFunctionDefinitions.NOT_EQUALS,
"<>");
+ funcToStr.put(BuiltInFunctionDefinitions.GREATER_THAN,
">");
+
funcToStr.put(BuiltInFunctionDefinitions.GREATER_THAN_OR_EQUAL, ">=");
+ funcToStr.put(BuiltInFunctionDefinitions.LESS_THAN,
"<");
+
funcToStr.put(BuiltInFunctionDefinitions.LESS_THAN_OR_EQUAL, "<=");
+ }
+
+ // used to shift field reference index
+ private final int numNonPartCol;
+ private final List<String> partColNames;
+
+ ExpressionExtractor(int numNonPartCol, List<String>
partColNames) {
+ this.numNonPartCol = numNonPartCol;
+ this.partColNames = partColNames;
+ }
+
+ private String funcToString(BuiltInFunctionDefinition funcDef) {
+ return funcToStr.containsKey(funcDef) ?
funcToStr.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:
Looks like `and` and `or` not work.
Can you add unit tests for this class?
----------------------------------------------------------------
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