Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leftJoin returning Nothing for right hand table (PgRN) #39

Open
wduncanfraser opened this issue Nov 27, 2017 · 1 comment
Open

leftJoin returning Nothing for right hand table (PgRN) #39

wduncanfraser opened this issue Nov 27, 2017 · 1 comment

Comments

@wduncanfraser
Copy link

leftJoins appear to always return Nothing for the right hand table. For example, in this query:

queryById :: FeatureId -> Query Db1 () (PgR FeatureTable, PgRN FV.VersionTable)
queryById fid = proc () -> do
  (a, b) <- leftJoin
    (query FeatureTable) (query FV.VersionTable)
    (\f v -> eq (#id f) (#feature_id v)) -< ()
  restrict -< eq (#id a) (kol . untag $ fid)
  returnA -< (a, b)

Called here:

fetchById
  :: (MonadIO m, MonadThrow m)
  => FeatureId -> Conn' Db1 -> ExceptT QueryError m FeatureReadable
fetchById fid conn =
  runQuery conn (queryById fid) >>= \r -> do
    traceShowM r

A trace if the result shows:

[(HsR {unHsR = RCons (Tagged (Tagged 1)) (RCons (Tagged (Tagged "things")) (RCons (Tagged (Tagged 2017-11-22 02:29:46.473068 UTC)) (RNil)))},Nothing),(HsR {unHsR = RCons (Tagged (Tagged 1)) (RCons (Tagged (Tagged "things")) (RCons (Tagged (Tagged 2017-11-22 02:29:46.473068 UTC)) (RNil)))},Nothing),(HsR {unHsR = RCons (Tagged (Tagged 1)) (RCons (Tagged (Tagged "things")) (RCons (Tagged (Tagged 2017-11-22 02:29:46.473068 UTC)) (RNil)))},Nothing)]

Which corresponds to the 3 results I should be getting from the leftJoin, however the right side is Nothing. I did some digging and turned query logging on and the query being generated is as follows:

SELECT "result1_0_3" as "result1_4",
	        "result1_1_3" as "result2_4",
	        "result1_2_3" as "result3_4",
	        "result2_0_3" as "result4_4",
	        "result2_1_3" as "result5_4",
	        "result2_2_3" as "result6_4",
	        "result2_3_3" as "result7_4",
	        "result2_4_3" as "result8_4",
	        "result2_5_3" as "result9_4",
	        "result2_6_3" as "result10_4",
	        "result2_7_3" as "result11_4",
	        "result2_8_3" as "result12_4"
	 FROM (SELECT *
	       FROM (SELECT *
	             FROM
	            (SELECT "id0_1" as "result1_0_3",
	                     "name1_1" as "result1_1_3",
	                     "created_date2_1" as "result1_2_3",
	                     *
	              FROM (SELECT *
	                    FROM (SELECT "id" as "id0_1",
	                                 "name" as "name1_1",
	                                 "created_date" as "created_date2_1"
	                          FROM "public"."feature" as "T1") as "T1") as "T1") as "T1"
	             LEFT OUTER JOIN
	            (SELECT "feature_id0_2" as "result2_0_3",
	                     "version_number1_2" as "result2_1_3",
	                     "version_status2_2" as "result2_2_3",
	                     "allow_internal_users3_2" as "result2_3_3",
	                     "created_date4_2" as "result2_4_3",
	                     "private_preview_date5_2" as "result2_5_3",
	                     "public_preview_date6_2" as "result2_6_3",
	                     "public_date7_2" as "result2_7_3",
	                     "core_date8_2" as "result2_8_3",
	                     *
	              FROM (SELECT *
	                    FROM (SELECT "feature_id" as "feature_id0_2",
	                                 "version_number" as "version_number1_2",
	                                 "version_status" as "version_status2_2",
	                                 "allow_internal_users" as "allow_internal_users3_2",
	                                 "created_date" as "created_date4_2",
	                                 "private_preview_date" as "private_preview_date5_2",
	                                 "public_preview_date" as "public_preview_date6_2",
	                                 "public_date" as "public_date7_2",
	                                 "core_date" as "core_date8_2"
	                          FROM "public"."feature_version" as "T1") as "T1") as "T1") as "T2"
	             ON
	             ("id0_1") = ("feature_id0_2")) as "T1"
	       WHERE (("result1_0_3") = 1)) as "T1";

Which when run manually returns the expected results:

 result1_4 | result2_4 |           result3_4           | result4_4 | result5_4 | result6_4 | result7_4 |           result8_4           | result9_4 | result10_4 | result11_4 | result12_4 
-----------+-----------+-------------------------------+-----------+-----------+-----------+-----------+-------------------------------+-----------+------------+------------+------------
         1 | things    | 2017-11-22 02:29:46.473068+00 |         1 |         1 |         0 |           | 2017-11-22 21:49:48.955197+00 |           |            |            | 
         1 | things    | 2017-11-22 02:29:46.473068+00 |         1 |         2 |         0 |           | 2017-11-22 22:04:41.536123+00 |           |            |            | 
         1 | things    | 2017-11-22 02:29:46.473068+00 |         1 |         3 |         0 |           | 2017-11-22 22:23:53.527832+00 |           |            |            | 
(3 rows)

Direct queries of the right hand table and innerJoins work without issue.

This looks like it may be related to #37

CC @boj

@wduncanfraser wduncanfraser changed the title leftJoin returning Nothing for right hand column (PgRN) leftJoin returning Nothing for right hand table (PgRN) Nov 27, 2017
@wduncanfraser
Copy link
Author

For reference, table definitions in SQL and Tisch:

type instance Columns FeatureTable =
  [ 'Column "id"           'WD 'R  PGInt4        FeatureId
  , 'Column "name"         'W  'R  PGText        FeatureName
  , 'Column "created_date" 'WD 'R  PGTimestamptz CreatedDate
  ]
  CREATE TABLE feature (
    id serial PRIMARY KEY,
    name text UNIQUE,
    created_date timestamptz NOT NULL DEFAULT (now() at time zone 'utc')
  );
type instance Columns VersionTable =
  [ 'Column "feature_id"           'W  'R  PGInt4        FeatureId
  , 'Column "version_number"       'W  'R  PGInt4        VersionNumber
  , 'Column "version_status"       'W  'RN VersionStatus VersionStatus
  , 'Column "allow_internal_users" 'W  'RN PGBool        Bool
  , 'Column "created_date"         'WD 'R  PGTimestamptz CreatedDate
  , 'Column "private_preview_date" 'W  'RN PGTimestamptz UTCTime
  , 'Column "public_preview_date"  'W  'RN PGTimestamptz UTCTime
  , 'Column "public_date"          'W  'RN PGTimestamptz UTCTime
  , 'Column "core_date"            'W  'RN PGTimestamptz UTCTime
  ]
  CREATE TABLE feature_version (
    feature_id int REFERENCES feature(id),
    version_number int NOT NULL,
    version_status int, -- This is tied to a haskell enum type
    allow_internal_users boolean,
    created_date timestamptz NOT NULL DEFAULT (now() at time zone 'utc'),
    private_preview_date timestamptz,
    public_preview_date timestamptz,
    public_date timestamptz,
    core_date timestamptz,
    PRIMARY KEY(feature_id, version_number)
  );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant