Skip to content

FilterExpressionTextParser treats all number literals as Integer, failing on Long values #4705

@kacetal

Description

@kacetal

Bug description
FilterExpressionTextParser fails when parsing large integer literals (long values).
All numeric constants are currently treated as Integer, which causes NumberFormatException when the literal exceeds the int range.

This prevents using filter expressions with long metadata IDs (for example, when deleting by ID).

Problematic code in FilterExpressionTextParser:

@Override
public Filter.Operand visitIntegerConstant(FiltersParser.IntegerConstantContext ctx) {
    return new Filter.Value(Integer.valueOf(ctx.getText()));
}

Example exception:

java.lang.NumberFormatException: For input string: "9223372036854775807"

Environment

  • Spring AI version: 1.0.3
  • Java version: 21
  • Vector store: PostgreSQL (pgvector), but issue occurs at parse stage (independent of backend)
  • OS: Linux (Ubuntu 24.04)

Steps to reproduce

import org.springframework.ai.vectorstore.filter.FilterExpressionTextParser;

public class ParserExample {
    public static void main(String[] args) {
        FilterExpressionTextParser parser = new FilterExpressionTextParser();

        parser.parse("id == " + Integer.MAX_VALUE); // Works
        parser.parse("id == " + Integer.MIN_VALUE); // Works
        parser.parse("id == " + Long.MAX_VALUE);    // Fails with NumberFormatException
        parser.parse("id == " + Long.MIN_VALUE);    // Fails with NumberFormatException
    }
}

Actual behavior:
Parsing fails for long values with:

java.lang.NumberFormatException: For input string: "9223372036854775807"

Expected behavior
The parser should correctly handle 64-bit long literals and produce a Filter.Value containing a Long (or BigInteger if needed). All valid Java-style numeric literals should be parsable regardless of size.


Minimal Complete Reproducible example

FilterExpressionTextParser parser = new FilterExpressionTextParser();
parser.parse("id == " + Long.MAX_VALUE);

Expected: no exception
Actual: NumberFormatException


Proposed fix
Change numeric literal handling to support long values:

@Override
public Filter.Operand visitNumberConstant(FiltersParser.IntegerConstantContext ctx) {
  String text = ctx.getText();
  try {
    return new Filter.Value(Integer.parseInt(text));
  } catch (NumberFormatException ignored) {}
  return new Filter.Value(Long.parseLong(text));
}

This preserves compatibility for small numbers while correctly handling large ones.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions