private void applyVariableDeclaration()

in zetasql-toolkit-core/src/main/java/com/google/zetasql/toolkit/ZetaSQLToolkitAnalyzer.java [279:306]


    private void applyVariableDeclaration(ASTVariableDeclaration declaration) {
      Optional<Type> explicitType =
          Optional.ofNullable(declaration.getType()).map(this::parseASTType);

      Optional<ResolvedExpr> defaultValueExpr =
          Optional.ofNullable(declaration.getDefaultValue())
              .map(
                  expression ->
                      AnalyzerExtensions.analyzeExpression(
                          query, expression, analyzerOptions, catalog.getZetaSQLCatalog()));

      if (!explicitType.isPresent() && !defaultValueExpr.isPresent()) {
        // Should not happen, since this is enforced by the parser
        throw new AnalysisException(
            "Either the type or the default value must be present for variable declarations");
      }

      if (explicitType.isPresent() && defaultValueExpr.isPresent()) {
        this.coerceExpressionToType(explicitType.get(), defaultValueExpr.get());
      }

      Type variableType = explicitType.orElseGet(() -> defaultValueExpr.get().getType());

      declaration.getVariableList().getIdentifierList().stream()
          .map(ASTIdentifier::getIdString)
          .map(variableName -> this.buildConstant(variableName, variableType))
          .forEach(catalog::register);
    }