Skip to content

Commit ede35cc

Browse files
authored
feat: update _parse_create method to support CreateIndexStmt and add quantile validation tests (#18)
1 parent 2a21081 commit ede35cc

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/qql/parser.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def _parse_insert_bulk_body(self) -> InsertBulkStmt:
151151
model=model, hybrid=hybrid, sparse_model=sparse_model,
152152
)
153153

154-
def _parse_create(self) -> CreateCollectionStmt:
154+
def _parse_create(self) -> CreateCollectionStmt | CreateIndexStmt:
155155
self._expect(TokenKind.CREATE)
156156
if self._peek().kind == TokenKind.COLLECTION:
157157
self._advance()
@@ -213,7 +213,13 @@ def _parse_quantize_clause(self) -> QuantizationConfig:
213213
always_ram: bool = False
214214
if self._peek().kind == TokenKind.QUANTILE:
215215
self._advance()
216-
quantile = float(self._expect(TokenKind.FLOAT).value)
216+
quantile_tok = self._peek()
217+
quantile = float(self._parse_number())
218+
if not 0.0 <= quantile <= 1.0:
219+
raise QQLSyntaxError(
220+
f"QUANTILE must be between 0 and 1 inclusive, got {quantile}",
221+
quantile_tok.pos,
222+
)
217223
if self._peek().kind == TokenKind.ALWAYS:
218224
self._advance()
219225
self._expect(TokenKind.RAM)

tests/test_parser.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,16 @@ def test_scalar_with_quantile(self):
928928
assert node.quantization.type == QuantizationType.SCALAR
929929
assert node.quantization.quantile == pytest.approx(0.99)
930930

931+
def test_scalar_with_quantile_zero(self):
932+
node = parse("CREATE COLLECTION articles QUANTIZE SCALAR QUANTILE 0")
933+
assert node.quantization.type == QuantizationType.SCALAR
934+
assert node.quantization.quantile == pytest.approx(0.0)
935+
936+
def test_scalar_with_quantile_one(self):
937+
node = parse("CREATE COLLECTION articles QUANTIZE SCALAR QUANTILE 1")
938+
assert node.quantization.type == QuantizationType.SCALAR
939+
assert node.quantization.quantile == pytest.approx(1.0)
940+
931941
def test_scalar_with_always_ram(self):
932942
node = parse("CREATE COLLECTION articles QUANTIZE SCALAR ALWAYS RAM")
933943
assert node.quantization.always_ram is True
@@ -1013,3 +1023,11 @@ def test_quantize_missing_type_raises(self):
10131023
def test_quantize_unknown_type_raises(self):
10141024
with pytest.raises(QQLSyntaxError):
10151025
parse("CREATE COLLECTION articles QUANTIZE FULL")
1026+
1027+
def test_scalar_quantile_above_one_raises(self):
1028+
with pytest.raises(QQLSyntaxError):
1029+
parse("CREATE COLLECTION articles QUANTIZE SCALAR QUANTILE 1.5")
1030+
1031+
def test_scalar_quantile_integer_above_one_raises(self):
1032+
with pytest.raises(QQLSyntaxError):
1033+
parse("CREATE COLLECTION articles QUANTIZE SCALAR QUANTILE 2")

0 commit comments

Comments
 (0)