|
19 | 19 |
|
20 | 20 | import org.apache.seatunnel.api.configuration.ReadonlyConfig; |
21 | 21 | import org.apache.seatunnel.api.table.catalog.CatalogTable; |
| 22 | +import org.apache.seatunnel.api.table.catalog.CatalogTableUtil; |
22 | 23 | import org.apache.seatunnel.api.table.catalog.PhysicalColumn; |
23 | 24 | import org.apache.seatunnel.api.table.catalog.TableIdentifier; |
24 | 25 | import org.apache.seatunnel.api.table.catalog.TableSchema; |
25 | 26 | import org.apache.seatunnel.api.table.type.BasicType; |
26 | 27 | import org.apache.seatunnel.api.table.type.LocalTimeType; |
| 28 | +import org.apache.seatunnel.api.table.type.MapType; |
27 | 29 | import org.apache.seatunnel.api.table.type.SeaTunnelDataType; |
| 30 | +import org.apache.seatunnel.api.table.type.SeaTunnelRow; |
28 | 31 | import org.apache.seatunnel.api.table.type.SeaTunnelRowType; |
29 | 32 |
|
30 | 33 | import org.junit.jupiter.api.Assertions; |
31 | 34 | import org.junit.jupiter.api.Test; |
32 | 35 |
|
33 | 36 | import java.util.ArrayList; |
| 37 | +import java.util.Collections; |
34 | 38 | import java.util.HashMap; |
35 | 39 | import java.util.Objects; |
36 | 40 |
|
@@ -144,4 +148,136 @@ private CatalogTable getCatalogTable() { |
144 | 148 | new ArrayList<>(), |
145 | 149 | "It has column information."); |
146 | 150 | } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testEscapeIdentifier() { |
| 154 | + String tableName = "test"; |
| 155 | + String[] fields = new String[] {"id", "apply"}; |
| 156 | + CatalogTable table = |
| 157 | + CatalogTableUtil.getCatalogTable( |
| 158 | + tableName, |
| 159 | + new SeaTunnelRowType( |
| 160 | + fields, |
| 161 | + new SeaTunnelDataType[] { |
| 162 | + BasicType.INT_TYPE, BasicType.STRING_TYPE |
| 163 | + })); |
| 164 | + ReadonlyConfig config = |
| 165 | + ReadonlyConfig.fromMap( |
| 166 | + Collections.singletonMap( |
| 167 | + "query", |
| 168 | + "select id, trim(`apply`) as `apply` from test where `apply` = 'a'")); |
| 169 | + SQLTransform sqlTransform = new SQLTransform(config, table); |
| 170 | + TableSchema tableSchema = sqlTransform.transformTableSchema(); |
| 171 | + SeaTunnelRow result = |
| 172 | + sqlTransform.transformRow( |
| 173 | + new SeaTunnelRow(new Object[] {Integer.valueOf(1), String.valueOf("a")})); |
| 174 | + Assertions.assertEquals("apply", tableSchema.getFieldNames()[1]); |
| 175 | + Assertions.assertEquals("a", result.getField(1)); |
| 176 | + result = |
| 177 | + sqlTransform.transformRow( |
| 178 | + new SeaTunnelRow(new Object[] {Integer.valueOf(1), String.valueOf("b")})); |
| 179 | + Assertions.assertNull(result); |
| 180 | + |
| 181 | + config = |
| 182 | + ReadonlyConfig.fromMap( |
| 183 | + Collections.singletonMap( |
| 184 | + "query", |
| 185 | + "select id, IFNULL(`apply`, '1') as `apply` from test where `apply` = 'a'")); |
| 186 | + sqlTransform = new SQLTransform(config, table); |
| 187 | + tableSchema = sqlTransform.transformTableSchema(); |
| 188 | + result = |
| 189 | + sqlTransform.transformRow( |
| 190 | + new SeaTunnelRow(new Object[] {Integer.valueOf(1), String.valueOf("a")})); |
| 191 | + Assertions.assertEquals("apply", tableSchema.getFieldNames()[1]); |
| 192 | + Assertions.assertEquals( |
| 193 | + BasicType.STRING_TYPE, tableSchema.getColumns().get(1).getDataType()); |
| 194 | + Assertions.assertEquals("a", result.getField(1)); |
| 195 | + |
| 196 | + table = |
| 197 | + CatalogTableUtil.getCatalogTable( |
| 198 | + tableName, |
| 199 | + new SeaTunnelRowType( |
| 200 | + fields, |
| 201 | + new SeaTunnelDataType[] {BasicType.INT_TYPE, BasicType.LONG_TYPE})); |
| 202 | + config = |
| 203 | + ReadonlyConfig.fromMap( |
| 204 | + Collections.singletonMap( |
| 205 | + "query", |
| 206 | + "select id, `apply` + 1 as `apply` from test where `apply` > 0")); |
| 207 | + sqlTransform = new SQLTransform(config, table); |
| 208 | + tableSchema = sqlTransform.transformTableSchema(); |
| 209 | + result = |
| 210 | + sqlTransform.transformRow( |
| 211 | + new SeaTunnelRow(new Object[] {Integer.valueOf(1), Long.valueOf(1)})); |
| 212 | + Assertions.assertEquals("apply", tableSchema.getFieldNames()[1]); |
| 213 | + Assertions.assertEquals(BasicType.LONG_TYPE, tableSchema.getColumns().get(1).getDataType()); |
| 214 | + Assertions.assertEquals(Long.valueOf(2), result.getField(1)); |
| 215 | + result = |
| 216 | + sqlTransform.transformRow( |
| 217 | + new SeaTunnelRow(new Object[] {Integer.valueOf(1), Long.valueOf(0)})); |
| 218 | + Assertions.assertNull(result); |
| 219 | + |
| 220 | + table = |
| 221 | + CatalogTableUtil.getCatalogTable( |
| 222 | + tableName, |
| 223 | + new SeaTunnelRowType( |
| 224 | + fields, |
| 225 | + new SeaTunnelDataType[] { |
| 226 | + BasicType.INT_TYPE, |
| 227 | + new MapType<String, String>( |
| 228 | + BasicType.STRING_TYPE, BasicType.STRING_TYPE) |
| 229 | + })); |
| 230 | + config = |
| 231 | + ReadonlyConfig.fromMap( |
| 232 | + Collections.singletonMap( |
| 233 | + "query", |
| 234 | + "select id, `apply`.k1 as `apply` from test where `apply`.k1 = 'a'")); |
| 235 | + sqlTransform = new SQLTransform(config, table); |
| 236 | + tableSchema = sqlTransform.transformTableSchema(); |
| 237 | + result = |
| 238 | + sqlTransform.transformRow( |
| 239 | + new SeaTunnelRow( |
| 240 | + new Object[] { |
| 241 | + Integer.valueOf(1), Collections.singletonMap("k1", "a") |
| 242 | + })); |
| 243 | + Assertions.assertEquals("apply", tableSchema.getFieldNames()[1]); |
| 244 | + Assertions.assertEquals( |
| 245 | + BasicType.STRING_TYPE, tableSchema.getColumns().get(1).getDataType()); |
| 246 | + Assertions.assertEquals("a", result.getField(1)); |
| 247 | + result = |
| 248 | + sqlTransform.transformRow( |
| 249 | + new SeaTunnelRow( |
| 250 | + new Object[] { |
| 251 | + Integer.valueOf(1), Collections.singletonMap("k1", "b") |
| 252 | + })); |
| 253 | + Assertions.assertNull(result); |
| 254 | + |
| 255 | + table = |
| 256 | + CatalogTableUtil.getCatalogTable( |
| 257 | + tableName, |
| 258 | + new SeaTunnelRowType( |
| 259 | + new String[] {"id", "map"}, |
| 260 | + new SeaTunnelDataType[] { |
| 261 | + BasicType.INT_TYPE, |
| 262 | + new MapType<String, String>( |
| 263 | + BasicType.STRING_TYPE, BasicType.STRING_TYPE) |
| 264 | + })); |
| 265 | + config = |
| 266 | + ReadonlyConfig.fromMap( |
| 267 | + Collections.singletonMap( |
| 268 | + "query", |
| 269 | + "select id, map.`apply` as `apply` from test where map.`apply` = 'a'")); |
| 270 | + sqlTransform = new SQLTransform(config, table); |
| 271 | + tableSchema = sqlTransform.transformTableSchema(); |
| 272 | + result = |
| 273 | + sqlTransform.transformRow( |
| 274 | + new SeaTunnelRow( |
| 275 | + new Object[] { |
| 276 | + Integer.valueOf(1), Collections.singletonMap("apply", "a") |
| 277 | + })); |
| 278 | + Assertions.assertEquals("apply", tableSchema.getFieldNames()[1]); |
| 279 | + Assertions.assertEquals( |
| 280 | + BasicType.STRING_TYPE, tableSchema.getColumns().get(1).getDataType()); |
| 281 | + Assertions.assertEquals("a", result.getField(1)); |
| 282 | + } |
147 | 283 | } |
0 commit comments