Esempio n. 1
0
 /**
  * Given "INTEGER", returns BasicSqlType(INTEGER). Given "VARCHAR(10)", returns
  * BasicSqlType(VARCHAR, 10). Given "NUMERIC(10, 2)", returns BasicSqlType(NUMERIC, 10, 2).
  */
 private RelDataType parseTypeString(RelDataTypeFactory typeFactory, String typeString) {
   int precision = -1;
   int scale = -1;
   int open = typeString.indexOf("(");
   if (open >= 0) {
     int close = typeString.indexOf(")", open);
     if (close >= 0) {
       String rest = typeString.substring(open + 1, close);
       typeString = typeString.substring(0, open);
       int comma = rest.indexOf(",");
       if (comma >= 0) {
         precision = Integer.parseInt(rest.substring(0, comma));
         scale = Integer.parseInt(rest.substring(comma));
       } else {
         precision = Integer.parseInt(rest);
       }
     }
   }
   try {
     final SqlTypeName typeName = SqlTypeName.valueOf(typeString);
     return typeName.allowsPrecScale(true, true)
         ? typeFactory.createSqlType(typeName, precision, scale)
         : typeName.allowsPrecScale(true, false)
             ? typeFactory.createSqlType(typeName, precision)
             : typeFactory.createSqlType(typeName);
   } catch (IllegalArgumentException e) {
     return typeFactory.createSqlType(SqlTypeName.ANY);
   }
 }
Esempio n. 2
0
 private RelDataType sqlType(
     RelDataTypeFactory typeFactory, int dataType, int precision, int scale, String typeString) {
   SqlTypeName sqlTypeName = SqlTypeName.getNameForJdbcType(dataType);
   switch (sqlTypeName) {
     case ARRAY:
       RelDataType component = null;
       if (typeString != null && typeString.endsWith(" ARRAY")) {
         // E.g. hsqldb gives "INTEGER ARRAY", so we deduce the component type
         // "INTEGER".
         final String remaining = typeString.substring(0, typeString.length() - " ARRAY".length());
         component = parseTypeString(typeFactory, remaining);
       }
       if (component == null) {
         component = typeFactory.createSqlType(SqlTypeName.ANY);
       }
       return typeFactory.createArrayType(component, -1);
   }
   if (precision >= 0 && scale >= 0 && sqlTypeName.allowsPrecScale(true, true)) {
     return typeFactory.createSqlType(sqlTypeName, precision, scale);
   } else if (precision >= 0 && sqlTypeName.allowsPrecNoScale()) {
     return typeFactory.createSqlType(sqlTypeName, precision);
   } else {
     assert sqlTypeName.allowsNoPrecNoScale();
     return typeFactory.createSqlType(sqlTypeName);
   }
 }
Esempio n. 3
0
  /** Initializes this catalog reader. */
  protected void init() {
    final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);
    final RelDataType varchar10Type = typeFactory.createSqlType(SqlTypeName.VARCHAR, 10);
    final RelDataType varchar20Type = typeFactory.createSqlType(SqlTypeName.VARCHAR, 20);
    final RelDataType timestampType = typeFactory.createSqlType(SqlTypeName.TIMESTAMP);
    final RelDataType booleanType = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
    final RelDataType rectilinearCoordType =
        typeFactory.createStructType(new RelDataType[] {intType, intType}, new String[] {"X", "Y"});

    // TODO jvs 12-Feb-2005: register this canonical instance with type
    // factory
    addressType =
        new ObjectSqlType(
            SqlTypeName.STRUCTURED,
            new SqlIdentifier("ADDRESS", SqlParserPos.ZERO),
            false,
            Arrays.asList(
                new RelDataTypeFieldImpl("STREET", 0, varchar20Type),
                new RelDataTypeFieldImpl("CITY", 1, varchar20Type),
                new RelDataTypeFieldImpl("ZIP", 1, intType),
                new RelDataTypeFieldImpl("STATE", 1, varchar20Type)),
            RelDataTypeComparability.None);

    // Register "SALES" schema.
    MockSchema salesSchema = new MockSchema("SALES");
    registerSchema(salesSchema);

    // Register "EMP" table.
    MockTable empTable = new MockTable(this, salesSchema, "EMP");
    empTable.addColumn("EMPNO", intType);
    empTable.addColumn("ENAME", varchar20Type);
    empTable.addColumn("JOB", varchar10Type);
    empTable.addColumn("MGR", intType);
    empTable.addColumn("HIREDATE", timestampType);
    empTable.addColumn("SAL", intType);
    empTable.addColumn("COMM", intType);
    empTable.addColumn("DEPTNO", intType);
    empTable.addColumn("SLACKER", booleanType);
    registerTable(empTable);

    // Register "DEPT" table.
    MockTable deptTable = new MockTable(this, salesSchema, "DEPT");
    deptTable.addColumn("DEPTNO", intType);
    deptTable.addColumn("NAME", varchar10Type);
    registerTable(deptTable);

    // Register "BONUS" table.
    MockTable bonusTable = new MockTable(this, salesSchema, "BONUS");
    bonusTable.addColumn("ENAME", varchar20Type);
    bonusTable.addColumn("JOB", varchar10Type);
    bonusTable.addColumn("SAL", intType);
    bonusTable.addColumn("COMM", intType);
    registerTable(bonusTable);

    // Register "SALGRADE" table.
    MockTable salgradeTable = new MockTable(this, salesSchema, "SALGRADE");
    salgradeTable.addColumn("GRADE", intType);
    salgradeTable.addColumn("LOSAL", intType);
    salgradeTable.addColumn("HISAL", intType);
    registerTable(salgradeTable);

    // Register "EMP_ADDRESS" table
    MockTable contactAddressTable = new MockTable(this, salesSchema, "EMP_ADDRESS");
    contactAddressTable.addColumn("EMPNO", intType);
    contactAddressTable.addColumn("HOME_ADDRESS", addressType);
    contactAddressTable.addColumn("MAILING_ADDRESS", addressType);
    registerTable(contactAddressTable);

    // Register "CUSTOMER" schema.
    MockSchema customerSchema = new MockSchema("CUSTOMER");
    registerSchema(customerSchema);

    // Register "CONTACT" table.
    MockTable contactTable = new MockTable(this, customerSchema, "CONTACT");
    contactTable.addColumn("CONTACTNO", intType);
    contactTable.addColumn("FNAME", varchar10Type);
    contactTable.addColumn("LNAME", varchar10Type);
    contactTable.addColumn("EMAIL", varchar20Type);
    contactTable.addColumn("COORD", rectilinearCoordType);
    registerTable(contactTable);

    // Register "ACCOUNT" table.
    MockTable accountTable = new MockTable(this, customerSchema, "ACCOUNT");
    accountTable.addColumn("ACCTNO", intType);
    accountTable.addColumn("TYPE", varchar20Type);
    accountTable.addColumn("BALANCE", intType);
    registerTable(accountTable);
  }
  private RelDataType deriveType(
      SqlValidator validator,
      SqlValidatorScope scope,
      SqlCall call,
      boolean convertRowArgToColumnList) {
    final SqlNode[] operands = call.operands;
    RelDataType[] argTypes = new RelDataType[operands.length];

    // Scope for operands. Usually the same as 'scope'.
    final SqlValidatorScope operandScope = scope.getOperandScope(call);

    // Indicate to the validator that we're validating a new function call
    validator.pushFunctionCall();

    try {
      boolean containsRowArg = false;
      for (int i = 0; i < operands.length; ++i) {
        RelDataType nodeType;

        // for row arguments that should be converted to ColumnList
        // types, set the nodeType to a ColumnList type but defer
        // validating the arguments of the row constructor until we know
        // for sure that the row argument maps to a ColumnList type
        if (operands[i].getKind() == SqlKind.ROW && convertRowArgToColumnList) {
          containsRowArg = true;
          RelDataTypeFactory typeFactory = validator.getTypeFactory();
          nodeType = typeFactory.createSqlType(SqlTypeName.COLUMN_LIST);
        } else {
          nodeType = validator.deriveType(operandScope, operands[i]);
        }
        validator.setValidatedNodeType(operands[i], nodeType);
        argTypes[i] = nodeType;
      }

      SqlFunction function =
          SqlUtil.lookupRoutine(
              validator.getOperatorTable(), getNameAsId(), argTypes, getFunctionType());

      // if we have a match on function name and parameter count, but
      // couldn't find a function with  a COLUMN_LIST type, retry, but
      // this time, don't convert the row argument to a COLUMN_LIST type;
      // if we did find a match, go back and revalidate the row operands
      // (corresponding to column references), now that we can set the
      // scope to that of the source cursor referenced by that ColumnList
      // type
      if (containsRowArg) {
        if ((function == null)
            && SqlUtil.matchRoutinesByParameterCount(
                validator.getOperatorTable(), getNameAsId(), argTypes, getFunctionType())) {
          // remove the already validated node types corresponding to
          // row arguments before revalidating
          for (SqlNode operand : operands) {
            if (operand.getKind() == SqlKind.ROW) {
              validator.removeValidatedNodeType(operand);
            }
          }
          return deriveType(validator, scope, call, false);
        } else if (function != null) {
          validator.validateColumnListParams(function, argTypes, operands);
        }
      }

      if (getFunctionType() == SqlFunctionCategory.UserDefinedConstructor) {
        return validator.deriveConstructorType(scope, call, this, function, argTypes);
      }
      if (function == null) {
        validator.handleUnresolvedFunction(call, this, argTypes);
      }

      // REVIEW jvs 25-Mar-2005:  This is, in a sense, expanding
      // identifiers, but we ignore shouldExpandIdentifiers()
      // because otherwise later validation code will
      // choke on the unresolved function.
      call.setOperator(function);
      return function.validateOperands(validator, operandScope, call);
    } finally {
      validator.popFunctionCall();
    }
  }