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

The following examples show how to use org.apache.calcite.sql.SqlIdentifier#isSimple() . 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: ListSqlOperatorTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void lookupOperatorOverloads(SqlIdentifier opName,
    SqlFunctionCategory category,
    SqlSyntax syntax,
    List<SqlOperator> operatorList,
    SqlNameMatcher nameMatcher) {
  for (SqlOperator operator : this.operatorList) {
    if (operator.getSyntax() != syntax) {
      continue;
    }
    if (!opName.isSimple()
        || !nameMatcher.matches(operator.getName(), opName.getSimple())) {
      continue;
    }
    if (category != null
        && category != category(operator)
        && !category.isUserDefinedNotSpecificFunction()) {
      continue;
    }
    operatorList.add(operator);
  }
}
 
Example 3
Source File: OperatorTable.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public void lookupOperatorOverloads(SqlIdentifier opName, SqlFunctionCategory category, SqlSyntax syntax, List<SqlOperator> operatorList) {
  // don't try to evaluate operators that have non name.
  if(opName == null || opName.names == null) {
    return;
  }

  inner.lookupOperatorOverloads(opName, category, syntax, operatorList);

  if (operatorList.isEmpty() && syntax == SqlSyntax.FUNCTION && opName.isSimple()) {
    List<SqlOperator> ops = opMap.get(opName.getSimple().toUpperCase());
    if (ops != null) {
      operatorList.addAll(ops);
    }
  }
}
 
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: 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 7
Source File: OrderByScope.java    From calcite with Apache License 2.0 6 votes vote down vote up
public SqlQualified fullyQualify(SqlIdentifier identifier) {
  // If it's a simple identifier, look for an alias.
  if (identifier.isSimple()
      && validator.config().sqlConformance().isSortByAlias()) {
    final String name = identifier.names.get(0);
    final SqlValidatorNamespace selectNs =
        validator.getNamespace(select);
    final RelDataType rowType = selectNs.getRowType();

    final SqlNameMatcher nameMatcher = validator.catalogReader.nameMatcher();
    final RelDataTypeField field = nameMatcher.field(rowType, name);
    final int aliasCount = aliasCount(nameMatcher, name);
    if (aliasCount > 1) {
      // More than one column has this alias.
      throw validator.newValidationError(identifier,
          RESOURCE.columnAmbiguous(name));
    }
    if (field != null && !field.isDynamicStar() && aliasCount == 1) {
      // if identifier is resolved to a dynamic star, use super.fullyQualify() for such case.
      return SqlQualified.create(this, 1, selectNs, identifier);
    }
  }
  return super.fullyQualify(identifier);
}
 
Example 8
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 9
Source File: DrillOperatorTable.java    From Bats with Apache License 2.0 6 votes vote down vote up
private void populateFromTypeInference(SqlIdentifier opName, SqlFunctionCategory category,
                                       SqlSyntax syntax, List<SqlOperator> operatorList) {
  final List<SqlOperator> calciteOperatorList = Lists.newArrayList();
  inner.lookupOperatorOverloads(opName, category, syntax, calciteOperatorList);
  if (!calciteOperatorList.isEmpty()) {
    for (SqlOperator calciteOperator : calciteOperatorList) {
      if (calciteToWrapper.containsKey(calciteOperator)) {
        operatorList.add(calciteToWrapper.get(calciteOperator));
      } else {
        operatorList.add(calciteOperator);
      }
    }
  } else {
    // if no function is found, check in Drill UDFs
    if (operatorList.isEmpty() && (syntax == SqlSyntax.FUNCTION || syntax == SqlSyntax.FUNCTION_ID) && opName.isSimple()) {
      List<SqlOperator> drillOps = drillOperatorsWithInferenceMap.get(opName.getSimple().toLowerCase());
      if (drillOps != null && !drillOps.isEmpty()) {
        operatorList.addAll(drillOps);
      }
    }
  }
}
 
Example 10
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 11
Source File: OrderByScope.java    From Bats with Apache License 2.0 6 votes vote down vote up
public SqlQualified fullyQualify(SqlIdentifier identifier) {
  // If it's a simple identifier, look for an alias.
  if (identifier.isSimple()
      && validator.getConformance().isSortByAlias()) {
    final String name = identifier.names.get(0);
    final SqlValidatorNamespace selectNs =
        validator.getNamespace(select);
    final RelDataType rowType = selectNs.getRowType();

    final SqlNameMatcher nameMatcher = validator.catalogReader.nameMatcher();
    final RelDataTypeField field = nameMatcher.field(rowType, name);
    final int aliasCount = aliasCount(nameMatcher, name);
    if (aliasCount > 1) {
      // More than one column has this alias.
      throw validator.newValidationError(identifier,
          RESOURCE.columnAmbiguous(name));
    }
    if (field != null && !field.isDynamicStar() && aliasCount == 1) {
      // if identifier is resolved to a dynamic star, use super.fullyQualify() for such case.
      return SqlQualified.create(this, 1, selectNs, identifier);
    }
  }
  return super.fullyQualify(identifier);
}
 
Example 12
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 13
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()) {
		return id;
	}
	SqlOperator operator = id.names.get(0).equals(alpha)
		? SqlStdOperatorTable.PREV : SqlStdOperatorTable.LAST;

	return operator.createCall(SqlParserPos.ZERO, id,
		SqlLiteral.createExactNumeric("0", SqlParserPos.ZERO));
}
 
Example 14
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
private SqlValidatorNamespace getNamespace(SqlIdentifier id, DelegatingScope scope) {
	if (id.isSimple()) {
		final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
		final SqlValidatorScope.ResolvedImpl resolved =
			new SqlValidatorScope.ResolvedImpl();
		scope.resolve(id.names, nameMatcher, false, resolved);
		if (resolved.count() == 1) {
			return resolved.only().namespace;
		}
	}
	return getNamespace(id);
}
 
Example 15
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 16
Source File: ExpressionGenerator.java    From streamline with Apache License 2.0 5 votes vote down vote up
@Override
public Expression visit(SqlIdentifier id) {
    if (id.isStar()) {
        return STAR;
    } else if (id.isSimple()) {
        return new FieldExpression(getField(getSchema(), id.getSimple()));
    } else if (id.names.size() == 2) {
        return new FieldExpression(getField(getSchema(id.names.get(0)), id.names.get(1)));
    } else {
        throw new UnsupportedOperationException("Compound identifier with more than two levels");
    }
}
 
Example 17
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 18
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private SqlValidatorNamespace getNamespace(SqlIdentifier id, DelegatingScope scope) {
	if (id.isSimple()) {
		final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
		final SqlValidatorScope.ResolvedImpl resolved =
			new SqlValidatorScope.ResolvedImpl();
		scope.resolve(id.names, nameMatcher, false, resolved);
		if (resolved.count() == 1) {
			return resolved.only().namespace;
		}
	}
	return getNamespace(id);
}
 
Example 19
Source File: SqlValidatorUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Lookup sql function by sql identifier and function category.
 *
 * @param opTab    operator table to look up
 * @param funName  function name
 * @param funcType function category
 * @return A sql function if and only if there is one operator matches, else null
 */
public static SqlOperator lookupSqlFunctionByID(SqlOperatorTable opTab,
    SqlIdentifier funName,
    SqlFunctionCategory funcType) {
  if (funName.isSimple()) {
    final List<SqlOperator> list = new ArrayList<>();
    opTab.lookupOperatorOverloads(funName, funcType, SqlSyntax.FUNCTION, list,
        SqlNameMatchers.withCaseSensitive(funName.isComponentQuoted(0)));
    if (list.size() == 1) {
      return list.get(0);
    }
  }
  return null;
}
 
Example 20
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();
  }
}