|
| 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 com.dtstack.flink.sql.side; |
| 20 | + |
| 21 | +import com.dtstack.flink.sql.side.rdb.table.RdbSideTableInfo; |
| 22 | +import org.apache.calcite.sql.parser.SqlParseException; |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +/** |
| 32 | + * Date: 2019/12/18 |
| 33 | + * Company: www.dtstack.com |
| 34 | + * @author maqi |
| 35 | + */ |
| 36 | +public class SidePredicatesParserTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testfillPredicatesForSideTable() throws SqlParseException { |
| 40 | + String testSql = |
| 41 | + "insert \n" + |
| 42 | + "into\n" + |
| 43 | + " MyResult\n" + |
| 44 | + " select\n" + |
| 45 | + " MyTable.a,\n" + |
| 46 | + " MyTable.b,\n" + |
| 47 | + " s.c,\n" + |
| 48 | + " s.d\n" + |
| 49 | + " from\n" + |
| 50 | + " MyTable\n" + |
| 51 | + " join\n" + |
| 52 | + " sideTable s\n" + |
| 53 | + " on MyTable.a = s.c\n" + |
| 54 | + " where\n" + |
| 55 | + " MyTable.a='1' and s.d='1' and s.d <> '3' and s.c LIKE '%xx%' and s.c in ('1','2') and s.c between '10' and '23' and s.d is not null\n"; |
| 56 | + |
| 57 | + |
| 58 | + SideTableInfo sideTableInfo = new RdbSideTableInfo(); |
| 59 | + sideTableInfo.setName("sideTable"); |
| 60 | + |
| 61 | + Map<String, SideTableInfo> sideTableMap = new HashMap<>(); |
| 62 | + sideTableMap.put("sideTable", sideTableInfo); |
| 63 | + |
| 64 | + SidePredicatesParser sidePredicatesParser = new SidePredicatesParser(); |
| 65 | + sidePredicatesParser.fillPredicatesForSideTable(testSql, sideTableMap); |
| 66 | + List<PredicateInfo> sideTablePredicateInfoes = sideTableMap.get("sideTable").getPredicateInfoes(); |
| 67 | + |
| 68 | + List<PredicateInfo> expectedsPredicateInfoes = new ArrayList<>(); |
| 69 | + expectedsPredicateInfoes.add(PredicateInfo.builder().setOperatorName("=").setOperatorKind("EQUALS").setOwnerTable("s").setFieldName("d").setCondition("'1'").build()); |
| 70 | + expectedsPredicateInfoes.add(PredicateInfo.builder().setOperatorName("<>").setOperatorKind("NOT_EQUALS").setOwnerTable("s").setFieldName("d").setCondition("'3'").build()); |
| 71 | + expectedsPredicateInfoes.add(PredicateInfo.builder().setOperatorName("LIKE").setOperatorKind("LIKE").setOwnerTable("s").setFieldName("c").setCondition("'%xx%'").build()); |
| 72 | + expectedsPredicateInfoes.add(PredicateInfo.builder().setOperatorName("IN").setOperatorKind("IN").setOwnerTable("s").setFieldName("c").setCondition("'1', '2'").build()); |
| 73 | + expectedsPredicateInfoes.add(PredicateInfo.builder().setOperatorName("BETWEEN ASYMMETRIC").setOperatorKind("BETWEEN").setOwnerTable("s").setFieldName("c").setCondition("'10' AND '23'").build()); |
| 74 | + expectedsPredicateInfoes.add(PredicateInfo.builder().setOperatorName("IS NOT NULL").setOperatorKind("IS_NOT_NULL").setOwnerTable("s").setFieldName("d").setCondition("s.d").build()); |
| 75 | + |
| 76 | + |
| 77 | + Assert.assertEquals(expectedsPredicateInfoes.toString(), sideTablePredicateInfoes.toString()); |
| 78 | + } |
| 79 | +} |
0 commit comments