use of org.apache.calcite.sql.SqlIdentifier in project flink by apache.
the class RichSqlHiveInsert method unparse.
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.startList(SqlWriter.FrameTypeEnum.SELECT);
String insertKeyword = "INSERT INTO";
if (isUpsert()) {
insertKeyword = "UPSERT INTO";
} else if (isOverwrite()) {
insertKeyword = "INSERT OVERWRITE";
}
writer.sep(insertKeyword);
final int opLeft = getOperator().getLeftPrec();
final int opRight = getOperator().getRightPrec();
getTargetTable().unparse(writer, opLeft, opRight);
if (getTargetColumnList() != null) {
getTargetColumnList().unparse(writer, opLeft, opRight);
}
writer.newlineAndIndent();
if (allPartKeys != null && allPartKeys.size() > 0) {
writer.keyword("PARTITION");
SqlWriter.Frame frame = writer.startList("(", ")");
for (SqlNode node : allPartKeys) {
writer.sep(",", false);
SqlIdentifier partKey = (SqlIdentifier) node;
SqlProperty spec = partKeyToSpec.get(partKey);
if (spec != null) {
spec.unparse(writer, leftPrec, rightPrec);
} else {
partKey.unparse(writer, leftPrec, rightPrec);
}
}
writer.endList(frame);
writer.newlineAndIndent();
}
getSource().unparse(writer, 0, 0);
}
use of org.apache.calcite.sql.SqlIdentifier in project flink by apache.
the class TableApiIdentifierParsingTest method testTableApiIdentifierParsing.
@Test
public void testTableApiIdentifierParsing() throws ParseException {
FlinkSqlParserImpl parser = createFlinkParser(stringIdentifier);
SqlIdentifier sqlIdentifier = parser.TableApiIdentifier();
assertThat(sqlIdentifier.names, equalTo(expectedParsedIdentifier));
}
use of org.apache.calcite.sql.SqlIdentifier in project flink by apache.
the class SqlValidatorImpl method checkConstraint.
/**
* Validates updates against the constraint of a modifiable view.
*
* @param validatorTable A {@link SqlValidatorTable} that may wrap a ModifiableViewTable
* @param update The UPDATE parse tree node
* @param targetRowType The target type
*/
private void checkConstraint(SqlValidatorTable validatorTable, SqlUpdate update, RelDataType targetRowType) {
final ModifiableViewTable modifiableViewTable = validatorTable.unwrap(ModifiableViewTable.class);
if (modifiableViewTable != null) {
final Table table = modifiableViewTable.unwrap(Table.class);
final RelDataType tableRowType = table.getRowType(typeFactory);
final Map<Integer, RexNode> projectMap = RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType, typeFactory);
final Map<String, Integer> nameToIndex = SqlValidatorUtil.mapNameToIndex(tableRowType.getFieldList());
// Validate update values against the view constraint.
final List<SqlNode> targets = update.getTargetColumnList().getList();
final List<SqlNode> sources = update.getSourceExpressionList().getList();
for (final Pair<SqlNode, SqlNode> column : Pair.zip(targets, sources)) {
final String columnName = ((SqlIdentifier) column.left).getSimple();
final Integer columnIndex = nameToIndex.get(columnName);
if (projectMap.containsKey(columnIndex)) {
final RexNode columnConstraint = projectMap.get(columnIndex);
final ValidationError validationError = new ValidationError(column.right, RESOURCE.viewConstraintNotSatisfied(columnName, Util.last(validatorTable.getQualifiedName())));
RelOptUtil.validateValueAgainstConstraint(column.right, columnConstraint, validationError);
}
}
}
}
use of org.apache.calcite.sql.SqlIdentifier in project flink by apache.
the class SqlValidatorImpl method createTargetRowType.
/**
* Derives a row-type for INSERT and UPDATE operations.
*
* @param table Target table for INSERT/UPDATE
* @param targetColumnList List of target columns, or null if not specified
* @param append Whether to append fields to those in <code>
* baseRowType</code>
* @return Rowtype
*/
protected RelDataType createTargetRowType(SqlValidatorTable table, SqlNodeList targetColumnList, boolean append) {
RelDataType baseRowType = table.getRowType();
if (targetColumnList == null) {
return baseRowType;
}
List<RelDataTypeField> targetFields = baseRowType.getFieldList();
final List<Map.Entry<String, RelDataType>> fields = new ArrayList<>();
if (append) {
for (RelDataTypeField targetField : targetFields) {
fields.add(Pair.of(SqlUtil.deriveAliasFromOrdinal(fields.size()), targetField.getType()));
}
}
final Set<Integer> assignedFields = new HashSet<>();
final RelOptTable relOptTable = table instanceof RelOptTable ? ((RelOptTable) table) : null;
for (SqlNode node : targetColumnList) {
SqlIdentifier id = (SqlIdentifier) node;
RelDataTypeField targetField = SqlValidatorUtil.getTargetField(baseRowType, typeFactory, id, catalogReader, relOptTable);
if (targetField == null) {
throw newValidationError(id, RESOURCE.unknownTargetColumn(id.toString()));
}
if (!assignedFields.add(targetField.getIndex())) {
throw newValidationError(id, RESOURCE.duplicateTargetColumn(targetField.getName()));
}
fields.add(targetField);
}
return typeFactory.createStructType(fields);
}
use of org.apache.calcite.sql.SqlIdentifier in project flink by apache.
the class AliasNamespace method validateImpl.
// ~ Methods ----------------------------------------------------------------
protected RelDataType validateImpl(RelDataType targetRowType) {
final List<String> nameList = new ArrayList<>();
final List<SqlNode> operands = call.getOperandList();
final SqlValidatorNamespace childNs = validator.getNamespace(operands.get(0));
final RelDataType rowType = childNs.getRowTypeSansSystemColumns();
final List<SqlNode> columnNames = Util.skip(operands, 2);
for (final SqlNode operand : columnNames) {
String name = ((SqlIdentifier) operand).getSimple();
if (nameList.contains(name)) {
throw validator.newValidationError(operand, RESOURCE.aliasListDuplicate(name));
}
nameList.add(name);
}
if (nameList.size() != rowType.getFieldCount()) {
// Position error over all column names
final SqlNode node = operands.size() == 3 ? operands.get(2) : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
throw validator.newValidationError(node, RESOURCE.aliasListDegree(rowType.getFieldCount(), getString(rowType), nameList.size()));
}
final List<RelDataType> typeList = new ArrayList<>();
for (RelDataTypeField field : rowType.getFieldList()) {
typeList.add(field.getType());
}
RelDataType aliasedType = validator.getTypeFactory().createStructType(rowType.getStructKind(), typeList, nameList);
return validator.getTypeFactory().createTypeWithNullability(aliasedType, rowType.isNullable());
}
Aggregations