|
| 1 | +package io.substrait.isthmus.expression; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import io.substrait.expression.EnumArg; |
| 6 | +import io.substrait.expression.Expression.ScalarFunctionInvocation; |
| 7 | +import io.substrait.expression.Expression.StrLiteral; |
| 8 | +import io.substrait.expression.FunctionArg; |
| 9 | +import io.substrait.extension.DefaultExtensionCatalog; |
| 10 | +import io.substrait.isthmus.PlanTestBase; |
| 11 | +import io.substrait.type.TypeCreator; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Optional; |
| 14 | +import org.apache.calcite.rex.RexBuilder; |
| 15 | +import org.apache.calcite.rex.RexCall; |
| 16 | +import org.apache.calcite.rex.RexNode; |
| 17 | +import org.apache.calcite.sql.fun.SqlStdOperatorTable; |
| 18 | +import org.apache.calcite.sql.fun.SqlTrimFunction; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +class TrimFunctionMapperTest extends PlanTestBase { |
| 22 | + final TrimFunctionMapper trimFunctionMapper = |
| 23 | + new TrimFunctionMapper(DefaultExtensionCatalog.DEFAULT_COLLECTION.scalarFunctions()); |
| 24 | + |
| 25 | + final RexBuilder rexBuilder = builder.getRexBuilder(); |
| 26 | + |
| 27 | + @Test |
| 28 | + void calciteTrimBothArgumentOrder() { |
| 29 | + final RexNode characters = rexBuilder.makeLiteral(" "); |
| 30 | + final RexNode input = rexBuilder.makeLiteral(" whitespace "); |
| 31 | + final RexNode trimBothRexCall = |
| 32 | + rexBuilder.makeCall( |
| 33 | + SqlStdOperatorTable.TRIM, |
| 34 | + List.of(rexBuilder.makeFlag(SqlTrimFunction.Flag.BOTH), characters, input)); |
| 35 | + |
| 36 | + Optional<SubstraitFunctionMapping> substraitCall = |
| 37 | + trimFunctionMapper.toSubstrait((RexCall) trimBothRexCall); |
| 38 | + |
| 39 | + assertEquals("trim", substraitCall.get().substraitName()); |
| 40 | + // operands should be swapped now |
| 41 | + assertEquals(input, substraitCall.get().operands().get(0)); |
| 42 | + assertEquals(characters, substraitCall.get().operands().get(1)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void calciteTrimLeadingArgumentOrder() { |
| 47 | + final RexNode characters = rexBuilder.makeLiteral(" "); |
| 48 | + final RexNode input = rexBuilder.makeLiteral(" whitespace "); |
| 49 | + final RexNode trimBothRexCall = |
| 50 | + rexBuilder.makeCall( |
| 51 | + SqlStdOperatorTable.TRIM, |
| 52 | + List.of(rexBuilder.makeFlag(SqlTrimFunction.Flag.LEADING), characters, input)); |
| 53 | + |
| 54 | + Optional<SubstraitFunctionMapping> substraitCall = |
| 55 | + trimFunctionMapper.toSubstrait((RexCall) trimBothRexCall); |
| 56 | + |
| 57 | + assertEquals("ltrim", substraitCall.get().substraitName()); |
| 58 | + // operands should be swapped now |
| 59 | + assertEquals(input, substraitCall.get().operands().get(0)); |
| 60 | + assertEquals(characters, substraitCall.get().operands().get(1)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void calciteTrimTrailingArgumentOrder() { |
| 65 | + final RexNode characters = rexBuilder.makeLiteral(" "); |
| 66 | + final RexNode input = rexBuilder.makeLiteral(" whitespace "); |
| 67 | + final RexNode trimBothRexCall = |
| 68 | + rexBuilder.makeCall( |
| 69 | + SqlStdOperatorTable.TRIM, |
| 70 | + List.of(rexBuilder.makeFlag(SqlTrimFunction.Flag.TRAILING), characters, input)); |
| 71 | + |
| 72 | + Optional<SubstraitFunctionMapping> substraitCall = |
| 73 | + trimFunctionMapper.toSubstrait((RexCall) trimBothRexCall); |
| 74 | + |
| 75 | + assertEquals("rtrim", substraitCall.get().substraitName()); |
| 76 | + // operands should be swapped now |
| 77 | + assertEquals(input, substraitCall.get().operands().get(0)); |
| 78 | + assertEquals(characters, substraitCall.get().operands().get(1)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void substraitTrimArgumentOrder() { |
| 83 | + final StrLiteral characters = sb.str(" "); |
| 84 | + final StrLiteral input = sb.str(" whitespace "); |
| 85 | + ScalarFunctionInvocation trimFn = |
| 86 | + sb.scalarFn( |
| 87 | + DefaultExtensionCatalog.FUNCTIONS_STRING, |
| 88 | + "trim:str_str", |
| 89 | + TypeCreator.REQUIRED.STRING, |
| 90 | + input, |
| 91 | + characters); |
| 92 | + |
| 93 | + Optional<List<FunctionArg>> arguments = trimFunctionMapper.getExpressionArguments(trimFn); |
| 94 | + |
| 95 | + assertEquals(EnumArg.of(SqlTrimFunction.Flag.BOTH.name()), arguments.get().get(0)); |
| 96 | + // arguments should be swapped now |
| 97 | + assertEquals(characters, arguments.get().get(1)); |
| 98 | + assertEquals(input, arguments.get().get(2)); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void substraitLtrimArgumentOrder() { |
| 103 | + final StrLiteral characters = sb.str(" "); |
| 104 | + final StrLiteral input = sb.str(" whitespace "); |
| 105 | + ScalarFunctionInvocation trimFn = |
| 106 | + sb.scalarFn( |
| 107 | + DefaultExtensionCatalog.FUNCTIONS_STRING, |
| 108 | + "ltrim:str_str", |
| 109 | + TypeCreator.REQUIRED.STRING, |
| 110 | + input, |
| 111 | + characters); |
| 112 | + |
| 113 | + Optional<List<FunctionArg>> arguments = trimFunctionMapper.getExpressionArguments(trimFn); |
| 114 | + |
| 115 | + assertEquals(EnumArg.of(SqlTrimFunction.Flag.LEADING.name()), arguments.get().get(0)); |
| 116 | + // arguments should be swapped now |
| 117 | + assertEquals(characters, arguments.get().get(1)); |
| 118 | + assertEquals(input, arguments.get().get(2)); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void substraitRtrimArgumentOrder() { |
| 123 | + final StrLiteral characters = sb.str(" "); |
| 124 | + final StrLiteral input = sb.str(" whitespace "); |
| 125 | + ScalarFunctionInvocation trimFn = |
| 126 | + sb.scalarFn( |
| 127 | + DefaultExtensionCatalog.FUNCTIONS_STRING, |
| 128 | + "rtrim:str_str", |
| 129 | + TypeCreator.REQUIRED.STRING, |
| 130 | + input, |
| 131 | + characters); |
| 132 | + |
| 133 | + Optional<List<FunctionArg>> arguments = trimFunctionMapper.getExpressionArguments(trimFn); |
| 134 | + |
| 135 | + assertEquals(EnumArg.of(SqlTrimFunction.Flag.TRAILING.name()), arguments.get().get(0)); |
| 136 | + // arguments should be swapped now |
| 137 | + assertEquals(characters, arguments.get().get(1)); |
| 138 | + assertEquals(input, arguments.get().get(2)); |
| 139 | + } |
| 140 | +} |
0 commit comments