Java Code Examples for org.apache.calcite.sql.SqlIdentifier#getSimple()

The following examples show how to use org.apache.calcite.sql.SqlIdentifier#getSimple() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqlValidatorUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Finds a {@link org.apache.calcite.schema.CalciteSchema.TypeEntry} in a
 * given schema whose type has the given name, possibly qualified.
 *
 * @param rootSchema root schema
 * @param typeName name of the type, may be qualified or fully-qualified
 *
 * @return TypeEntry with a table with the given name, or null
 */
public static CalciteSchema.TypeEntry getTypeEntry(
    CalciteSchema rootSchema, SqlIdentifier typeName) {
  final String name;
  final List<String> path;
  if (typeName.isSimple()) {
    path = ImmutableList.of();
    name = typeName.getSimple();
  } else {
    path = Util.skipLast(typeName.names);
    name = Util.last(typeName.names);
  }
  CalciteSchema schema = rootSchema;
  for (String p : path) {
    if (schema == rootSchema
        && SqlNameMatchers.withCaseSensitive(true).matches(p, schema.getName())) {
      continue;
    }
    schema = schema.getSubSchema(p, true);
  }
  return schema == null ? null : schema.getType(name, false);
}
 
Example 2
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public SqlNode visit(SqlIdentifier id) {
	// Aliases, e.g. 'select a as x, b from t order by x'.
	if (id.isSimple()
		&& getConformance().isSortByAlias()) {
		String alias = id.getSimple();
		final SqlValidatorNamespace selectNs = getNamespace(select);
		final RelDataType rowType =
			selectNs.getRowTypeSansSystemColumns();
		final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
		RelDataTypeField field = nameMatcher.field(rowType, alias);
		if (field != null) {
			return nthSelectItem(
				field.getIndex(),
				id.getParserPosition());
		}
	}

	// No match. Return identifier unchanged.
	return getScope().fullyQualify(id).identifier;
}
 
Example 3
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
public SqlNode visit(SqlIdentifier id) {
	// Aliases, e.g. 'select a as x, b from t order by x'.
	if (id.isSimple()
		&& getConformance().isSortByAlias()) {
		String alias = id.getSimple();
		final SqlValidatorNamespace selectNs = getNamespace(select);
		final RelDataType rowType =
			selectNs.getRowTypeSansSystemColumns();
		final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
		RelDataTypeField field = nameMatcher.field(rowType, alias);
		if (field != null) {
			return nthSelectItem(
				field.getIndex(),
				id.getParserPosition());
		}
	}

	// No match. Return identifier unchanged.
	return getScope().fullyQualify(id).identifier;
}
 
Example 4
Source File: FunctionCatalogOperatorTable.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void lookupOperatorOverloads(
		SqlIdentifier opName,
		SqlFunctionCategory category,
		SqlSyntax syntax,
		List<SqlOperator> operatorList,
		SqlNameMatcher nameMatcher) {
	if (!opName.isSimple()) {
		return;
	}

	// We lookup only user functions via CatalogOperatorTable. Built in functions should
	// go through BasicOperatorTable
	if (isNotUserFunction(category)) {
		return;
	}

	String name = opName.getSimple();
	Optional<FunctionLookup.Result> candidateFunction = functionCatalog.lookupFunction(name);

	candidateFunction.flatMap(lookupResult ->
		convertToSqlFunction(category, name, lookupResult.getFunctionDefinition())
	).ifPresent(operatorList::add);
}
 
Example 5
Source File: FunctionCatalogOperatorTable.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void lookupOperatorOverloads(
		SqlIdentifier opName,
		SqlFunctionCategory category,
		SqlSyntax syntax,
		List<SqlOperator> operatorList,
		SqlNameMatcher nameMatcher) {
	if (!opName.isSimple()) {
		return;
	}

	// We lookup only user functions via CatalogOperatorTable. Built in functions should
	// go through BasicOperatorTable
	if (isNotUserFunction(category)) {
		return;
	}

	String name = opName.getSimple();
	Optional<FunctionLookup.Result> candidateFunction = functionCatalog.lookupFunction(name);

	candidateFunction.flatMap(lookupResult ->
		convertToSqlFunction(category, name, lookupResult.getFunctionDefinition())
	).ifPresent(operatorList::add);
}
 
Example 6
Source File: SqlValidatorUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Finds a {@link org.apache.calcite.jdbc.CalciteSchema.TypeEntry} in a
 * given schema whose type has the given name, possibly qualified.
 *
 * @param rootSchema root schema
 * @param typeName name of the type, may be qualified or fully-qualified
 *
 * @return TypeEntry with a table with the given name, or null
 */
public static CalciteSchema.TypeEntry getTypeEntry(
    CalciteSchema rootSchema, SqlIdentifier typeName) {
  final String name;
  final List<String> path;
  if (typeName.isSimple()) {
    path = ImmutableList.of();
    name = typeName.getSimple();
  } else {
    path = Util.skipLast(typeName.names);
    name = Util.last(typeName.names);
  }
  CalciteSchema schema = rootSchema;
  for (String p : path) {
    if (schema == rootSchema
        && SqlNameMatchers.withCaseSensitive(true).matches(p, schema.getName())) {
      continue;
    }
    schema = schema.getSubSchema(p, true);
  }
  return schema == null ? null : schema.getType(name, false);
}
 
Example 7
Source File: FunctionCatalogOperatorTable.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void lookupOperatorOverloads(
		SqlIdentifier opName,
		SqlFunctionCategory category,
		SqlSyntax syntax,
		List<SqlOperator> operatorList,
		SqlNameMatcher nameMatcher) {
	if (!opName.isSimple()) {
		return;
	}

	// We lookup only user functions via CatalogOperatorTable. Built in functions should
	// go through BasicOperatorTable
	if (isNotUserFunction(category)) {
		return;
	}

	String name = opName.getSimple();
	Optional<FunctionLookup.Result> candidateFunction = functionCatalog.lookupFunction(
		UnresolvedIdentifier.of(name));

	candidateFunction.flatMap(lookupResult ->
		convertToSqlFunction(category, name, lookupResult.getFunctionDefinition())
	).ifPresent(operatorList::add);
}
 
Example 8
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
protected SqlWindow getWindowByName(
	SqlIdentifier id,
	SqlValidatorScope scope) {
	SqlWindow window = null;
	if (id.isSimple()) {
		final String name = id.getSimple();
		window = scope.lookupWindow(name);
	}
	if (window == null) {
		throw newValidationError(id, RESOURCE.windowNotFound(id.toString()));
	}
	return window;
}
 
Example 9
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode visit(SqlIdentifier id) {
	if (id.isSimple()
		&& (havingExpr
			    ? validator.getConformance().isHavingAlias()
			    : validator.getConformance().isGroupByAlias())) {
		String name = id.getSimple();
		SqlNode expr = null;
		final SqlNameMatcher nameMatcher =
			validator.catalogReader.nameMatcher();
		int n = 0;
		for (SqlNode s : select.getSelectList()) {
			final String alias = SqlValidatorUtil.getAlias(s, -1);
			if (alias != null && nameMatcher.matches(alias, name)) {
				expr = s;
				n++;
			}
		}
		if (n == 0) {
			return super.visit(id);
		} else if (n > 1) {
			// More than one column has this alias.
			throw validator.newValidationError(id,
				RESOURCE.columnAmbiguous(name));
		}
		if (havingExpr && validator.isAggregate(root)) {
			return super.visit(id);
		}
		expr = stripAs(expr);
		if (expr instanceof SqlIdentifier) {
			expr = getScope().fullyQualify((SqlIdentifier) expr).identifier;
		}
		return expr;
	}
	return super.visit(id);
}
 
Example 10
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
protected SqlWindow getWindowByName(
	SqlIdentifier id,
	SqlValidatorScope scope) {
	SqlWindow window = null;
	if (id.isSimple()) {
		final String name = id.getSimple();
		window = scope.lookupWindow(name);
	}
	if (window == null) {
		throw newValidationError(id, RESOURCE.windowNotFound(id.toString()));
	}
	return window;
}
 
Example 11
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode visit(SqlIdentifier id) {
	if (id.isSimple()
		&& (havingExpr
			    ? validator.getConformance().isHavingAlias()
			    : validator.getConformance().isGroupByAlias())) {
		String name = id.getSimple();
		SqlNode expr = null;
		final SqlNameMatcher nameMatcher =
			validator.catalogReader.nameMatcher();
		int n = 0;
		for (SqlNode s : select.getSelectList()) {
			final String alias = SqlValidatorUtil.getAlias(s, -1);
			if (alias != null && nameMatcher.matches(alias, name)) {
				expr = s;
				n++;
			}
		}
		if (n == 0) {
			return super.visit(id);
		} else if (n > 1) {
			// More than one column has this alias.
			throw validator.newValidationError(id,
				RESOURCE.columnAmbiguous(name));
		}
		if (havingExpr && validator.isAggregate(root)) {
			return super.visit(id);
		}
		expr = stripAs(expr);
		if (expr instanceof SqlIdentifier) {
			SqlIdentifier sid = (SqlIdentifier) expr;
			final SqlIdentifier fqId = getScope().fullyQualify(sid).identifier;
			expr = expandDynamicStar(sid, fqId);
		}
		return expr;
	}
	return super.visit(id);
}
 
Example 12
Source File: SqlParseUtil.java    From alchemy with Apache License 2.0 5 votes vote down vote up
private static String findSinkName(SqlInsert sqlInsert) {
    SqlNode target = sqlInsert.getTargetTable();
    SqlKind targetKind = target.getKind();
    if (targetKind != SqlKind.IDENTIFIER) {
        throw new IllegalArgumentException("invalid insert SQL, sql:" + sqlInsert.toString());
    }
    SqlIdentifier identifier = (SqlIdentifier)target;
    return identifier.getSimple();
}
 
Example 13
Source File: QuerySemantics.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private static String idToRef(FromNode from, SqlIdentifier id) {
  if (isSimpleID(id)) {
    // TODO: need to check if quotes are correct
    return id.getSimple();
  } else {
    SqlIdentifier finalId = id;
    // removing unnecessary table prefix from col ref.
    if (id.names.size() > from.alias.names.size()) {
      boolean isPrefix = true;
      for (int i = 0; i < from.alias.names.size(); i++) {
        String an = from.alias.names.get(i);
        String idn = id.names.get(i);
        if (!an.equals(idn)) {
          isPrefix = false;
          break;
        }
      }
      if (isPrefix) {
        finalId = id.getComponent(from.alias.names.size(), id.names.size());
      }
    }
    if (!finalId.isSimple()) {
      throw new IllegalArgumentException("expected a simple type column name (directly or prefixed with table name/alias)");
    }
    return finalId.getSimple();
  }
}
 
Example 14
Source File: ReflectiveSqlOperatorTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void lookupOperatorOverloads(SqlIdentifier opName,
    SqlFunctionCategory category, SqlSyntax syntax,
    List<SqlOperator> operatorList, SqlNameMatcher nameMatcher) {
  // NOTE jvs 3-Mar-2005:  ignore category until someone cares

  String simpleName;
  if (opName.names.size() > 1) {
    if (opName.names.get(opName.names.size() - 2).equals(IS_NAME)) {
      // per SQL99 Part 2 Section 10.4 Syntax Rule 7.b.ii.1
      simpleName = Util.last(opName.names);
    } else {
      return;
    }
  } else {
    simpleName = opName.getSimple();
  }

  final Collection<SqlOperator> list =
      lookUpOperators(simpleName, syntax, nameMatcher);
  if (list.isEmpty()) {
    return;
  }
  for (SqlOperator op : list) {
    if (op.getSyntax() == syntax) {
      operatorList.add(op);
    } else if (syntax == SqlSyntax.FUNCTION
        && op instanceof SqlFunction) {
      // this special case is needed for operators like CAST,
      // which are treated as functions but have special syntax
      operatorList.add(op);
    }
  }

  // REVIEW jvs 1-Jan-2005:  why is this extra lookup required?
  // Shouldn't it be covered by search above?
  switch (syntax) {
  case BINARY:
  case PREFIX:
  case POSTFIX:
    for (SqlOperator extra
        : lookUpOperators(simpleName, syntax, nameMatcher)) {
      // REVIEW: should only search operators added during this method?
      if (extra != null && !operatorList.contains(extra)) {
        operatorList.add(extra);
      }
    }
    break;
  }
}
 
Example 15
Source File: SqlAlterTableSetOption.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private SqlAlterTableSetOption(SqlParserPos pos, SqlIdentifier table, SqlIdentifier scope, SqlIdentifier name, SqlNode value) {
  super(pos, scope.getSimple(), name, value);
  this.table = table;
}
 
Example 16
Source File: FlinkSqlDataTypeSpec.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
	final SqlIdentifier typeName = getTypeName();
	String name = typeName.getSimple();
	if (typeName instanceof ExtendedSqlType) {
		typeName.unparse(writer, leftPrec, rightPrec);
	} else if (SqlTypeName.get(name) != null) {
		SqlTypeName sqlTypeName = SqlTypeName.get(name);
		writer.keyword(name);
		if (sqlTypeName.allowsPrec() && this.getPrecision() >= 0) {
			SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")");
			writer.print(this.getPrecision());
			if (sqlTypeName.allowsScale() && this.getScale() >= 0) {
				writer.sep(",", true);
				writer.print(this.getScale());
			}

			writer.endList(frame);
		}

		if (this.getCharSetName() != null) {
			writer.keyword("CHARACTER SET");
			writer.identifier(this.getCharSetName(), false);
		}

		if (this.getCollectionsTypeName() != null) {
			// Fix up nullable attribute if this is a collection type.
			if (elementNullable != null && !elementNullable) {
				writer.keyword("NOT NULL");
			}
			writer.keyword(this.getCollectionsTypeName().getSimple());
		}
	} else if (name.startsWith("_")) {
		writer.keyword(name.substring(1));
	} else {
		this.getTypeName().unparse(writer, leftPrec, rightPrec);
	}
	if (getNullable() != null && !getNullable()) {
		writer.keyword("NOT NULL");
	}
}
 
Example 17
Source File: ReflectiveSqlOperatorTable.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void lookupOperatorOverloads(SqlIdentifier opName,
    SqlFunctionCategory category,
    SqlSyntax syntax,
    List<SqlOperator> operatorList) {
  // NOTE jvs 3-Mar-2005:  ignore category until someone cares

  String simpleName;
  if (opName.names.size() > 1) {
    if (opName.names.get(opName.names.size() - 2).equals(IS_NAME)) {
      // per SQL99 Part 2 Section 10.4 Syntax Rule 7.b.ii.1
      simpleName = Util.last(opName.names);
    } else {
      return;
    }
  } else {
    simpleName = opName.getSimple();
  }

  // Always look up built-in operators case-insensitively. Even in sessions
  // with unquotedCasing=UNCHANGED and caseSensitive=true.
  final Collection<SqlOperator> list =
      operators.get(new Key(simpleName, syntax));
  if (list.isEmpty()) {
    return;
  }
  for (SqlOperator op : list) {
    if (op.getSyntax() == syntax) {
      operatorList.add(op);
    } else if (syntax == SqlSyntax.FUNCTION
        && op instanceof SqlFunction) {
      // this special case is needed for operators like CAST,
      // which are treated as functions but have special syntax
      operatorList.add(op);
    }
  }

  // REVIEW jvs 1-Jan-2005:  why is this extra lookup required?
  // Shouldn't it be covered by search above?
  switch (syntax) {
  case BINARY:
  case PREFIX:
  case POSTFIX:
    for (SqlOperator extra : operators.get(new Key(simpleName, syntax))) {
      // REVIEW: should only search operators added during this method?
      if (extra != null && !operatorList.contains(extra)) {
        operatorList.add(extra);
      }
    }
    break;
  }
}