This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
PyPika is a Python SQL query builder library that uses a builder design pattern to construct SQL queries programmatically. It supports 10+ database dialects (MySQL, PostgreSQL, Oracle, MSSQL, Vertica, ClickHouse, Snowflake, SQLite, Redshift, JQL) with no external dependencies.
# Install dev dependencies and pre-commit hooks
make install
# Run all tests
python -m unittest discover
# Run a single test file
python -m unittest pypika.tests.test_criterions
# Run a specific test class or method
python -m unittest pypika.tests.test_criterions.CriterionTests.test_empty_criterion
# Code formatting (Black, line length: 120)
black pypika/
black --check pypika/
# Build documentation
make docs.buildThe codebase follows a layered builder pattern:
queries.py- Main query builder classes:Query,QueryBuilder,CreateQueryBuilder,DropQueryBuilder, table/schema/join managementterms.py- Term hierarchy:Term,Field,Criterion,Function,ArithmeticExpression,Case,ValueWrapper,Parameterdialects.py- Database-specific query builders that override base behavior for MySQL, PostgreSQL, Oracle, MSSQL, etc.functions.py- SQL function wrappers (aggregates, string, date/time, math, casting)analytics.py- Window/analytic functions:Rank,DenseRank,RowNumber,LAG,LEADenums.py- Enumerations:JoinType,Order,Comparator,Arithmetic,DatePart,Dialectsutils.py- Exceptions and decorators (@builder,@ignore_copy)
-
Builder Pattern with Immutability: The
@builderdecorator inutils.pycreates deep copies on each method call, enabling safe method chaining. -
Visitor Pattern: Every term/query class implements
get_sql()to serialize to SQL strings recursively. -
Operator Overloading:
CriterionandFieldclasses overload operators for readable query conditions:field == value # BasicCriterion with Equality.eq field[18:65] # BetweenCriterion crit1 & crit2 # ComplexCriterion with AND
Term (base for query components)
├── Field → extends Criterion (column references)
├── Criterion (boolean conditions)
│ ├── BasicCriterion, ContainsCriterion, BetweenCriterion
│ └── ComplexCriterion (nested AND/OR)
├── Function → AggregateFunction → AnalyticFunction
├── ArithmeticExpression, Case, ValueWrapper, Parameter
└── Array, Tuple, Bracket, Interval
QueryBuilder (fluent builder for SELECT)
└── [dialect-specific variants in dialects.py]
Query (entry point, creates QueryBuilder instances)
Query.from_(table).select(...).where(...).orderby(...)
Query.into(table).columns(...).insert(...)
Query.update(table).set(...).where(...)
Query.delete().from_(table).where(...)
Query.create_table(name).columns(...)Tests are in pypika/tests/ using unittest. Key test files:
test_criterions.py- Filtering/conditionstest_joins.py- JOIN operationstest_functions.py- SQL functionstest_inserts.py,test_updates.py,test_deletes.py- DML operationsdialects/- Dialect-specific testsclickhouse/- ClickHouse function tests
- Black formatter with 120-char line length
- No external dependencies in core library
- Public API exported via
pypika/__init__.pywith explicit__all__
Documentation is published to three places from a single source:
README.rst (single source of truth)
│
├──► PyPI (setup.py reads README.rst as long_description)
├──► GitHub (renders README.rst directly)
└──► ReadTheDocs (docs/*.rst files include sections from README.rst)
README.rst- Main documentation file with all content. Uses RST markers like_tutorial_start:and_tutorial_end:to define includable sections.docs/*.rst- Sphinx source files that.. include::sections from README.rst using markers.docs/conf.py- Sphinx configuration.
.. _intro_start: → docs/index.rst
.. _intro_end:
.. _installation_start: → docs/1_installation.rst
.. _installation_end:
.. _tutorial_start: → docs/2_tutorial.rst (main content)
.. _tutorial_end:
.. _advanced_start: → docs/3_advanced.rst
.. _advanced_end:- Add new content to
README.rstwithin the appropriate markers - Content automatically appears on PyPI, GitHub, and ReadTheDocs
- Run
make docs.buildto verify Sphinx builds correctly