Skip to content

Commit 07fadea

Browse files
committed
Clarify comments/documentation regarding DBIC/SQLA/DQ relationship
This discussion comes up several times per year, just put it to bed for good
1 parent 8920356 commit 07fadea

File tree

4 files changed

+191
-44
lines changed

4 files changed

+191
-44
lines changed

lib/DBIx/Class/SQLMaker.pm

Lines changed: 112 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ DBIx::Class::SQLMaker - An SQL::Abstract-based SQL maker class
99
1010
=head1 DESCRIPTION
1111
12-
This module is a subclass of L<SQL::Abstract> and includes a number of
13-
DBIC-specific workarounds, not yet suitable for inclusion into the
12+
This module is currently a subclass of L<SQL::Abstract> and includes a number of
13+
DBIC-specific extensions/workarounds, not suitable for inclusion into the
1414
L<SQL::Abstract> core. It also provides all (and more than) the functionality
1515
of L<SQL::Abstract::Limit>, see L<DBIx::Class::SQLMaker::LimitDialects> for
1616
more info.
1717
18-
Currently the enhancements to L<SQL::Abstract> are:
18+
Currently the enhancements over L<SQL::Abstract> are:
1919
2020
=over
2121
@@ -25,10 +25,102 @@ Currently the enhancements to L<SQL::Abstract> are:
2525
2626
=item * C<GROUP BY>/C<HAVING> support (via extensions to the order_by parameter)
2727
28+
=item * A rudimentary multicolumn IN operator
29+
2830
=item * Support of C<...FOR UPDATE> type of select statement modifiers
2931
3032
=back
3133
34+
=head1 ROADMAP
35+
36+
Some maintainer musings on the current state of SQL generation within DBIC as
37+
of Oct 2015
38+
39+
=head2 Folding of most (or all) of L<SQL::Abstract (SQLA)|SQL::Abstract> into DBIC
40+
41+
The rise of complex prefetch use, and the general streamlining of result
42+
parsing within DBIC ended up pushing the actual SQL generation to the forefront
43+
of many casual performance profiles. While the idea behind SQLA's API is sound,
44+
the actual implementation is terribly inefficient (once again bumping into the
45+
ridiculously high overhead of perl function calls).
46+
47+
Given that SQLA has a B<very> distinct life on its own, and is used within an
48+
order of magnitude more projects compared to DBIC, it is prudent to B<not>
49+
disturb the current call chains within SQLA itself. Instead in the near future
50+
an effort will be undertaken to seek a more thorough decoupling of DBIC SQL
51+
generation from reliance on SQLA, possibly to a point where B<DBIC will no
52+
longer depend on SQLA> at all.
53+
54+
B<The L<SQL::Abstract> library itself will continue being maintained> although
55+
it is not likely to gain many extra features, notably dialect support, at least
56+
not within the base C<SQL::Abstract> namespace.
57+
58+
This work (if undertaken) will take into consideration the following
59+
constraints:
60+
61+
=over
62+
63+
=item Main API compatibility
64+
65+
The object returned by C<< $schema->storage->sqlmaker >> needs to be able to
66+
satisfy most of the basic tests found in the current-at-the-time SQLA dist.
67+
While things like L<case|SQL::Abstract/case> or L<logic|SQL::Abstract/logic>
68+
or even worse L<convert|SQL::Abstract/convert> will definitely remain
69+
unsupported, the rest of the tests should pass (within reason).
70+
71+
=item Ability to plug back an SQL::Abstract (or derivative)
72+
73+
During the initial work on L<Data::Query> the test suite of DBIC turned out to
74+
be an invaluable asset to iron out hard-to-reason-about corner cases. In
75+
addition the test suite is much more vast and intricate than the tests of SQLA
76+
itself. This state of affairs is way too valuable to sacrifice in order to gain
77+
faster SQL generation. Thus a compile-time-ENV-check will be introduced along
78+
with an extra CI configuration to ensure that DBIC is used with an off-the-CPAN
79+
SQLA and that it continues to flawlessly run its entire test suite. While this
80+
will undoubtedly complicate the implementation of the better performing SQL
81+
generator, it will preserve both the usability of the test suite for external
82+
projects and will keep L<SQL::Abstract> from regressions in the future.
83+
84+
=back
85+
86+
Aside from these constraints it is becoming more and more practical to simply
87+
stop using SQLA in day-to-day production deployments of DBIC. The flexibility
88+
of the internals is simply not worth the performance cost.
89+
90+
=head2 Relationship to L<Data::Query (DQ)|Data::Query>
91+
92+
When initial work on DQ was taking place, the tools in L<::Storage::DBIHacks
93+
|http://github.com/dbsrgits/dbix-class/blob/current/blead/lib/DBIx/Class/Storage/DBIHacks.pm>
94+
were only beginning to take shape, and it wasn't clear how important they will
95+
become further down the road. In fact the I<regexing all over the place> was
96+
considered an ugly stop-gap, and even a couple of highly entertaining talks
97+
were given to that effect. As the use-cases of DBIC were progressing, and
98+
evidence for the importance of supporting arbitrary SQL was mounting, it became
99+
clearer that DBIC itself would not really benefit in any way from an
100+
integration with DQ, but on the contrary is likely to lose functionality while
101+
the corners of the brand new DQ codebase are sanded off.
102+
103+
The current status of DBIC/DQ integration is that the only benefit is for DQ by
104+
having access to the very extensive "early adopter" test suite, in the same
105+
manner as early DBIC benefitted tremendously from usurping the Class::DBI test
106+
suite. As far as the DBIC user-base - there are no immediate practical upsides
107+
to DQ integration, neither in terms of API nor in performance.
108+
109+
So (as described higher up) the DBIC development effort will in the foreseable
110+
future ignore the existence of DQ, and will continue optimizing the preexisting
111+
SQLA-based solution, potentially "organically growing" its own compatible
112+
implementation. Also (again, as described higher up) the ability to plug a
113+
separate SQLA-compatible class providing the necessary surface API will remain
114+
possible, and will be protected at all costs in order to continue providing DQ
115+
access to the test cases of DBIC.
116+
117+
In the short term, after one more pass over the ResultSet internals is
118+
undertaken I<real soon now (tm)>, and before the SQLA/SQLMaker integration
119+
takes place, the preexisting DQ-based branches will be pulled/modified/rebased
120+
to get up-to-date with the current state of the codebase, which changed very
121+
substantially since the last migration effort, especially in the SQL
122+
classification meta-parsing codepath.
123+
32124
=cut
33125

34126
use base qw/
@@ -146,8 +238,9 @@ sub select {
146238
if( $limiter = $self->can ('emulate_limit') ) {
147239
carp_unique(
148240
'Support for the legacy emulate_limit() mechanism inherited from '
149-
. 'SQL::Abstract::Limit has been deprecated, and will be removed when '
150-
. 'DBIC transitions to Data::Query. If your code uses this type of '
241+
. 'SQL::Abstract::Limit has been deprecated, and will be removed at '
242+
. 'some future point, as it gets in the way of architectural and/or '
243+
. 'performance advances within DBIC. If your code uses this type of '
151244
. 'limit specification please file an RT and provide the source of '
152245
. 'your emulate_limit() implementation, so an acceptable upgrade-path '
153246
. 'can be devised'
@@ -211,9 +304,9 @@ sub insert {
211304
# optimized due to hotttnesss
212305
# my ($self, $table, $data, $options) = @_;
213306

214-
# SQLA will emit INSERT INTO $table ( ) VALUES ( )
307+
# FIXME SQLA will emit INSERT INTO $table ( ) VALUES ( )
215308
# which is sadly understood only by MySQL. Change default behavior here,
216-
# until SQLA2 comes with proper dialect support
309+
# until we fold the extra pieces into SQLMaker properly
217310
if (! $_[2] or (ref $_[2] eq 'HASH' and !keys %{$_[2]} ) ) {
218311
my @bind;
219312
my $sql = sprintf(
@@ -294,7 +387,10 @@ sub _recurse_fields {
294387
# things in the SQLA space need to have more info about the $rs they
295388
# create SQL for. The alternative would be to keep expanding the
296389
# signature of _select with more and more positional parameters, which
297-
# is just gross. All hail SQLA2!
390+
# is just gross.
391+
#
392+
# FIXME - this will have to transition out to a subclass when the effort
393+
# of folding the SQLA machinery into SQLMaker takes place
298394
sub _parse_rs_attrs {
299395
my ($self, $arg) = @_;
300396

@@ -495,9 +591,14 @@ sub _join_condition {
495591
return $self->_recurse_where($cond);
496592
}
497593

498-
# This is hideously ugly, but SQLA does not understand multicol IN expressions
499-
# FIXME TEMPORARY - DQ should have native syntax for this
500-
# moved here to raise API questions
594+
# !!! EXPERIMENTAL API !!! WILL CHANGE !!!
595+
#
596+
# This is rather odd, but vanilla SQLA does not have support for multicolumn IN
597+
# expressions
598+
# Currently has only one callsite in ResultSet, body moved into this subclass
599+
# of SQLA to raise API questions like:
600+
# - how do we convey a list of idents...?
601+
# - can binds reside on lhs?
501602
#
502603
# !!! EXPERIMENTAL API !!! WILL CHANGE !!!
503604
sub _where_op_multicolumn_in {

lib/DBIx/Class/SQLMaker/LimitDialects.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ EOS
273273
# method, and the slower BETWEEN query is used instead
274274
#
275275
# FIXME - this is quite expensive, and does not perform caching of any sort
276-
# as soon as some of the DQ work becomes viable consider switching this
277-
# over
276+
# as soon as some of the SQLA-inlining work becomes viable consider adding
277+
# some rudimentary caching support
278278
if (
279279
$rs_attrs->{order_by}
280280
and

lib/DBIx/Class/Storage/DBI/mysql.pm

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@ sub _prep_for_execute {
4444
return $self->next::method(@_) if ( $_[0] eq 'select' or $_[0] eq 'insert' );
4545

4646

47-
# FIXME FIXME FIXME - this is a terrible, gross, incomplete hack
48-
# it should be trivial for mst to port this to DQ (and a good
49-
# exercise as well, since we do not yet have such wide tree walking
50-
# in place). For the time being this will work in limited cases,
51-
# mainly complex update/delete, which is really all we want it for
52-
# currently (allows us to fix some bugs without breaking MySQL in
53-
# the process, and is also crucial for Shadow to be usable)
47+
# FIXME FIXME FIXME - this is a terrible, gross, incomplete, MySQL-specific
48+
# hack but it works rather well for the limited amount of actual use cases
49+
# which can not be done in any other way on MySQL. This allows us to fix
50+
# some bugs without breaking MySQL support in the process and is also
51+
# crucial for more complex things like Shadow to be usable
52+
#
53+
# This code is just a pre-analyzer, working in tandem with ::SQLMaker::MySQL,
54+
# where the possibly-set value of {_modification_target_referenced_re} is
55+
# used to demarcate which part of the final SQL to double-wrap in a subquery.
56+
#
57+
# This is covered extensively by "offline" tests, so when the DQ work
58+
# resumes, this will get flagged. Afaik there are no AST-visitor code of that
59+
# magnitude yet (Oct 2015) within DQ, so a good exercise overall.
5460

5561
# extract the source name, construct modification indicator re
5662
my $sm = $self->sql_maker;

lib/DBIx/Class/Storage/DBIHacks.pm

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,24 @@ package #hide from PAUSE
22
DBIx::Class::Storage::DBIHacks;
33

44
#
5-
# This module contains code that should never have seen the light of day,
6-
# does not belong in the Storage, or is otherwise unfit for public
7-
# display. The arrival of SQLA2 should immediately obsolete 90% of this
5+
# This module contains code supporting a battery of special cases and tests for
6+
# many corner cases pushing the envelope of what DBIC can do. When work on
7+
# these utilities began in mid 2009 (51a296b402c) it wasn't immediately obvious
8+
# that these pieces, despite their misleading on-first-sighe-flakiness, will
9+
# become part of the generic query rewriting machinery of DBIC, allowing it to
10+
# both generate and process queries representing incredibly complex sets with
11+
# reasonable efficiency.
12+
#
13+
# Now (end of 2015), more than 6 years later the routines in this class have
14+
# stabilized enough, and are meticulously covered with tests, to a point where
15+
# an effort to formalize them into user-facing APIs might be worthwhile.
16+
#
17+
# An implementor working on publicizing and/or replacing the routines with a
18+
# more modern SQL generation framework should keep in mind that pretty much all
19+
# existing tests are constructed on the basis of real-world code used in
20+
# production somewhere.
21+
#
22+
# Please hack on this responsibly ;)
823
#
924

1025
use strict;
@@ -346,27 +361,53 @@ sub _adjust_select_args_for_complex_prefetch {
346361
});
347362
}
348363

349-
# This is totally horrific - the {where} ends up in both the inner and outer query
350-
# Unfortunately not much can be done until SQLA2 introspection arrives, and even
351-
# then if where conditions apply to the *right* side of the prefetch, you may have
352-
# to both filter the inner select (e.g. to apply a limit) and then have to re-filter
353-
# the outer select to exclude joins you didn't want in the first place
364+
# FIXME: The {where} ends up in both the inner and outer query, i.e. *twice*
365+
#
366+
# This is rather horrific, and while we currently *do* have enough
367+
# introspection tooling available to attempt a stab at properly deciding
368+
# whether or not to include the where condition on the outside, the
369+
# machinery is still too slow to apply it here.
370+
# Thus for the time being we do not attempt any sanitation of the where
371+
# clause and just pass it through on both sides of the subquery. This *will*
372+
# be addressed at a later stage, most likely after folding the SQL generator
373+
# into SQLMaker proper
354374
#
355375
# OTOH it can be seen as a plus: <ash> (notes that this query would make a DBA cry ;)
376+
#
356377
return $outer_attrs;
357378
}
358379

380+
# This is probably the ickiest, yet most relied upon part of the codebase:
381+
# this is the place where we take arbitrary SQL input and break it into its
382+
# constituent parts, making sure we know which *sources* are used in what
383+
# *capacity* ( selecting / restricting / grouping / ordering / joining, etc )
384+
# Although the method is pretty horrific, the worst thing that can happen is
385+
# for a classification failure, which in turn will result in a vocal exception,
386+
# and will lead to a relatively prompt fix.
387+
# The code has been slowly improving and is covered with a formiddable battery
388+
# of tests, so can be considered "reliably stable" at this point (Oct 2015).
359389
#
360-
# I KNOW THIS SUCKS! GET SQLA2 OUT THE DOOR SO THIS CAN DIE!
390+
# A note to implementors attempting to "replace" this - keep in mind that while
391+
# there are multiple optimization avenues, the actual "scan literal elements"
392+
# part *MAY NEVER BE REMOVED*, even if it is limited only ot the (future) AST
393+
# nodes that are deemed opaque (i.e. contain literal expressions). The use of
394+
# blackbox literals is at this point firmly a user-facing API, and is one of
395+
# *the* reasons DBIC remains as flexible as it is. In other words, when working
396+
# on this keep in mind that the following is widespread and *encouraged* way
397+
# of using DBIC in the wild when push comes to shove:
398+
#
399+
# $rs->search( {}, {
400+
# select => \[ $random, @stuff],
401+
# from => \[ $random, @stuff ],
402+
# where => \[ $random, @stuff ],
403+
# group_by => \[ $random, @stuff ],
404+
# order_by => \[ $random, @stuff ],
405+
# } )
406+
#
407+
# Various incarnations of the above are reflected in many of the tests. If one
408+
# gets to fail, you get to fix it. A "this is crazy, nobody does that" is not
409+
# acceptable going forward.
361410
#
362-
# Due to a lack of SQLA2 we fall back to crude scans of all the
363-
# select/where/order/group attributes, in order to determine what
364-
# aliases are needed to fulfill the query. This information is used
365-
# throughout the code to prune unnecessary JOINs from the queries
366-
# in an attempt to reduce the execution time.
367-
# Although the method is pretty horrific, the worst thing that can
368-
# happen is for it to fail due to some scalar SQL, which in turn will
369-
# result in a vocal exception.
370411
sub _resolve_aliastypes_from_select_args {
371412
my ( $self, $attrs ) = @_;
372413

@@ -678,19 +719,16 @@ sub _group_over_selection {
678719
# of the external order and convert them to MIN(X) for ASC or MAX(X)
679720
# for DESC, and group_by the root columns. The end result should be
680721
# exactly what we expect
681-
682-
# FIXME - this code is a joke, will need to be completely rewritten in
683-
# the DQ branch. But I need to push a POC here, otherwise the
684-
# pesky tests won't pass
685-
# wrap any part of the order_by that "responds" to an ordering alias
686-
# into a MIN/MAX
722+
#
687723
$sql_maker ||= $self->sql_maker;
688724
$order_chunks ||= [
689725
map { ref $_ eq 'ARRAY' ? $_ : [ $_ ] } $sql_maker->_order_by_chunks($attrs->{order_by})
690726
];
691727

692728
my ($chunk, $is_desc) = $sql_maker->_split_order_chunk($order_chunks->[$o_idx][0]);
693729

730+
# we reached that far - wrap any part of the order_by that "responded"
731+
# to an ordering alias into a MIN/MAX
694732
$new_order_by[$o_idx] = \[
695733
sprintf( '%s( %s )%s',
696734
($is_desc ? 'MAX' : 'MIN'),
@@ -1041,7 +1079,9 @@ sub _extract_colinfo_of_stable_main_source_order_by_portion {
10411079
# resultset {where} stacks
10421080
#
10431081
# FIXME - while relatively robust, this is still imperfect, one of the first
1044-
# things to tackle with DQ
1082+
# things to tackle when we get access to a formalized AST. Note that this code
1083+
# is covered by a *ridiculous* amount of tests, so starting with porting this
1084+
# code would be a rather good exercise
10451085
sub _collapse_cond {
10461086
my ($self, $where, $where_is_anded_array) = @_;
10471087

0 commit comments

Comments
 (0)