in core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java [2380:2514]
protected void convertFrom(
Blackboard bb,
@Nullable SqlNode from,
@Nullable List<String> fieldNames) {
if (from == null) {
bb.setRoot(LogicalValues.createOneRow(cluster), false);
return;
}
final SqlCall call;
switch (from.getKind()) {
case AS:
call = (SqlCall) from;
SqlNode firstOperand = call.operand(0);
final List<String> fieldNameList = call.operandCount() > 2
? SqlIdentifier.simpleNames(Util.skip(call.getOperandList(), 2))
: null;
convertFrom(bb, firstOperand, fieldNameList);
return;
case MATCH_RECOGNIZE:
convertMatchRecognize(bb, (SqlMatchRecognize) from);
return;
case PIVOT:
convertPivot(bb, (SqlPivot) from);
return;
case UNPIVOT:
convertUnpivot(bb, (SqlUnpivot) from);
return;
case WITH_ITEM:
convertFrom(bb, ((SqlWithItem) from).query);
return;
case WITH:
convertFrom(bb, ((SqlWith) from).body);
return;
case TABLESAMPLE:
final List<SqlNode> operands = ((SqlCall) from).getOperandList();
SqlSampleSpec sampleSpec =
SqlLiteral.sampleValue(
requireNonNull(operands.get(1), () -> "operand[1] of " + from));
if (sampleSpec instanceof SqlSampleSpec.SqlSubstitutionSampleSpec) {
String sampleName =
((SqlSampleSpec.SqlSubstitutionSampleSpec) sampleSpec)
.getName();
datasetStack.push(sampleName);
convertFrom(bb, operands.get(0));
datasetStack.pop();
} else if (sampleSpec instanceof SqlSampleSpec.SqlTableSampleSpec) {
SqlSampleSpec.SqlTableSampleSpec tableSampleSpec =
(SqlSampleSpec.SqlTableSampleSpec) sampleSpec;
convertFrom(bb, operands.get(0));
bb.setRoot(
relBuilder.push(bb.root())
.sample(tableSampleSpec.isBernoulli(),
tableSampleSpec.sampleRate,
tableSampleSpec.isRepeatable()
? tableSampleSpec.getRepeatableSeed()
: null)
.build(),
true);
} else {
throw new AssertionError("unknown TABLESAMPLE type: " + sampleSpec);
}
return;
case TABLE_REF:
call = (SqlCall) from;
convertIdentifier(bb, call.operand(0), null, call.operand(1));
return;
case IDENTIFIER:
convertIdentifier(bb, (SqlIdentifier) from, null, null);
return;
case WITH_ITEM_TABLE_REF:
SqlWithItemTableRef withItemTableRef = (SqlWithItemTableRef) from;
convertTransientScan(bb, withItemTableRef.getWithItem());
return;
case EXTEND:
call = (SqlCall) from;
final SqlNode operand0 = call.getOperandList().get(0);
final SqlIdentifier id = operand0.getKind() == SqlKind.TABLE_REF
? ((SqlCall) operand0).operand(0)
: (SqlIdentifier) operand0;
SqlNodeList extendedColumns = (SqlNodeList) call.getOperandList().get(1);
convertIdentifier(bb, id, extendedColumns, null);
return;
case SNAPSHOT:
convertTemporalTable(bb, (SqlCall) from);
return;
case JOIN:
convertJoin(bb, (SqlJoin) from);
return;
case SELECT:
case INTERSECT:
case EXCEPT:
case UNION:
final RelNode rel = convertQueryRecursive(from, false, null).project();
bb.setRoot(rel, true);
return;
case VALUES:
convertValuesImpl(bb, (SqlCall) from, null);
if (fieldNames != null) {
bb.setRoot(relBuilder.push(bb.root()).rename(fieldNames).build(), true);
}
return;
case UNNEST:
convertUnnest(bb, (SqlCall) from, fieldNames);
return;
case COLLECTION_TABLE:
call = (SqlCall) from;
// Dig out real call; TABLE() wrapper is just syntactic.
assert call.getOperandList().size() == 1;
final SqlCall call2 = call.operand(0);
convertCollectionTable(bb, call2);
return;
default:
throw new AssertionError("not a join operator " + from);
}
}