|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.python; |
| 20 | + |
| 21 | +import org.apache.paimon.data.BinaryString; |
| 22 | +import org.apache.paimon.predicate.Predicate; |
| 23 | +import org.apache.paimon.predicate.PredicateBuilder; |
| 24 | +import org.apache.paimon.types.DataType; |
| 25 | +import org.apache.paimon.types.RowType; |
| 26 | + |
| 27 | +import java.util.List; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +/** For building Predicate. */ |
| 31 | +public class PredicationUtil { |
| 32 | + |
| 33 | + public static Predicate build( |
| 34 | + RowType rowType, |
| 35 | + PredicateBuilder builder, |
| 36 | + String method, |
| 37 | + int index, |
| 38 | + List<Object> literals) { |
| 39 | + literals = |
| 40 | + literals.stream() |
| 41 | + .map(l -> convertJavaObject(rowType.getTypeAt(index), l)) |
| 42 | + .collect(Collectors.toList()); |
| 43 | + switch (method) { |
| 44 | + case "equal": |
| 45 | + return builder.equal(index, literals.get(0)); |
| 46 | + case "notEqual": |
| 47 | + return builder.notEqual(index, literals.get(0)); |
| 48 | + case "lessThan": |
| 49 | + return builder.lessThan(index, literals.get(0)); |
| 50 | + case "lessOrEqual": |
| 51 | + return builder.lessOrEqual(index, literals.get(0)); |
| 52 | + case "greaterThan": |
| 53 | + return builder.greaterThan(index, literals.get(0)); |
| 54 | + case "greaterOrEqual": |
| 55 | + return builder.greaterOrEqual(index, literals.get(0)); |
| 56 | + case "isNull": |
| 57 | + return builder.isNull(index); |
| 58 | + case "isNotNull": |
| 59 | + return builder.isNotNull(index); |
| 60 | + case "startsWith": |
| 61 | + return builder.startsWith(index, literals.get(0)); |
| 62 | + case "endsWith": |
| 63 | + return builder.endsWith(index, literals.get(0)); |
| 64 | + case "contains": |
| 65 | + return builder.contains(index, literals.get(0)); |
| 66 | + case "in": |
| 67 | + return builder.in(index, literals); |
| 68 | + case "notIn": |
| 69 | + return builder.notIn(index, literals); |
| 70 | + case "between": |
| 71 | + return builder.between(index, literals.get(0), literals.get(1)); |
| 72 | + default: |
| 73 | + throw new UnsupportedOperationException( |
| 74 | + "Unknown PredicateBuilder method " + method); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** Some type is not convenient to transfer from Python to Java. */ |
| 79 | + private static Object convertJavaObject(DataType literalType, Object literal) { |
| 80 | + switch (literalType.getTypeRoot()) { |
| 81 | + case BOOLEAN: |
| 82 | + case DOUBLE: |
| 83 | + case INTEGER: |
| 84 | + return literal; |
| 85 | + case CHAR: |
| 86 | + case VARCHAR: |
| 87 | + return BinaryString.fromString((String) literal); |
| 88 | + case FLOAT: |
| 89 | + return ((Number) literal).floatValue(); |
| 90 | + case TINYINT: |
| 91 | + return ((Number) literal).byteValue(); |
| 92 | + case SMALLINT: |
| 93 | + return ((Number) literal).shortValue(); |
| 94 | + case BIGINT: |
| 95 | + return ((Number) literal).longValue(); |
| 96 | + default: |
| 97 | + throw new UnsupportedOperationException( |
| 98 | + "Unsupported predicate leaf type " + literalType.getTypeRoot().name()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public static Predicate buildAnd(List<Predicate> predicates) { |
| 103 | + // 'and' is keyword of Python |
| 104 | + return PredicateBuilder.and(predicates); |
| 105 | + } |
| 106 | + |
| 107 | + public static Predicate buildOr(List<Predicate> predicates) { |
| 108 | + // 'or' is keyword of Python |
| 109 | + return PredicateBuilder.or(predicates); |
| 110 | + } |
| 111 | +} |
0 commit comments