use of org.apache.calcite.sql.SqlIdentifier in project calcite by apache.
the class SqlDdlNodes method renameColumns.
/**
* Wraps a query to rename its columns. Used by CREATE VIEW and CREATE
* MATERIALIZED VIEW.
*/
static SqlNode renameColumns(SqlNodeList columnList, SqlNode query) {
if (columnList == null) {
return query;
}
final SqlParserPos p = query.getParserPosition();
final SqlNodeList selectList = new SqlNodeList(ImmutableList.<SqlNode>of(SqlIdentifier.star(p)), p);
final SqlCall from = SqlStdOperatorTable.AS.createCall(p, ImmutableList.<SqlNode>builder().add(query).add(new SqlIdentifier("_", p)).addAll(columnList).build());
return new SqlSelect(p, null, selectList, from, null, null, null, null, null, null, null);
}
use of org.apache.calcite.sql.SqlIdentifier in project calcite by apache.
the class CalciteCatalogReader method operatorTable.
/**
* Creates an operator table that contains functions in the given class.
*
* @see ModelHandler#addFunctions
*/
public static SqlOperatorTable operatorTable(String className) {
// Dummy schema to collect the functions
final CalciteSchema schema = CalciteSchema.createRootSchema(false, false);
ModelHandler.addFunctions(schema.plus(), null, ImmutableList.<String>of(), className, "*", true);
// The following is technical debt; see [CALCITE-2082] Remove
// RelDataTypeFactory argument from SqlUserDefinedAggFunction constructor
final SqlTypeFactoryImpl typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final ListSqlOperatorTable table = new ListSqlOperatorTable();
for (String name : schema.getFunctionNames()) {
for (Function function : schema.getFunctions(name, true)) {
final SqlIdentifier id = new SqlIdentifier(name, SqlParserPos.ZERO);
table.add(toOp(typeFactory, id, function));
}
}
return table;
}
use of org.apache.calcite.sql.SqlIdentifier in project calcite by apache.
the class IdentifierNamespace method validateImpl.
public RelDataType validateImpl(RelDataType targetRowType) {
resolvedNamespace = Preconditions.checkNotNull(resolveImpl(id));
if (resolvedNamespace instanceof TableNamespace) {
SqlValidatorTable table = resolvedNamespace.getTable();
if (validator.shouldExpandIdentifiers()) {
// TODO: expand qualifiers for column references also
List<String> qualifiedNames = table.getQualifiedName();
if (qualifiedNames != null) {
// Assign positions to the components of the fully-qualified
// identifier, as best we can. We assume that qualification
// adds names to the front, e.g. FOO.BAR becomes BAZ.FOO.BAR.
List<SqlParserPos> poses = new ArrayList<>(Collections.nCopies(qualifiedNames.size(), id.getParserPosition()));
int offset = qualifiedNames.size() - id.names.size();
// reader.
if (offset >= 0) {
for (int i = 0; i < id.names.size(); i++) {
poses.set(i + offset, id.getComponentParserPosition(i));
}
}
id.setNames(qualifiedNames, poses);
}
}
}
RelDataType rowType = resolvedNamespace.getRowType();
if (extendList != null) {
if (!(resolvedNamespace instanceof TableNamespace)) {
throw new RuntimeException("cannot convert");
}
resolvedNamespace = ((TableNamespace) resolvedNamespace).extend(extendList);
rowType = resolvedNamespace.getRowType();
}
// Build a list of monotonic expressions.
final ImmutableList.Builder<Pair<SqlNode, SqlMonotonicity>> builder = ImmutableList.builder();
List<RelDataTypeField> fields = rowType.getFieldList();
for (RelDataTypeField field : fields) {
final String fieldName = field.getName();
final SqlMonotonicity monotonicity = resolvedNamespace.getMonotonicity(fieldName);
if (monotonicity != SqlMonotonicity.NOT_MONOTONIC) {
builder.add(Pair.of((SqlNode) new SqlIdentifier(fieldName, SqlParserPos.ZERO), monotonicity));
}
}
monotonicExprs = builder.build();
// Validation successful.
return rowType;
}
use of org.apache.calcite.sql.SqlIdentifier in project calcite by apache.
the class AliasNamespace method validateImpl.
// ~ Methods ----------------------------------------------------------------
protected RelDataType validateImpl(RelDataType targetRowType) {
final List<String> nameList = new ArrayList<String>();
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<RelDataType>();
for (RelDataTypeField field : rowType.getFieldList()) {
typeList.add(field.getType());
}
return validator.getTypeFactory().createStructType(typeList, nameList);
}
use of org.apache.calcite.sql.SqlIdentifier in project calcite by apache.
the class LookupOperatorOverloadsTest method test.
@Test
public void test() throws SQLException {
final String schemaName = "MySchema";
final String funcName = "MyFUNC";
final String anotherName = "AnotherFunc";
try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) {
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
SchemaPlus rootSchema = calciteConnection.getRootSchema();
SchemaPlus schema = rootSchema.add(schemaName, new AbstractSchema());
final TableFunction table = TableFunctionImpl.create(Smalls.MAZE_METHOD);
schema.add(funcName, table);
schema.add(anotherName, table);
final TableFunction table2 = TableFunctionImpl.create(Smalls.MAZE3_METHOD);
schema.add(funcName, table2);
final CalciteServerStatement statement = connection.createStatement().unwrap(CalciteServerStatement.class);
final CalcitePrepare.Context prepareContext = statement.createPrepareContext();
final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
CalciteCatalogReader reader = new CalciteCatalogReader(prepareContext.getRootSchema(), ImmutableList.<String>of(), typeFactory, prepareContext.config());
final List<SqlOperator> operatorList = new ArrayList<>();
SqlIdentifier myFuncIdentifier = new SqlIdentifier(Lists.newArrayList(schemaName, funcName), null, SqlParserPos.ZERO, null);
reader.lookupOperatorOverloads(myFuncIdentifier, SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION, SqlSyntax.FUNCTION, operatorList);
checkFunctionType(2, funcName, operatorList);
operatorList.clear();
reader.lookupOperatorOverloads(myFuncIdentifier, SqlFunctionCategory.USER_DEFINED_FUNCTION, SqlSyntax.FUNCTION, operatorList);
checkFunctionType(0, null, operatorList);
operatorList.clear();
SqlIdentifier anotherFuncIdentifier = new SqlIdentifier(Lists.newArrayList(schemaName, anotherName), null, SqlParserPos.ZERO, null);
reader.lookupOperatorOverloads(anotherFuncIdentifier, SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION, SqlSyntax.FUNCTION, operatorList);
checkFunctionType(1, anotherName, operatorList);
}
}
Aggregations