Skip to content

Commit

Permalink
Add source data type for connector cast expression
Browse files Browse the repository at this point in the history
  • Loading branch information
urosstan-db committed May 15, 2024
1 parent d0385c4 commit f03f08b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,31 @@
@Evolving
public class Cast extends ExpressionWithToString {
private Expression expression;

/**
* Original data type of given expression
*/
private DataType expressionDataType;

/**
* Target data type, i.e. data type in which expression will be cast
*/
private DataType dataType;

@Deprecated
public Cast(Expression expression, DataType dataType) {
this.expression = expression;
this.dataType = dataType;
}

public Cast(Expression expression, DataType expressionDataType, DataType targetDataType) {
this.expression = expression;
this.expressionDataType = expressionDataType;
this.dataType = targetDataType;
}

public Expression expression() { return expression; }
public DataType expressionDataType() { return expressionDataType; }
public DataType dataType() { return dataType; }

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public String build(Expression expr) {
} else if (expr instanceof NamedReference namedReference) {
return visitNamedReference(namedReference);
} else if (expr instanceof Cast cast) {
return visitCast(build(cast.expression()), cast.dataType());
return visitCast(build(cast.expression()), cast.expressionDataType(), cast.dataType());
} else if (expr instanceof Extract extract) {
return visitExtract(extract.field(), build(extract.source()));
} else if (expr instanceof SortOrder sortOrder) {
Expand Down Expand Up @@ -231,10 +231,15 @@ protected String visitBinaryArithmetic(String name, String l, String r) {
return l + " " + name + " " + r;
}

@Deprecated
protected String visitCast(String l, DataType dataType) {
return "CAST(" + l + " AS " + dataType.typeName() + ")";
}

protected String visitCast(String expr, DataType exprDataType, DataType targetDataType) {
return "CAST(" + expr + " AS " + targetDataType.typeName() + ")";
}

protected String visitAnd(String name, String l, String r) {
return "(" + l + ") " + name + " (" + r + ")";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class V2ExpressionBuilder(e: Expression, isPredicate: Boolean = false) {
}
case Cast(child, dataType, _, evalMode)
if evalMode == EvalMode.ANSI || Cast.canUpCast(child.dataType, dataType) =>
generateExpression(child).map(v => new V2Cast(v, dataType))
generateExpression(child).map(v => new V2Cast(v, child.dataType, dataType))
case AggregateExpression(aggregateFunction, Complete, isDistinct, None, _) =>
generateAggregateFunc(aggregateFunction, isDistinct)
case Abs(_, true) => generateExpressionWithName("ABS", expr, isPredicate)
Expand Down

0 comments on commit f03f08b

Please sign in to comment.