Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

branch-3.0: [fix](nereids) fix compare ipv4 / ipv6 always equals #47513

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions fe/fe-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ under the License.
<artifactId>guava-testlib</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.java-ipv6/java-ipv6 -->
<dependency>
<groupId>com.googlecode.java-ipv6</groupId>
<artifactId>java-ipv6</artifactId>
<version>0.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ public int compareLiteral(LiteralExpr expr) {
return diff < 0 ? -1 : (diff == 0 ? 0 : 1);
}
// date time will not overflow when doing addition and subtraction
return getStringValue().compareTo(expr.getStringValue());
return Integer.signum(getStringValue().compareTo(expr.getStringValue()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ public int compareLiteral(LiteralExpr expr) {
if (expr instanceof NullLiteral) {
return 1;
}
if (expr == MaxLiteral.MAX_VALUE) {
return -1;
}
if (expr instanceof DecimalLiteral) {
return this.value.compareTo(((DecimalLiteral) expr).value);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public int compareLiteral(LiteralExpr expr) {
if (expr instanceof NullLiteral) {
return 1;
}
if (expr == MaxLiteral.MAX_VALUE) {
return -1;
}
return Double.compare(value, expr.getDoubleValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,18 @@ public boolean isMinValue() {
}

@Override
public int compareLiteral(LiteralExpr expr) {
return 0;
public int compareLiteral(LiteralExpr other) {
if (other instanceof IPv4Literal) {
return Long.compare(value, ((IPv4Literal) other).value);
}
if (other instanceof NullLiteral) {
return 1;
}
if (other instanceof MaxLiteral) {
return -1;
}
throw new RuntimeException("Cannot compare two values with different data types: "
+ this + " (" + getClass() + ") vs " + other + " (" + other.getClass() + ")");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
import org.apache.doris.thrift.TExprNodeType;
import org.apache.doris.thrift.TIPv6Literal;

import com.google.common.base.Suppliers;
import com.google.gson.annotations.SerializedName;
import com.googlecode.ipv6.IPv6Address;

import java.util.function.Supplier;
import java.util.regex.Pattern;

public class IPv6Literal extends LiteralExpr {
Expand All @@ -40,6 +43,8 @@ public class IPv6Literal extends LiteralExpr {
@SerializedName("v")
private String value;

private Supplier<IPv6Address> ipv6Value = Suppliers.memoize(() -> IPv6Address.fromString(value));

/**
* C'tor forcing type, e.g., due to implicit cast
*/
Expand Down Expand Up @@ -95,8 +100,18 @@ public boolean isMinValue() {
}

@Override
public int compareLiteral(LiteralExpr expr) {
return 0;
public int compareLiteral(LiteralExpr other) {
if (other instanceof IPv6Literal) {
return ipv6Value.get().compareTo(((IPv6Literal) other).ipv6Value.get());
}
if (other instanceof NullLiteral) {
return 1;
}
if (other instanceof MaxLiteral) {
return -1;
}
throw new RuntimeException("Cannot compare two values with different data types: "
+ this + " (" + getClass() + ") vs " + other + " (" + other.getClass() + ")");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,13 @@ public int compareLiteral(LiteralExpr expr) {
if (expr instanceof NullLiteral) {
return 1;
}
if (expr instanceof StringLiteral) {
return ((StringLiteral) expr).compareLiteral(this);
}
if (expr == MaxLiteral.MAX_VALUE) {
return -1;
}
if (value == expr.getLongValue()) {
return 0;
} else {
return value > expr.getLongValue() ? 1 : -1;
if (expr instanceof StringLiteral) {
return - ((StringLiteral) expr).compareLiteral(this);
}
return Long.compare(value, expr.getLongValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public void checkLegalityBeforeTypeCoercion() {
for (Expression c : children) {
if (c.getDataType().isComplexType() && !c.getDataType().isArrayType()) {
throw new AnalysisException("comparison predicate could not contains complex type: " + this.toSql());
} else if (c.getDataType().isJsonType()) {
throw new AnalysisException("comparison predicate could not contains json type: " + this.toSql());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.trees.expressions.literal;

import org.apache.doris.common.ExceptionChecker;
import org.apache.doris.utframe.TestWithFeService;

import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class CompareLiteralTest extends TestWithFeService {

@Test
public void testScalar() {
// ip type
checkCompareSameType(0, new IPv4Literal("170.0.0.100"), new IPv4Literal("170.0.0.100"));
checkCompareSameType(1, new IPv4Literal("170.0.0.100"), new IPv4Literal("160.0.0.200"));
checkCompareDiffType(new IPv4Literal("172.0.0.100"), new IPv6Literal("1080:0:0:0:8:800:200C:417A"));
checkCompareSameType(0, new IPv6Literal("1080:0:0:0:8:800:200C:417A"), new IPv6Literal("1080:0:0:0:8:800:200C:417A"));
checkCompareSameType(1, new IPv6Literal("1080:0:0:0:8:800:200C:417A"), new IPv6Literal("1000:0:0:0:8:800:200C:41AA"));
IPv4Literal ipv4 = new IPv4Literal("170.0.0.100");
Assertions.assertEquals(ipv4, new IPv4Literal(ipv4.toLegacyLiteral().getStringValue()));
IPv6Literal ipv6 = new IPv6Literal("1080:0:0:0:8:800:200C:417A");
Assertions.assertEquals(ipv6, new IPv6Literal(ipv6.toLegacyLiteral().getStringValue()));
}

@Test
public void testComplex() throws Exception {
// array type
checkCompareSameType(0,
new ArrayLiteral(ImmutableList.of(new IntegerLiteral(100), new IntegerLiteral(200))),
new ArrayLiteral(ImmutableList.of(new IntegerLiteral(100), new IntegerLiteral(200))));
checkCompareSameType(1,
new ArrayLiteral(ImmutableList.of(new IntegerLiteral(200))),
new ArrayLiteral(ImmutableList.of(new IntegerLiteral(100), new IntegerLiteral(200))));
checkCompareSameType(1,
new ArrayLiteral(ImmutableList.of(new IntegerLiteral(100), new IntegerLiteral(200), new IntegerLiteral(1))),
new ArrayLiteral(ImmutableList.of(new IntegerLiteral(100), new IntegerLiteral(200))));
checkComparableNoException("select array(1,2) = array(1, 2)");
checkComparableNoException("select array(1,2) > array(1, 2)");

// json type
checkNotComparable("select cast ('[1, 2]' as json) = cast('[1, 2]' as json)",
"comparison predicate could not contains json type");
checkNotComparable("select cast('[1, 2]' as json) > cast('[1, 2]' as json)",
"comparison predicate could not contains json type");

// map type
checkNotComparable("select map(1, 2) = map(1, 2)",
"comparison predicate could not contains complex type");
checkNotComparable("select map(1, 2) > map(1, 2)",
"comparison predicate could not contains complex type");
checkNotComparable("select cast('(1, 2)' as map<int, int>) = cast('(1, 2)' as map<int, int>)",
"comparison predicate could not contains complex type");

// struct type
checkNotComparable("select struct(1, 2) = struct(1, 2)",
"comparison predicate could not contains complex type");
checkNotComparable("select struct(1, 2) > struct(1, 2)",
"comparison predicate could not contains complex type");
}

private void checkCompareSameType(int expect, Literal left, Literal right) {
Assertions.assertEquals(expect, left.compareTo(right));
Assertions.assertEquals(- expect, right.compareTo(left));
}

private void checkCompareDiffType(Literal left, Literal right) {
Assertions.assertThrowsExactly(RuntimeException.class, () -> left.compareTo(right));
Assertions.assertThrowsExactly(RuntimeException.class, () -> right.compareTo(left));
}

private void checkComparableNoException(String sql) throws Exception {
ExceptionChecker.expectThrowsNoException(() -> executeSql(sql));
}

private void checkNotComparable(String sql, String expectErrMsg) throws Exception {
ExceptionChecker.expectThrowsWithMsg(IllegalStateException.class, expectErrMsg,
() -> executeSql(sql));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,16 @@ public StmtExecutor getSqlStmtExecutor(String queryStr) throws Exception {
}
}

public void executeSql(String queryStr) throws Exception {
connectContext.getState().reset();
StmtExecutor stmtExecutor = new StmtExecutor(connectContext, queryStr);
stmtExecutor.execute();
if (connectContext.getState().getStateType() == QueryState.MysqlStateType.ERR
|| connectContext.getState().getErrorCode() != null) {
throw new IllegalStateException(connectContext.getState().getErrorMessage());
}
}

public void createDatabase(String db) throws Exception {
String createDbStmtStr = "CREATE DATABASE " + db;
CreateDbStmt createDbStmt = (CreateDbStmt) parseAndAnalyzeStmt(createDbStmtStr);
Expand Down
Loading
Loading