Skip to content

Commit

Permalink
[Coral-Hive] Create new object for ShiftArrayIndexTransformer (#494) (#…
Browse files Browse the repository at this point in the history
…498)

Co-authored-by: Jiefan Li <[email protected]>
  • Loading branch information
KevinGe00 and ljfgem authored Mar 14, 2024
1 parent 79ff567 commit dcadf74
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022-2023 LinkedIn Corporation. All rights reserved.
* Copyright 2022-2024 LinkedIn Corporation. All rights reserved.
* Licensed under the BSD-2 Clause license.
* See LICENSE in the project root for license information.
*/
Expand Down Expand Up @@ -42,16 +42,19 @@ public boolean condition(SqlCall sqlCall) {
@Override
public SqlCall transform(SqlCall sqlCall) {
final SqlNode itemNode = sqlCall.getOperandList().get(1);
SqlNode newIndex;
if (itemNode instanceof SqlNumericLiteral
&& deriveRelDatatype(itemNode).getSqlTypeName().equals(SqlTypeName.INTEGER)) {
final Integer value = ((SqlNumericLiteral) itemNode).getValueAs(Integer.class);
sqlCall.setOperand(1,
SqlNumericLiteral.createExactNumeric(new BigDecimal(value + 1).toString(), itemNode.getParserPosition()));
newIndex =
SqlNumericLiteral.createExactNumeric(new BigDecimal(value + 1).toString(), itemNode.getParserPosition());
} else {
final SqlCall oneBasedIndex = SqlStdOperatorTable.PLUS.createCall(itemNode.getParserPosition(), itemNode,
newIndex = SqlStdOperatorTable.PLUS.createCall(itemNode.getParserPosition(), itemNode,
SqlNumericLiteral.createExactNumeric("1", SqlParserPos.ZERO));
sqlCall.setOperand(1, oneBasedIndex);
}
return sqlCall;
// Create new object instead of modifying the old SqlCall to avoid transforming the same object
// multiple times if it appears multiple times in SqlNode
// TODO: Add unit test to verify the necessity of creating a new object
return SqlStdOperatorTable.ITEM.createCall(SqlParserPos.ZERO, sqlCall.getOperandList().get(0), newIndex);
}
}

0 comments on commit dcadf74

Please sign in to comment.