Skip to content

Commit

Permalink
feature: degrade jsqlparser version to 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
lintingbin committed Feb 18, 2025
1 parent f8c5734 commit 7276704
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
2 changes: 1 addition & 1 deletion amoro-format-iceberg/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>5.1</version>
<version>4.5</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

package org.apache.amoro.optimizing.plan;

import net.sf.jsqlparser.expression.BooleanValue;
import net.sf.jsqlparser.expression.CastExpression;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import net.sf.jsqlparser.expression.DoubleValue;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.NotExpression;
Expand All @@ -32,12 +31,14 @@
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.expression.operators.relational.IsNullExpression;
import net.sf.jsqlparser.expression.operators.relational.ItemsList;
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import org.apache.amoro.ServerTableIdentifier;
import org.apache.amoro.TableFormat;
import org.apache.amoro.config.OptimizingConfig;
Expand Down Expand Up @@ -149,7 +150,8 @@ protected Expression getPartitionFilter() {
protected static Expression convertSqlToIcebergExpression(
String sql, List<Types.NestedField> tableColumns) {
try {
PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse("SELECT * FROM dummy WHERE " + sql);
Select statement = (Select) CCJSqlParserUtil.parse("SELECT * FROM dummy WHERE " + sql);
PlainSelect select = (PlainSelect) statement.getSelectBody();
return convertSparkExpressionToIceberg(select.getWhere(), tableColumns);
} catch (Exception e) {
throw new IllegalArgumentException("Failed to parse where condition: " + sql, e);
Expand Down Expand Up @@ -192,10 +194,11 @@ private static Expression convertSparkExpressionToIceberg(
} else if (whereExpr instanceof InExpression) {
InExpression in = (InExpression) whereExpr;
Types.NestedField column = getColumn(in.getLeftExpression(), tableColumns);
net.sf.jsqlparser.expression.Expression rightExpr = in.getRightExpression();
ItemsList rightItems = in.getRightItemsList();
List<Object> values = new ArrayList<>();
if (rightExpr instanceof ExpressionList) {
for (net.sf.jsqlparser.expression.Expression expr : ((ExpressionList<?>) rightExpr)) {
if (rightItems instanceof ExpressionList) {
for (net.sf.jsqlparser.expression.Expression expr :
((ExpressionList) rightItems).getExpressions()) {
values.add(getValue(expr, column));
}
} else {
Expand Down Expand Up @@ -249,7 +252,7 @@ private static Object convertValue(
net.sf.jsqlparser.expression.Expression expr, Types.NestedField column) {
switch (column.type().typeId()) {
case BOOLEAN:
return ((BooleanValue) expr).getValue();
return Boolean.valueOf(((Column) expr).getColumnName());
case STRING:
return ((StringValue) expr).getValue();
case INTEGER:
Expand All @@ -259,40 +262,19 @@ private static Object convertValue(
case DOUBLE:
return ((DoubleValue) expr).getValue();
case DATE:
String dateStr = null;
if (expr instanceof StringValue) {
dateStr = ((StringValue) expr).getValue();
} else if (expr instanceof CastExpression
&& ((CastExpression) expr).getColDataType().getDataType().equalsIgnoreCase("date")) {
dateStr = ((StringValue) ((CastExpression) expr).getLeftExpression()).getValue();
}
String dateStr = getDateTimeLiteralStr(expr, "date");
if (dateStr != null) {
return Date.valueOf(dateStr).toLocalDate().toEpochDay();
}
break;
case TIME:
String timeStr = null;
if (expr instanceof StringValue) {
timeStr = ((StringValue) expr).getValue();
} else if (expr instanceof CastExpression
&& ((CastExpression) expr).getColDataType().getDataType().equalsIgnoreCase("time")) {
timeStr = ((StringValue) ((CastExpression) expr).getLeftExpression()).getValue();
}
String timeStr = getDateTimeLiteralStr(expr, "time");
if (timeStr != null) {
return Time.valueOf(timeStr).toLocalTime().getLong(ChronoField.MICRO_OF_DAY);
}
break;
case TIMESTAMP:
String timestampStr = null;
if (expr instanceof StringValue) {
timestampStr = ((StringValue) expr).getValue();
} else if (expr instanceof CastExpression
&& ((CastExpression) expr)
.getColDataType()
.getDataType()
.equalsIgnoreCase("timestamp")) {
timestampStr = ((StringValue) ((CastExpression) expr).getLeftExpression()).getValue();
}
String timestampStr = getDateTimeLiteralStr(expr, "timestamp");
if (timestampStr != null) {
return Timestamp.valueOf(timestampStr)
.toLocalDateTime()
Expand All @@ -305,6 +287,18 @@ private static Object convertValue(
expr + " can not be converted to column type: " + column.type());
}

private static String getDateTimeLiteralStr(
net.sf.jsqlparser.expression.Expression expr, String type) {
String timestampStr = null;
if (expr instanceof StringValue) {
timestampStr = ((StringValue) expr).getValue();
} else if (expr instanceof DateTimeLiteralExpression
&& ((DateTimeLiteralExpression) expr).getType().name().equalsIgnoreCase(type)) {
timestampStr = ((DateTimeLiteralExpression) expr).getValue().replaceAll("^'(.*)'$", "$1");
}
return timestampStr;
}

private void initPartitionPlans(TableFileScanHelper tableFileScanHelper) {
long startTime = System.currentTimeMillis();
long count = 0;
Expand Down

0 comments on commit 7276704

Please sign in to comment.