Java Code Examples for org.apache.calcite.util.Util#skipLast()

The following examples show how to use org.apache.calcite.util.Util#skipLast() . 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: SqlConverter.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * check if the schema provided is a valid schema:
 * <li>schema is not indicated (only one element in the names list)<li/>
 *
 * @param names list of schema and table names, table name is always the last element
 * @throws UserException if the schema is not valid.
 */
private void isValidSchema(final List<String> names) throws UserException {
  SchemaPlus defaultSchema = session.getDefaultSchema(this.rootSchema);
  String defaultSchemaCombinedPath = SchemaUtilites.getSchemaPath(defaultSchema);
  List<String> schemaPath = Util.skipLast(names);
  String schemaPathCombined = SchemaUtilites.getSchemaPath(schemaPath);
  String commonPrefix = SchemaUtilites.getPrefixSchemaPath(defaultSchemaCombinedPath,
          schemaPathCombined,
          parserConfig.caseSensitive());
  boolean isPrefixDefaultPath = commonPrefix.length() == defaultSchemaCombinedPath.length();
  List<String> fullSchemaPath = Strings.isNullOrEmpty(defaultSchemaCombinedPath) ? schemaPath :
          isPrefixDefaultPath ? schemaPath : ListUtils.union(SchemaUtilites.getSchemaPathAsList(defaultSchema), schemaPath);
  if (names.size() > 1 && (SchemaUtilites.findSchema(this.rootSchema, fullSchemaPath) == null &&
          SchemaUtilites.findSchema(this.rootSchema, schemaPath) == null)) {
    SchemaUtilites.throwSchemaNotFoundException(defaultSchema, schemaPath);
  }
}
 
Example 3
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 4
Source File: SqlValidatorUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static void getSchemaObjectMonikers(
    SqlValidatorCatalogReader catalogReader,
    List<String> names,
    List<SqlMoniker> hints) {
  // Assume that the last name is 'dummy' or similar.
  List<String> subNames = Util.skipLast(names);

  // Try successively with catalog.schema, catalog and no prefix
  for (List<String> x : catalogReader.getSchemaPaths()) {
    final List<String> names2 =
        ImmutableList.<String>builder().addAll(x).addAll(subNames).build();
    hints.addAll(catalogReader.getAllSchemaObjectNames(names2));
  }
}
 
Example 5
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
protected SqlNode createLeftCall(SqlOperator op, List<SqlNode> nodeList) {
  if (nodeList.size() == 2) {
    return op.createCall(new SqlNodeList(nodeList, POS));
  }
  final List<SqlNode> butLast = Util.skipLast(nodeList);
  final SqlNode last = nodeList.get(nodeList.size() - 1);
  final SqlNode call = createLeftCall(op, butLast);
  return op.createCall(new SqlNodeList(ImmutableList.of(call, last), POS));
}
 
Example 6
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
private SqlNode createLeftCall(SqlOperator op, List<SqlNode> nodeList) {
  if (nodeList.size() == 2) {
    return op.createCall(new SqlNodeList(nodeList, POS));
  }
  final List<SqlNode> butLast = Util.skipLast(nodeList);
  final SqlNode last = nodeList.get(nodeList.size() - 1);
  final SqlNode call = createLeftCall(op, butLast);
  return op.createCall(new SqlNodeList(ImmutableList.of(call, last), POS));
}
 
Example 7
Source File: SqlValidatorUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static void getSchemaObjectMonikers(
    SqlValidatorCatalogReader catalogReader,
    List<String> names,
    List<SqlMoniker> hints) {
  // Assume that the last name is 'dummy' or similar.
  List<String> subNames = Util.skipLast(names);

  // Try successively with catalog.schema, catalog and no prefix
  for (List<String> x : catalogReader.getSchemaPaths()) {
    final List<String> names2 =
        ImmutableList.<String>builder().addAll(x).addAll(subNames).build();
    hints.addAll(catalogReader.getAllSchemaObjectNames(names2));
  }
}
 
Example 8
Source File: DescribeTableHandler.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Rewrite the parse tree as SELECT ... FROM INFORMATION_SCHEMA.COLUMNS ... */
@Override
public SqlNode rewrite(SqlNode sqlNode) throws ForemanSetupException {
  DrillSqlDescribeTable node = unwrap(sqlNode, DrillSqlDescribeTable.class);

  try {
    List<SqlNode> selectList = Arrays.asList(
        new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO),
        new SqlIdentifier(COLS_COL_DATA_TYPE, SqlParserPos.ZERO),
        new SqlIdentifier(COLS_COL_IS_NULLABLE, SqlParserPos.ZERO));

    SqlNode fromClause = new SqlIdentifier(Arrays.asList(IS_SCHEMA_NAME, InfoSchemaTableType.COLUMNS.name()), SqlParserPos.ZERO);

    SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
    List<String> schemaPathGivenInCmd = Util.skipLast(node.getTable().names);
    SchemaPlus schema = SchemaUtilites.findSchema(defaultSchema, schemaPathGivenInCmd);

    if (schema == null) {
      SchemaUtilites.throwSchemaNotFoundException(defaultSchema, SchemaUtilites.getSchemaPath(schemaPathGivenInCmd));
    }

    if (SchemaUtilites.isRootSchema(schema)) {
      throw UserException.validationError()
          .message("No schema selected.")
          .build(logger);
    }

    // find resolved schema path
    AbstractSchema drillSchema = SchemaUtilites.unwrapAsDrillSchemaInstance(schema);
    String schemaPath = drillSchema.getFullSchemaName();

    String tableName = Util.last(node.getTable().names);

    if (schema.getTable(tableName) == null) {
      throw UserException.validationError()
          .message("Unknown table [%s] in schema [%s]", tableName, schemaPath)
          .build(logger);
    }

    SqlNode schemaCondition = null;
    if (!SchemaUtilites.isRootSchema(schema)) {
      schemaCondition = DrillParserUtil.createCondition(
          new SqlIdentifier(SHRD_COL_TABLE_SCHEMA, SqlParserPos.ZERO),
          SqlStdOperatorTable.EQUALS,
          SqlLiteral.createCharString(schemaPath, Util.getDefaultCharset().name(), SqlParserPos.ZERO)
      );
    }

    SqlNode tableNameColumn = new SqlIdentifier(SHRD_COL_TABLE_NAME, SqlParserPos.ZERO);

    // if table names are case insensitive, wrap column values and condition in lower function
    if (!drillSchema.areTableNamesCaseSensitive()) {
      tableNameColumn = SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, tableNameColumn);
      tableName = tableName.toLowerCase();
    }

    SqlNode where = DrillParserUtil.createCondition(tableNameColumn,
        SqlStdOperatorTable.EQUALS,
        SqlLiteral.createCharString(tableName, Util.getDefaultCharset().name(), SqlParserPos.ZERO));

    where = DrillParserUtil.createCondition(schemaCondition, SqlStdOperatorTable.AND, where);

    SqlNode columnFilter = null;
    if (node.getColumn() != null) {
      columnFilter =
          DrillParserUtil.createCondition(
              SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO)),
              SqlStdOperatorTable.EQUALS,
              SqlLiteral.createCharString(node.getColumn().toString().toLowerCase(), Util.getDefaultCharset().name(), SqlParserPos.ZERO));
    } else if (node.getColumnQualifier() != null) {
      SqlNode columnQualifier = node.getColumnQualifier();
      SqlNode column = new SqlIdentifier(COLS_COL_COLUMN_NAME, SqlParserPos.ZERO);
      if (columnQualifier instanceof SqlCharStringLiteral) {
        NlsString conditionString = ((SqlCharStringLiteral) columnQualifier).getNlsString();
        columnQualifier = SqlCharStringLiteral.createCharString(
            conditionString.getValue().toLowerCase(),
            conditionString.getCharsetName(),
            columnQualifier.getParserPosition());
        column = SqlStdOperatorTable.LOWER.createCall(SqlParserPos.ZERO, column);
      }
      columnFilter = DrillParserUtil.createCondition(column, SqlStdOperatorTable.LIKE, columnQualifier);
    }

    where = DrillParserUtil.createCondition(where, SqlStdOperatorTable.AND, columnFilter);

    return new SqlSelect(SqlParserPos.ZERO, null, new SqlNodeList(selectList, SqlParserPos.ZERO),
        fromClause, where, null, null, null, null, null, null);
  } catch (Exception ex) {
    throw UserException.planError(ex)
        .message("Error while rewriting DESCRIBE query: %d", ex.getMessage())
        .build(logger);
  }
}