Class SqlFilterParser

java.lang.Object
dev.langchain4j.store.embedding.filter.parser.sql.SqlFilterParser
All Implemented Interfaces:
FilterParser

public class SqlFilterParser extends Object implements FilterParser
Parses an SQL "WHERE" clause into a Filter object using JSqlParser.
Can parse a complete SQL statement (e.g., SELECT * FROM fake_table WHERE id = 7) as well as just the contents of a "WHERE" clause (e.g., id = 7).
Currently, supports all SQL dialects supported by JSqlParser.
But we recommend using ANSI SQL or PostgreSQL as we cannot guarantee that this class will support all SQL dialects going forward.

Currently, the following operations are supported:
 - IsEqualTo: name = 'Klaus'
 - IsNotEqualTo: id != 7
 - IsGreaterThan: age > 18
 - IsGreaterThanOrEqualTo: age >= 18
 - IsLessThan: age < 18
 - IsLessThanOrEqualTo: age <= 18
 - IsIn: name IN ('Klaus', 'Francine')
 - IsNotIn: id NOT IN (1, 2, 3)
 - BETWEEN: year BETWEEN 2000 AND 2020: will be parsed into key("year").gte(2000).and(key("year").lte(2020))

 - And: name = 'Klaus' AND age = 18
 - Not: NOT(name = 'Klaus') / NOT name = 'Klaus'
 - Or: name = 'Klaus' OR age = 18

 - YEAR/MONTH(CURDATE()): For example, year = YEAR(CURDATE()) to get the current year. Provided Clock will be used to resolve CURDATE().
 - EXTRACT(YEAR/MONTH/WEEK/DAY/DOW/DOY/HOUR/MINUTE FROM CURRENT_DATE/CURRENT_TIME/CURRENT_TIMESTAMP): For example: year = EXTRACT(YEAR FROM CURRENT_DATE) to get the current year. Provided Clock will be used to resolve CURRENT_DATE.

 - Arithmetic: +, -, *, /. For example: year = YEAR(CURDATE()) - 1 to get previous year.

 - Parentheses: (name = 'Klaus' OR name = 'Francine') AND age = 18. Expressions within parentheses are evaluated first.
 
If you require additional operations, please open an issue.

Here are a few examples of how a String is parsed into a Filter:
 name = 'Klaus' -> key("name").eq("Klaus")
 name = 'Klaus' AND age >= 18 -> key("name").eq("Klaus").and(key("age").gte(18))
 
  • Constructor Details

    • SqlFilterParser

      public SqlFilterParser()
      Creates an instance of SqlFilterParser.
      By default, Clock.systemDefaultZone() will be used to get the current date and/or time when required. For example, when parsing the SQL query SELECT * FROM fake_table WHERE year = EXTRACT(YEAR FROM CURRENT_DATE), the Clock.systemDefaultZone() will be used to resolve CURRENT_DATE.
    • SqlFilterParser

      public SqlFilterParser(Clock clock)
      Creates an instance of SqlFilterParser.
      Parameters:
      clock - A Clock to be used to get the current date and/or time when required. For example, when parsing the SQL query SELECT * FROM fake_table WHERE year = EXTRACT(YEAR FROM CURRENT_DATE), the provided Clock will be used to resolve CURRENT_DATE.
  • Method Details

    • parse

      public Filter parse(String sql)
      Description copied from interface: FilterParser
      Parses a filter expression string into a Filter object.
      Specified by:
      parse in interface FilterParser
      Parameters:
      sql - The filter expression as a string.
      Returns:
      A Filter object.