queryparser-demo (empty) → 0.1.0.0
raw patch · 5 files changed
+212/−0 lines, 5 filesdep +basedep +containersdep +prettysetup-changed
Dependencies added: base, containers, pretty, queryparser, queryparser-vertica, text, unordered-containers
Files
- CHANGELOG.md +4/−0
- LICENSE +20/−0
- Setup.hs +2/−0
- queryparser-demo.cabal +30/−0
- src/Demo.hs +156/−0
+ CHANGELOG.md view
@@ -0,0 +1,4 @@+# 0.1 to 0.2++## 0.1.0 to 0.1.1+- Upgraded to GHC8, base 4.9
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2017 Uber Technologies, Inc.++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ queryparser-demo.cabal view
@@ -0,0 +1,30 @@+name: queryparser-demo+version: 0.1.0.0+synopsis: Demo package containing queryparser examples+description:+ Library containing sample queryparser calls, for user exploration and+ playgrounding.+license: MIT+license-file: LICENSE+author: Heli Wang, David Thomas, Matt Halverson+maintainer: heli@uber.com+-- copyright:+category: Database+build-type: Simple+extra-source-files:+ CHANGELOG.md+cabal-version: >=1.10++library+ exposed-modules: Demo+ -- other-modules:+ -- other-extensions:+ build-depends: base >=4.9 && <4.11+ , queryparser+ , queryparser-vertica+ , unordered-containers >=0.2 && <0.3+ , containers >=0.5 && <0.6+ , text >=1.2 && <1.3+ , pretty >=1.1 && <1.2+ hs-source-dirs: src+ default-language: Haskell2010
+ src/Demo.hs view
@@ -0,0 +1,156 @@+{-# LANGUAGE RecordWildCards #-}+{-# LANGUAGE OverloadedStrings #-}++module Demo+ ( parse+ , parseAndResolve+ , catalog+ , demoTablesAccessed+ , demoColumnsAccessedByClause+ , demoJoins+ , demoTableLineage+ , demoAllAnalyses+ ) where++import Database.Sql.Type hiding (catalog)++import Database.Sql.Util.Scope (runResolverWarn)+import qualified Database.Sql.Vertica.Parser as VP+import Database.Sql.Vertica.Type (VerticaStatement, resolveVerticaStatement, Vertica)++import Database.Sql.Util.Tables+import Database.Sql.Util.Columns+import Database.Sql.Util.Joins+import Database.Sql.Util.Lineage.Table++import Data.Either+import Data.Functor (void)+import qualified Data.HashMap.Strict as HMS+import qualified Data.List as L+import qualified Data.Map as M+import Data.Proxy+import qualified Data.Set as S+import qualified Data.Text.Lazy as TL++import Text.PrettyPrint+++-- let's provide a really simple function to do parsing!+-- It will have ungraceful error handling.+parse :: TL.Text -> VerticaStatement RawNames ()+parse sql = case void <$> VP.parse sql of+ Right q -> q+ Left err -> error $ show err++-- and construct a catalog, with tables `foo` (columns a, b, and c) and `bar` (columns x, y, and z)+catalog :: Catalog+catalog = makeDefaultingCatalog catalogMap [defaultSchema] defaultDatabase+ where+ defaultDatabase :: DatabaseName ()+ defaultDatabase = DatabaseName () "defaultDatabase"++ defaultSchema :: UQSchemaName ()+ defaultSchema = mkNormalSchema "public" ()++ foo :: (UQTableName (), SchemaMember)+ foo = ( QTableName () None "foo", persistentTable [ QColumnName () None "a"+ , QColumnName () None "b"+ , QColumnName () None "c"+ ] )++ bar :: (UQTableName (), SchemaMember)+ bar = ( QTableName () None "bar", persistentTable [ QColumnName () None "x"+ , QColumnName () None "y"+ , QColumnName () None "z"+ ] )++ catalogMap :: CatalogMap+ catalogMap = HMS.singleton defaultDatabase $+ HMS.fromList [ ( defaultSchema, HMS.fromList [ foo , bar ] ) ]++-- let's provide a really simple function that combines parsing + resolving.+-- We'll hardcode the catalog and leave the error handling ungraceful, still.+parseAndResolve :: TL.Text -> (VerticaStatement ResolvedNames (), [ResolutionError ()])+parseAndResolve sql = case runResolverWarn (resolveVerticaStatement $ parse sql) (Proxy :: Proxy Vertica) catalog of+ (Right queryResolved, resolutions) -> (queryResolved, lefts resolutions)+ (Left err, _) -> error $ show err++-- let's run some analyses!+demoTablesAccessed :: TL.Text -> Doc+demoTablesAccessed sql = draw $ getTables $ fst $ parseAndResolve sql+ where+ draw :: S.Set FullyQualifiedTableName -> Doc+ draw xs = case S.toList xs of+ [] -> text "no tables accessed"+ xs' -> vcat $ map drawFQTN xs'++demoColumnsAccessedByClause :: TL.Text -> Doc+demoColumnsAccessedByClause sql = draw $ getColumns $ fst $ parseAndResolve sql+ where+ draw :: S.Set (FullyQualifiedColumnName, Clause) -> Doc+ draw xs = case S.toList xs of+ [] -> text "no columns accessed"+ xs' -> vcat $ map drawCol xs'++ drawCol :: (FullyQualifiedColumnName, Clause) -> Doc+ drawCol (col, clause) = hcat [drawFQCN col, text "\t", text (TL.unpack clause)]++demoJoins :: TL.Text -> Doc+demoJoins sql = draw $ getJoins $ fst $ parseAndResolve sql+ where+ draw :: S.Set ((FullyQualifiedColumnName, [StructFieldName ()]), (FullyQualifiedColumnName, [StructFieldName ()])) -> Doc+ draw xs = case S.toList xs of+ [] -> text "no joins"+ xs' -> vcat $ map drawJoin xs'++ drawJoin :: ((FullyQualifiedColumnName, [StructFieldName ()]), (FullyQualifiedColumnName, [StructFieldName ()])) -> Doc+ drawJoin (f1, f2) = hsep [drawField f1, text "<->", drawField f2]++demoTableLineage :: TL.Text -> Doc+demoTableLineage sql = draw $ getTableLineage $ fst $ parseAndResolve sql+ where+ draw :: M.Map FQTN (S.Set FQTN) -> Doc+ draw xs = case M.assocs xs of+ [] -> text "no tables modified"+ xs' -> vcat $ map drawAssoc xs'++ drawAssoc :: (FQTN, S.Set FQTN) -> Doc+ drawAssoc (tgt, srcs) = case S.toList srcs of+ [] -> hsep [drawFQTN tgt, text "no longer has data"]+ srcs' -> hsep [ drawFQTN tgt+ , text "after the query depends on"+ , drawDeps srcs'+ , text "before the query"+ ]+++ drawDeps :: [FQTN] -> Doc+ drawDeps srcs = hcat $ L.intersperse ", " $ map drawFQTN srcs++demoAllAnalyses :: TL.Text -> Doc+demoAllAnalyses sql = vcat+ -- note the absence of Column Lineage from this list: that analysis is a work in progress.+ [ text "Tables accessed:"+ , nest indent $ demoTablesAccessed sql+ , text "Columns accessed by clause:"+ , nest indent $ demoColumnsAccessedByClause sql+ , text "Joins:"+ , nest indent $ demoJoins sql+ , text "Table lineage:"+ , nest indent $ demoTableLineage sql+ ]+ where+ indent = 4++-- pretty printing helpers+drawFQTN :: FullyQualifiedTableName -> Doc+drawFQTN FullyQualifiedTableName{..} = hcat $ map (text . TL.unpack) $ L.intersperse "." [fqtnSchemaName, fqtnTableName]++drawFQCN :: FullyQualifiedColumnName -> Doc+drawFQCN FullyQualifiedColumnName{..} = hcat $ map (text . TL.unpack) $ L.intersperse "." [fqcnSchemaName, fqcnTableName, fqcnColumnName]++drawField :: (FullyQualifiedColumnName, [StructFieldName ()]) -> Doc+drawField (fqcn, fields) = foldl1 combineWithDot (drawFQCN fqcn : map drawStructFieldName fields)+ where+ combineWithDot x y = x <> text "." <> y+ drawStructFieldName (StructFieldName _ name) = text $ TL.unpack name