Skip to content

Commit

Permalink
Merge pull request #63 from ni-ze/temp
Browse files Browse the repository at this point in the history
[ISSUE #64]Make the expression right when filter data
  • Loading branch information
ni-ze authored Jan 23, 2023
2 parents f0691dc + 2e3bde6 commit e77da08
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.rsqldb.common.exception;

public class RSQLClientException extends RuntimeException {
public RSQLClientException() {
}

public RSQLClientException(String message) {
super(message);
}

public RSQLClientException(String message, Throwable cause) {
super(message, cause);
}

public RSQLClientException(Throwable cause) {
super(cause);
}

public RSQLClientException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ public Node visitBetweenExpression(SqlParser.BetweenExpressionContext ctx) {
String high = ctx.NUMBER(1).getText();


return new RangeValueExpression(ParserUtil.getText(ctx), field, Long.parseLong(low), Long.parseLong(high));
return new RangeValueExpression(ParserUtil.getText(ctx), field, Double.parseDouble(low), Double.parseDouble(high));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
import com.alibaba.rsqldb.parser.model.Node;
import com.alibaba.rsqldb.parser.model.Operator;
import com.alibaba.rsqldb.parser.model.baseType.BooleanType;
import com.alibaba.rsqldb.parser.model.baseType.Literal;
import com.alibaba.rsqldb.parser.model.baseType.NumberType;
import com.alibaba.rsqldb.parser.model.baseType.StringType;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.JsonNode;
import org.antlr.v4.runtime.ParserRuleContext;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.TextNode;

import java.util.Objects;

// fieldName > 10 and AVG(fieldName) < 12 ...
@JsonTypeInfo(
Expand Down Expand Up @@ -52,4 +57,40 @@ public Expression(String content) {
* @return
*/
public abstract boolean isTrue(JsonNode jsonNode);


protected boolean isEqual(JsonNode node, Literal<?> literal) {
if (node == null && literal == null) {
return true;
}

if (node == null || literal == null) {
return false;
}

String value = node.asText();
String target = String.valueOf(literal.result());

if (literal instanceof StringType) {
if (!(node instanceof TextNode)) {
return false;
}

return Objects.equals(value, target);
} else if (literal instanceof NumberType) {
if (!(node instanceof NumericNode)) {
return false;
}

return Objects.equals(value, target);
} else if (literal instanceof BooleanType) {
if (!(node instanceof BooleanNode)) {
return false;
}

return Objects.equals(value, target);
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import com.alibaba.rsqldb.parser.model.Operator;
import com.alibaba.rsqldb.parser.model.baseType.Literal;
import com.alibaba.rsqldb.parser.model.baseType.MultiLiteral;
import com.alibaba.rsqldb.parser.model.baseType.StringType;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.TextNode;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
Expand Down Expand Up @@ -57,15 +59,13 @@ public Operator getOperator() {
public boolean isTrue(JsonNode jsonNode) {
String fieldName = this.getField().getFieldName();
JsonNode node = jsonNode.get(fieldName);

String value = node.asText();
if (node == null) {
return this.values == null;
}

List<Literal<?>> literals = values.getLiterals();
for (Literal<?> literal : literals) {
String target = String.valueOf(literal.result());
if (StringUtils.equalsIgnoreCase(value, target)) {
return true;
}
return super.isEqual(node, literal);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@

@JsonIgnoreProperties(ignoreUnknown = true)
public class RangeValueExpression extends SingleExpression {
private long low;
private long high;
private double low;
private double high;

@JsonCreator
public RangeValueExpression(@JsonProperty("content") String content, @JsonProperty("fieldName") Field field,
@JsonProperty("low") long low, @JsonProperty("high") long high) {
@JsonProperty("low") double low, @JsonProperty("high") double high) {
super(content, field, Operator.BETWEEN_AND);
this.low = low;
this.high = high;
}

public long getLow() {
public double getLow() {
return low;
}

public void setLow(long low) {
public void setLow(double low) {
this.low = low;
}

public long getHigh() {
public double getHigh() {
return high;
}

public void setHigh(long high) {
public void setHigh(double high) {
this.high = high;
}

Expand All @@ -64,7 +64,7 @@ public boolean isTrue(JsonNode jsonNode) {
return false;
}

long value = node.asLong();
double value = node.asDouble();

return low <= value && value <= high;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@
import com.alibaba.rsqldb.common.exception.SyntaxErrorException;
import com.alibaba.rsqldb.parser.model.Field;
import com.alibaba.rsqldb.parser.model.Operator;
import com.alibaba.rsqldb.parser.model.baseType.BooleanType;
import com.alibaba.rsqldb.parser.model.baseType.Literal;
import com.alibaba.rsqldb.parser.model.baseType.NumberType;
import com.alibaba.rsqldb.parser.model.baseType.StringType;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.TextNode;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

@JsonIgnoreProperties(ignoreUnknown = true)
public class SingleValueExpression extends SingleExpression {
private Literal<?> value;
Expand All @@ -47,23 +55,31 @@ public void setValue(Literal<?> value) {
@Override
public boolean isTrue(JsonNode jsonNode) {
String fieldName = this.getField().getFieldName();
JsonNode node = jsonNode.get(fieldName);
if (node == null) {
return this.value == null;
}


switch (this.getOperator()) {
case EQUAL: {
JsonNode node = jsonNode.get(fieldName);

String value = node.asText();
String target = String.valueOf(this.value.result());

return StringUtils.equalsIgnoreCase(value, target);
try {
return super.isEqual(node, this.value);
} catch (Throwable t) {
return false;
}
}
default: {
if (!(this.getOperator() == Operator.GREATER) && !(this.getOperator() == Operator.LESS)
&& !(this.getOperator() == Operator.NOT_EQUAL) && !(this.getOperator() == Operator.GREATER_EQUAL)
&& !(this.getOperator() == Operator.LESS_EQUAL)) {
throw new SyntaxErrorException("unknown operator type: " + this.getOperator());
}
JsonNode node = jsonNode.get(fieldName);

if (!(this.value instanceof NumberType) || !(node instanceof NumericNode)) {
return false;
}

Double value = Double.valueOf(node.asText());
Double target = Double.valueOf(String.valueOf(this.value.result()));

Expand All @@ -81,7 +97,7 @@ private boolean compare(Operator operator, Double value, Double target) {
return value < target;
}
case NOT_EQUAL: {
return value != target;
return !Objects.equals(value, target);
}
case GREATER_EQUAL: {
return value >= target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ public void query11() throws Throwable {
assertTrue(filter instanceof RangeValueExpression);
RangeValueExpression valueExpression = (RangeValueExpression) filter;

assertEquals(valueExpression.getLow(), 1);
assertEquals(valueExpression.getHigh(), 10);
assertEquals(1, valueExpression.getLow(), 0);
assertEquals(10, valueExpression.getHigh(), 0);
assertEquals(valueExpression.getField().getFieldName(), "field_1");

assertEquals(valueExpression.getOperator(), Operator.BETWEEN_AND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public BaseResult executeSql(@RequestBody String sql, @RequestParam(value = "job

return new SuccessResult<>(result, RequestStatus.SUCCESS);
} catch (Throwable t) {
logger.error("executeSql error, sql=[{}], jobId=[{}]", sql, jobId, t);
logger.error("executeSql error, jobId=[{}], sql=[{}], error msg:{}", jobId, sql, t.getMessage(), t);
if (t instanceof RSQLServerException) {
return new FailedResult(RestUtil.getStackInfo(t), RequestStatus.RSQLDB_SERVER_EXCEPTION);
return new FailedResult(t.getMessage(), RequestStatus.RSQLDB_SERVER_EXCEPTION);
} else {
return new FailedResult(RestUtil.getStackInfo(t), RequestStatus.CLIENT_EXCEPTION);
return new FailedResult(t.getMessage(), RequestStatus.CLIENT_EXCEPTION);
}
}

Expand All @@ -93,12 +93,12 @@ public BaseResult queryTask() {

return new SuccessResult<>(list, RequestStatus.SUCCESS);
} catch (Throwable t) {
logger.error("queryTask error", t);
logger.error("remove error, error message:{}", t.getMessage(), t);

if (t instanceof RSQLServerException) {
return new FailedResult(RestUtil.getStackInfo(t), RequestStatus.RSQLDB_SERVER_EXCEPTION);
return new FailedResult(t.getMessage(), RequestStatus.RSQLDB_SERVER_EXCEPTION);
} else {
return new FailedResult(RestUtil.getStackInfo(t), RequestStatus.CLIENT_EXCEPTION);
return new FailedResult(t.getMessage(), RequestStatus.CLIENT_EXCEPTION);
}
}
}
Expand All @@ -122,12 +122,12 @@ public BaseResult queryTaskByJobId(@RequestParam(value = "jobId") String jobId)

return new SuccessResult<>(result, RequestStatus.SUCCESS);
} catch (Throwable t) {
logger.error("queryTaskByJobId error, jobId=[{}]",jobId, t);
logger.error("remove error, jobId=[{}], error message:{}", jobId, t.getMessage(), t);

if (t instanceof RSQLServerException) {
return new FailedResult(RestUtil.getStackInfo(t), RequestStatus.RSQLDB_SERVER_EXCEPTION);
return new FailedResult(t.getMessage(), RequestStatus.RSQLDB_SERVER_EXCEPTION);
} else {
return new FailedResult(RestUtil.getStackInfo(t), RequestStatus.CLIENT_EXCEPTION);
return new FailedResult(t.getMessage(), RequestStatus.CLIENT_EXCEPTION);
}
}
}
Expand All @@ -144,12 +144,12 @@ public BaseResult terminate(@RequestParam(value = "jobId") String jobId) {

return new SuccessResult<>(jobId, RequestStatus.SUCCESS);
} catch (Throwable t) {
logger.error("terminate error, jobId=[{}]",jobId, t);
logger.error("remove error, jobId=[{}], error message:{}", jobId, t.getMessage(), t);

if (t instanceof RSQLServerException) {
return new FailedResult(RestUtil.getStackInfo(t), RequestStatus.RSQLDB_SERVER_EXCEPTION);
return new FailedResult(t.getMessage(), RequestStatus.RSQLDB_SERVER_EXCEPTION);
} else {
return new FailedResult(RestUtil.getStackInfo(t), RequestStatus.CLIENT_EXCEPTION);
return new FailedResult(t.getMessage(), RequestStatus.CLIENT_EXCEPTION);
}
}
}
Expand All @@ -165,12 +165,12 @@ public BaseResult restart(@RequestParam(value = "jobId") String jobId) {

return new SuccessResult<>(jobId, RequestStatus.SUCCESS);
} catch (Throwable t) {
logger.error("restart error, jobId=[{}]",jobId, t);
logger.error("remove error, jobId=[{}], error message:{}", jobId, t.getMessage(), t);

if (t instanceof RSQLServerException) {
return new FailedResult(RestUtil.getStackInfo(t), RequestStatus.RSQLDB_SERVER_EXCEPTION);
return new FailedResult(t.getMessage(), RequestStatus.RSQLDB_SERVER_EXCEPTION);
} else {
return new FailedResult(RestUtil.getStackInfo(t), RequestStatus.CLIENT_EXCEPTION);
return new FailedResult(t.getMessage(), RequestStatus.CLIENT_EXCEPTION);
}
}
}
Expand All @@ -187,12 +187,12 @@ public BaseResult remove(@RequestParam(value = "jobId") String jobId) {

return new SuccessResult<>(jobId, RequestStatus.SUCCESS);
} catch (Throwable t) {
logger.error("remove error, jobId=[{}]",jobId, t);
logger.error("remove error, jobId=[{}], error message:{}", jobId, t.getMessage(), t);

if (t instanceof RSQLServerException) {
return new FailedResult(RestUtil.getStackInfo(t), RequestStatus.RSQLDB_SERVER_EXCEPTION);
return new FailedResult(t.getMessage(), RequestStatus.RSQLDB_SERVER_EXCEPTION);
} else {
return new FailedResult(RestUtil.getStackInfo(t), RequestStatus.CLIENT_EXCEPTION);
return new FailedResult(t.getMessage(), RequestStatus.CLIENT_EXCEPTION);
}
}
}
Expand Down
Loading

0 comments on commit e77da08

Please sign in to comment.