use of org.apache.calcite.rel.logical.LogicalProject in project samza by apache.
the class SamzaSqlValidator method validateDeleteOp.
private void validateDeleteOp(RelRoot relRoot) throws SamzaSqlValidatorException {
LogicalProject project = (LogicalProject) relRoot.rel;
RelRecordType projectRecord = (RelRecordType) project.getRowType();
if (projectRecord.getFieldCount() != 2) {
throw new SamzaSqlValidatorException(String.format("Only two select query fields are expected for DELETE op." + " But there are %d fields given in the query.", projectRecord.getFieldCount()));
}
RelDataTypeField keyField = projectRecord.getField(SamzaSqlRelMessage.KEY_NAME, true, true);
if (keyField == null) {
throw new SamzaSqlValidatorException(String.format("Select query needs to specify '%s' field while using DELETE" + " op. Eg: 'SELECT myKey AS %s, '%s' AS %s FROM myTable'", SamzaSqlRelMessage.KEY_NAME, SamzaSqlRelMessage.KEY_NAME, SamzaSqlRelMessage.DELETE_OP, SamzaSqlRelMessage.OP_NAME));
}
int keyIdx = projectRecord.getFieldList().indexOf(keyField);
// Get the node corresponding to the special op.
RexNode node = project.getProjects().get(1 - keyIdx);
if (!node.toString().equals(String.format("'%s'", SamzaSqlRelMessage.DELETE_OP))) {
throw new SamzaSqlValidatorException(String.format("%s op is not supported. Please note that only '%s' op is" + " currently supported. Eg:'SELECT myKey AS %s, '%s' AS %s FROM myStream'", node.toString(), SamzaSqlRelMessage.DELETE_OP, SamzaSqlRelMessage.KEY_NAME, SamzaSqlRelMessage.DELETE_OP, SamzaSqlRelMessage.OP_NAME));
}
}
use of org.apache.calcite.rel.logical.LogicalProject in project samza by apache.
the class JoinTranslator method resolveSQlIOForTable.
static SqlIOConfig resolveSQlIOForTable(RelNode relNode, Map<String, SqlIOConfig> systemStreamConfigBySource) {
if (relNode instanceof HepRelVertex) {
return resolveSQlIOForTable(((HepRelVertex) relNode).getCurrentRel(), systemStreamConfigBySource);
}
if (relNode instanceof LogicalProject) {
return resolveSQlIOForTable(((LogicalProject) relNode).getInput(), systemStreamConfigBySource);
}
if (relNode instanceof LogicalFilter) {
return resolveSQlIOForTable(((LogicalFilter) relNode).getInput(), systemStreamConfigBySource);
}
// is considered a stream. Hence, we return null for the table.
if (relNode instanceof LogicalJoin && relNode.getInputs().size() > 1) {
return null;
}
if (!(relNode instanceof TableScan)) {
throw new SamzaException(String.format("Unsupported query. relNode %s is not of type TableScan.", relNode.toString()));
}
String sourceName = SqlIOConfig.getSourceFromSourceParts(relNode.getTable().getQualifiedName());
SqlIOConfig sourceConfig = systemStreamConfigBySource.get(sourceName);
if (sourceConfig == null) {
throw new SamzaException("Unsupported source found in join statement: " + sourceName);
}
return sourceConfig;
}
use of org.apache.calcite.rel.logical.LogicalProject in project samza by apache.
the class QueryTranslator method translate.
/**
* Translate Calcite plan to Samza stream operators.
* @param relRoot Calcite plan in the form of {@link RelRoot}. RelRoot should not include the sink ({@link TableModify})
* @param outputSystemStream Sink associated with the Calcite plan.
* @param translatorContext Context maintained across translations.
* @param queryId query index of the sql statement corresponding to the Calcite plan in multi SQL statement scenario
* starting with index 0.
*/
public void translate(RelRoot relRoot, String outputSystemStream, TranslatorContext translatorContext, int queryId) {
final RelNode node = relRoot.project();
ScanTranslator scanTranslator = new ScanTranslator(sqlConfig.getSamzaRelConverters(), sqlConfig.getInputSystemStreamConfigBySource(), queryId);
/* update input metrics */
String queryLogicalId = String.format(TranslatorConstants.LOGSQLID_TEMPLATE, queryId);
opId = 0;
node.accept(new RelShuttleImpl() {
@Override
public RelNode visit(RelNode relNode) {
// There should never be a TableModify in the calcite plan.
Validate.isTrue(!(relNode instanceof TableModify));
return super.visit(relNode);
}
@Override
public RelNode visit(TableScan scan) {
RelNode node = super.visit(scan);
String logicalOpId = String.format(TranslatorConstants.LOGOPID_TEMPLATE, queryId, "scan", opId++);
scanTranslator.translate(scan, queryLogicalId, logicalOpId, translatorContext, systemDescriptors, inputMsgStreams);
return node;
}
@Override
public RelNode visit(LogicalFilter filter) {
RelNode node = visitChild(filter, 0, filter.getInput());
String logicalOpId = String.format(TranslatorConstants.LOGOPID_TEMPLATE, queryId, "filter", opId++);
new FilterTranslator(queryId).translate(filter, logicalOpId, translatorContext);
return node;
}
@Override
public RelNode visit(LogicalProject project) {
RelNode node = super.visit(project);
String logicalOpId = String.format(TranslatorConstants.LOGOPID_TEMPLATE, queryId, "project", opId++);
new ProjectTranslator(queryId).translate(project, logicalOpId, translatorContext);
return node;
}
@Override
public RelNode visit(LogicalJoin join) {
RelNode node = super.visit(join);
String logicalOpId = String.format(TranslatorConstants.LOGOPID_TEMPLATE, queryId, "join", opId++);
new JoinTranslator(logicalOpId, sqlConfig.getMetadataTopicPrefix(), queryId).translate(join, translatorContext);
return node;
}
@Override
public RelNode visit(LogicalAggregate aggregate) {
RelNode node = super.visit(aggregate);
String logicalOpId = String.format(TranslatorConstants.LOGOPID_TEMPLATE, queryId, "window", opId++);
new LogicalAggregateTranslator(logicalOpId, sqlConfig.getMetadataTopicPrefix()).translate(aggregate, translatorContext);
return node;
}
});
String logicalOpId = String.format(TranslatorConstants.LOGOPID_TEMPLATE, queryId, "insert", opId);
sendToOutputStream(queryLogicalId, logicalOpId, outputSystemStream, streamAppDescriptor, translatorContext, node, queryId);
}
use of org.apache.calcite.rel.logical.LogicalProject in project calcite by apache.
the class RelMetadataTest method checkAverageRowSize.
private void checkAverageRowSize(RelOptCluster cluster, RelOptTable empTable, RelOptTable deptTable) {
final RexBuilder rexBuilder = cluster.getRexBuilder();
final RelMetadataQuery mq = RelMetadataQuery.instance();
final LogicalTableScan empScan = LogicalTableScan.create(cluster, empTable);
Double rowSize = mq.getAverageRowSize(empScan);
List<Double> columnSizes = mq.getAverageColumnSizes(empScan);
assertThat(columnSizes.size(), equalTo(empScan.getRowType().getFieldCount()));
assertThat(columnSizes, equalTo(Arrays.asList(4.0, 40.0, 20.0, 4.0, 8.0, 4.0, 4.0, 4.0, 1.0)));
assertThat(rowSize, equalTo(89.0));
// Empty values
final LogicalValues emptyValues = LogicalValues.createEmpty(cluster, empTable.getRowType());
rowSize = mq.getAverageRowSize(emptyValues);
columnSizes = mq.getAverageColumnSizes(emptyValues);
assertThat(columnSizes.size(), equalTo(emptyValues.getRowType().getFieldCount()));
assertThat(columnSizes, equalTo(Arrays.asList(4.0, 40.0, 20.0, 4.0, 8.0, 4.0, 4.0, 4.0, 1.0)));
assertThat(rowSize, equalTo(89.0));
// Values
final RelDataType rowType = cluster.getTypeFactory().builder().add("a", SqlTypeName.INTEGER).add("b", SqlTypeName.VARCHAR).add("c", SqlTypeName.VARCHAR).build();
final ImmutableList.Builder<ImmutableList<RexLiteral>> tuples = ImmutableList.builder();
addRow(tuples, rexBuilder, 1, "1234567890", "ABC");
addRow(tuples, rexBuilder, 2, "1", "A");
addRow(tuples, rexBuilder, 3, "2", null);
final LogicalValues values = LogicalValues.create(cluster, rowType, tuples.build());
rowSize = mq.getAverageRowSize(values);
columnSizes = mq.getAverageColumnSizes(values);
assertThat(columnSizes.size(), equalTo(values.getRowType().getFieldCount()));
assertThat(columnSizes, equalTo(Arrays.asList(4.0, 8.0, 3.0)));
assertThat(rowSize, equalTo(15.0));
// Union
final LogicalUnion union = LogicalUnion.create(ImmutableList.<RelNode>of(empScan, emptyValues), true);
rowSize = mq.getAverageRowSize(union);
columnSizes = mq.getAverageColumnSizes(union);
assertThat(columnSizes.size(), equalTo(9));
assertThat(columnSizes, equalTo(Arrays.asList(4.0, 40.0, 20.0, 4.0, 8.0, 4.0, 4.0, 4.0, 1.0)));
assertThat(rowSize, equalTo(89.0));
// Filter
final LogicalTableScan deptScan = LogicalTableScan.create(cluster, deptTable);
final LogicalFilter filter = LogicalFilter.create(deptScan, rexBuilder.makeCall(SqlStdOperatorTable.LESS_THAN, rexBuilder.makeInputRef(deptScan, 0), rexBuilder.makeExactLiteral(BigDecimal.TEN)));
rowSize = mq.getAverageRowSize(filter);
columnSizes = mq.getAverageColumnSizes(filter);
assertThat(columnSizes.size(), equalTo(2));
assertThat(columnSizes, equalTo(Arrays.asList(4.0, 20.0)));
assertThat(rowSize, equalTo(24.0));
// Project
final LogicalProject deptProject = LogicalProject.create(filter, ImmutableList.of(rexBuilder.makeInputRef(filter, 0), rexBuilder.makeInputRef(filter, 1), rexBuilder.makeCall(SqlStdOperatorTable.PLUS, rexBuilder.makeInputRef(filter, 0), rexBuilder.makeExactLiteral(BigDecimal.ONE)), rexBuilder.makeCall(SqlStdOperatorTable.CHAR_LENGTH, rexBuilder.makeInputRef(filter, 1))), (List<String>) null);
rowSize = mq.getAverageRowSize(deptProject);
columnSizes = mq.getAverageColumnSizes(deptProject);
assertThat(columnSizes.size(), equalTo(4));
assertThat(columnSizes, equalTo(Arrays.asList(4.0, 20.0, 4.0, 4.0)));
assertThat(rowSize, equalTo(32.0));
// Join
final LogicalJoin join = LogicalJoin.create(empScan, deptProject, rexBuilder.makeLiteral(true), ImmutableSet.<CorrelationId>of(), JoinRelType.INNER);
rowSize = mq.getAverageRowSize(join);
columnSizes = mq.getAverageColumnSizes(join);
assertThat(columnSizes.size(), equalTo(13));
assertThat(columnSizes, equalTo(Arrays.asList(4.0, 40.0, 20.0, 4.0, 8.0, 4.0, 4.0, 4.0, 1.0, 4.0, 20.0, 4.0, 4.0)));
assertThat(rowSize, equalTo(121.0));
// Aggregate
final LogicalAggregate aggregate = LogicalAggregate.create(join, ImmutableBitSet.of(2, 0), ImmutableList.<ImmutableBitSet>of(), ImmutableList.of(AggregateCall.create(SqlStdOperatorTable.COUNT, false, false, ImmutableIntList.of(), -1, 2, join, null, null)));
rowSize = mq.getAverageRowSize(aggregate);
columnSizes = mq.getAverageColumnSizes(aggregate);
assertThat(columnSizes.size(), equalTo(3));
assertThat(columnSizes, equalTo(Arrays.asList(4.0, 20.0, 8.0)));
assertThat(rowSize, equalTo(32.0));
// Smoke test Parallelism and Memory metadata providers
assertThat(mq.memory(aggregate), nullValue());
assertThat(mq.cumulativeMemoryWithinPhase(aggregate), nullValue());
assertThat(mq.cumulativeMemoryWithinPhaseSplit(aggregate), nullValue());
assertThat(mq.isPhaseTransition(aggregate), is(false));
assertThat(mq.splitCount(aggregate), is(1));
}
use of org.apache.calcite.rel.logical.LogicalProject in project calcite by apache.
the class RelMetadataTest method checkCollation.
private void checkCollation(RelOptCluster cluster, RelOptTable empTable, RelOptTable deptTable) {
final RexBuilder rexBuilder = cluster.getRexBuilder();
final LogicalTableScan empScan = LogicalTableScan.create(cluster, empTable);
List<RelCollation> collations = RelMdCollation.table(empScan.getTable());
assertThat(collations.size(), equalTo(0));
// ORDER BY field#0 ASC, field#1 ASC
final RelCollation collation = RelCollations.of(new RelFieldCollation(0), new RelFieldCollation(1));
collations = RelMdCollation.sort(collation);
assertThat(collations.size(), equalTo(1));
assertThat(collations.get(0).getFieldCollations().size(), equalTo(2));
final Sort empSort = LogicalSort.create(empScan, collation, null, null);
final List<RexNode> projects = ImmutableList.of(rexBuilder.makeInputRef(empSort, 1), rexBuilder.makeLiteral("foo"), rexBuilder.makeInputRef(empSort, 0), rexBuilder.makeCall(SqlStdOperatorTable.MINUS, rexBuilder.makeInputRef(empSort, 0), rexBuilder.makeInputRef(empSort, 3)));
final RelMetadataQuery mq = RelMetadataQuery.instance();
collations = RelMdCollation.project(mq, empSort, projects);
assertThat(collations.size(), equalTo(1));
assertThat(collations.get(0).getFieldCollations().size(), equalTo(2));
assertThat(collations.get(0).getFieldCollations().get(0).getFieldIndex(), equalTo(2));
assertThat(collations.get(0).getFieldCollations().get(1).getFieldIndex(), equalTo(0));
final LogicalProject project = LogicalProject.create(empSort, projects, ImmutableList.of("a", "b", "c", "d"));
final LogicalTableScan deptScan = LogicalTableScan.create(cluster, deptTable);
final RelCollation deptCollation = RelCollations.of(new RelFieldCollation(0), new RelFieldCollation(1));
final Sort deptSort = LogicalSort.create(deptScan, deptCollation, null, null);
final ImmutableIntList leftKeys = ImmutableIntList.of(2);
final ImmutableIntList rightKeys = ImmutableIntList.of(0);
final EnumerableMergeJoin join;
try {
join = EnumerableMergeJoin.create(project, deptSort, rexBuilder.makeLiteral(true), leftKeys, rightKeys, JoinRelType.INNER);
} catch (InvalidRelException e) {
throw new RuntimeException(e);
}
collations = RelMdCollation.mergeJoin(mq, project, deptSort, leftKeys, rightKeys);
assertThat(collations, equalTo(join.getTraitSet().getTraits(RelCollationTraitDef.INSTANCE)));
// Values (empty)
collations = RelMdCollation.values(mq, empTable.getRowType(), ImmutableList.<ImmutableList<RexLiteral>>of());
assertThat(collations.toString(), equalTo("[[0, 1, 2, 3, 4, 5, 6, 7, 8], " + "[1, 2, 3, 4, 5, 6, 7, 8], " + "[2, 3, 4, 5, 6, 7, 8], " + "[3, 4, 5, 6, 7, 8], " + "[4, 5, 6, 7, 8], " + "[5, 6, 7, 8], " + "[6, 7, 8], " + "[7, 8], " + "[8]]"));
final LogicalValues emptyValues = LogicalValues.createEmpty(cluster, empTable.getRowType());
assertThat(mq.collations(emptyValues), equalTo(collations));
// Values (non-empty)
final RelDataType rowType = cluster.getTypeFactory().builder().add("a", SqlTypeName.INTEGER).add("b", SqlTypeName.INTEGER).add("c", SqlTypeName.INTEGER).add("d", SqlTypeName.INTEGER).build();
final ImmutableList.Builder<ImmutableList<RexLiteral>> tuples = ImmutableList.builder();
// sort keys are [a], [a, b], [a, b, c], [a, b, c, d], [a, c], [b], [b, a],
// [b, d]
// algorithm deduces [a, b, c, d], [b, d] which is a useful sub-set
addRow(tuples, rexBuilder, 1, 1, 1, 1);
addRow(tuples, rexBuilder, 1, 2, 0, 3);
addRow(tuples, rexBuilder, 2, 3, 2, 2);
addRow(tuples, rexBuilder, 3, 3, 1, 4);
collations = RelMdCollation.values(mq, rowType, tuples.build());
assertThat(collations.toString(), equalTo("[[0, 1, 2, 3], [1, 3]]"));
final LogicalValues values = LogicalValues.create(cluster, rowType, tuples.build());
assertThat(mq.collations(values), equalTo(collations));
}
Aggregations