|
15 | 15 | import static org.mockito.Mockito.when; |
16 | 16 |
|
17 | 17 | import java.lang.reflect.Field; |
| 18 | +import java.util.ArrayList; |
18 | 19 | import java.util.Arrays; |
19 | 20 | import java.util.Collections; |
| 21 | +import java.util.List; |
20 | 22 | import java.util.concurrent.atomic.AtomicReference; |
21 | 23 | import java.util.stream.Collectors; |
| 24 | +import org.apache.calcite.plan.RelOptCluster; |
| 25 | +import org.apache.calcite.plan.hep.HepPlanner; |
| 26 | +import org.apache.calcite.plan.hep.HepProgramBuilder; |
| 27 | +import org.apache.calcite.rel.RelCollations; |
22 | 28 | import org.apache.calcite.rel.RelNode; |
| 29 | +import org.apache.calcite.rel.logical.LogicalProject; |
| 30 | +import org.apache.calcite.rel.logical.LogicalSort; |
| 31 | +import org.apache.calcite.rel.logical.LogicalValues; |
23 | 32 | import org.apache.calcite.rel.type.RelDataType; |
24 | 33 | import org.apache.calcite.rel.type.RelDataTypeFactory; |
25 | 34 | import org.apache.calcite.rel.type.RelDataTypeSystem; |
| 35 | +import org.apache.calcite.rex.RexBuilder; |
| 36 | +import org.apache.calcite.rex.RexNode; |
| 37 | +import org.apache.calcite.sql.fun.SqlStdOperatorTable; |
26 | 38 | import org.apache.calcite.sql.type.SqlTypeFactoryImpl; |
27 | 39 | import org.apache.calcite.sql.type.SqlTypeName; |
28 | 40 | import org.junit.jupiter.api.BeforeEach; |
@@ -276,8 +288,141 @@ void physicalPlanExplain_callsOnFailure() { |
276 | 288 | + errorRef.get().getMessage()); |
277 | 289 | } |
278 | 290 |
|
| 291 | + // --- schema recovery: structural detection of CAST(<datetime> AS VARCHAR) projects --- |
| 292 | + |
| 293 | + @Test |
| 294 | + void buildSchema_recoversDatetimeLabelsFromOutputCastProject() { |
| 295 | + RelNode plan = |
| 296 | + buildOutputCastPlan( |
| 297 | + new String[] {"ts", "d", "t"}, |
| 298 | + new SqlTypeName[] {SqlTypeName.TIMESTAMP, SqlTypeName.DATE, SqlTypeName.TIME}); |
| 299 | + Iterable<Object[]> rows = |
| 300 | + Collections.singletonList(new Object[] {"2024-01-15 10:30:00", "2024-01-15", "10:30:00"}); |
| 301 | + stubExecutorWith(plan, rows); |
| 302 | + |
| 303 | + QueryResponse response = executeAndCapture(plan); |
| 304 | + String dump = dumpResponse(response); |
| 305 | + |
| 306 | + assertEquals( |
| 307 | + ExprCoreType.TIMESTAMP, response.getSchema().getColumns().get(0).getExprType(), dump); |
| 308 | + assertEquals(ExprCoreType.DATE, response.getSchema().getColumns().get(1).getExprType(), dump); |
| 309 | + assertEquals(ExprCoreType.TIME, response.getSchema().getColumns().get(2).getExprType(), dump); |
| 310 | + } |
| 311 | + |
| 312 | + @Test |
| 313 | + void buildSchema_walksThroughLogicalSortWrapper() { |
| 314 | + RelNode castProject = |
| 315 | + buildOutputCastPlan(new String[] {"ts"}, new SqlTypeName[] {SqlTypeName.TIMESTAMP}); |
| 316 | + // Mimic the LogicalSystemLimit wrapper that wraps the rule-emitted Project at the root. |
| 317 | + RexBuilder rexBuilder = castProject.getCluster().getRexBuilder(); |
| 318 | + RexNode fetch = |
| 319 | + rexBuilder.makeLiteral( |
| 320 | + 10000, castProject.getCluster().getTypeFactory().createSqlType(SqlTypeName.INTEGER)); |
| 321 | + RelNode wrapped = LogicalSort.create(castProject, RelCollations.EMPTY, null, fetch); |
| 322 | + stubExecutorWith(wrapped, Collections.emptyList()); |
| 323 | + |
| 324 | + QueryResponse response = executeAndCapture(wrapped); |
| 325 | + String dump = dumpResponse(response); |
| 326 | + |
| 327 | + assertEquals( |
| 328 | + ExprCoreType.TIMESTAMP, response.getSchema().getColumns().get(0).getExprType(), dump); |
| 329 | + } |
| 330 | + |
| 331 | + @Test |
| 332 | + void buildSchema_projectWithUserExpressionDoesNotRecover() { |
| 333 | + // A Project that mixes a CAST(<datetime> AS VARCHAR) slot with a user-authored |
| 334 | + // expression slot (here: ts + INTERVAL — an unrelated function call) is NOT the |
| 335 | + // rule's emit shape. Recovery must NOT happen; the wire schema reflects the |
| 336 | + // Project's row type as-is (the cast slot stays VARCHAR/STRING). |
| 337 | + RelNode plan = |
| 338 | + buildMixedProject( |
| 339 | + new String[] {"ts_str", "calc"}, |
| 340 | + new SqlTypeName[] {SqlTypeName.TIMESTAMP, SqlTypeName.INTEGER}); |
| 341 | + stubExecutorWith(plan, Collections.emptyList()); |
| 342 | + |
| 343 | + QueryResponse response = executeAndCapture(plan); |
| 344 | + String dump = dumpResponse(response); |
| 345 | + |
| 346 | + // ts_str slot is CAST(<TIMESTAMP> AS VARCHAR) but sits next to a user expression, |
| 347 | + // so the structural shape doesn't match — schema stays STRING (VARCHAR). |
| 348 | + assertEquals(ExprCoreType.STRING, response.getSchema().getColumns().get(0).getExprType(), dump); |
| 349 | + } |
| 350 | + |
| 351 | + @Test |
| 352 | + void buildSchema_nonProjectRootKeepsFieldType() { |
| 353 | + // When the rule didn't fire (no datetime fields), the root is whatever the |
| 354 | + // planner produced — the recovery path must fall back to the field type. |
| 355 | + RelNode plan = mockRelNode("name", SqlTypeName.VARCHAR, "age", SqlTypeName.INTEGER); |
| 356 | + stubExecutorWith(plan, Collections.emptyList()); |
| 357 | + |
| 358 | + QueryResponse response = executeAndCapture(plan); |
| 359 | + String dump = dumpResponse(response); |
| 360 | + |
| 361 | + assertEquals(ExprCoreType.STRING, response.getSchema().getColumns().get(0).getExprType(), dump); |
| 362 | + assertEquals( |
| 363 | + ExprCoreType.INTEGER, response.getSchema().getColumns().get(1).getExprType(), dump); |
| 364 | + } |
| 365 | + |
279 | 366 | // --- helpers --- |
280 | 367 |
|
| 368 | + /** |
| 369 | + * Builds a {@code LogicalProject(CAST(<typed> AS VARCHAR))} over a {@link LogicalValues} input — |
| 370 | + * mirrors what {@code DatetimeOutputCastRule} emits at the root. |
| 371 | + */ |
| 372 | + private RelNode buildOutputCastPlan(String[] names, SqlTypeName[] types) { |
| 373 | + SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT); |
| 374 | + RexBuilder rexBuilder = new RexBuilder(typeFactory); |
| 375 | + HepPlanner planner = new HepPlanner(new HepProgramBuilder().build()); |
| 376 | + RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder); |
| 377 | + |
| 378 | + RelDataTypeFactory.Builder rowBuilder = typeFactory.builder(); |
| 379 | + for (int i = 0; i < names.length; i++) { |
| 380 | + rowBuilder.add(names[i], types[i]).nullable(true); |
| 381 | + } |
| 382 | + RelDataType rowType = rowBuilder.build(); |
| 383 | + LogicalValues input = LogicalValues.createEmpty(cluster, rowType); |
| 384 | + |
| 385 | + RelDataType varchar = |
| 386 | + typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.VARCHAR), true); |
| 387 | + List<RexNode> projects = new ArrayList<>(names.length); |
| 388 | + List<String> projectNames = new ArrayList<>(names.length); |
| 389 | + for (int i = 0; i < names.length; i++) { |
| 390 | + RexNode ref = rexBuilder.makeInputRef(input, i); |
| 391 | + projects.add(rexBuilder.makeCast(varchar, ref)); |
| 392 | + projectNames.add(names[i]); |
| 393 | + } |
| 394 | + return LogicalProject.create(input, List.of(), projects, projectNames); |
| 395 | + } |
| 396 | + |
| 397 | + /** |
| 398 | + * Builds a {@code LogicalProject} where the first slot is {@code CAST(<datetime> AS VARCHAR)} and |
| 399 | + * the second slot is a user-authored expression ({@code col + 1}) — i.e. NOT the shape that the |
| 400 | + * rule emits. |
| 401 | + */ |
| 402 | + private RelNode buildMixedProject(String[] names, SqlTypeName[] types) { |
| 403 | + SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT); |
| 404 | + RexBuilder rexBuilder = new RexBuilder(typeFactory); |
| 405 | + HepPlanner planner = new HepPlanner(new HepProgramBuilder().build()); |
| 406 | + RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder); |
| 407 | + |
| 408 | + RelDataTypeFactory.Builder rowBuilder = typeFactory.builder(); |
| 409 | + for (int i = 0; i < names.length; i++) { |
| 410 | + rowBuilder.add(names[i], types[i]).nullable(true); |
| 411 | + } |
| 412 | + LogicalValues input = LogicalValues.createEmpty(cluster, rowBuilder.build()); |
| 413 | + |
| 414 | + RelDataType varchar = |
| 415 | + typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.VARCHAR), true); |
| 416 | + RexNode castSlot = rexBuilder.makeCast(varchar, rexBuilder.makeInputRef(input, 0)); |
| 417 | + RexNode plusSlot = |
| 418 | + rexBuilder.makeCall( |
| 419 | + SqlStdOperatorTable.PLUS, |
| 420 | + rexBuilder.makeInputRef(input, 1), |
| 421 | + rexBuilder.makeLiteral(1, typeFactory.createSqlType(SqlTypeName.INTEGER))); |
| 422 | + return LogicalProject.create( |
| 423 | + input, List.of(), List.of(castSlot, plusSlot), List.of(names[0], names[1])); |
| 424 | + } |
| 425 | + |
281 | 426 | private QueryResponse executeAndCapture(RelNode relNode) { |
282 | 427 | AtomicReference<QueryResponse> ref = new AtomicReference<>(); |
283 | 428 | engine.execute(relNode, mockContext, captureListener(ref)); |
|
0 commit comments