Class SqlFilterParser
java.lang.Object
dev.langchain4j.store.embedding.filter.parser.sql.SqlFilterParser
- All Implemented Interfaces:
FilterParser
Parses an SQL "WHERE" clause into a
Can parse a complete SQL statement (e.g.,
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:
Here are a few examples of how 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:
-If you require additional operations, please open an issue.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 intokey("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. ProvidedClock
will be used to resolveCURDATE()
. - 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. ProvidedClock
will be used to resolveCURRENT_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.
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 Summary
ConstructorsConstructorDescriptionCreates an instance ofSqlFilterParser
.SqlFilterParser
(Clock clock) Creates an instance ofSqlFilterParser
. -
Method Summary
-
Constructor Details
-
SqlFilterParser
public SqlFilterParser()Creates an instance ofSqlFilterParser
.
By default,Clock.systemDefaultZone()
will be used to get the current date and/or time when required. For example, when parsing the SQL querySELECT * FROM fake_table WHERE year = EXTRACT(YEAR FROM CURRENT_DATE)
, theClock.systemDefaultZone()
will be used to resolveCURRENT_DATE
. -
SqlFilterParser
Creates an instance ofSqlFilterParser
.
-
-
Method Details
-
parse
Description copied from interface:FilterParser
Parses a filter expression string into aFilter
object.- Specified by:
parse
in interfaceFilterParser
- Parameters:
sql
- The filter expression as a string.- Returns:
- A
Filter
object.
-