|
10 | 10 | package net.sf.jsqlparser.expression.operators.relational; |
11 | 11 |
|
12 | 12 | import net.sf.jsqlparser.JSQLParserException; |
| 13 | +import net.sf.jsqlparser.expression.BinaryExpression; |
| 14 | +import net.sf.jsqlparser.expression.operators.arithmetic.Addition; |
| 15 | +import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseAnd; |
13 | 16 | import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
14 | 17 | import net.sf.jsqlparser.test.TestUtils; |
15 | 18 | import org.junit.jupiter.api.Assertions; |
16 | 19 | import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.params.ParameterizedTest; |
| 21 | +import org.junit.jupiter.params.provider.ValueSource; |
17 | 22 |
|
18 | 23 |
|
19 | 24 | class ComparisonOperatorTest { |
@@ -43,4 +48,95 @@ void testCosineSimilarity() throws JSQLParserException { |
43 | 48 | Assertions.assertInstanceOf(CosineSimilarity.class, |
44 | 49 | CCJSqlParserUtil.parseExpression("embedding <=> '[3,1,2]'")); |
45 | 50 | } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testVectorDistanceOperatorsInProjection() throws JSQLParserException { |
| 54 | + TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT a <-> b FROM t"); |
| 55 | + TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT a <#> b FROM t"); |
| 56 | + TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT a <=> b FROM t"); |
| 57 | + Assertions.assertInstanceOf(GeometryDistance.class, |
| 58 | + CCJSqlParserUtil.parseExpression("a <-> b")); |
| 59 | + Assertions.assertInstanceOf(GeometryDistance.class, |
| 60 | + CCJSqlParserUtil.parseExpression("a <#> b")); |
| 61 | + Assertions.assertInstanceOf(CosineSimilarity.class, |
| 62 | + CCJSqlParserUtil.parseExpression("a <=> b")); |
| 63 | + } |
| 64 | + |
| 65 | + @ParameterizedTest |
| 66 | + @ValueSource(strings = {"<->", "<#>", "<=>"}) |
| 67 | + void testVectorDistanceOperatorsFollowedByComparison(String distance) |
| 68 | + throws JSQLParserException { |
| 69 | + String[] comparators = {"<", ">", "="}; |
| 70 | + for (String comparator : comparators) { |
| 71 | + TestUtils.assertSqlCanBeParsedAndDeparsed( |
| 72 | + "SELECT * FROM t WHERE a " + distance + " b " + comparator + " c"); |
| 73 | + TestUtils.assertSqlCanBeParsedAndDeparsed( |
| 74 | + "SELECT * FROM t WHERE (a " + distance + " b) " + comparator + " c"); |
| 75 | + } |
| 76 | + Assertions.assertInstanceOf(MinorThan.class, |
| 77 | + CCJSqlParserUtil.parseExpression("a " + distance + " b < c")); |
| 78 | + } |
| 79 | + |
| 80 | + @ParameterizedTest |
| 81 | + @ValueSource(strings = {"<->", "<#>", "<=>"}) |
| 82 | + void testVectorDistanceOperatorsInProjectionFollowedByComparison(String distance) |
| 83 | + throws JSQLParserException { |
| 84 | + String[] comparators = {"<", ">", "="}; |
| 85 | + for (String comparator : comparators) { |
| 86 | + TestUtils.assertSqlCanBeParsedAndDeparsed( |
| 87 | + "SELECT a " + distance + " b " + comparator + " c FROM t"); |
| 88 | + TestUtils.assertSqlCanBeParsedAndDeparsed( |
| 89 | + "SELECT (a " + distance + " b) " + comparator + " c FROM t"); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @ParameterizedTest |
| 94 | + @ValueSource(strings = {"<->", "<#>", "<=>"}) |
| 95 | + void testVectorDistanceOperatorsShareBitwisePrecedence(String distance) |
| 96 | + throws JSQLParserException { |
| 97 | + Class<?> distanceType = |
| 98 | + "<=>".equals(distance) ? CosineSimilarity.class : GeometryDistance.class; |
| 99 | + |
| 100 | + TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT a " + distance + " b & c FROM t"); |
| 101 | + TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT a & b " + distance + " c FROM t"); |
| 102 | + |
| 103 | + BinaryExpression bitwiseRoot = |
| 104 | + (BinaryExpression) CCJSqlParserUtil.parseExpression("a " + distance + " b & c"); |
| 105 | + Assertions.assertInstanceOf(BitwiseAnd.class, bitwiseRoot); |
| 106 | + Assertions.assertInstanceOf(distanceType, bitwiseRoot.getLeftExpression()); |
| 107 | + |
| 108 | + BinaryExpression distanceRoot = |
| 109 | + (BinaryExpression) CCJSqlParserUtil.parseExpression("a & b " + distance + " c"); |
| 110 | + Assertions.assertInstanceOf(distanceType, distanceRoot); |
| 111 | + Assertions.assertInstanceOf(BitwiseAnd.class, distanceRoot.getLeftExpression()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void testGeometryDistanceOrderByControl() throws JSQLParserException { |
| 116 | + TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT * FROM t ORDER BY a <-> b"); |
| 117 | + TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT * FROM t ORDER BY a <#> b"); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void testVectorDistancePrecedenceAboveComparisonBelowArithmetic() throws JSQLParserException { |
| 122 | + BinaryExpression geometryComparison = Assertions.assertInstanceOf(MinorThan.class, |
| 123 | + CCJSqlParserUtil.parseExpression("a <-> b + c < 5")); |
| 124 | + GeometryDistance geometryDistance = Assertions.assertInstanceOf(GeometryDistance.class, |
| 125 | + geometryComparison.getLeftExpression()); |
| 126 | + Assertions.assertInstanceOf(Addition.class, geometryDistance.getRightExpression()); |
| 127 | + |
| 128 | + BinaryExpression cosineComparison = Assertions.assertInstanceOf(MinorThan.class, |
| 129 | + CCJSqlParserUtil.parseExpression("a <=> b + c < 5")); |
| 130 | + CosineSimilarity cosineSimilarity = Assertions.assertInstanceOf(CosineSimilarity.class, |
| 131 | + cosineComparison.getLeftExpression()); |
| 132 | + Assertions.assertInstanceOf(Addition.class, cosineSimilarity.getRightExpression()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void testComparisonChainsRemainInvalid() { |
| 137 | + Assertions.assertThrows(JSQLParserException.class, |
| 138 | + () -> CCJSqlParserUtil.parse("SELECT * FROM t WHERE a < b < c")); |
| 139 | + Assertions.assertThrows(JSQLParserException.class, |
| 140 | + () -> CCJSqlParserUtil.parse("SELECT * FROM t WHERE a <-> b < c < d")); |
| 141 | + } |
46 | 142 | } |
0 commit comments