Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Expressions (No Speaker Notes)

Josh Smeaton
November 15, 2015

Expressions (No Speaker Notes)

A copy of the slides used in my talk on expressions without speaker notes attached.

Josh Smeaton

November 15, 2015
Tweet

More Decks by Josh Smeaton

Other Decks in Programming

Transcript

  1. ME JOSH SMEATON ▸ https://github.com/jarshwah and jarshwah on IRC ▸

    Software Engineer at Engage (a LivePerson company) ▸ SQL Nerd ▸ Primary author of Expressions ▸ Django core developer
  2. ALTERNATIVES .RAW() AND .EXTRA() ▸ .raw() for writing entire query

    in SQL ▸ .extra() for appending bits of SQL ▸ Escape hatches
  3. ALTERNATIVES .RAW() AND .EXTRA() PROBLEMS ▸ Database backend specific ▸

    3rd party libraries and apps ▸ Unknown table aliases ▸ Broken GROUP BY
  4. EXPRESSIONS WHERE CAN THEY BE USED? ▸ .update(price=F('price') + tax)

    ▸ .create(..., username=Lower(username)) ▸ .annotate(total=F('price') + shipping) ▸ .aggregate(sum_total=Sum('total')) ▸ .order_by(Coalesce('last_name', 'first_name')) ▸ .filter(name=Lower(Value(user_input)))

  5. EXPRESSIONS BATTERIES INCLUDED ▸ Functions ▸ Coalesce(), Concat(), Lower(), Upper()

    ▸ Hidden complexity ▸ F(), Case(), When() ▸ Building blocks ▸ Aggregate(), Func(), Value()
  6. OLD EXPRESSIONS AGGREGATION PRE 1.8 Sum() Aggregate Public Interface Query

    sql.Sum sql.Aggregate Implementation nonrel.Sum nonrel.Aggregate Implementation Rendered SQL
  7. OLD EXPRESSIONS F() EXPRESSIONS PRE 1.8 F() ExpressionNode Public Interface

    Query Rendered SQL evaluate recursively sql.Evaluator Implementation nonrel.Evaluator Implementation prepare recursively
  8. NEW EXPRESSIONS Sum() or F() Expression Public Interface Query Render

    Children Rendered SQL Implementation EXPRESSIONS 1.8
  9. SO, WHAT'S NEW? EXPRESSIONS IN 1.8 ▸ Public API with

    Documentation ▸ Composable ▸ Sum(F('talk') + F('hold')) + Sum('wrap') ▸ Simplified internals
  10. WHO CARES? PRETTY GOOD FOR 3RD PARTY BACKENDS ▸ DB2

    uses Date() so would work with standard implementation. ▸ Monkey patch support for builtins. ▸ Backends can create their own types for obscure functions. See GIS backend.
  11. WHO CARES? THE NOT SO GREATS ▸ Not able to

    add custom joins. Must resort to .extra(). ▸ Still early days and a few bugs still to fix. ▸ Documentation and examples. Too many people using ExpressionWrapper().
  12. EXPRESSIONS API Combinable Resolvable Expression F('field') + F('other') :: Combined(left,

    "+", right) F('field').resolve_expression() :: Col("table"."field") Col("table"."field").as_sql() :: "table"."field"
  13. EXPRESSIONS API RESOLVE_EXPRESSION ▸ Validates against the current query ▸

    Returns a new expression or a copy ▸ Sets the new query state
  14. EXPRESSIONS API AS_SQL ▸ as_sql() outputs default SQL ▸ as_{connection.vendor}()

    outputs vendor specific SQL ▸ Compile protocol. compiler.compile()
  15. EXPRESSIONS API AS_SQL ▸ as_sql() outputs default SQL ▸ as_{connection.vendor}()

    outputs vendor specific SQL ▸ Compile protocol. compiler.compile()
  16. EXPRESSIONS API OUTPUT_FIELD ▸ What field will the database return?

    ▸ convert_value() and db_converters() like to_python() ▸ lookups and transforms
  17. THE FUTURE OF EXPRESSIONS WHAT ELSE CAN WE BUILD? ▸

    Analytical/Window functions? ▸ GROUP BY CUBE/ROLLUP ▸ Aggregate FILTER WITH ▸ CTE (Common Table Expressions)
  18. JOSH SMEATON THANK YOU! ▸ jarshwah on most of the

    medias ▸ Slides @ https://speakerdeck.com/jarshwah ▸ Code @ https://github.com/jarshwah/expressions_talk/