pg_query 2.0.2 → 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +77 -0
  3. data/README.md +12 -0
  4. data/Rakefile +5 -19
  5. data/ext/pg_query/extconf.rb +3 -1
  6. data/ext/pg_query/include/c.h +12 -0
  7. data/ext/pg_query/include/executor/executor.h +6 -0
  8. data/ext/pg_query/include/nodes/execnodes.h +9 -6
  9. data/ext/pg_query/include/nodes/pathnodes.h +1 -1
  10. data/ext/pg_query/include/optimizer/paths.h +8 -0
  11. data/ext/pg_query/include/pg_config.h +9 -6
  12. data/ext/pg_query/include/pg_config_manual.h +7 -0
  13. data/ext/pg_query/include/pg_query.h +2 -2
  14. data/ext/pg_query/include/pg_query_outfuncs_defs.c +1 -0
  15. data/ext/pg_query/include/pg_query_readfuncs_defs.c +1 -0
  16. data/ext/pg_query/include/protobuf/pg_query.pb-c.h +472 -467
  17. data/ext/pg_query/include/protobuf-c/protobuf-c.h +7 -3
  18. data/ext/pg_query/include/protobuf-c.h +7 -3
  19. data/ext/pg_query/include/utils/array.h +1 -0
  20. data/ext/pg_query/include/utils/lsyscache.h +1 -0
  21. data/ext/pg_query/include/utils/probes.h +57 -57
  22. data/ext/pg_query/pg_query.pb-c.c +502 -487
  23. data/ext/pg_query/pg_query_deparse.c +14 -4
  24. data/ext/pg_query/pg_query_fingerprint.c +119 -32
  25. data/ext/pg_query/pg_query_fingerprint.h +3 -1
  26. data/ext/pg_query/pg_query_normalize.c +222 -61
  27. data/ext/pg_query/pg_query_parse_plpgsql.c +21 -1
  28. data/ext/pg_query/pg_query_ruby.c +1 -1
  29. data/ext/pg_query/pg_query_ruby.sym +1 -0
  30. data/ext/pg_query/protobuf-c.c +34 -27
  31. data/ext/pg_query/src_backend_utils_mmgr_mcxt.c +36 -0
  32. data/ext/pg_query/src_common_hashfn.c +420 -0
  33. data/ext/pg_query/src_pl_plpgsql_src_pl_gram.c +1 -1
  34. data/lib/pg_query/filter_columns.rb +1 -1
  35. data/lib/pg_query/fingerprint.rb +1 -3
  36. data/lib/pg_query/parse.rb +110 -44
  37. data/lib/pg_query/pg_query_pb.rb +1385 -1383
  38. data/lib/pg_query/truncate.rb +12 -4
  39. data/lib/pg_query/version.rb +1 -1
  40. data/lib/pg_query.rb +0 -1
  41. metadata +8 -8
  42. data/lib/pg_query/json_field_names.rb +0 -1402
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 710ab71b06be359f6072242754970015712cdad8839c1811470292fad47c880e
4
- data.tar.gz: 1c16c458bdc5f32798dc74839a9d6721bbfa56b754768a64a8bbcbd3b63f13c8
3
+ metadata.gz: 5a4724c7ac9b16756c258b015aa4576da20258c0b3beff05d65fcead44b9ba63
4
+ data.tar.gz: 8513d20ca3bfc1f14301b3d7e473b45f5d3234e3ddb03a06213c040b74b236cb
5
5
  SHA512:
6
- metadata.gz: 4263eebc1add9d84d008d9405e6188dcccc01beec592ed915c80ae6edaaf18a452bb947a18b1af0279d7c5be7e68e871e727cb50d2f36c831694825774fa74de
7
- data.tar.gz: 6e4b4ed4202cf46562cd2dc2281cec07c487e19db79f1df14b3797dba512182daedcccb9299cd0374d4b3163c5400579b71aa18800280e75f9fa499eec997a9a
6
+ metadata.gz: 1be0d5b046bc54c26c223d2114371f93ff7fdb12376518c109cebee009d1a61a4883ef12e07e7692c20668b000db2ece079f9c76204edf497a476b3f83271854
7
+ data.tar.gz: 024e2a9cb8be13b5f4885dbae15a6c2481d0fde469de0d82ca8040dc951fa58ef2780a0b849fc8092faf608fa5404886576af5c2759b03b86f92069d7d4d230b
data/CHANGELOG.md CHANGED
@@ -2,6 +2,83 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ * ...
6
+
7
+
8
+ ## 2.1.2 2021-11-12
9
+
10
+ * Find tables in using clause of delete statement ([#234](https://github.com/pganalyze/pg_query/pull/234))
11
+ * Find tables in case statements ([#235](https://github.com/pganalyze/pg_query/pull/235))
12
+ * Correctly find nested tables in a subselect in a join condition ([#233](https://github.com/pganalyze/pg_query/pull/233))
13
+ * Mark Postgres methods as visibility hidden, to avoid bloating dynamic symbol table ([#232](https://github.com/pganalyze/pg_query/pull/232))
14
+ - This is required on ELF platforms (i.e. Linux, etc) to avoid including all global
15
+ symbols in the shared library's symbol table, bloating the size, and causing
16
+ potential conflicts with other C libraries using the same symbol names.
17
+
18
+
19
+ ## 2.1.1 2021-10-13
20
+
21
+ * Update to libpg_query 13-2.1.0 ([#230](https://github.com/pganalyze/pg_query/pull/230))
22
+ - Normalize: add funcname error object
23
+ - Normalize: Match GROUP BY against target list and re-use param refs
24
+ - PL/pgSQL: Setup namespace items for parameters, support RECORD types
25
+ - This significantly improves parsing for PL/pgSQL functions, to the extent
26
+ that most functions should now parse successfully
27
+ - Normalize: Don't modify constants in TypeName typmods/arrayBounds fields
28
+ - This matches how pg_stat_statement behaves, and avoids causing parsing
29
+ errors on the normalized statement
30
+ - Don't fail builds on systems that have strchrnul support (FreeBSD)
31
+ * Fix build on FreeBSD ([#222](https://github.com/pganalyze/pg_query/pull/222))
32
+ * Add workaround for Ruby garbage collection bug ([#227](https://github.com/pganalyze/pg_query/pull/227))
33
+ - The Ruby interpreter has a bug in `String#concat` where the appended
34
+ array may be garbage collected prematurely because the compiler
35
+ optimized out a Ruby stack variable. We now call `to_ary` on the
36
+ Protobuf object to ensure the array lands on the Ruby stack so the
37
+ garbage collector sees it.
38
+ - The real fix in the interpreter is described in
39
+ https://bugs.ruby-lang.org/issues/18140#note-2, but most current Ruby
40
+ interpreters won't have this fix for some time.
41
+ * Table/function extraction: Support subselects and LATERAL better ([#229](https://github.com/pganalyze/pg_query/pull/229))
42
+ - This reworks the parsing logic so we don't ignore certain kinds of
43
+ subselects.
44
+
45
+
46
+ ## 2.1.0 2021-07-04
47
+
48
+ * Update to libpg_query 13-2.0.6
49
+ - Update to Postgres 13.3 patch release
50
+ - Normalize: Don't touch "GROUP BY 1" and "ORDER BY 1" expressions, keep original text
51
+ - Fingerprint: Cache list item hashes to fingerprint complex queries faster
52
+ - Deparser: Emit the RangeVar catalogname if present
53
+ - Fix crash in pg_scan function when encountering backslash escapes
54
+ * Support extracting functions from a parsed query ([#147](https://github.com/pganalyze/pg_query/pull/147))
55
+ - Adds new `functions`, `ddl_functions` and `call_functions` methods
56
+ - Note that functions are identified by their name only, not their full type definition,
57
+ since raw query parsetrees don't contain sufficient data to identify the types of
58
+ arguments when functions are called
59
+ * Relax google-protobuf dependency ([#213](https://github.com/pganalyze/pg_query/pull/213))
60
+ * Update google-protobuf to 3.17.1 ([#212](https://github.com/pganalyze/pg_query/pull/212))
61
+ - google-protobuf 3.15.x has a bug that causes a seg fault in Ruby under
62
+ certain conditions (https://github.com/protocolbuffers/protobuf/pull/8639). Use
63
+ google-protobuf 3.17.1 instead.
64
+ * Use Protobuf definition for determining JSON field names
65
+ - Note you may see a breaking change if you were using `PgQuery::ParseResult.encode_json`
66
+ to map the protobuf result to JSON, since this now respects the intended JSON names
67
+ from the Proto3 definition (instead of the differently formatted Protobuf field names)
68
+ * Rakefile: Fix "rake clean" by using CLEAN.include instead of CLEAN.<<
69
+ * Find tables inside COALESCE/MIN/MAX functions, UPDATE FROM list
70
+ * Extconf: Add library include path using $INCFLAGS, list it first
71
+ - This ensures any system installed libpg_query gets considered after
72
+ the bundled libpg_query, avoiding errors where the wrong header files
73
+ are used.
74
+
75
+
76
+ ## 2.0.3 2021-04-05
77
+
78
+ * Update to libpg_query 13-2.0.4
79
+ - Normalize: Fix handling of two subsequent DefElem elements (avoids crash)
80
+ - Deparser: Fix crash in CopyStmt with HEADER or FREEZE inside WITH parens
81
+
5
82
 
6
83
  ## 2.0.2 2021-03-31
7
84
 
data/README.md CHANGED
@@ -133,6 +133,18 @@ PgQuery.fingerprint("SELECT ?")
133
133
  => "50fde20626009aba"
134
134
  ```
135
135
 
136
+ ### Scanning a query into tokens
137
+
138
+ ```ruby
139
+ PgQuery.scan('SELECT 1 --comment')
140
+
141
+ => [<PgQuery::ScanResult: version: 130002, tokens: [
142
+ <PgQuery::ScanToken: start: 0, end: 6, token: :SELECT, keyword_kind: :RESERVED_KEYWORD>,
143
+ <PgQuery::ScanToken: start: 7, end: 8, token: :ICONST, keyword_kind: :NO_KEYWORD>,
144
+ <PgQuery::ScanToken: start: 9, end: 18, token: :SQL_COMMENT, keyword_kind: :NO_KEYWORD>]>,
145
+ []]
146
+ ```
147
+
136
148
  ## Differences from Upstream PostgreSQL
137
149
 
138
150
  This gem is based on [libpg_query](https://github.com/pganalyze/libpg_query),
data/Rakefile CHANGED
@@ -5,8 +5,8 @@ require 'rspec/core/rake_task'
5
5
  require 'rubocop/rake_task'
6
6
  require 'open-uri'
7
7
 
8
- LIB_PG_QUERY_TAG = '13-2.0.2'.freeze
9
- LIB_PG_QUERY_SHA256SUM = 'fe485f48bb51dcc5ed146b1c9929da417357ae8844c2f0c8fe1e235fa1d14257'.freeze
8
+ LIB_PG_QUERY_TAG = '13-2.1.0'.freeze
9
+ LIB_PG_QUERY_SHA256SUM = 'a01329ae5bac19b10b8ddf8012bd663a20f85f180d6d7b900c1a1ca8444d19a5'.freeze
10
10
 
11
11
  Rake::ExtensionTask.new 'pg_query' do |ext|
12
12
  ext.lib_dir = 'lib/pg_query'
@@ -21,9 +21,9 @@ task default: %i[spec lint]
21
21
  task test: :spec
22
22
  task lint: :rubocop
23
23
 
24
- CLEAN << 'tmp/**/*'
25
- CLEAN << 'ext/pg_query/*.o'
26
- CLEAN << 'lib/pg_query/pg_query.bundle'
24
+ CLEAN.include 'tmp/**/*'
25
+ CLEAN.include 'ext/pg_query/*.o'
26
+ CLEAN.include 'lib/pg_query/pg_query.bundle'
27
27
 
28
28
  task :update_source do
29
29
  workdir = File.join(__dir__, 'tmp')
@@ -86,18 +86,4 @@ task :update_source do
86
86
  system("cp -a #{libdir}/testdata/* #{testfilesdir}")
87
87
  # Copy back the custom ext files
88
88
  system("cp -a #{extbakdir}/pg_query_ruby.{c,sym} #{extbakdir}/extconf.rb #{extdir}")
89
-
90
- # Generate JSON field name helper (workaround until https://github.com/protocolbuffers/protobuf/pull/8356 is merged)
91
- str = "module PgQuery\n INTERNAL_PROTO_FIELD_NAME_TO_JSON_NAME = {\n"
92
- cur_type = nil
93
- File.read(File.join(libdir, 'protobuf/pg_query.proto')).each_line do |line|
94
- if line[/^message (\w+)/]
95
- cur_type = $1
96
- next
97
- end
98
- next unless line[/(repeated )?\w+ (\w+) = \d+( \[json_name="(\w+)"\])?;/]
99
- str += format(" [%s, :%s] => '%s',\n", cur_type, $2, $4 || $2)
100
- end
101
- str += " }\nend\n"
102
- File.write(File.join(__dir__, 'lib/pg_query/json_field_names.rb'), str)
103
89
  end
@@ -7,7 +7,9 @@ require 'pathname'
7
7
 
8
8
  $objs = Dir.glob(File.join(__dir__, '*.c')).map { |f| Pathname.new(f).sub_ext('.o').to_s }
9
9
 
10
- $CFLAGS << " -I#{File.join(__dir__, 'include')} -O3 -Wall -fno-strict-aliasing -fwrapv -fstack-protector -Wno-unused-function -Wno-unused-variable -g"
10
+ $CFLAGS << " -fvisibility=hidden -O3 -Wall -fno-strict-aliasing -fwrapv -fstack-protector -Wno-unused-function -Wno-unused-variable -Wno-clobbered -Wno-sign-compare -Wno-discarded-qualifiers -g"
11
+
12
+ $INCFLAGS = "-I#{File.join(__dir__, 'include')} " + $INCFLAGS
11
13
 
12
14
  SYMFILE = File.join(__dir__, 'pg_query_ruby.sym')
13
15
  if RUBY_PLATFORM =~ /darwin/
@@ -110,6 +110,18 @@
110
110
  #define pg_attribute_unused()
111
111
  #endif
112
112
 
113
+ /*
114
+ * Place this macro before functions that should be allowed to make misaligned
115
+ * accesses. Think twice before using it on non-x86-specific code!
116
+ * Testing can be done with "-fsanitize=alignment -fsanitize-trap=alignment"
117
+ * on clang, or "-fsanitize=alignment -fno-sanitize-recover=alignment" on gcc.
118
+ */
119
+ #if __clang_major__ >= 7 || __GNUC__ >= 8
120
+ #define pg_attribute_no_sanitize_alignment() __attribute__((no_sanitize("alignment")))
121
+ #else
122
+ #define pg_attribute_no_sanitize_alignment()
123
+ #endif
124
+
113
125
  /*
114
126
  * Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
115
127
  * used in assert-enabled builds, to avoid compiler warnings about unused
@@ -271,6 +271,12 @@ extern ProjectionInfo *ExecBuildProjectionInfo(List *targetList,
271
271
  TupleTableSlot *slot,
272
272
  PlanState *parent,
273
273
  TupleDesc inputDesc);
274
+ extern ProjectionInfo *ExecBuildProjectionInfoExt(List *targetList,
275
+ ExprContext *econtext,
276
+ TupleTableSlot *slot,
277
+ bool assignJunkEntries,
278
+ PlanState *parent,
279
+ TupleDesc inputDesc);
274
280
  extern ExprState *ExecPrepareExpr(Expr *node, EState *estate);
275
281
  extern ExprState *ExecPrepareQual(List *qual, EState *estate);
276
282
  extern ExprState *ExecPrepareCheck(List *qual, EState *estate);
@@ -395,12 +395,15 @@ typedef struct OnConflictSetState
395
395
  * relation, and perhaps also fire triggers. ResultRelInfo holds all the
396
396
  * information needed about a result relation, including indexes.
397
397
  *
398
- * Normally, a ResultRelInfo refers to a table that is in the query's
399
- * range table; then ri_RangeTableIndex is the RT index and ri_RelationDesc
400
- * is just a copy of the relevant es_relations[] entry. But sometimes,
401
- * in ResultRelInfos used only for triggers, ri_RangeTableIndex is zero
402
- * and ri_RelationDesc is a separately-opened relcache pointer that needs
403
- * to be separately closed. See ExecGetTriggerResultRel.
398
+ * Normally, a ResultRelInfo refers to a table that is in the query's range
399
+ * table; then ri_RangeTableIndex is the RT index and ri_RelationDesc is
400
+ * just a copy of the relevant es_relations[] entry. However, in some
401
+ * situations we create ResultRelInfos for relations that are not in the
402
+ * range table, namely for targets of tuple routing in a partitioned table,
403
+ * and when firing triggers in tables other than the target tables (See
404
+ * ExecGetTriggerResultRel). In these situations, ri_RangeTableIndex is 0
405
+ * and ri_RelationDesc is a separately-opened relcache pointer that needs to
406
+ * be separately closed.
404
407
  */
405
408
  typedef struct ResultRelInfo
406
409
  {
@@ -912,7 +912,7 @@ typedef struct StatisticExtInfo
912
912
 
913
913
  Oid statOid; /* OID of the statistics row */
914
914
  RelOptInfo *rel; /* back-link to statistic's table */
915
- char kind; /* statistic kind of this entry */
915
+ char kind; /* statistics kind of this entry */
916
916
  Bitmapset *keys; /* attnums of the columns covered */
917
917
  } StatisticExtInfo;
918
918
 
@@ -135,6 +135,14 @@ extern EquivalenceClass *get_eclass_for_sort_expr(PlannerInfo *root,
135
135
  Index sortref,
136
136
  Relids rel,
137
137
  bool create_it);
138
+ extern EquivalenceMember *find_ec_member_matching_expr(EquivalenceClass *ec,
139
+ Expr *expr,
140
+ Relids relids);
141
+ extern EquivalenceMember *find_computable_ec_member(PlannerInfo *root,
142
+ EquivalenceClass *ec,
143
+ List *exprs,
144
+ Relids relids,
145
+ bool require_parallel_safe);
138
146
  extern Expr *find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel);
139
147
  extern Expr *find_em_expr_usable_for_sorting_rel(PlannerInfo *root,
140
148
  EquivalenceClass *ec,
@@ -757,7 +757,7 @@
757
757
  #define PACKAGE_NAME "PostgreSQL"
758
758
 
759
759
  /* Define to the full name and version of this package. */
760
- #define PACKAGE_STRING "PostgreSQL 13.2"
760
+ #define PACKAGE_STRING "PostgreSQL 13.3"
761
761
 
762
762
  /* Define to the one symbol short name of this package. */
763
763
  #define PACKAGE_TARNAME "postgresql"
@@ -766,7 +766,7 @@
766
766
  #define PACKAGE_URL "https://www.postgresql.org/"
767
767
 
768
768
  /* Define to the version of this package. */
769
- #define PACKAGE_VERSION "13.2"
769
+ #define PACKAGE_VERSION "13.3"
770
770
 
771
771
  /* Define to the name of a signed 128-bit integer type. */
772
772
  #define PG_INT128_TYPE __int128
@@ -785,7 +785,7 @@
785
785
  #define PG_MAJORVERSION_NUM 13
786
786
 
787
787
  /* PostgreSQL minor version number */
788
- #define PG_MINORVERSION_NUM 2
788
+ #define PG_MINORVERSION_NUM 3
789
789
 
790
790
  /* Define to best printf format archetype, usually gnu_printf if available. */
791
791
  #define PG_PRINTF_ATTRIBUTE printf
@@ -794,13 +794,13 @@
794
794
  #define PG_USE_STDBOOL 1
795
795
 
796
796
  /* PostgreSQL version as a string */
797
- #define PG_VERSION "13.2"
797
+ #define PG_VERSION "13.3"
798
798
 
799
799
  /* PostgreSQL version as a number */
800
- #define PG_VERSION_NUM 130002
800
+ #define PG_VERSION_NUM 130003
801
801
 
802
802
  /* A string containing the version number, platform, and C compiler */
803
- #define PG_VERSION_STR "PostgreSQL 13.2 on x86_64-apple-darwin19.6.0, compiled by Apple clang version 12.0.0 (clang-1200.0.32.29), 64-bit"
803
+ #define PG_VERSION_STR "PostgreSQL 13.3 on x86_64-apple-darwin19.6.0, compiled by Apple clang version 12.0.0 (clang-1200.0.32.29), 64-bit"
804
804
 
805
805
  /* Define to 1 to allow profiling output to be saved separately for each
806
806
  process. */
@@ -987,3 +987,6 @@
987
987
  #undef HAVE_EXECINFO_H
988
988
  #undef HAVE_BACKTRACE_SYMBOLS
989
989
  #undef HAVE__GET_CPUID
990
+ #ifdef __FreeBSD__
991
+ #define HAVE_STRCHRNUL
992
+ #endif
@@ -135,6 +135,13 @@
135
135
  #define EXEC_BACKEND
136
136
  #endif
137
137
 
138
+ /*
139
+ * Define this if your operating system supports link()
140
+ */
141
+ #if !defined(WIN32) && !defined(__CYGWIN__)
142
+ #define HAVE_WORKING_LINK 1
143
+ #endif
144
+
138
145
  /*
139
146
  * USE_POSIX_FADVISE controls whether Postgres will attempt to use the
140
147
  * posix_fadvise() kernel call. Usually the automatic configure tests are
@@ -106,9 +106,9 @@ void pg_query_free_fingerprint_result(PgQueryFingerprintResult result);
106
106
  void pg_query_exit(void);
107
107
 
108
108
  // Postgres version information
109
- #define PG_VERSION "13.2"
109
+ #define PG_VERSION "13.3"
110
110
  #define PG_MAJORVERSION "13"
111
- #define PG_VERSION_NUM 130002
111
+ #define PG_VERSION_NUM 130003
112
112
 
113
113
  // Deprecated APIs below
114
114
 
@@ -2270,6 +2270,7 @@ _outTableLikeClause(OUT_TYPE(TableLikeClause, TableLikeClause) out, const TableL
2270
2270
  {
2271
2271
  WRITE_SPECIFIC_NODE_PTR_FIELD(RangeVar, range_var, relation, relation, relation);
2272
2272
  WRITE_UINT_FIELD(options, options, options);
2273
+ WRITE_UINT_FIELD(relation_oid, relationOid, relationOid);
2273
2274
  }
2274
2275
 
2275
2276
  static void
@@ -2674,6 +2674,7 @@ _readTableLikeClause(OUT_TYPE(TableLikeClause, TableLikeClause) msg)
2674
2674
  TableLikeClause *node = makeNode(TableLikeClause);
2675
2675
  READ_SPECIFIC_NODE_PTR_FIELD(RangeVar, range_var, relation, relation, relation);
2676
2676
  READ_UINT_FIELD(options, options, options);
2677
+ READ_UINT_FIELD(relation_oid, relationOid, relationOid);
2677
2678
  return node;
2678
2679
  }
2679
2680